blob: 92cea9d77ec913e8bc21417dd07ff97e8d27cb9c [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080039#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
Dave Airlief453ba02008-11-07 14:05:41 -080041/* Avoid boilerplate. I'm tired of typing. */
42#define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53/*
54 * Global properties
55 */
56static struct drm_prop_enum_list drm_dpms_enum_list[] =
57{ { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61};
62
63DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65/*
66 * Optional properties
67 */
68static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69{
Jesse Barnes53bd8382009-07-01 10:04:40 -070070 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080074};
75
76static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77{
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100080 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080081};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87{
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91};
92
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96{
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100};
101
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106{
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117{
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000128static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132};
133
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
Dave Airlief453ba02008-11-07 14:05:41 -0800137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
165static struct drm_prop_enum_list drm_encoder_enum_list[] =
166{ { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
174char *drm_get_encoder_name(struct drm_encoder *encoder)
175{
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182}
Dave Airlie13a81952009-09-07 15:45:33 +1000183EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800184
185char *drm_get_connector_name(struct drm_connector *connector)
186{
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193}
194EXPORT_SYMBOL(drm_get_connector_name);
195
196char *drm_get_connector_status_name(enum drm_connector_status status)
197{
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204}
205
206/**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223{
224 int new_id = 0;
225 int ret;
226
Dave Airlief453ba02008-11-07 14:05:41 -0800227again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200230 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800231 }
232
Jesse Barnesad2563c2009-01-19 17:21:45 +1000233 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000235 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800236 if (ret == -EAGAIN)
237 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200238 else if (ret)
239 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800240
241 obj->id = new_id;
242 obj->type = obj_type;
243 return 0;
244}
245
246/**
247 * drm_mode_object_put - free an identifer
248 * @dev: DRM device
249 * @id: ID to free
250 *
251 * LOCKING:
252 * Caller must hold DRM mode_config lock.
253 *
254 * Free @id from @dev's unique identifier pool.
255 */
256static void drm_mode_object_put(struct drm_device *dev,
257 struct drm_mode_object *object)
258{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800262}
263
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200264struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
265 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800266{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800268
Jesse Barnesad2563c2009-01-19 17:21:45 +1000269 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800270 obj = idr_find(&dev->mode_config.crtc_idr, id);
271 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000272 obj = NULL;
273 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800274
275 return obj;
276}
277EXPORT_SYMBOL(drm_mode_object_find);
278
279/**
Dave Airlief453ba02008-11-07 14:05:41 -0800280 * drm_framebuffer_init - initialize a framebuffer
281 * @dev: DRM device
282 *
283 * LOCKING:
284 * Caller must hold mode config lock.
285 *
286 * Allocates an ID for the framebuffer's parent mode object, sets its mode
287 * functions & device file and adds it to the master fd list.
288 *
289 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200290 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800291 */
292int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
293 const struct drm_framebuffer_funcs *funcs)
294{
295 int ret;
296
297 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200298 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800299 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800300
301 fb->dev = dev;
302 fb->funcs = funcs;
303 dev->mode_config.num_fb++;
304 list_add(&fb->head, &dev->mode_config.fb_list);
305
306 return 0;
307}
308EXPORT_SYMBOL(drm_framebuffer_init);
309
310/**
311 * drm_framebuffer_cleanup - remove a framebuffer object
312 * @fb: framebuffer to remove
313 *
314 * LOCKING:
315 * Caller must hold mode config lock.
316 *
317 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
318 * it, setting it to NULL.
319 */
320void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
321{
322 struct drm_device *dev = fb->dev;
323 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800324 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000325 struct drm_mode_set set;
326 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800327
328 /* remove from any CRTC */
329 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000330 if (crtc->fb == fb) {
331 /* should turn off the crtc */
332 memset(&set, 0, sizeof(struct drm_mode_set));
333 set.crtc = crtc;
334 set.fb = NULL;
335 ret = crtc->funcs->set_config(&set);
336 if (ret)
337 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
338 }
Dave Airlief453ba02008-11-07 14:05:41 -0800339 }
340
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800341 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
342 if (plane->fb == fb) {
343 /* should turn off the crtc */
344 ret = plane->funcs->disable_plane(plane);
345 if (ret)
346 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600347 /* disconnect the plane from the fb and crtc: */
348 plane->fb = NULL;
349 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800350 }
351 }
352
Dave Airlief453ba02008-11-07 14:05:41 -0800353 drm_mode_object_put(dev, &fb->base);
354 list_del(&fb->head);
355 dev->mode_config.num_fb--;
356}
357EXPORT_SYMBOL(drm_framebuffer_cleanup);
358
359/**
360 * drm_crtc_init - Initialise a new CRTC object
361 * @dev: DRM device
362 * @crtc: CRTC object to init
363 * @funcs: callbacks for the new CRTC
364 *
365 * LOCKING:
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200366 * Takes mode_config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800367 *
368 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200369 *
370 * RETURNS:
371 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800372 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200373int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800374 const struct drm_crtc_funcs *funcs)
375{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200376 int ret;
377
Dave Airlief453ba02008-11-07 14:05:41 -0800378 crtc->dev = dev;
379 crtc->funcs = funcs;
380
381 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200382
383 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
384 if (ret)
385 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800386
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300387 crtc->base.properties = &crtc->properties;
388
Dave Airlief453ba02008-11-07 14:05:41 -0800389 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
390 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200391
392 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800393 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200394
395 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800396}
397EXPORT_SYMBOL(drm_crtc_init);
398
399/**
400 * drm_crtc_cleanup - Cleans up the core crtc usage.
401 * @crtc: CRTC to cleanup
402 *
403 * LOCKING:
404 * Caller must hold mode config lock.
405 *
406 * Cleanup @crtc. Removes from drm modesetting space
407 * does NOT free object, caller does that.
408 */
409void drm_crtc_cleanup(struct drm_crtc *crtc)
410{
411 struct drm_device *dev = crtc->dev;
412
413 if (crtc->gamma_store) {
414 kfree(crtc->gamma_store);
415 crtc->gamma_store = NULL;
416 }
417
418 drm_mode_object_put(dev, &crtc->base);
419 list_del(&crtc->head);
420 dev->mode_config.num_crtc--;
421}
422EXPORT_SYMBOL(drm_crtc_cleanup);
423
424/**
425 * drm_mode_probed_add - add a mode to a connector's probed mode list
426 * @connector: connector the new mode
427 * @mode: mode data
428 *
429 * LOCKING:
430 * Caller must hold mode config lock.
431 *
432 * Add @mode to @connector's mode list for later use.
433 */
434void drm_mode_probed_add(struct drm_connector *connector,
435 struct drm_display_mode *mode)
436{
437 list_add(&mode->head, &connector->probed_modes);
438}
439EXPORT_SYMBOL(drm_mode_probed_add);
440
441/**
442 * drm_mode_remove - remove and free a mode
443 * @connector: connector list to modify
444 * @mode: mode to remove
445 *
446 * LOCKING:
447 * Caller must hold mode config lock.
448 *
449 * Remove @mode from @connector's mode list, then free it.
450 */
451void drm_mode_remove(struct drm_connector *connector,
452 struct drm_display_mode *mode)
453{
454 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100455 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800456}
457EXPORT_SYMBOL(drm_mode_remove);
458
459/**
460 * drm_connector_init - Init a preallocated connector
461 * @dev: DRM device
462 * @connector: the connector to init
463 * @funcs: callbacks for this connector
464 * @name: user visible name of the connector
465 *
466 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100467 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800468 *
469 * Initialises a preallocated connector. Connectors should be
470 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200471 *
472 * RETURNS:
473 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800474 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200475int drm_connector_init(struct drm_device *dev,
476 struct drm_connector *connector,
477 const struct drm_connector_funcs *funcs,
478 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800479{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200480 int ret;
481
Dave Airlief453ba02008-11-07 14:05:41 -0800482 mutex_lock(&dev->mode_config.mutex);
483
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200484 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
485 if (ret)
486 goto out;
487
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300488 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800489 connector->dev = dev;
490 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800491 connector->connector_type = connector_type;
492 connector->connector_type_id =
493 ++drm_connector_enum_list[connector_type].count; /* TODO */
494 INIT_LIST_HEAD(&connector->user_modes);
495 INIT_LIST_HEAD(&connector->probed_modes);
496 INIT_LIST_HEAD(&connector->modes);
497 connector->edid_blob_ptr = NULL;
498
499 list_add_tail(&connector->head, &dev->mode_config.connector_list);
500 dev->mode_config.num_connector++;
501
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200502 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
503 drm_connector_attach_property(connector,
504 dev->mode_config.edid_property,
505 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800506
507 drm_connector_attach_property(connector,
508 dev->mode_config.dpms_property, 0);
509
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200510 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800511 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200512
513 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800514}
515EXPORT_SYMBOL(drm_connector_init);
516
517/**
518 * drm_connector_cleanup - cleans up an initialised connector
519 * @connector: connector to cleanup
520 *
521 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100522 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800523 *
524 * Cleans up the connector but doesn't free the object.
525 */
526void drm_connector_cleanup(struct drm_connector *connector)
527{
528 struct drm_device *dev = connector->dev;
529 struct drm_display_mode *mode, *t;
530
531 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
532 drm_mode_remove(connector, mode);
533
534 list_for_each_entry_safe(mode, t, &connector->modes, head)
535 drm_mode_remove(connector, mode);
536
537 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
538 drm_mode_remove(connector, mode);
539
540 mutex_lock(&dev->mode_config.mutex);
541 drm_mode_object_put(dev, &connector->base);
542 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000543 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800544 mutex_unlock(&dev->mode_config.mutex);
545}
546EXPORT_SYMBOL(drm_connector_cleanup);
547
Dave Airliecbc7e222012-02-20 14:16:40 +0000548void drm_connector_unplug_all(struct drm_device *dev)
549{
550 struct drm_connector *connector;
551
552 /* taking the mode config mutex ends up in a clash with sysfs */
553 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
554 drm_sysfs_connector_remove(connector);
555
556}
557EXPORT_SYMBOL(drm_connector_unplug_all);
558
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200559int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000560 struct drm_encoder *encoder,
561 const struct drm_encoder_funcs *funcs,
562 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800563{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200564 int ret;
565
Dave Airlief453ba02008-11-07 14:05:41 -0800566 mutex_lock(&dev->mode_config.mutex);
567
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200568 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
569 if (ret)
570 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800571
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200572 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800573 encoder->encoder_type = encoder_type;
574 encoder->funcs = funcs;
575
576 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
577 dev->mode_config.num_encoder++;
578
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200579 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800580 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200581
582 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800583}
584EXPORT_SYMBOL(drm_encoder_init);
585
586void drm_encoder_cleanup(struct drm_encoder *encoder)
587{
588 struct drm_device *dev = encoder->dev;
589 mutex_lock(&dev->mode_config.mutex);
590 drm_mode_object_put(dev, &encoder->base);
591 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000592 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800593 mutex_unlock(&dev->mode_config.mutex);
594}
595EXPORT_SYMBOL(drm_encoder_cleanup);
596
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800597int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
598 unsigned long possible_crtcs,
599 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600600 const uint32_t *formats, uint32_t format_count,
601 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800602{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200603 int ret;
604
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800605 mutex_lock(&dev->mode_config.mutex);
606
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200607 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
608 if (ret)
609 goto out;
610
Rob Clark4d939142012-05-17 02:23:27 -0600611 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800612 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800613 plane->funcs = funcs;
614 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
615 GFP_KERNEL);
616 if (!plane->format_types) {
617 DRM_DEBUG_KMS("out of memory when allocating plane\n");
618 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200619 ret = -ENOMEM;
620 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800621 }
622
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800623 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800624 plane->format_count = format_count;
625 plane->possible_crtcs = possible_crtcs;
626
Rob Clark0a7eb242011-12-13 20:19:36 -0600627 /* private planes are not exposed to userspace, but depending on
628 * display hardware, might be convenient to allow sharing programming
629 * for the scanout engine with the crtc implementation.
630 */
631 if (!priv) {
632 list_add_tail(&plane->head, &dev->mode_config.plane_list);
633 dev->mode_config.num_plane++;
634 } else {
635 INIT_LIST_HEAD(&plane->head);
636 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800637
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200638 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800639 mutex_unlock(&dev->mode_config.mutex);
640
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200641 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642}
643EXPORT_SYMBOL(drm_plane_init);
644
645void drm_plane_cleanup(struct drm_plane *plane)
646{
647 struct drm_device *dev = plane->dev;
648
649 mutex_lock(&dev->mode_config.mutex);
650 kfree(plane->format_types);
651 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600652 /* if not added to a list, it must be a private plane */
653 if (!list_empty(&plane->head)) {
654 list_del(&plane->head);
655 dev->mode_config.num_plane--;
656 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800657 mutex_unlock(&dev->mode_config.mutex);
658}
659EXPORT_SYMBOL(drm_plane_cleanup);
660
Dave Airlief453ba02008-11-07 14:05:41 -0800661/**
662 * drm_mode_create - create a new display mode
663 * @dev: DRM device
664 *
665 * LOCKING:
666 * Caller must hold DRM mode_config lock.
667 *
668 * Create a new drm_display_mode, give it an ID, and return it.
669 *
670 * RETURNS:
671 * Pointer to new mode on success, NULL on error.
672 */
673struct drm_display_mode *drm_mode_create(struct drm_device *dev)
674{
675 struct drm_display_mode *nmode;
676
677 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
678 if (!nmode)
679 return NULL;
680
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200681 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
682 kfree(nmode);
683 return NULL;
684 }
685
Dave Airlief453ba02008-11-07 14:05:41 -0800686 return nmode;
687}
688EXPORT_SYMBOL(drm_mode_create);
689
690/**
691 * drm_mode_destroy - remove a mode
692 * @dev: DRM device
693 * @mode: mode to remove
694 *
695 * LOCKING:
696 * Caller must hold mode config lock.
697 *
698 * Free @mode's unique identifier, then free it.
699 */
700void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
701{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200702 if (!mode)
703 return;
704
Dave Airlief453ba02008-11-07 14:05:41 -0800705 drm_mode_object_put(dev, &mode->base);
706
707 kfree(mode);
708}
709EXPORT_SYMBOL(drm_mode_destroy);
710
711static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
712{
713 struct drm_property *edid;
714 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800715
716 /*
717 * Standard properties (apply to all connectors)
718 */
719 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
720 DRM_MODE_PROP_IMMUTABLE,
721 "EDID", 0);
722 dev->mode_config.edid_property = edid;
723
Sascha Hauer4a67d392012-02-06 10:58:17 +0100724 dpms = drm_property_create_enum(dev, 0,
725 "DPMS", drm_dpms_enum_list,
726 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800727 dev->mode_config.dpms_property = dpms;
728
729 return 0;
730}
731
732/**
733 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
734 * @dev: DRM device
735 *
736 * Called by a driver the first time a DVI-I connector is made.
737 */
738int drm_mode_create_dvi_i_properties(struct drm_device *dev)
739{
740 struct drm_property *dvi_i_selector;
741 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800742
743 if (dev->mode_config.dvi_i_select_subconnector_property)
744 return 0;
745
746 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100747 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800748 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100749 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800750 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800751 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
752
Sascha Hauer4a67d392012-02-06 10:58:17 +0100753 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800754 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100755 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800756 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800757 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
758
759 return 0;
760}
761EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
762
763/**
764 * drm_create_tv_properties - create TV specific connector properties
765 * @dev: DRM device
766 * @num_modes: number of different TV formats (modes) supported
767 * @modes: array of pointers to strings containing name of each format
768 *
769 * Called by a driver's TV initialization routine, this function creates
770 * the TV specific connector properties for a given device. Caller is
771 * responsible for allocating a list of format names and passing them to
772 * this routine.
773 */
774int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
775 char *modes[])
776{
777 struct drm_property *tv_selector;
778 struct drm_property *tv_subconnector;
779 int i;
780
781 if (dev->mode_config.tv_select_subconnector_property)
782 return 0;
783
784 /*
785 * Basic connector properties
786 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100787 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800788 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100789 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800790 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800791 dev->mode_config.tv_select_subconnector_property = tv_selector;
792
793 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100794 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
795 "subconnector",
796 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800797 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800798 dev->mode_config.tv_subconnector_property = tv_subconnector;
799
800 /*
801 * Other, TV specific properties: margins & TV modes.
802 */
803 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100804 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800805
806 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100807 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800808
809 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100810 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800811
812 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100813 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800814
815 dev->mode_config.tv_mode_property =
816 drm_property_create(dev, DRM_MODE_PROP_ENUM,
817 "mode", num_modes);
818 for (i = 0; i < num_modes; i++)
819 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
820 i, modes[i]);
821
Francisco Jerezb6b79022009-08-02 04:19:20 +0200822 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100823 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200824
825 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100826 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200827
828 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100829 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200830
Francisco Jereza75f0232009-08-12 02:30:10 +0200831 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100832 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200833
834 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100835 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200836
837 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100838 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200839
Dave Airlief453ba02008-11-07 14:05:41 -0800840 return 0;
841}
842EXPORT_SYMBOL(drm_mode_create_tv_properties);
843
844/**
845 * drm_mode_create_scaling_mode_property - create scaling mode property
846 * @dev: DRM device
847 *
848 * Called by a driver the first time it's needed, must be attached to desired
849 * connectors.
850 */
851int drm_mode_create_scaling_mode_property(struct drm_device *dev)
852{
853 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800854
855 if (dev->mode_config.scaling_mode_property)
856 return 0;
857
858 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100859 drm_property_create_enum(dev, 0, "scaling mode",
860 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800861 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800862
863 dev->mode_config.scaling_mode_property = scaling_mode;
864
865 return 0;
866}
867EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
868
869/**
870 * drm_mode_create_dithering_property - create dithering property
871 * @dev: DRM device
872 *
873 * Called by a driver the first time it's needed, must be attached to desired
874 * connectors.
875 */
876int drm_mode_create_dithering_property(struct drm_device *dev)
877{
878 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800879
880 if (dev->mode_config.dithering_mode_property)
881 return 0;
882
883 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100884 drm_property_create_enum(dev, 0, "dithering",
885 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800886 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800887 dev->mode_config.dithering_mode_property = dithering_mode;
888
889 return 0;
890}
891EXPORT_SYMBOL(drm_mode_create_dithering_property);
892
893/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000894 * drm_mode_create_dirty_property - create dirty property
895 * @dev: DRM device
896 *
897 * Called by a driver the first time it's needed, must be attached to desired
898 * connectors.
899 */
900int drm_mode_create_dirty_info_property(struct drm_device *dev)
901{
902 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000903
904 if (dev->mode_config.dirty_info_property)
905 return 0;
906
907 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100908 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000909 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100910 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000911 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000912 dev->mode_config.dirty_info_property = dirty_info;
913
914 return 0;
915}
916EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
917
918/**
Dave Airlief453ba02008-11-07 14:05:41 -0800919 * drm_mode_config_init - initialize DRM mode_configuration structure
920 * @dev: DRM device
921 *
922 * LOCKING:
923 * None, should happen single threaded at init time.
924 *
925 * Initialize @dev's mode_config structure, used for tracking the graphics
926 * configuration of @dev.
927 */
928void drm_mode_config_init(struct drm_device *dev)
929{
930 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000931 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800932 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800933 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
934 INIT_LIST_HEAD(&dev->mode_config.connector_list);
935 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
936 INIT_LIST_HEAD(&dev->mode_config.property_list);
937 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800938 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800939 idr_init(&dev->mode_config.crtc_idr);
940
941 mutex_lock(&dev->mode_config.mutex);
942 drm_mode_create_standard_connector_properties(dev);
943 mutex_unlock(&dev->mode_config.mutex);
944
945 /* Just to be sure */
946 dev->mode_config.num_fb = 0;
947 dev->mode_config.num_connector = 0;
948 dev->mode_config.num_crtc = 0;
949 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800950}
951EXPORT_SYMBOL(drm_mode_config_init);
952
953int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
954{
955 uint32_t total_objects = 0;
956
957 total_objects += dev->mode_config.num_crtc;
958 total_objects += dev->mode_config.num_connector;
959 total_objects += dev->mode_config.num_encoder;
960
Dave Airlief453ba02008-11-07 14:05:41 -0800961 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
962 if (!group->id_list)
963 return -ENOMEM;
964
965 group->num_crtcs = 0;
966 group->num_connectors = 0;
967 group->num_encoders = 0;
968 return 0;
969}
970
971int drm_mode_group_init_legacy_group(struct drm_device *dev,
972 struct drm_mode_group *group)
973{
974 struct drm_crtc *crtc;
975 struct drm_encoder *encoder;
976 struct drm_connector *connector;
977 int ret;
978
979 if ((ret = drm_mode_group_init(dev, group)))
980 return ret;
981
982 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
983 group->id_list[group->num_crtcs++] = crtc->base.id;
984
985 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
986 group->id_list[group->num_crtcs + group->num_encoders++] =
987 encoder->base.id;
988
989 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
990 group->id_list[group->num_crtcs + group->num_encoders +
991 group->num_connectors++] = connector->base.id;
992
993 return 0;
994}
Dave Airlie9c1dfc52012-03-20 06:59:29 +0000995EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -0800996
997/**
998 * drm_mode_config_cleanup - free up DRM mode_config info
999 * @dev: DRM device
1000 *
1001 * LOCKING:
1002 * Caller must hold mode config lock.
1003 *
1004 * Free up all the connectors and CRTCs associated with this DRM device, then
1005 * free up the framebuffers and associated buffer objects.
1006 *
1007 * FIXME: cleanup any dangling user buffer objects too
1008 */
1009void drm_mode_config_cleanup(struct drm_device *dev)
1010{
1011 struct drm_connector *connector, *ot;
1012 struct drm_crtc *crtc, *ct;
1013 struct drm_encoder *encoder, *enct;
1014 struct drm_framebuffer *fb, *fbt;
1015 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001016 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001017
1018 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1019 head) {
1020 encoder->funcs->destroy(encoder);
1021 }
1022
1023 list_for_each_entry_safe(connector, ot,
1024 &dev->mode_config.connector_list, head) {
1025 connector->funcs->destroy(connector);
1026 }
1027
1028 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1029 head) {
1030 drm_property_destroy(dev, property);
1031 }
1032
1033 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1034 fb->funcs->destroy(fb);
1035 }
1036
1037 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1038 crtc->funcs->destroy(crtc);
1039 }
1040
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001041 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1042 head) {
1043 plane->funcs->destroy(plane);
1044 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001045
1046 idr_remove_all(&dev->mode_config.crtc_idr);
1047 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001048}
1049EXPORT_SYMBOL(drm_mode_config_cleanup);
1050
Dave Airlief453ba02008-11-07 14:05:41 -08001051/**
1052 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1053 * @out: drm_mode_modeinfo struct to return to the user
1054 * @in: drm_display_mode to use
1055 *
1056 * LOCKING:
1057 * None.
1058 *
1059 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1060 * the user.
1061 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001062static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1063 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001064{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001065 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1066 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1067 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1068 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1069 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1070 "timing values too large for mode info\n");
1071
Dave Airlief453ba02008-11-07 14:05:41 -08001072 out->clock = in->clock;
1073 out->hdisplay = in->hdisplay;
1074 out->hsync_start = in->hsync_start;
1075 out->hsync_end = in->hsync_end;
1076 out->htotal = in->htotal;
1077 out->hskew = in->hskew;
1078 out->vdisplay = in->vdisplay;
1079 out->vsync_start = in->vsync_start;
1080 out->vsync_end = in->vsync_end;
1081 out->vtotal = in->vtotal;
1082 out->vscan = in->vscan;
1083 out->vrefresh = in->vrefresh;
1084 out->flags = in->flags;
1085 out->type = in->type;
1086 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1087 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1088}
1089
1090/**
1091 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1092 * @out: drm_display_mode to return to the user
1093 * @in: drm_mode_modeinfo to use
1094 *
1095 * LOCKING:
1096 * None.
1097 *
1098 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1099 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001100 *
1101 * RETURNS:
1102 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001103 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001104static int drm_crtc_convert_umode(struct drm_display_mode *out,
1105 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001106{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001107 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1108 return -ERANGE;
1109
Dave Airlief453ba02008-11-07 14:05:41 -08001110 out->clock = in->clock;
1111 out->hdisplay = in->hdisplay;
1112 out->hsync_start = in->hsync_start;
1113 out->hsync_end = in->hsync_end;
1114 out->htotal = in->htotal;
1115 out->hskew = in->hskew;
1116 out->vdisplay = in->vdisplay;
1117 out->vsync_start = in->vsync_start;
1118 out->vsync_end = in->vsync_end;
1119 out->vtotal = in->vtotal;
1120 out->vscan = in->vscan;
1121 out->vrefresh = in->vrefresh;
1122 out->flags = in->flags;
1123 out->type = in->type;
1124 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1125 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001126
1127 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001128}
1129
1130/**
1131 * drm_mode_getresources - get graphics configuration
1132 * @inode: inode from the ioctl
1133 * @filp: file * from the ioctl
1134 * @cmd: cmd from ioctl
1135 * @arg: arg from ioctl
1136 *
1137 * LOCKING:
1138 * Takes mode config lock.
1139 *
1140 * Construct a set of configuration description structures and return
1141 * them to the user, including CRTC, connector and framebuffer configuration.
1142 *
1143 * Called by the user via ioctl.
1144 *
1145 * RETURNS:
1146 * Zero on success, errno on failure.
1147 */
1148int drm_mode_getresources(struct drm_device *dev, void *data,
1149 struct drm_file *file_priv)
1150{
1151 struct drm_mode_card_res *card_res = data;
1152 struct list_head *lh;
1153 struct drm_framebuffer *fb;
1154 struct drm_connector *connector;
1155 struct drm_crtc *crtc;
1156 struct drm_encoder *encoder;
1157 int ret = 0;
1158 int connector_count = 0;
1159 int crtc_count = 0;
1160 int fb_count = 0;
1161 int encoder_count = 0;
1162 int copied = 0, i;
1163 uint32_t __user *fb_id;
1164 uint32_t __user *crtc_id;
1165 uint32_t __user *connector_id;
1166 uint32_t __user *encoder_id;
1167 struct drm_mode_group *mode_group;
1168
Dave Airliefb3b06c2011-02-08 13:55:21 +10001169 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1170 return -EINVAL;
1171
Dave Airlief453ba02008-11-07 14:05:41 -08001172 mutex_lock(&dev->mode_config.mutex);
1173
1174 /*
1175 * For the non-control nodes we need to limit the list of resources
1176 * by IDs in the group list for this node
1177 */
1178 list_for_each(lh, &file_priv->fbs)
1179 fb_count++;
1180
1181 mode_group = &file_priv->master->minor->mode_group;
1182 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1183
1184 list_for_each(lh, &dev->mode_config.crtc_list)
1185 crtc_count++;
1186
1187 list_for_each(lh, &dev->mode_config.connector_list)
1188 connector_count++;
1189
1190 list_for_each(lh, &dev->mode_config.encoder_list)
1191 encoder_count++;
1192 } else {
1193
1194 crtc_count = mode_group->num_crtcs;
1195 connector_count = mode_group->num_connectors;
1196 encoder_count = mode_group->num_encoders;
1197 }
1198
1199 card_res->max_height = dev->mode_config.max_height;
1200 card_res->min_height = dev->mode_config.min_height;
1201 card_res->max_width = dev->mode_config.max_width;
1202 card_res->min_width = dev->mode_config.min_width;
1203
1204 /* handle this in 4 parts */
1205 /* FBs */
1206 if (card_res->count_fbs >= fb_count) {
1207 copied = 0;
1208 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001209 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001210 if (put_user(fb->base.id, fb_id + copied)) {
1211 ret = -EFAULT;
1212 goto out;
1213 }
1214 copied++;
1215 }
1216 }
1217 card_res->count_fbs = fb_count;
1218
1219 /* CRTCs */
1220 if (card_res->count_crtcs >= crtc_count) {
1221 copied = 0;
1222 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1223 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1224 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1225 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001226 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001227 if (put_user(crtc->base.id, crtc_id + copied)) {
1228 ret = -EFAULT;
1229 goto out;
1230 }
1231 copied++;
1232 }
1233 } else {
1234 for (i = 0; i < mode_group->num_crtcs; i++) {
1235 if (put_user(mode_group->id_list[i],
1236 crtc_id + copied)) {
1237 ret = -EFAULT;
1238 goto out;
1239 }
1240 copied++;
1241 }
1242 }
1243 }
1244 card_res->count_crtcs = crtc_count;
1245
1246 /* Encoders */
1247 if (card_res->count_encoders >= encoder_count) {
1248 copied = 0;
1249 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1250 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1251 list_for_each_entry(encoder,
1252 &dev->mode_config.encoder_list,
1253 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001254 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1255 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001256 if (put_user(encoder->base.id, encoder_id +
1257 copied)) {
1258 ret = -EFAULT;
1259 goto out;
1260 }
1261 copied++;
1262 }
1263 } else {
1264 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1265 if (put_user(mode_group->id_list[i],
1266 encoder_id + copied)) {
1267 ret = -EFAULT;
1268 goto out;
1269 }
1270 copied++;
1271 }
1272
1273 }
1274 }
1275 card_res->count_encoders = encoder_count;
1276
1277 /* Connectors */
1278 if (card_res->count_connectors >= connector_count) {
1279 copied = 0;
1280 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1281 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1282 list_for_each_entry(connector,
1283 &dev->mode_config.connector_list,
1284 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1286 connector->base.id,
1287 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001288 if (put_user(connector->base.id,
1289 connector_id + copied)) {
1290 ret = -EFAULT;
1291 goto out;
1292 }
1293 copied++;
1294 }
1295 } else {
1296 int start = mode_group->num_crtcs +
1297 mode_group->num_encoders;
1298 for (i = start; i < start + mode_group->num_connectors; i++) {
1299 if (put_user(mode_group->id_list[i],
1300 connector_id + copied)) {
1301 ret = -EFAULT;
1302 goto out;
1303 }
1304 copied++;
1305 }
1306 }
1307 }
1308 card_res->count_connectors = connector_count;
1309
Jerome Glisse94401062010-07-15 15:43:25 -04001310 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001311 card_res->count_connectors, card_res->count_encoders);
1312
1313out:
1314 mutex_unlock(&dev->mode_config.mutex);
1315 return ret;
1316}
1317
1318/**
1319 * drm_mode_getcrtc - get CRTC configuration
1320 * @inode: inode from the ioctl
1321 * @filp: file * from the ioctl
1322 * @cmd: cmd from ioctl
1323 * @arg: arg from ioctl
1324 *
1325 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001326 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001327 *
1328 * Construct a CRTC configuration structure to return to the user.
1329 *
1330 * Called by the user via ioctl.
1331 *
1332 * RETURNS:
1333 * Zero on success, errno on failure.
1334 */
1335int drm_mode_getcrtc(struct drm_device *dev,
1336 void *data, struct drm_file *file_priv)
1337{
1338 struct drm_mode_crtc *crtc_resp = data;
1339 struct drm_crtc *crtc;
1340 struct drm_mode_object *obj;
1341 int ret = 0;
1342
Dave Airliefb3b06c2011-02-08 13:55:21 +10001343 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1344 return -EINVAL;
1345
Dave Airlief453ba02008-11-07 14:05:41 -08001346 mutex_lock(&dev->mode_config.mutex);
1347
1348 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1349 DRM_MODE_OBJECT_CRTC);
1350 if (!obj) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354 crtc = obj_to_crtc(obj);
1355
1356 crtc_resp->x = crtc->x;
1357 crtc_resp->y = crtc->y;
1358 crtc_resp->gamma_size = crtc->gamma_size;
1359 if (crtc->fb)
1360 crtc_resp->fb_id = crtc->fb->base.id;
1361 else
1362 crtc_resp->fb_id = 0;
1363
1364 if (crtc->enabled) {
1365
1366 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1367 crtc_resp->mode_valid = 1;
1368
1369 } else {
1370 crtc_resp->mode_valid = 0;
1371 }
1372
1373out:
1374 mutex_unlock(&dev->mode_config.mutex);
1375 return ret;
1376}
1377
1378/**
1379 * drm_mode_getconnector - get connector configuration
1380 * @inode: inode from the ioctl
1381 * @filp: file * from the ioctl
1382 * @cmd: cmd from ioctl
1383 * @arg: arg from ioctl
1384 *
1385 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001386 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001387 *
1388 * Construct a connector configuration structure to return to the user.
1389 *
1390 * Called by the user via ioctl.
1391 *
1392 * RETURNS:
1393 * Zero on success, errno on failure.
1394 */
1395int drm_mode_getconnector(struct drm_device *dev, void *data,
1396 struct drm_file *file_priv)
1397{
1398 struct drm_mode_get_connector *out_resp = data;
1399 struct drm_mode_object *obj;
1400 struct drm_connector *connector;
1401 struct drm_display_mode *mode;
1402 int mode_count = 0;
1403 int props_count = 0;
1404 int encoders_count = 0;
1405 int ret = 0;
1406 int copied = 0;
1407 int i;
1408 struct drm_mode_modeinfo u_mode;
1409 struct drm_mode_modeinfo __user *mode_ptr;
1410 uint32_t __user *prop_ptr;
1411 uint64_t __user *prop_values;
1412 uint32_t __user *encoder_ptr;
1413
Dave Airliefb3b06c2011-02-08 13:55:21 +10001414 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1415 return -EINVAL;
1416
Dave Airlief453ba02008-11-07 14:05:41 -08001417 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1418
Jerome Glisse94401062010-07-15 15:43:25 -04001419 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001420
1421 mutex_lock(&dev->mode_config.mutex);
1422
1423 obj = drm_mode_object_find(dev, out_resp->connector_id,
1424 DRM_MODE_OBJECT_CONNECTOR);
1425 if (!obj) {
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429 connector = obj_to_connector(obj);
1430
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001431 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001432
1433 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1434 if (connector->encoder_ids[i] != 0) {
1435 encoders_count++;
1436 }
1437 }
1438
1439 if (out_resp->count_modes == 0) {
1440 connector->funcs->fill_modes(connector,
1441 dev->mode_config.max_width,
1442 dev->mode_config.max_height);
1443 }
1444
1445 /* delayed so we get modes regardless of pre-fill_modes state */
1446 list_for_each_entry(mode, &connector->modes, head)
1447 mode_count++;
1448
1449 out_resp->connector_id = connector->base.id;
1450 out_resp->connector_type = connector->connector_type;
1451 out_resp->connector_type_id = connector->connector_type_id;
1452 out_resp->mm_width = connector->display_info.width_mm;
1453 out_resp->mm_height = connector->display_info.height_mm;
1454 out_resp->subpixel = connector->display_info.subpixel_order;
1455 out_resp->connection = connector->status;
1456 if (connector->encoder)
1457 out_resp->encoder_id = connector->encoder->base.id;
1458 else
1459 out_resp->encoder_id = 0;
1460
1461 /*
1462 * This ioctl is called twice, once to determine how much space is
1463 * needed, and the 2nd time to fill it.
1464 */
1465 if ((out_resp->count_modes >= mode_count) && mode_count) {
1466 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001467 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001468 list_for_each_entry(mode, &connector->modes, head) {
1469 drm_crtc_convert_to_umode(&u_mode, mode);
1470 if (copy_to_user(mode_ptr + copied,
1471 &u_mode, sizeof(u_mode))) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 }
1478 out_resp->count_modes = mode_count;
1479
1480 if ((out_resp->count_props >= props_count) && props_count) {
1481 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001482 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1483 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001484 for (i = 0; i < connector->properties.count; i++) {
1485 if (put_user(connector->properties.ids[i],
1486 prop_ptr + copied)) {
1487 ret = -EFAULT;
1488 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001489 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001490
1491 if (put_user(connector->properties.values[i],
1492 prop_values + copied)) {
1493 ret = -EFAULT;
1494 goto out;
1495 }
1496 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001497 }
1498 }
1499 out_resp->count_props = props_count;
1500
1501 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1502 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001503 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001504 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1505 if (connector->encoder_ids[i] != 0) {
1506 if (put_user(connector->encoder_ids[i],
1507 encoder_ptr + copied)) {
1508 ret = -EFAULT;
1509 goto out;
1510 }
1511 copied++;
1512 }
1513 }
1514 }
1515 out_resp->count_encoders = encoders_count;
1516
1517out:
1518 mutex_unlock(&dev->mode_config.mutex);
1519 return ret;
1520}
1521
1522int drm_mode_getencoder(struct drm_device *dev, void *data,
1523 struct drm_file *file_priv)
1524{
1525 struct drm_mode_get_encoder *enc_resp = data;
1526 struct drm_mode_object *obj;
1527 struct drm_encoder *encoder;
1528 int ret = 0;
1529
Dave Airliefb3b06c2011-02-08 13:55:21 +10001530 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1531 return -EINVAL;
1532
Dave Airlief453ba02008-11-07 14:05:41 -08001533 mutex_lock(&dev->mode_config.mutex);
1534 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1535 DRM_MODE_OBJECT_ENCODER);
1536 if (!obj) {
1537 ret = -EINVAL;
1538 goto out;
1539 }
1540 encoder = obj_to_encoder(obj);
1541
1542 if (encoder->crtc)
1543 enc_resp->crtc_id = encoder->crtc->base.id;
1544 else
1545 enc_resp->crtc_id = 0;
1546 enc_resp->encoder_type = encoder->encoder_type;
1547 enc_resp->encoder_id = encoder->base.id;
1548 enc_resp->possible_crtcs = encoder->possible_crtcs;
1549 enc_resp->possible_clones = encoder->possible_clones;
1550
1551out:
1552 mutex_unlock(&dev->mode_config.mutex);
1553 return ret;
1554}
1555
1556/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001557 * drm_mode_getplane_res - get plane info
1558 * @dev: DRM device
1559 * @data: ioctl data
1560 * @file_priv: DRM file info
1561 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001562 * LOCKING:
1563 * Takes mode config lock.
1564 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001565 * Return an plane count and set of IDs.
1566 */
1567int drm_mode_getplane_res(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569{
1570 struct drm_mode_get_plane_res *plane_resp = data;
1571 struct drm_mode_config *config;
1572 struct drm_plane *plane;
1573 uint32_t __user *plane_ptr;
1574 int copied = 0, ret = 0;
1575
1576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1577 return -EINVAL;
1578
1579 mutex_lock(&dev->mode_config.mutex);
1580 config = &dev->mode_config;
1581
1582 /*
1583 * This ioctl is called twice, once to determine how much space is
1584 * needed, and the 2nd time to fill it.
1585 */
1586 if (config->num_plane &&
1587 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001588 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001589
1590 list_for_each_entry(plane, &config->plane_list, head) {
1591 if (put_user(plane->base.id, plane_ptr + copied)) {
1592 ret = -EFAULT;
1593 goto out;
1594 }
1595 copied++;
1596 }
1597 }
1598 plane_resp->count_planes = config->num_plane;
1599
1600out:
1601 mutex_unlock(&dev->mode_config.mutex);
1602 return ret;
1603}
1604
1605/**
1606 * drm_mode_getplane - get plane info
1607 * @dev: DRM device
1608 * @data: ioctl data
1609 * @file_priv: DRM file info
1610 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001611 * LOCKING:
1612 * Takes mode config lock.
1613 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001614 * Return plane info, including formats supported, gamma size, any
1615 * current fb, etc.
1616 */
1617int drm_mode_getplane(struct drm_device *dev, void *data,
1618 struct drm_file *file_priv)
1619{
1620 struct drm_mode_get_plane *plane_resp = data;
1621 struct drm_mode_object *obj;
1622 struct drm_plane *plane;
1623 uint32_t __user *format_ptr;
1624 int ret = 0;
1625
1626 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1627 return -EINVAL;
1628
1629 mutex_lock(&dev->mode_config.mutex);
1630 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1631 DRM_MODE_OBJECT_PLANE);
1632 if (!obj) {
1633 ret = -ENOENT;
1634 goto out;
1635 }
1636 plane = obj_to_plane(obj);
1637
1638 if (plane->crtc)
1639 plane_resp->crtc_id = plane->crtc->base.id;
1640 else
1641 plane_resp->crtc_id = 0;
1642
1643 if (plane->fb)
1644 plane_resp->fb_id = plane->fb->base.id;
1645 else
1646 plane_resp->fb_id = 0;
1647
1648 plane_resp->plane_id = plane->base.id;
1649 plane_resp->possible_crtcs = plane->possible_crtcs;
1650 plane_resp->gamma_size = plane->gamma_size;
1651
1652 /*
1653 * This ioctl is called twice, once to determine how much space is
1654 * needed, and the 2nd time to fill it.
1655 */
1656 if (plane->format_count &&
1657 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001658 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001659 if (copy_to_user(format_ptr,
1660 plane->format_types,
1661 sizeof(uint32_t) * plane->format_count)) {
1662 ret = -EFAULT;
1663 goto out;
1664 }
1665 }
1666 plane_resp->count_format_types = plane->format_count;
1667
1668out:
1669 mutex_unlock(&dev->mode_config.mutex);
1670 return ret;
1671}
1672
1673/**
1674 * drm_mode_setplane - set up or tear down an plane
1675 * @dev: DRM device
1676 * @data: ioctl data*
1677 * @file_prive: DRM file info
1678 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001679 * LOCKING:
1680 * Takes mode config lock.
1681 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001682 * Set plane info, including placement, fb, scaling, and other factors.
1683 * Or pass a NULL fb to disable.
1684 */
1685int drm_mode_setplane(struct drm_device *dev, void *data,
1686 struct drm_file *file_priv)
1687{
1688 struct drm_mode_set_plane *plane_req = data;
1689 struct drm_mode_object *obj;
1690 struct drm_plane *plane;
1691 struct drm_crtc *crtc;
1692 struct drm_framebuffer *fb;
1693 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001694 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001695 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001696
1697 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1698 return -EINVAL;
1699
1700 mutex_lock(&dev->mode_config.mutex);
1701
1702 /*
1703 * First, find the plane, crtc, and fb objects. If not available,
1704 * we don't bother to call the driver.
1705 */
1706 obj = drm_mode_object_find(dev, plane_req->plane_id,
1707 DRM_MODE_OBJECT_PLANE);
1708 if (!obj) {
1709 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1710 plane_req->plane_id);
1711 ret = -ENOENT;
1712 goto out;
1713 }
1714 plane = obj_to_plane(obj);
1715
1716 /* No fb means shut it down */
1717 if (!plane_req->fb_id) {
1718 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001719 plane->crtc = NULL;
1720 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001721 goto out;
1722 }
1723
1724 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1725 DRM_MODE_OBJECT_CRTC);
1726 if (!obj) {
1727 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1728 plane_req->crtc_id);
1729 ret = -ENOENT;
1730 goto out;
1731 }
1732 crtc = obj_to_crtc(obj);
1733
1734 obj = drm_mode_object_find(dev, plane_req->fb_id,
1735 DRM_MODE_OBJECT_FB);
1736 if (!obj) {
1737 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1738 plane_req->fb_id);
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742 fb = obj_to_fb(obj);
1743
Ville Syrjälä62443be2011-12-20 00:06:47 +02001744 /* Check whether this plane supports the fb pixel format. */
1745 for (i = 0; i < plane->format_count; i++)
1746 if (fb->pixel_format == plane->format_types[i])
1747 break;
1748 if (i == plane->format_count) {
1749 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1750 ret = -EINVAL;
1751 goto out;
1752 }
1753
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001754 fb_width = fb->width << 16;
1755 fb_height = fb->height << 16;
1756
1757 /* Make sure source coordinates are inside the fb. */
1758 if (plane_req->src_w > fb_width ||
1759 plane_req->src_x > fb_width - plane_req->src_w ||
1760 plane_req->src_h > fb_height ||
1761 plane_req->src_y > fb_height - plane_req->src_h) {
1762 DRM_DEBUG_KMS("Invalid source coordinates "
1763 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1764 plane_req->src_w >> 16,
1765 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1766 plane_req->src_h >> 16,
1767 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1768 plane_req->src_x >> 16,
1769 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1770 plane_req->src_y >> 16,
1771 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1772 ret = -ENOSPC;
1773 goto out;
1774 }
1775
Ville Syrjälä687a0402011-12-20 00:06:45 +02001776 /* Give drivers some help against integer overflows */
1777 if (plane_req->crtc_w > INT_MAX ||
1778 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1779 plane_req->crtc_h > INT_MAX ||
1780 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1781 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1782 plane_req->crtc_w, plane_req->crtc_h,
1783 plane_req->crtc_x, plane_req->crtc_y);
1784 ret = -ERANGE;
1785 goto out;
1786 }
1787
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001788 ret = plane->funcs->update_plane(plane, crtc, fb,
1789 plane_req->crtc_x, plane_req->crtc_y,
1790 plane_req->crtc_w, plane_req->crtc_h,
1791 plane_req->src_x, plane_req->src_y,
1792 plane_req->src_w, plane_req->src_h);
1793 if (!ret) {
1794 plane->crtc = crtc;
1795 plane->fb = fb;
1796 }
1797
1798out:
1799 mutex_unlock(&dev->mode_config.mutex);
1800
1801 return ret;
1802}
1803
1804/**
Dave Airlief453ba02008-11-07 14:05:41 -08001805 * drm_mode_setcrtc - set CRTC configuration
1806 * @inode: inode from the ioctl
1807 * @filp: file * from the ioctl
1808 * @cmd: cmd from ioctl
1809 * @arg: arg from ioctl
1810 *
1811 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001812 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001813 *
1814 * Build a new CRTC configuration based on user request.
1815 *
1816 * Called by the user via ioctl.
1817 *
1818 * RETURNS:
1819 * Zero on success, errno on failure.
1820 */
1821int drm_mode_setcrtc(struct drm_device *dev, void *data,
1822 struct drm_file *file_priv)
1823{
1824 struct drm_mode_config *config = &dev->mode_config;
1825 struct drm_mode_crtc *crtc_req = data;
1826 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001827 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001828 struct drm_connector **connector_set = NULL, *connector;
1829 struct drm_framebuffer *fb = NULL;
1830 struct drm_display_mode *mode = NULL;
1831 struct drm_mode_set set;
1832 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001833 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001834 int i;
1835
Dave Airliefb3b06c2011-02-08 13:55:21 +10001836 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1837 return -EINVAL;
1838
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001839 /* For some reason crtc x/y offsets are signed internally. */
1840 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1841 return -ERANGE;
1842
Dave Airlief453ba02008-11-07 14:05:41 -08001843 mutex_lock(&dev->mode_config.mutex);
1844 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1845 DRM_MODE_OBJECT_CRTC);
1846 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001847 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001848 ret = -EINVAL;
1849 goto out;
1850 }
1851 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001852 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001853
1854 if (crtc_req->mode_valid) {
1855 /* If we have a mode we need a framebuffer. */
1856 /* If we pass -1, set the mode with the currently bound fb */
1857 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001858 if (!crtc->fb) {
1859 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1860 ret = -EINVAL;
1861 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001862 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001863 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001864 } else {
1865 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1866 DRM_MODE_OBJECT_FB);
1867 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001868 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1869 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001870 ret = -EINVAL;
1871 goto out;
1872 }
1873 fb = obj_to_fb(obj);
1874 }
1875
1876 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001877 if (!mode) {
1878 ret = -ENOMEM;
1879 goto out;
1880 }
1881
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001882 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1883 if (ret) {
1884 DRM_DEBUG_KMS("Invalid mode\n");
1885 goto out;
1886 }
1887
Dave Airlief453ba02008-11-07 14:05:41 -08001888 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001889
1890 if (mode->hdisplay > fb->width ||
1891 mode->vdisplay > fb->height ||
1892 crtc_req->x > fb->width - mode->hdisplay ||
1893 crtc_req->y > fb->height - mode->vdisplay) {
1894 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1895 mode->hdisplay, mode->vdisplay,
1896 crtc_req->x, crtc_req->y,
1897 fb->width, fb->height);
1898 ret = -ENOSPC;
1899 goto out;
1900 }
Dave Airlief453ba02008-11-07 14:05:41 -08001901 }
1902
1903 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001904 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001905 ret = -EINVAL;
1906 goto out;
1907 }
1908
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001909 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001910 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001911 crtc_req->count_connectors);
1912 ret = -EINVAL;
1913 goto out;
1914 }
1915
1916 if (crtc_req->count_connectors > 0) {
1917 u32 out_id;
1918
1919 /* Avoid unbounded kernel memory allocation */
1920 if (crtc_req->count_connectors > config->num_connector) {
1921 ret = -EINVAL;
1922 goto out;
1923 }
1924
1925 connector_set = kmalloc(crtc_req->count_connectors *
1926 sizeof(struct drm_connector *),
1927 GFP_KERNEL);
1928 if (!connector_set) {
1929 ret = -ENOMEM;
1930 goto out;
1931 }
1932
1933 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001934 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001935 if (get_user(out_id, &set_connectors_ptr[i])) {
1936 ret = -EFAULT;
1937 goto out;
1938 }
1939
1940 obj = drm_mode_object_find(dev, out_id,
1941 DRM_MODE_OBJECT_CONNECTOR);
1942 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001943 DRM_DEBUG_KMS("Connector id %d unknown\n",
1944 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001945 ret = -EINVAL;
1946 goto out;
1947 }
1948 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001949 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1950 connector->base.id,
1951 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001952
1953 connector_set[i] = connector;
1954 }
1955 }
1956
1957 set.crtc = crtc;
1958 set.x = crtc_req->x;
1959 set.y = crtc_req->y;
1960 set.mode = mode;
1961 set.connectors = connector_set;
1962 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001963 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001964 ret = crtc->funcs->set_config(&set);
1965
1966out:
1967 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001968 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001969 mutex_unlock(&dev->mode_config.mutex);
1970 return ret;
1971}
1972
1973int drm_mode_cursor_ioctl(struct drm_device *dev,
1974 void *data, struct drm_file *file_priv)
1975{
1976 struct drm_mode_cursor *req = data;
1977 struct drm_mode_object *obj;
1978 struct drm_crtc *crtc;
1979 int ret = 0;
1980
Dave Airliefb3b06c2011-02-08 13:55:21 +10001981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1982 return -EINVAL;
1983
Jesse Barnesacb4b992011-11-07 12:03:23 -08001984 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001985 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001986
1987 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001988 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001989 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001990 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 ret = -EINVAL;
1992 goto out;
1993 }
1994 crtc = obj_to_crtc(obj);
1995
1996 if (req->flags & DRM_MODE_CURSOR_BO) {
1997 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001998 ret = -ENXIO;
1999 goto out;
2000 }
2001 /* Turns off the cursor if handle is 0 */
2002 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2003 req->width, req->height);
2004 }
2005
2006 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2007 if (crtc->funcs->cursor_move) {
2008 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2009 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002010 ret = -EFAULT;
2011 goto out;
2012 }
2013 }
2014out:
2015 mutex_unlock(&dev->mode_config.mutex);
2016 return ret;
2017}
2018
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002019/* Original addfb only supported RGB formats, so figure out which one */
2020uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2021{
2022 uint32_t fmt;
2023
2024 switch (bpp) {
2025 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002026 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002027 break;
2028 case 16:
2029 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002030 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002031 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 break;
2034 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002035 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002036 break;
2037 case 32:
2038 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002039 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002040 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002041 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002042 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 break;
2045 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002046 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2047 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002048 break;
2049 }
2050
2051 return fmt;
2052}
2053EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2054
Dave Airlief453ba02008-11-07 14:05:41 -08002055/**
2056 * drm_mode_addfb - add an FB to the graphics configuration
2057 * @inode: inode from the ioctl
2058 * @filp: file * from the ioctl
2059 * @cmd: cmd from ioctl
2060 * @arg: arg from ioctl
2061 *
2062 * LOCKING:
2063 * Takes mode config lock.
2064 *
2065 * Add a new FB to the specified CRTC, given a user request.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * RETURNS:
2070 * Zero on success, errno on failure.
2071 */
2072int drm_mode_addfb(struct drm_device *dev,
2073 void *data, struct drm_file *file_priv)
2074{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002075 struct drm_mode_fb_cmd *or = data;
2076 struct drm_mode_fb_cmd2 r = {};
2077 struct drm_mode_config *config = &dev->mode_config;
2078 struct drm_framebuffer *fb;
2079 int ret = 0;
2080
2081 /* Use new struct with format internally */
2082 r.fb_id = or->fb_id;
2083 r.width = or->width;
2084 r.height = or->height;
2085 r.pitches[0] = or->pitch;
2086 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2087 r.handles[0] = or->handle;
2088
2089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2090 return -EINVAL;
2091
Jesse Barnesacb4b992011-11-07 12:03:23 -08002092 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002093 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002094
2095 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002096 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002097
2098 mutex_lock(&dev->mode_config.mutex);
2099
2100 /* TODO check buffer is sufficiently large */
2101 /* TODO setup destructor callback */
2102
2103 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2104 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002105 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002106 ret = PTR_ERR(fb);
2107 goto out;
2108 }
2109
2110 or->fb_id = fb->base.id;
2111 list_add(&fb->filp_head, &file_priv->fbs);
2112 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2113
2114out:
2115 mutex_unlock(&dev->mode_config.mutex);
2116 return ret;
2117}
2118
Ville Syrjälä935b5972011-12-20 00:06:48 +02002119static int format_check(struct drm_mode_fb_cmd2 *r)
2120{
2121 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2122
2123 switch (format) {
2124 case DRM_FORMAT_C8:
2125 case DRM_FORMAT_RGB332:
2126 case DRM_FORMAT_BGR233:
2127 case DRM_FORMAT_XRGB4444:
2128 case DRM_FORMAT_XBGR4444:
2129 case DRM_FORMAT_RGBX4444:
2130 case DRM_FORMAT_BGRX4444:
2131 case DRM_FORMAT_ARGB4444:
2132 case DRM_FORMAT_ABGR4444:
2133 case DRM_FORMAT_RGBA4444:
2134 case DRM_FORMAT_BGRA4444:
2135 case DRM_FORMAT_XRGB1555:
2136 case DRM_FORMAT_XBGR1555:
2137 case DRM_FORMAT_RGBX5551:
2138 case DRM_FORMAT_BGRX5551:
2139 case DRM_FORMAT_ARGB1555:
2140 case DRM_FORMAT_ABGR1555:
2141 case DRM_FORMAT_RGBA5551:
2142 case DRM_FORMAT_BGRA5551:
2143 case DRM_FORMAT_RGB565:
2144 case DRM_FORMAT_BGR565:
2145 case DRM_FORMAT_RGB888:
2146 case DRM_FORMAT_BGR888:
2147 case DRM_FORMAT_XRGB8888:
2148 case DRM_FORMAT_XBGR8888:
2149 case DRM_FORMAT_RGBX8888:
2150 case DRM_FORMAT_BGRX8888:
2151 case DRM_FORMAT_ARGB8888:
2152 case DRM_FORMAT_ABGR8888:
2153 case DRM_FORMAT_RGBA8888:
2154 case DRM_FORMAT_BGRA8888:
2155 case DRM_FORMAT_XRGB2101010:
2156 case DRM_FORMAT_XBGR2101010:
2157 case DRM_FORMAT_RGBX1010102:
2158 case DRM_FORMAT_BGRX1010102:
2159 case DRM_FORMAT_ARGB2101010:
2160 case DRM_FORMAT_ABGR2101010:
2161 case DRM_FORMAT_RGBA1010102:
2162 case DRM_FORMAT_BGRA1010102:
2163 case DRM_FORMAT_YUYV:
2164 case DRM_FORMAT_YVYU:
2165 case DRM_FORMAT_UYVY:
2166 case DRM_FORMAT_VYUY:
2167 case DRM_FORMAT_AYUV:
2168 case DRM_FORMAT_NV12:
2169 case DRM_FORMAT_NV21:
2170 case DRM_FORMAT_NV16:
2171 case DRM_FORMAT_NV61:
2172 case DRM_FORMAT_YUV410:
2173 case DRM_FORMAT_YVU410:
2174 case DRM_FORMAT_YUV411:
2175 case DRM_FORMAT_YVU411:
2176 case DRM_FORMAT_YUV420:
2177 case DRM_FORMAT_YVU420:
2178 case DRM_FORMAT_YUV422:
2179 case DRM_FORMAT_YVU422:
2180 case DRM_FORMAT_YUV444:
2181 case DRM_FORMAT_YVU444:
2182 return 0;
2183 default:
2184 return -EINVAL;
2185 }
2186}
2187
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002188static int framebuffer_check(struct drm_mode_fb_cmd2 *r)
2189{
2190 int ret, hsub, vsub, num_planes, i;
2191
2192 ret = format_check(r);
2193 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002194 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002195 return ret;
2196 }
2197
2198 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2199 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2200 num_planes = drm_format_num_planes(r->pixel_format);
2201
2202 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002203 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002204 return -EINVAL;
2205 }
2206
2207 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002208 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002209 return -EINVAL;
2210 }
2211
2212 for (i = 0; i < num_planes; i++) {
2213 unsigned int width = r->width / (i != 0 ? hsub : 1);
2214
2215 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002216 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002217 return -EINVAL;
2218 }
2219
2220 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002221 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002222 return -EINVAL;
2223 }
2224 }
2225
2226 return 0;
2227}
2228
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002229/**
2230 * drm_mode_addfb2 - add an FB to the graphics configuration
2231 * @inode: inode from the ioctl
2232 * @filp: file * from the ioctl
2233 * @cmd: cmd from ioctl
2234 * @arg: arg from ioctl
2235 *
2236 * LOCKING:
2237 * Takes mode config lock.
2238 *
2239 * Add a new FB to the specified CRTC, given a user request with format.
2240 *
2241 * Called by the user via ioctl.
2242 *
2243 * RETURNS:
2244 * Zero on success, errno on failure.
2245 */
2246int drm_mode_addfb2(struct drm_device *dev,
2247 void *data, struct drm_file *file_priv)
2248{
2249 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002250 struct drm_mode_config *config = &dev->mode_config;
2251 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002252 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002253
Dave Airliefb3b06c2011-02-08 13:55:21 +10002254 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2255 return -EINVAL;
2256
Dave Airlief453ba02008-11-07 14:05:41 -08002257 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002258 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002259 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002260 return -EINVAL;
2261 }
2262 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002263 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002264 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002265 return -EINVAL;
2266 }
2267
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002268 ret = framebuffer_check(r);
2269 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002270 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002271
Dave Airlief453ba02008-11-07 14:05:41 -08002272 mutex_lock(&dev->mode_config.mutex);
2273
Dave Airlief453ba02008-11-07 14:05:41 -08002274 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002275 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002276 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002277 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002278 goto out;
2279 }
2280
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002281 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002282 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002283 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002284
2285out:
2286 mutex_unlock(&dev->mode_config.mutex);
2287 return ret;
2288}
2289
2290/**
2291 * drm_mode_rmfb - remove an FB from the configuration
2292 * @inode: inode from the ioctl
2293 * @filp: file * from the ioctl
2294 * @cmd: cmd from ioctl
2295 * @arg: arg from ioctl
2296 *
2297 * LOCKING:
2298 * Takes mode config lock.
2299 *
2300 * Remove the FB specified by the user.
2301 *
2302 * Called by the user via ioctl.
2303 *
2304 * RETURNS:
2305 * Zero on success, errno on failure.
2306 */
2307int drm_mode_rmfb(struct drm_device *dev,
2308 void *data, struct drm_file *file_priv)
2309{
2310 struct drm_mode_object *obj;
2311 struct drm_framebuffer *fb = NULL;
2312 struct drm_framebuffer *fbl = NULL;
2313 uint32_t *id = data;
2314 int ret = 0;
2315 int found = 0;
2316
Dave Airliefb3b06c2011-02-08 13:55:21 +10002317 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2318 return -EINVAL;
2319
Dave Airlief453ba02008-11-07 14:05:41 -08002320 mutex_lock(&dev->mode_config.mutex);
2321 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002322 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002323 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002324 ret = -EINVAL;
2325 goto out;
2326 }
2327 fb = obj_to_fb(obj);
2328
2329 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2330 if (fb == fbl)
2331 found = 1;
2332
2333 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002334 ret = -EINVAL;
2335 goto out;
2336 }
2337
2338 /* TODO release all crtc connected to the framebuffer */
2339 /* TODO unhock the destructor from the buffer object */
2340
2341 list_del(&fb->filp_head);
2342 fb->funcs->destroy(fb);
2343
2344out:
2345 mutex_unlock(&dev->mode_config.mutex);
2346 return ret;
2347}
2348
2349/**
2350 * drm_mode_getfb - get FB info
2351 * @inode: inode from the ioctl
2352 * @filp: file * from the ioctl
2353 * @cmd: cmd from ioctl
2354 * @arg: arg from ioctl
2355 *
2356 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002357 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002358 *
2359 * Lookup the FB given its ID and return info about it.
2360 *
2361 * Called by the user via ioctl.
2362 *
2363 * RETURNS:
2364 * Zero on success, errno on failure.
2365 */
2366int drm_mode_getfb(struct drm_device *dev,
2367 void *data, struct drm_file *file_priv)
2368{
2369 struct drm_mode_fb_cmd *r = data;
2370 struct drm_mode_object *obj;
2371 struct drm_framebuffer *fb;
2372 int ret = 0;
2373
Dave Airliefb3b06c2011-02-08 13:55:21 +10002374 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2375 return -EINVAL;
2376
Dave Airlief453ba02008-11-07 14:05:41 -08002377 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002378 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002379 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002380 ret = -EINVAL;
2381 goto out;
2382 }
2383 fb = obj_to_fb(obj);
2384
2385 r->height = fb->height;
2386 r->width = fb->width;
2387 r->depth = fb->depth;
2388 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002389 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002390 fb->funcs->create_handle(fb, file_priv, &r->handle);
2391
2392out:
2393 mutex_unlock(&dev->mode_config.mutex);
2394 return ret;
2395}
2396
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002397int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2398 void *data, struct drm_file *file_priv)
2399{
2400 struct drm_clip_rect __user *clips_ptr;
2401 struct drm_clip_rect *clips = NULL;
2402 struct drm_mode_fb_dirty_cmd *r = data;
2403 struct drm_mode_object *obj;
2404 struct drm_framebuffer *fb;
2405 unsigned flags;
2406 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002407 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002408
Dave Airliefb3b06c2011-02-08 13:55:21 +10002409 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2410 return -EINVAL;
2411
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002412 mutex_lock(&dev->mode_config.mutex);
2413 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2414 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002415 ret = -EINVAL;
2416 goto out_err1;
2417 }
2418 fb = obj_to_fb(obj);
2419
2420 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002421 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002422
2423 if (!num_clips != !clips_ptr) {
2424 ret = -EINVAL;
2425 goto out_err1;
2426 }
2427
2428 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2429
2430 /* If userspace annotates copy, clips must come in pairs */
2431 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2432 ret = -EINVAL;
2433 goto out_err1;
2434 }
2435
2436 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002437 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2438 ret = -EINVAL;
2439 goto out_err1;
2440 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002441 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2442 if (!clips) {
2443 ret = -ENOMEM;
2444 goto out_err1;
2445 }
2446
2447 ret = copy_from_user(clips, clips_ptr,
2448 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002449 if (ret) {
2450 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002451 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002452 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002453 }
2454
2455 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002456 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2457 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002458 } else {
2459 ret = -ENOSYS;
2460 goto out_err2;
2461 }
2462
2463out_err2:
2464 kfree(clips);
2465out_err1:
2466 mutex_unlock(&dev->mode_config.mutex);
2467 return ret;
2468}
2469
2470
Dave Airlief453ba02008-11-07 14:05:41 -08002471/**
2472 * drm_fb_release - remove and free the FBs on this file
2473 * @filp: file * from the ioctl
2474 *
2475 * LOCKING:
2476 * Takes mode config lock.
2477 *
2478 * Destroy all the FBs associated with @filp.
2479 *
2480 * Called by the user via ioctl.
2481 *
2482 * RETURNS:
2483 * Zero on success, errno on failure.
2484 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002485void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002486{
Dave Airlief453ba02008-11-07 14:05:41 -08002487 struct drm_device *dev = priv->minor->dev;
2488 struct drm_framebuffer *fb, *tfb;
2489
2490 mutex_lock(&dev->mode_config.mutex);
2491 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2492 list_del(&fb->filp_head);
2493 fb->funcs->destroy(fb);
2494 }
2495 mutex_unlock(&dev->mode_config.mutex);
2496}
2497
2498/**
2499 * drm_mode_attachmode - add a mode to the user mode list
2500 * @dev: DRM device
2501 * @connector: connector to add the mode to
2502 * @mode: mode to add
2503 *
2504 * Add @mode to @connector's user mode list.
2505 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002506static void drm_mode_attachmode(struct drm_device *dev,
2507 struct drm_connector *connector,
2508 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002509{
Dave Airlief453ba02008-11-07 14:05:41 -08002510 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002511}
2512
2513int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002514 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002515{
2516 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002517 int ret = 0;
2518 struct drm_display_mode *dup_mode, *next;
2519 LIST_HEAD(list);
2520
Dave Airlief453ba02008-11-07 14:05:41 -08002521 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2522 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002523 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002524 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002525 dup_mode = drm_mode_duplicate(dev, mode);
2526 if (!dup_mode) {
2527 ret = -ENOMEM;
2528 goto out;
2529 }
2530 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002531 }
2532 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002533
2534 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2535 if (!connector->encoder)
2536 continue;
2537 if (connector->encoder->crtc == crtc)
2538 list_move_tail(list.next, &connector->user_modes);
2539 }
2540
2541 WARN_ON(!list_empty(&list));
2542
2543 out:
2544 list_for_each_entry_safe(dup_mode, next, &list, head)
2545 drm_mode_destroy(dev, dup_mode);
2546
2547 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002548}
2549EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2550
2551static int drm_mode_detachmode(struct drm_device *dev,
2552 struct drm_connector *connector,
2553 struct drm_display_mode *mode)
2554{
2555 int found = 0;
2556 int ret = 0;
2557 struct drm_display_mode *match_mode, *t;
2558
2559 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2560 if (drm_mode_equal(match_mode, mode)) {
2561 list_del(&match_mode->head);
2562 drm_mode_destroy(dev, match_mode);
2563 found = 1;
2564 break;
2565 }
2566 }
2567
2568 if (!found)
2569 ret = -EINVAL;
2570
2571 return ret;
2572}
2573
2574int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2575{
2576 struct drm_connector *connector;
2577
2578 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2579 drm_mode_detachmode(dev, connector, mode);
2580 }
2581 return 0;
2582}
2583EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2584
2585/**
2586 * drm_fb_attachmode - Attach a user mode to an connector
2587 * @inode: inode from the ioctl
2588 * @filp: file * from the ioctl
2589 * @cmd: cmd from ioctl
2590 * @arg: arg from ioctl
2591 *
2592 * This attaches a user specified mode to an connector.
2593 * Called by the user via ioctl.
2594 *
2595 * RETURNS:
2596 * Zero on success, errno on failure.
2597 */
2598int drm_mode_attachmode_ioctl(struct drm_device *dev,
2599 void *data, struct drm_file *file_priv)
2600{
2601 struct drm_mode_mode_cmd *mode_cmd = data;
2602 struct drm_connector *connector;
2603 struct drm_display_mode *mode;
2604 struct drm_mode_object *obj;
2605 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002606 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002607
Dave Airliefb3b06c2011-02-08 13:55:21 +10002608 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2609 return -EINVAL;
2610
Dave Airlief453ba02008-11-07 14:05:41 -08002611 mutex_lock(&dev->mode_config.mutex);
2612
2613 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2614 if (!obj) {
2615 ret = -EINVAL;
2616 goto out;
2617 }
2618 connector = obj_to_connector(obj);
2619
2620 mode = drm_mode_create(dev);
2621 if (!mode) {
2622 ret = -ENOMEM;
2623 goto out;
2624 }
2625
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002626 ret = drm_crtc_convert_umode(mode, umode);
2627 if (ret) {
2628 DRM_DEBUG_KMS("Invalid mode\n");
2629 drm_mode_destroy(dev, mode);
2630 goto out;
2631 }
Dave Airlief453ba02008-11-07 14:05:41 -08002632
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002633 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002634out:
2635 mutex_unlock(&dev->mode_config.mutex);
2636 return ret;
2637}
2638
2639
2640/**
2641 * drm_fb_detachmode - Detach a user specified mode from an connector
2642 * @inode: inode from the ioctl
2643 * @filp: file * from the ioctl
2644 * @cmd: cmd from ioctl
2645 * @arg: arg from ioctl
2646 *
2647 * Called by the user via ioctl.
2648 *
2649 * RETURNS:
2650 * Zero on success, errno on failure.
2651 */
2652int drm_mode_detachmode_ioctl(struct drm_device *dev,
2653 void *data, struct drm_file *file_priv)
2654{
2655 struct drm_mode_object *obj;
2656 struct drm_mode_mode_cmd *mode_cmd = data;
2657 struct drm_connector *connector;
2658 struct drm_display_mode mode;
2659 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002660 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002661
Dave Airliefb3b06c2011-02-08 13:55:21 +10002662 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2663 return -EINVAL;
2664
Dave Airlief453ba02008-11-07 14:05:41 -08002665 mutex_lock(&dev->mode_config.mutex);
2666
2667 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2668 if (!obj) {
2669 ret = -EINVAL;
2670 goto out;
2671 }
2672 connector = obj_to_connector(obj);
2673
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002674 ret = drm_crtc_convert_umode(&mode, umode);
2675 if (ret) {
2676 DRM_DEBUG_KMS("Invalid mode\n");
2677 goto out;
2678 }
2679
Dave Airlief453ba02008-11-07 14:05:41 -08002680 ret = drm_mode_detachmode(dev, connector, &mode);
2681out:
2682 mutex_unlock(&dev->mode_config.mutex);
2683 return ret;
2684}
2685
2686struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2687 const char *name, int num_values)
2688{
2689 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002690 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002691
2692 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2693 if (!property)
2694 return NULL;
2695
2696 if (num_values) {
2697 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2698 if (!property->values)
2699 goto fail;
2700 }
2701
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002702 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2703 if (ret)
2704 goto fail;
2705
Dave Airlief453ba02008-11-07 14:05:41 -08002706 property->flags = flags;
2707 property->num_values = num_values;
2708 INIT_LIST_HEAD(&property->enum_blob_list);
2709
Vinson Lee471dd2e2011-11-10 11:55:40 -08002710 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002711 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002712 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2713 }
Dave Airlief453ba02008-11-07 14:05:41 -08002714
2715 list_add_tail(&property->head, &dev->mode_config.property_list);
2716 return property;
2717fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002718 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002719 kfree(property);
2720 return NULL;
2721}
2722EXPORT_SYMBOL(drm_property_create);
2723
Sascha Hauer4a67d392012-02-06 10:58:17 +01002724struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2725 const char *name,
2726 const struct drm_prop_enum_list *props,
2727 int num_values)
2728{
2729 struct drm_property *property;
2730 int i, ret;
2731
2732 flags |= DRM_MODE_PROP_ENUM;
2733
2734 property = drm_property_create(dev, flags, name, num_values);
2735 if (!property)
2736 return NULL;
2737
2738 for (i = 0; i < num_values; i++) {
2739 ret = drm_property_add_enum(property, i,
2740 props[i].type,
2741 props[i].name);
2742 if (ret) {
2743 drm_property_destroy(dev, property);
2744 return NULL;
2745 }
2746 }
2747
2748 return property;
2749}
2750EXPORT_SYMBOL(drm_property_create_enum);
2751
Rob Clark49e27542012-05-17 02:23:26 -06002752struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2753 int flags, const char *name,
2754 const struct drm_prop_enum_list *props,
2755 int num_values)
2756{
2757 struct drm_property *property;
2758 int i, ret;
2759
2760 flags |= DRM_MODE_PROP_BITMASK;
2761
2762 property = drm_property_create(dev, flags, name, num_values);
2763 if (!property)
2764 return NULL;
2765
2766 for (i = 0; i < num_values; i++) {
2767 ret = drm_property_add_enum(property, i,
2768 props[i].type,
2769 props[i].name);
2770 if (ret) {
2771 drm_property_destroy(dev, property);
2772 return NULL;
2773 }
2774 }
2775
2776 return property;
2777}
2778EXPORT_SYMBOL(drm_property_create_bitmask);
2779
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002780struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2781 const char *name,
2782 uint64_t min, uint64_t max)
2783{
2784 struct drm_property *property;
2785
2786 flags |= DRM_MODE_PROP_RANGE;
2787
2788 property = drm_property_create(dev, flags, name, 2);
2789 if (!property)
2790 return NULL;
2791
2792 property->values[0] = min;
2793 property->values[1] = max;
2794
2795 return property;
2796}
2797EXPORT_SYMBOL(drm_property_create_range);
2798
Dave Airlief453ba02008-11-07 14:05:41 -08002799int drm_property_add_enum(struct drm_property *property, int index,
2800 uint64_t value, const char *name)
2801{
2802 struct drm_property_enum *prop_enum;
2803
Rob Clark49e27542012-05-17 02:23:26 -06002804 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2805 return -EINVAL;
2806
2807 /*
2808 * Bitmask enum properties have the additional constraint of values
2809 * from 0 to 63
2810 */
2811 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002812 return -EINVAL;
2813
2814 if (!list_empty(&property->enum_blob_list)) {
2815 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2816 if (prop_enum->value == value) {
2817 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2818 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2819 return 0;
2820 }
2821 }
2822 }
2823
2824 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2825 if (!prop_enum)
2826 return -ENOMEM;
2827
2828 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2829 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2830 prop_enum->value = value;
2831
2832 property->values[index] = value;
2833 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2834 return 0;
2835}
2836EXPORT_SYMBOL(drm_property_add_enum);
2837
2838void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2839{
2840 struct drm_property_enum *prop_enum, *pt;
2841
2842 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2843 list_del(&prop_enum->head);
2844 kfree(prop_enum);
2845 }
2846
2847 if (property->num_values)
2848 kfree(property->values);
2849 drm_mode_object_put(dev, &property->base);
2850 list_del(&property->head);
2851 kfree(property);
2852}
2853EXPORT_SYMBOL(drm_property_destroy);
2854
Paulo Zanoniafea2ad2012-05-15 18:09:00 -03002855void drm_connector_attach_property(struct drm_connector *connector,
Dave Airlief453ba02008-11-07 14:05:41 -08002856 struct drm_property *property, uint64_t init_val)
2857{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002858 drm_object_attach_property(&connector->base, property, init_val);
Dave Airlief453ba02008-11-07 14:05:41 -08002859}
2860EXPORT_SYMBOL(drm_connector_attach_property);
2861
2862int drm_connector_property_set_value(struct drm_connector *connector,
2863 struct drm_property *property, uint64_t value)
2864{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002865 return drm_object_property_set_value(&connector->base, property, value);
Dave Airlief453ba02008-11-07 14:05:41 -08002866}
2867EXPORT_SYMBOL(drm_connector_property_set_value);
2868
2869int drm_connector_property_get_value(struct drm_connector *connector,
2870 struct drm_property *property, uint64_t *val)
2871{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002872 return drm_object_property_get_value(&connector->base, property, val);
Dave Airlief453ba02008-11-07 14:05:41 -08002873}
2874EXPORT_SYMBOL(drm_connector_property_get_value);
2875
Paulo Zanonic5431882012-05-15 18:09:02 -03002876void drm_object_attach_property(struct drm_mode_object *obj,
2877 struct drm_property *property,
2878 uint64_t init_val)
2879{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002880 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002881
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002882 if (count == DRM_OBJECT_MAX_PROPERTY) {
2883 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2884 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2885 "you see this message on the same object type.\n",
2886 obj->type);
2887 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002888 }
2889
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002890 obj->properties->ids[count] = property->base.id;
2891 obj->properties->values[count] = init_val;
2892 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002893}
2894EXPORT_SYMBOL(drm_object_attach_property);
2895
2896int drm_object_property_set_value(struct drm_mode_object *obj,
2897 struct drm_property *property, uint64_t val)
2898{
2899 int i;
2900
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002901 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002902 if (obj->properties->ids[i] == property->base.id) {
2903 obj->properties->values[i] = val;
2904 return 0;
2905 }
2906 }
2907
2908 return -EINVAL;
2909}
2910EXPORT_SYMBOL(drm_object_property_set_value);
2911
2912int drm_object_property_get_value(struct drm_mode_object *obj,
2913 struct drm_property *property, uint64_t *val)
2914{
2915 int i;
2916
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002917 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002918 if (obj->properties->ids[i] == property->base.id) {
2919 *val = obj->properties->values[i];
2920 return 0;
2921 }
2922 }
2923
2924 return -EINVAL;
2925}
2926EXPORT_SYMBOL(drm_object_property_get_value);
2927
Dave Airlief453ba02008-11-07 14:05:41 -08002928int drm_mode_getproperty_ioctl(struct drm_device *dev,
2929 void *data, struct drm_file *file_priv)
2930{
2931 struct drm_mode_object *obj;
2932 struct drm_mode_get_property *out_resp = data;
2933 struct drm_property *property;
2934 int enum_count = 0;
2935 int blob_count = 0;
2936 int value_count = 0;
2937 int ret = 0, i;
2938 int copied;
2939 struct drm_property_enum *prop_enum;
2940 struct drm_mode_property_enum __user *enum_ptr;
2941 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002942 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002943 uint64_t __user *values_ptr;
2944 uint32_t __user *blob_length_ptr;
2945
Dave Airliefb3b06c2011-02-08 13:55:21 +10002946 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2947 return -EINVAL;
2948
Dave Airlief453ba02008-11-07 14:05:41 -08002949 mutex_lock(&dev->mode_config.mutex);
2950 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2951 if (!obj) {
2952 ret = -EINVAL;
2953 goto done;
2954 }
2955 property = obj_to_property(obj);
2956
Rob Clark49e27542012-05-17 02:23:26 -06002957 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002958 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2959 enum_count++;
2960 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2961 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2962 blob_count++;
2963 }
2964
2965 value_count = property->num_values;
2966
2967 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2968 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2969 out_resp->flags = property->flags;
2970
2971 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002972 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002973 for (i = 0; i < value_count; i++) {
2974 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2975 ret = -EFAULT;
2976 goto done;
2977 }
2978 }
2979 }
2980 out_resp->count_values = value_count;
2981
Rob Clark49e27542012-05-17 02:23:26 -06002982 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002983 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2984 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002985 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002986 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2987
2988 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2989 ret = -EFAULT;
2990 goto done;
2991 }
2992
2993 if (copy_to_user(&enum_ptr[copied].name,
2994 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2995 ret = -EFAULT;
2996 goto done;
2997 }
2998 copied++;
2999 }
3000 }
3001 out_resp->count_enum_blobs = enum_count;
3002 }
3003
3004 if (property->flags & DRM_MODE_PROP_BLOB) {
3005 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3006 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003007 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3008 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003009
3010 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3011 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3012 ret = -EFAULT;
3013 goto done;
3014 }
3015
3016 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3017 ret = -EFAULT;
3018 goto done;
3019 }
3020
3021 copied++;
3022 }
3023 }
3024 out_resp->count_enum_blobs = blob_count;
3025 }
3026done:
3027 mutex_unlock(&dev->mode_config.mutex);
3028 return ret;
3029}
3030
3031static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3032 void *data)
3033{
3034 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003035 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003036
3037 if (!length || !data)
3038 return NULL;
3039
3040 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3041 if (!blob)
3042 return NULL;
3043
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003044 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3045 if (ret) {
3046 kfree(blob);
3047 return NULL;
3048 }
3049
Dave Airlief453ba02008-11-07 14:05:41 -08003050 blob->length = length;
3051
3052 memcpy(blob->data, data, length);
3053
Dave Airlief453ba02008-11-07 14:05:41 -08003054 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3055 return blob;
3056}
3057
3058static void drm_property_destroy_blob(struct drm_device *dev,
3059 struct drm_property_blob *blob)
3060{
3061 drm_mode_object_put(dev, &blob->base);
3062 list_del(&blob->head);
3063 kfree(blob);
3064}
3065
3066int drm_mode_getblob_ioctl(struct drm_device *dev,
3067 void *data, struct drm_file *file_priv)
3068{
3069 struct drm_mode_object *obj;
3070 struct drm_mode_get_blob *out_resp = data;
3071 struct drm_property_blob *blob;
3072 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003073 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003074
Dave Airliefb3b06c2011-02-08 13:55:21 +10003075 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3076 return -EINVAL;
3077
Dave Airlief453ba02008-11-07 14:05:41 -08003078 mutex_lock(&dev->mode_config.mutex);
3079 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3080 if (!obj) {
3081 ret = -EINVAL;
3082 goto done;
3083 }
3084 blob = obj_to_blob(obj);
3085
3086 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003087 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003088 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3089 ret = -EFAULT;
3090 goto done;
3091 }
3092 }
3093 out_resp->length = blob->length;
3094
3095done:
3096 mutex_unlock(&dev->mode_config.mutex);
3097 return ret;
3098}
3099
3100int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3101 struct edid *edid)
3102{
3103 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003104 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003105
3106 if (connector->edid_blob_ptr)
3107 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3108
3109 /* Delete edid, when there is none. */
3110 if (!edid) {
3111 connector->edid_blob_ptr = NULL;
3112 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3113 return ret;
3114 }
3115
Adam Jackson7466f4c2010-03-29 21:43:23 +00003116 size = EDID_LENGTH * (1 + edid->extensions);
3117 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3118 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003119
3120 ret = drm_connector_property_set_value(connector,
3121 dev->mode_config.edid_property,
3122 connector->edid_blob_ptr->base.id);
3123
3124 return ret;
3125}
3126EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3127
Paulo Zanoni26a34812012-05-15 18:08:59 -03003128static bool drm_property_change_is_valid(struct drm_property *property,
3129 __u64 value)
3130{
3131 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3132 return false;
3133 if (property->flags & DRM_MODE_PROP_RANGE) {
3134 if (value < property->values[0] || value > property->values[1])
3135 return false;
3136 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003137 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3138 int i;
3139 __u64 valid_mask = 0;
3140 for (i = 0; i < property->num_values; i++)
3141 valid_mask |= (1ULL << property->values[i]);
3142 return !(value & ~valid_mask);
Paulo Zanoni26a34812012-05-15 18:08:59 -03003143 } else {
3144 int i;
3145 for (i = 0; i < property->num_values; i++)
3146 if (property->values[i] == value)
3147 return true;
3148 return false;
3149 }
3150}
3151
Dave Airlief453ba02008-11-07 14:05:41 -08003152int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3153 void *data, struct drm_file *file_priv)
3154{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003155 struct drm_mode_connector_set_property *conn_set_prop = data;
3156 struct drm_mode_obj_set_property obj_set_prop = {
3157 .value = conn_set_prop->value,
3158 .prop_id = conn_set_prop->prop_id,
3159 .obj_id = conn_set_prop->connector_id,
3160 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3161 };
Dave Airlief453ba02008-11-07 14:05:41 -08003162
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003163 /* It does all the locking and checking we need */
3164 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003165}
3166
Paulo Zanonic5431882012-05-15 18:09:02 -03003167static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3168 struct drm_property *property,
3169 uint64_t value)
3170{
3171 int ret = -EINVAL;
3172 struct drm_connector *connector = obj_to_connector(obj);
3173
3174 /* Do DPMS ourselves */
3175 if (property == connector->dev->mode_config.dpms_property) {
3176 if (connector->funcs->dpms)
3177 (*connector->funcs->dpms)(connector, (int)value);
3178 ret = 0;
3179 } else if (connector->funcs->set_property)
3180 ret = connector->funcs->set_property(connector, property, value);
3181
3182 /* store the property value if successful */
3183 if (!ret)
3184 drm_connector_property_set_value(connector, property, value);
3185 return ret;
3186}
3187
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003188static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3189 struct drm_property *property,
3190 uint64_t value)
3191{
3192 int ret = -EINVAL;
3193 struct drm_crtc *crtc = obj_to_crtc(obj);
3194
3195 if (crtc->funcs->set_property)
3196 ret = crtc->funcs->set_property(crtc, property, value);
3197 if (!ret)
3198 drm_object_property_set_value(obj, property, value);
3199
3200 return ret;
3201}
3202
Rob Clark4d939142012-05-17 02:23:27 -06003203static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3204 struct drm_property *property,
3205 uint64_t value)
3206{
3207 int ret = -EINVAL;
3208 struct drm_plane *plane = obj_to_plane(obj);
3209
3210 if (plane->funcs->set_property)
3211 ret = plane->funcs->set_property(plane, property, value);
3212 if (!ret)
3213 drm_object_property_set_value(obj, property, value);
3214
3215 return ret;
3216}
3217
Paulo Zanonic5431882012-05-15 18:09:02 -03003218int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3219 struct drm_file *file_priv)
3220{
3221 struct drm_mode_obj_get_properties *arg = data;
3222 struct drm_mode_object *obj;
3223 int ret = 0;
3224 int i;
3225 int copied = 0;
3226 int props_count = 0;
3227 uint32_t __user *props_ptr;
3228 uint64_t __user *prop_values_ptr;
3229
3230 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3231 return -EINVAL;
3232
3233 mutex_lock(&dev->mode_config.mutex);
3234
3235 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3236 if (!obj) {
3237 ret = -EINVAL;
3238 goto out;
3239 }
3240 if (!obj->properties) {
3241 ret = -EINVAL;
3242 goto out;
3243 }
3244
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003245 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003246
3247 /* This ioctl is called twice, once to determine how much space is
3248 * needed, and the 2nd time to fill it. */
3249 if ((arg->count_props >= props_count) && props_count) {
3250 copied = 0;
3251 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3252 prop_values_ptr = (uint64_t __user *)(unsigned long)
3253 (arg->prop_values_ptr);
3254 for (i = 0; i < props_count; i++) {
3255 if (put_user(obj->properties->ids[i],
3256 props_ptr + copied)) {
3257 ret = -EFAULT;
3258 goto out;
3259 }
3260 if (put_user(obj->properties->values[i],
3261 prop_values_ptr + copied)) {
3262 ret = -EFAULT;
3263 goto out;
3264 }
3265 copied++;
3266 }
3267 }
3268 arg->count_props = props_count;
3269out:
3270 mutex_unlock(&dev->mode_config.mutex);
3271 return ret;
3272}
3273
3274int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3275 struct drm_file *file_priv)
3276{
3277 struct drm_mode_obj_set_property *arg = data;
3278 struct drm_mode_object *arg_obj;
3279 struct drm_mode_object *prop_obj;
3280 struct drm_property *property;
3281 int ret = -EINVAL;
3282 int i;
3283
3284 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3285 return -EINVAL;
3286
3287 mutex_lock(&dev->mode_config.mutex);
3288
3289 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3290 if (!arg_obj)
3291 goto out;
3292 if (!arg_obj->properties)
3293 goto out;
3294
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003295 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003296 if (arg_obj->properties->ids[i] == arg->prop_id)
3297 break;
3298
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003299 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003300 goto out;
3301
3302 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3303 DRM_MODE_OBJECT_PROPERTY);
3304 if (!prop_obj)
3305 goto out;
3306 property = obj_to_property(prop_obj);
3307
3308 if (!drm_property_change_is_valid(property, arg->value))
3309 goto out;
3310
3311 switch (arg_obj->type) {
3312 case DRM_MODE_OBJECT_CONNECTOR:
3313 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3314 arg->value);
3315 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003316 case DRM_MODE_OBJECT_CRTC:
3317 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3318 break;
Rob Clark4d939142012-05-17 02:23:27 -06003319 case DRM_MODE_OBJECT_PLANE:
3320 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3321 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003322 }
3323
3324out:
3325 mutex_unlock(&dev->mode_config.mutex);
3326 return ret;
3327}
3328
Dave Airlief453ba02008-11-07 14:05:41 -08003329int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3330 struct drm_encoder *encoder)
3331{
3332 int i;
3333
3334 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3335 if (connector->encoder_ids[i] == 0) {
3336 connector->encoder_ids[i] = encoder->base.id;
3337 return 0;
3338 }
3339 }
3340 return -ENOMEM;
3341}
3342EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3343
3344void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3345 struct drm_encoder *encoder)
3346{
3347 int i;
3348 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3349 if (connector->encoder_ids[i] == encoder->base.id) {
3350 connector->encoder_ids[i] = 0;
3351 if (connector->encoder == encoder)
3352 connector->encoder = NULL;
3353 break;
3354 }
3355 }
3356}
3357EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3358
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003359int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003360 int gamma_size)
3361{
3362 crtc->gamma_size = gamma_size;
3363
3364 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3365 if (!crtc->gamma_store) {
3366 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003367 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003368 }
3369
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003370 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003371}
3372EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3373
3374int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3375 void *data, struct drm_file *file_priv)
3376{
3377 struct drm_mode_crtc_lut *crtc_lut = data;
3378 struct drm_mode_object *obj;
3379 struct drm_crtc *crtc;
3380 void *r_base, *g_base, *b_base;
3381 int size;
3382 int ret = 0;
3383
Dave Airliefb3b06c2011-02-08 13:55:21 +10003384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3385 return -EINVAL;
3386
Dave Airlief453ba02008-11-07 14:05:41 -08003387 mutex_lock(&dev->mode_config.mutex);
3388 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3389 if (!obj) {
3390 ret = -EINVAL;
3391 goto out;
3392 }
3393 crtc = obj_to_crtc(obj);
3394
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003395 if (crtc->funcs->gamma_set == NULL) {
3396 ret = -ENOSYS;
3397 goto out;
3398 }
3399
Dave Airlief453ba02008-11-07 14:05:41 -08003400 /* memcpy into gamma store */
3401 if (crtc_lut->gamma_size != crtc->gamma_size) {
3402 ret = -EINVAL;
3403 goto out;
3404 }
3405
3406 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3407 r_base = crtc->gamma_store;
3408 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3409 ret = -EFAULT;
3410 goto out;
3411 }
3412
3413 g_base = r_base + size;
3414 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3415 ret = -EFAULT;
3416 goto out;
3417 }
3418
3419 b_base = g_base + size;
3420 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3421 ret = -EFAULT;
3422 goto out;
3423 }
3424
James Simmons72034252010-08-03 01:33:19 +01003425 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003426
3427out:
3428 mutex_unlock(&dev->mode_config.mutex);
3429 return ret;
3430
3431}
3432
3433int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3434 void *data, struct drm_file *file_priv)
3435{
3436 struct drm_mode_crtc_lut *crtc_lut = data;
3437 struct drm_mode_object *obj;
3438 struct drm_crtc *crtc;
3439 void *r_base, *g_base, *b_base;
3440 int size;
3441 int ret = 0;
3442
Dave Airliefb3b06c2011-02-08 13:55:21 +10003443 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3444 return -EINVAL;
3445
Dave Airlief453ba02008-11-07 14:05:41 -08003446 mutex_lock(&dev->mode_config.mutex);
3447 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3448 if (!obj) {
3449 ret = -EINVAL;
3450 goto out;
3451 }
3452 crtc = obj_to_crtc(obj);
3453
3454 /* memcpy into gamma store */
3455 if (crtc_lut->gamma_size != crtc->gamma_size) {
3456 ret = -EINVAL;
3457 goto out;
3458 }
3459
3460 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3461 r_base = crtc->gamma_store;
3462 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3463 ret = -EFAULT;
3464 goto out;
3465 }
3466
3467 g_base = r_base + size;
3468 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3469 ret = -EFAULT;
3470 goto out;
3471 }
3472
3473 b_base = g_base + size;
3474 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3475 ret = -EFAULT;
3476 goto out;
3477 }
3478out:
3479 mutex_unlock(&dev->mode_config.mutex);
3480 return ret;
3481}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003482
3483int drm_mode_page_flip_ioctl(struct drm_device *dev,
3484 void *data, struct drm_file *file_priv)
3485{
3486 struct drm_mode_crtc_page_flip *page_flip = data;
3487 struct drm_mode_object *obj;
3488 struct drm_crtc *crtc;
3489 struct drm_framebuffer *fb;
3490 struct drm_pending_vblank_event *e = NULL;
3491 unsigned long flags;
3492 int ret = -EINVAL;
3493
3494 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3495 page_flip->reserved != 0)
3496 return -EINVAL;
3497
3498 mutex_lock(&dev->mode_config.mutex);
3499 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3500 if (!obj)
3501 goto out;
3502 crtc = obj_to_crtc(obj);
3503
Chris Wilson90c1efd2010-07-17 20:23:26 +01003504 if (crtc->fb == NULL) {
3505 /* The framebuffer is currently unbound, presumably
3506 * due to a hotplug event, that userspace has not
3507 * yet discovered.
3508 */
3509 ret = -EBUSY;
3510 goto out;
3511 }
3512
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003513 if (crtc->funcs->page_flip == NULL)
3514 goto out;
3515
3516 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3517 if (!obj)
3518 goto out;
3519 fb = obj_to_fb(obj);
3520
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003521 if (crtc->mode.hdisplay > fb->width ||
3522 crtc->mode.vdisplay > fb->height ||
3523 crtc->x > fb->width - crtc->mode.hdisplay ||
3524 crtc->y > fb->height - crtc->mode.vdisplay) {
3525 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3526 fb->width, fb->height,
3527 crtc->mode.hdisplay, crtc->mode.vdisplay,
3528 crtc->x, crtc->y);
3529 ret = -ENOSPC;
3530 goto out;
3531 }
3532
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003533 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3534 ret = -ENOMEM;
3535 spin_lock_irqsave(&dev->event_lock, flags);
3536 if (file_priv->event_space < sizeof e->event) {
3537 spin_unlock_irqrestore(&dev->event_lock, flags);
3538 goto out;
3539 }
3540 file_priv->event_space -= sizeof e->event;
3541 spin_unlock_irqrestore(&dev->event_lock, flags);
3542
3543 e = kzalloc(sizeof *e, GFP_KERNEL);
3544 if (e == NULL) {
3545 spin_lock_irqsave(&dev->event_lock, flags);
3546 file_priv->event_space += sizeof e->event;
3547 spin_unlock_irqrestore(&dev->event_lock, flags);
3548 goto out;
3549 }
3550
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003551 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003552 e->event.base.length = sizeof e->event;
3553 e->event.user_data = page_flip->user_data;
3554 e->base.event = &e->event.base;
3555 e->base.file_priv = file_priv;
3556 e->base.destroy =
3557 (void (*) (struct drm_pending_event *)) kfree;
3558 }
3559
3560 ret = crtc->funcs->page_flip(crtc, fb, e);
3561 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003562 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3563 spin_lock_irqsave(&dev->event_lock, flags);
3564 file_priv->event_space += sizeof e->event;
3565 spin_unlock_irqrestore(&dev->event_lock, flags);
3566 kfree(e);
3567 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003568 }
3569
3570out:
3571 mutex_unlock(&dev->mode_config.mutex);
3572 return ret;
3573}
Chris Wilsoneb033552011-01-24 15:11:08 +00003574
3575void drm_mode_config_reset(struct drm_device *dev)
3576{
3577 struct drm_crtc *crtc;
3578 struct drm_encoder *encoder;
3579 struct drm_connector *connector;
3580
3581 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3582 if (crtc->funcs->reset)
3583 crtc->funcs->reset(crtc);
3584
3585 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3586 if (encoder->funcs->reset)
3587 encoder->funcs->reset(encoder);
3588
3589 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3590 if (connector->funcs->reset)
3591 connector->funcs->reset(connector);
3592}
3593EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003594
3595int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3596 void *data, struct drm_file *file_priv)
3597{
3598 struct drm_mode_create_dumb *args = data;
3599
3600 if (!dev->driver->dumb_create)
3601 return -ENOSYS;
3602 return dev->driver->dumb_create(file_priv, dev, args);
3603}
3604
3605int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3606 void *data, struct drm_file *file_priv)
3607{
3608 struct drm_mode_map_dumb *args = data;
3609
3610 /* call driver ioctl to get mmap offset */
3611 if (!dev->driver->dumb_map_offset)
3612 return -ENOSYS;
3613
3614 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3615}
3616
3617int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3618 void *data, struct drm_file *file_priv)
3619{
3620 struct drm_mode_destroy_dumb *args = data;
3621
3622 if (!dev->driver->dumb_destroy)
3623 return -ENOSYS;
3624
3625 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3626}
Dave Airlie248dbc22011-11-29 20:02:54 +00003627
3628/*
3629 * Just need to support RGB formats here for compat with code that doesn't
3630 * use pixel formats directly yet.
3631 */
3632void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3633 int *bpp)
3634{
3635 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003636 case DRM_FORMAT_RGB332:
3637 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003638 *depth = 8;
3639 *bpp = 8;
3640 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003641 case DRM_FORMAT_XRGB1555:
3642 case DRM_FORMAT_XBGR1555:
3643 case DRM_FORMAT_RGBX5551:
3644 case DRM_FORMAT_BGRX5551:
3645 case DRM_FORMAT_ARGB1555:
3646 case DRM_FORMAT_ABGR1555:
3647 case DRM_FORMAT_RGBA5551:
3648 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003649 *depth = 15;
3650 *bpp = 16;
3651 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003652 case DRM_FORMAT_RGB565:
3653 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003654 *depth = 16;
3655 *bpp = 16;
3656 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003657 case DRM_FORMAT_RGB888:
3658 case DRM_FORMAT_BGR888:
3659 *depth = 24;
3660 *bpp = 24;
3661 break;
3662 case DRM_FORMAT_XRGB8888:
3663 case DRM_FORMAT_XBGR8888:
3664 case DRM_FORMAT_RGBX8888:
3665 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003666 *depth = 24;
3667 *bpp = 32;
3668 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003669 case DRM_FORMAT_XRGB2101010:
3670 case DRM_FORMAT_XBGR2101010:
3671 case DRM_FORMAT_RGBX1010102:
3672 case DRM_FORMAT_BGRX1010102:
3673 case DRM_FORMAT_ARGB2101010:
3674 case DRM_FORMAT_ABGR2101010:
3675 case DRM_FORMAT_RGBA1010102:
3676 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003677 *depth = 30;
3678 *bpp = 32;
3679 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003680 case DRM_FORMAT_ARGB8888:
3681 case DRM_FORMAT_ABGR8888:
3682 case DRM_FORMAT_RGBA8888:
3683 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003684 *depth = 32;
3685 *bpp = 32;
3686 break;
3687 default:
3688 DRM_DEBUG_KMS("unsupported pixel format\n");
3689 *depth = 0;
3690 *bpp = 0;
3691 break;
3692 }
3693}
3694EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003695
3696/**
3697 * drm_format_num_planes - get the number of planes for format
3698 * @format: pixel format (DRM_FORMAT_*)
3699 *
3700 * RETURNS:
3701 * The number of planes used by the specified pixel format.
3702 */
3703int drm_format_num_planes(uint32_t format)
3704{
3705 switch (format) {
3706 case DRM_FORMAT_YUV410:
3707 case DRM_FORMAT_YVU410:
3708 case DRM_FORMAT_YUV411:
3709 case DRM_FORMAT_YVU411:
3710 case DRM_FORMAT_YUV420:
3711 case DRM_FORMAT_YVU420:
3712 case DRM_FORMAT_YUV422:
3713 case DRM_FORMAT_YVU422:
3714 case DRM_FORMAT_YUV444:
3715 case DRM_FORMAT_YVU444:
3716 return 3;
3717 case DRM_FORMAT_NV12:
3718 case DRM_FORMAT_NV21:
3719 case DRM_FORMAT_NV16:
3720 case DRM_FORMAT_NV61:
3721 return 2;
3722 default:
3723 return 1;
3724 }
3725}
3726EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003727
3728/**
3729 * drm_format_plane_cpp - determine the bytes per pixel value
3730 * @format: pixel format (DRM_FORMAT_*)
3731 * @plane: plane index
3732 *
3733 * RETURNS:
3734 * The bytes per pixel value for the specified plane.
3735 */
3736int drm_format_plane_cpp(uint32_t format, int plane)
3737{
3738 unsigned int depth;
3739 int bpp;
3740
3741 if (plane >= drm_format_num_planes(format))
3742 return 0;
3743
3744 switch (format) {
3745 case DRM_FORMAT_YUYV:
3746 case DRM_FORMAT_YVYU:
3747 case DRM_FORMAT_UYVY:
3748 case DRM_FORMAT_VYUY:
3749 return 2;
3750 case DRM_FORMAT_NV12:
3751 case DRM_FORMAT_NV21:
3752 case DRM_FORMAT_NV16:
3753 case DRM_FORMAT_NV61:
3754 return plane ? 2 : 1;
3755 case DRM_FORMAT_YUV410:
3756 case DRM_FORMAT_YVU410:
3757 case DRM_FORMAT_YUV411:
3758 case DRM_FORMAT_YVU411:
3759 case DRM_FORMAT_YUV420:
3760 case DRM_FORMAT_YVU420:
3761 case DRM_FORMAT_YUV422:
3762 case DRM_FORMAT_YVU422:
3763 case DRM_FORMAT_YUV444:
3764 case DRM_FORMAT_YVU444:
3765 return 1;
3766 default:
3767 drm_fb_get_bpp_depth(format, &depth, &bpp);
3768 return bpp >> 3;
3769 }
3770}
3771EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003772
3773/**
3774 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3775 * @format: pixel format (DRM_FORMAT_*)
3776 *
3777 * RETURNS:
3778 * The horizontal chroma subsampling factor for the
3779 * specified pixel format.
3780 */
3781int drm_format_horz_chroma_subsampling(uint32_t format)
3782{
3783 switch (format) {
3784 case DRM_FORMAT_YUV411:
3785 case DRM_FORMAT_YVU411:
3786 case DRM_FORMAT_YUV410:
3787 case DRM_FORMAT_YVU410:
3788 return 4;
3789 case DRM_FORMAT_YUYV:
3790 case DRM_FORMAT_YVYU:
3791 case DRM_FORMAT_UYVY:
3792 case DRM_FORMAT_VYUY:
3793 case DRM_FORMAT_NV12:
3794 case DRM_FORMAT_NV21:
3795 case DRM_FORMAT_NV16:
3796 case DRM_FORMAT_NV61:
3797 case DRM_FORMAT_YUV422:
3798 case DRM_FORMAT_YVU422:
3799 case DRM_FORMAT_YUV420:
3800 case DRM_FORMAT_YVU420:
3801 return 2;
3802 default:
3803 return 1;
3804 }
3805}
3806EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3807
3808/**
3809 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3810 * @format: pixel format (DRM_FORMAT_*)
3811 *
3812 * RETURNS:
3813 * The vertical chroma subsampling factor for the
3814 * specified pixel format.
3815 */
3816int drm_format_vert_chroma_subsampling(uint32_t format)
3817{
3818 switch (format) {
3819 case DRM_FORMAT_YUV410:
3820 case DRM_FORMAT_YVU410:
3821 return 4;
3822 case DRM_FORMAT_YUV420:
3823 case DRM_FORMAT_YVU420:
3824 case DRM_FORMAT_NV12:
3825 case DRM_FORMAT_NV21:
3826 return 2;
3827 default:
3828 return 1;
3829 }
3830}
3831EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);