blob: 368e3e72a4525bf1748b14bc5af51d8a95059111 [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:
366 * Caller must hold mode config lock.
367 *
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
387 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
388 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200389
390 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800391 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200392
393 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800394}
395EXPORT_SYMBOL(drm_crtc_init);
396
397/**
398 * drm_crtc_cleanup - Cleans up the core crtc usage.
399 * @crtc: CRTC to cleanup
400 *
401 * LOCKING:
402 * Caller must hold mode config lock.
403 *
404 * Cleanup @crtc. Removes from drm modesetting space
405 * does NOT free object, caller does that.
406 */
407void drm_crtc_cleanup(struct drm_crtc *crtc)
408{
409 struct drm_device *dev = crtc->dev;
410
411 if (crtc->gamma_store) {
412 kfree(crtc->gamma_store);
413 crtc->gamma_store = NULL;
414 }
415
416 drm_mode_object_put(dev, &crtc->base);
417 list_del(&crtc->head);
418 dev->mode_config.num_crtc--;
419}
420EXPORT_SYMBOL(drm_crtc_cleanup);
421
422/**
423 * drm_mode_probed_add - add a mode to a connector's probed mode list
424 * @connector: connector the new mode
425 * @mode: mode data
426 *
427 * LOCKING:
428 * Caller must hold mode config lock.
429 *
430 * Add @mode to @connector's mode list for later use.
431 */
432void drm_mode_probed_add(struct drm_connector *connector,
433 struct drm_display_mode *mode)
434{
435 list_add(&mode->head, &connector->probed_modes);
436}
437EXPORT_SYMBOL(drm_mode_probed_add);
438
439/**
440 * drm_mode_remove - remove and free a mode
441 * @connector: connector list to modify
442 * @mode: mode to remove
443 *
444 * LOCKING:
445 * Caller must hold mode config lock.
446 *
447 * Remove @mode from @connector's mode list, then free it.
448 */
449void drm_mode_remove(struct drm_connector *connector,
450 struct drm_display_mode *mode)
451{
452 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100453 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800454}
455EXPORT_SYMBOL(drm_mode_remove);
456
457/**
458 * drm_connector_init - Init a preallocated connector
459 * @dev: DRM device
460 * @connector: the connector to init
461 * @funcs: callbacks for this connector
462 * @name: user visible name of the connector
463 *
464 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100465 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800466 *
467 * Initialises a preallocated connector. Connectors should be
468 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200469 *
470 * RETURNS:
471 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800472 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200473int drm_connector_init(struct drm_device *dev,
474 struct drm_connector *connector,
475 const struct drm_connector_funcs *funcs,
476 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800477{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200478 int ret;
479
Dave Airlief453ba02008-11-07 14:05:41 -0800480 mutex_lock(&dev->mode_config.mutex);
481
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200482 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
483 if (ret)
484 goto out;
485
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300486 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800487 connector->dev = dev;
488 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800489 connector->connector_type = connector_type;
490 connector->connector_type_id =
491 ++drm_connector_enum_list[connector_type].count; /* TODO */
492 INIT_LIST_HEAD(&connector->user_modes);
493 INIT_LIST_HEAD(&connector->probed_modes);
494 INIT_LIST_HEAD(&connector->modes);
495 connector->edid_blob_ptr = NULL;
496
497 list_add_tail(&connector->head, &dev->mode_config.connector_list);
498 dev->mode_config.num_connector++;
499
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200500 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
501 drm_connector_attach_property(connector,
502 dev->mode_config.edid_property,
503 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800504
505 drm_connector_attach_property(connector,
506 dev->mode_config.dpms_property, 0);
507
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200508 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800509 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200510
511 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800512}
513EXPORT_SYMBOL(drm_connector_init);
514
515/**
516 * drm_connector_cleanup - cleans up an initialised connector
517 * @connector: connector to cleanup
518 *
519 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100520 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800521 *
522 * Cleans up the connector but doesn't free the object.
523 */
524void drm_connector_cleanup(struct drm_connector *connector)
525{
526 struct drm_device *dev = connector->dev;
527 struct drm_display_mode *mode, *t;
528
529 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
530 drm_mode_remove(connector, mode);
531
532 list_for_each_entry_safe(mode, t, &connector->modes, head)
533 drm_mode_remove(connector, mode);
534
535 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
536 drm_mode_remove(connector, mode);
537
538 mutex_lock(&dev->mode_config.mutex);
539 drm_mode_object_put(dev, &connector->base);
540 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000541 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800542 mutex_unlock(&dev->mode_config.mutex);
543}
544EXPORT_SYMBOL(drm_connector_cleanup);
545
Dave Airliecbc7e222012-02-20 14:16:40 +0000546void drm_connector_unplug_all(struct drm_device *dev)
547{
548 struct drm_connector *connector;
549
550 /* taking the mode config mutex ends up in a clash with sysfs */
551 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
552 drm_sysfs_connector_remove(connector);
553
554}
555EXPORT_SYMBOL(drm_connector_unplug_all);
556
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200557int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000558 struct drm_encoder *encoder,
559 const struct drm_encoder_funcs *funcs,
560 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800561{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200562 int ret;
563
Dave Airlief453ba02008-11-07 14:05:41 -0800564 mutex_lock(&dev->mode_config.mutex);
565
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200566 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
567 if (ret)
568 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800569
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200570 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800571 encoder->encoder_type = encoder_type;
572 encoder->funcs = funcs;
573
574 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
575 dev->mode_config.num_encoder++;
576
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200577 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800578 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200579
580 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800581}
582EXPORT_SYMBOL(drm_encoder_init);
583
584void drm_encoder_cleanup(struct drm_encoder *encoder)
585{
586 struct drm_device *dev = encoder->dev;
587 mutex_lock(&dev->mode_config.mutex);
588 drm_mode_object_put(dev, &encoder->base);
589 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000590 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800591 mutex_unlock(&dev->mode_config.mutex);
592}
593EXPORT_SYMBOL(drm_encoder_cleanup);
594
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800595int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
596 unsigned long possible_crtcs,
597 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600598 const uint32_t *formats, uint32_t format_count,
599 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800600{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200601 int ret;
602
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800603 mutex_lock(&dev->mode_config.mutex);
604
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200605 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
606 if (ret)
607 goto out;
608
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800609 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800610 plane->funcs = funcs;
611 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
612 GFP_KERNEL);
613 if (!plane->format_types) {
614 DRM_DEBUG_KMS("out of memory when allocating plane\n");
615 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200616 ret = -ENOMEM;
617 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800618 }
619
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800620 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800621 plane->format_count = format_count;
622 plane->possible_crtcs = possible_crtcs;
623
Rob Clark0a7eb242011-12-13 20:19:36 -0600624 /* private planes are not exposed to userspace, but depending on
625 * display hardware, might be convenient to allow sharing programming
626 * for the scanout engine with the crtc implementation.
627 */
628 if (!priv) {
629 list_add_tail(&plane->head, &dev->mode_config.plane_list);
630 dev->mode_config.num_plane++;
631 } else {
632 INIT_LIST_HEAD(&plane->head);
633 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800634
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200635 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800636 mutex_unlock(&dev->mode_config.mutex);
637
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200638 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800639}
640EXPORT_SYMBOL(drm_plane_init);
641
642void drm_plane_cleanup(struct drm_plane *plane)
643{
644 struct drm_device *dev = plane->dev;
645
646 mutex_lock(&dev->mode_config.mutex);
647 kfree(plane->format_types);
648 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600649 /* if not added to a list, it must be a private plane */
650 if (!list_empty(&plane->head)) {
651 list_del(&plane->head);
652 dev->mode_config.num_plane--;
653 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800654 mutex_unlock(&dev->mode_config.mutex);
655}
656EXPORT_SYMBOL(drm_plane_cleanup);
657
Dave Airlief453ba02008-11-07 14:05:41 -0800658/**
659 * drm_mode_create - create a new display mode
660 * @dev: DRM device
661 *
662 * LOCKING:
663 * Caller must hold DRM mode_config lock.
664 *
665 * Create a new drm_display_mode, give it an ID, and return it.
666 *
667 * RETURNS:
668 * Pointer to new mode on success, NULL on error.
669 */
670struct drm_display_mode *drm_mode_create(struct drm_device *dev)
671{
672 struct drm_display_mode *nmode;
673
674 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
675 if (!nmode)
676 return NULL;
677
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200678 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
679 kfree(nmode);
680 return NULL;
681 }
682
Dave Airlief453ba02008-11-07 14:05:41 -0800683 return nmode;
684}
685EXPORT_SYMBOL(drm_mode_create);
686
687/**
688 * drm_mode_destroy - remove a mode
689 * @dev: DRM device
690 * @mode: mode to remove
691 *
692 * LOCKING:
693 * Caller must hold mode config lock.
694 *
695 * Free @mode's unique identifier, then free it.
696 */
697void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
698{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200699 if (!mode)
700 return;
701
Dave Airlief453ba02008-11-07 14:05:41 -0800702 drm_mode_object_put(dev, &mode->base);
703
704 kfree(mode);
705}
706EXPORT_SYMBOL(drm_mode_destroy);
707
708static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
709{
710 struct drm_property *edid;
711 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800712
713 /*
714 * Standard properties (apply to all connectors)
715 */
716 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
717 DRM_MODE_PROP_IMMUTABLE,
718 "EDID", 0);
719 dev->mode_config.edid_property = edid;
720
Sascha Hauer4a67d392012-02-06 10:58:17 +0100721 dpms = drm_property_create_enum(dev, 0,
722 "DPMS", drm_dpms_enum_list,
723 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800724 dev->mode_config.dpms_property = dpms;
725
726 return 0;
727}
728
729/**
730 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
731 * @dev: DRM device
732 *
733 * Called by a driver the first time a DVI-I connector is made.
734 */
735int drm_mode_create_dvi_i_properties(struct drm_device *dev)
736{
737 struct drm_property *dvi_i_selector;
738 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800739
740 if (dev->mode_config.dvi_i_select_subconnector_property)
741 return 0;
742
743 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100744 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800745 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100746 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800747 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800748 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
749
Sascha Hauer4a67d392012-02-06 10:58:17 +0100750 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800751 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100752 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800753 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800754 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
755
756 return 0;
757}
758EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
759
760/**
761 * drm_create_tv_properties - create TV specific connector properties
762 * @dev: DRM device
763 * @num_modes: number of different TV formats (modes) supported
764 * @modes: array of pointers to strings containing name of each format
765 *
766 * Called by a driver's TV initialization routine, this function creates
767 * the TV specific connector properties for a given device. Caller is
768 * responsible for allocating a list of format names and passing them to
769 * this routine.
770 */
771int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
772 char *modes[])
773{
774 struct drm_property *tv_selector;
775 struct drm_property *tv_subconnector;
776 int i;
777
778 if (dev->mode_config.tv_select_subconnector_property)
779 return 0;
780
781 /*
782 * Basic connector properties
783 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100784 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800785 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100786 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800787 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800788 dev->mode_config.tv_select_subconnector_property = tv_selector;
789
790 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100791 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
792 "subconnector",
793 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800794 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800795 dev->mode_config.tv_subconnector_property = tv_subconnector;
796
797 /*
798 * Other, TV specific properties: margins & TV modes.
799 */
800 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100801 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800802
803 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100804 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800805
806 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100807 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800808
809 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100810 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800811
812 dev->mode_config.tv_mode_property =
813 drm_property_create(dev, DRM_MODE_PROP_ENUM,
814 "mode", num_modes);
815 for (i = 0; i < num_modes; i++)
816 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
817 i, modes[i]);
818
Francisco Jerezb6b79022009-08-02 04:19:20 +0200819 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100820 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200821
822 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100823 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200824
825 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100826 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200827
Francisco Jereza75f0232009-08-12 02:30:10 +0200828 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100829 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200830
831 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100832 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200833
834 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100835 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200836
Dave Airlief453ba02008-11-07 14:05:41 -0800837 return 0;
838}
839EXPORT_SYMBOL(drm_mode_create_tv_properties);
840
841/**
842 * drm_mode_create_scaling_mode_property - create scaling mode property
843 * @dev: DRM device
844 *
845 * Called by a driver the first time it's needed, must be attached to desired
846 * connectors.
847 */
848int drm_mode_create_scaling_mode_property(struct drm_device *dev)
849{
850 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800851
852 if (dev->mode_config.scaling_mode_property)
853 return 0;
854
855 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100856 drm_property_create_enum(dev, 0, "scaling mode",
857 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800858 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800859
860 dev->mode_config.scaling_mode_property = scaling_mode;
861
862 return 0;
863}
864EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
865
866/**
867 * drm_mode_create_dithering_property - create dithering property
868 * @dev: DRM device
869 *
870 * Called by a driver the first time it's needed, must be attached to desired
871 * connectors.
872 */
873int drm_mode_create_dithering_property(struct drm_device *dev)
874{
875 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800876
877 if (dev->mode_config.dithering_mode_property)
878 return 0;
879
880 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100881 drm_property_create_enum(dev, 0, "dithering",
882 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800883 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800884 dev->mode_config.dithering_mode_property = dithering_mode;
885
886 return 0;
887}
888EXPORT_SYMBOL(drm_mode_create_dithering_property);
889
890/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000891 * drm_mode_create_dirty_property - create dirty property
892 * @dev: DRM device
893 *
894 * Called by a driver the first time it's needed, must be attached to desired
895 * connectors.
896 */
897int drm_mode_create_dirty_info_property(struct drm_device *dev)
898{
899 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000900
901 if (dev->mode_config.dirty_info_property)
902 return 0;
903
904 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100905 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000906 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100907 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000908 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000909 dev->mode_config.dirty_info_property = dirty_info;
910
911 return 0;
912}
913EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
914
915/**
Dave Airlief453ba02008-11-07 14:05:41 -0800916 * drm_mode_config_init - initialize DRM mode_configuration structure
917 * @dev: DRM device
918 *
919 * LOCKING:
920 * None, should happen single threaded at init time.
921 *
922 * Initialize @dev's mode_config structure, used for tracking the graphics
923 * configuration of @dev.
924 */
925void drm_mode_config_init(struct drm_device *dev)
926{
927 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000928 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800929 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800930 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
931 INIT_LIST_HEAD(&dev->mode_config.connector_list);
932 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
933 INIT_LIST_HEAD(&dev->mode_config.property_list);
934 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800935 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800936 idr_init(&dev->mode_config.crtc_idr);
937
938 mutex_lock(&dev->mode_config.mutex);
939 drm_mode_create_standard_connector_properties(dev);
940 mutex_unlock(&dev->mode_config.mutex);
941
942 /* Just to be sure */
943 dev->mode_config.num_fb = 0;
944 dev->mode_config.num_connector = 0;
945 dev->mode_config.num_crtc = 0;
946 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800947}
948EXPORT_SYMBOL(drm_mode_config_init);
949
950int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
951{
952 uint32_t total_objects = 0;
953
954 total_objects += dev->mode_config.num_crtc;
955 total_objects += dev->mode_config.num_connector;
956 total_objects += dev->mode_config.num_encoder;
957
Dave Airlief453ba02008-11-07 14:05:41 -0800958 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
959 if (!group->id_list)
960 return -ENOMEM;
961
962 group->num_crtcs = 0;
963 group->num_connectors = 0;
964 group->num_encoders = 0;
965 return 0;
966}
967
968int drm_mode_group_init_legacy_group(struct drm_device *dev,
969 struct drm_mode_group *group)
970{
971 struct drm_crtc *crtc;
972 struct drm_encoder *encoder;
973 struct drm_connector *connector;
974 int ret;
975
976 if ((ret = drm_mode_group_init(dev, group)))
977 return ret;
978
979 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
980 group->id_list[group->num_crtcs++] = crtc->base.id;
981
982 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
983 group->id_list[group->num_crtcs + group->num_encoders++] =
984 encoder->base.id;
985
986 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
987 group->id_list[group->num_crtcs + group->num_encoders +
988 group->num_connectors++] = connector->base.id;
989
990 return 0;
991}
Dave Airlie9c1dfc52012-03-20 06:59:29 +0000992EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -0800993
994/**
995 * drm_mode_config_cleanup - free up DRM mode_config info
996 * @dev: DRM device
997 *
998 * LOCKING:
999 * Caller must hold mode config lock.
1000 *
1001 * Free up all the connectors and CRTCs associated with this DRM device, then
1002 * free up the framebuffers and associated buffer objects.
1003 *
1004 * FIXME: cleanup any dangling user buffer objects too
1005 */
1006void drm_mode_config_cleanup(struct drm_device *dev)
1007{
1008 struct drm_connector *connector, *ot;
1009 struct drm_crtc *crtc, *ct;
1010 struct drm_encoder *encoder, *enct;
1011 struct drm_framebuffer *fb, *fbt;
1012 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001013 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001014
1015 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1016 head) {
1017 encoder->funcs->destroy(encoder);
1018 }
1019
1020 list_for_each_entry_safe(connector, ot,
1021 &dev->mode_config.connector_list, head) {
1022 connector->funcs->destroy(connector);
1023 }
1024
1025 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1026 head) {
1027 drm_property_destroy(dev, property);
1028 }
1029
1030 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1031 fb->funcs->destroy(fb);
1032 }
1033
1034 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1035 crtc->funcs->destroy(crtc);
1036 }
1037
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001038 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1039 head) {
1040 plane->funcs->destroy(plane);
1041 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001042
1043 idr_remove_all(&dev->mode_config.crtc_idr);
1044 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001045}
1046EXPORT_SYMBOL(drm_mode_config_cleanup);
1047
Dave Airlief453ba02008-11-07 14:05:41 -08001048/**
1049 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1050 * @out: drm_mode_modeinfo struct to return to the user
1051 * @in: drm_display_mode to use
1052 *
1053 * LOCKING:
1054 * None.
1055 *
1056 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1057 * the user.
1058 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001059static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1060 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001061{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001062 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1063 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1064 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1065 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1066 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1067 "timing values too large for mode info\n");
1068
Dave Airlief453ba02008-11-07 14:05:41 -08001069 out->clock = in->clock;
1070 out->hdisplay = in->hdisplay;
1071 out->hsync_start = in->hsync_start;
1072 out->hsync_end = in->hsync_end;
1073 out->htotal = in->htotal;
1074 out->hskew = in->hskew;
1075 out->vdisplay = in->vdisplay;
1076 out->vsync_start = in->vsync_start;
1077 out->vsync_end = in->vsync_end;
1078 out->vtotal = in->vtotal;
1079 out->vscan = in->vscan;
1080 out->vrefresh = in->vrefresh;
1081 out->flags = in->flags;
1082 out->type = in->type;
1083 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1084 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1085}
1086
1087/**
1088 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1089 * @out: drm_display_mode to return to the user
1090 * @in: drm_mode_modeinfo to use
1091 *
1092 * LOCKING:
1093 * None.
1094 *
1095 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1096 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001097 *
1098 * RETURNS:
1099 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001100 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001101static int drm_crtc_convert_umode(struct drm_display_mode *out,
1102 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001103{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001104 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1105 return -ERANGE;
1106
Dave Airlief453ba02008-11-07 14:05:41 -08001107 out->clock = in->clock;
1108 out->hdisplay = in->hdisplay;
1109 out->hsync_start = in->hsync_start;
1110 out->hsync_end = in->hsync_end;
1111 out->htotal = in->htotal;
1112 out->hskew = in->hskew;
1113 out->vdisplay = in->vdisplay;
1114 out->vsync_start = in->vsync_start;
1115 out->vsync_end = in->vsync_end;
1116 out->vtotal = in->vtotal;
1117 out->vscan = in->vscan;
1118 out->vrefresh = in->vrefresh;
1119 out->flags = in->flags;
1120 out->type = in->type;
1121 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1122 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001123
1124 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001125}
1126
1127/**
1128 * drm_mode_getresources - get graphics configuration
1129 * @inode: inode from the ioctl
1130 * @filp: file * from the ioctl
1131 * @cmd: cmd from ioctl
1132 * @arg: arg from ioctl
1133 *
1134 * LOCKING:
1135 * Takes mode config lock.
1136 *
1137 * Construct a set of configuration description structures and return
1138 * them to the user, including CRTC, connector and framebuffer configuration.
1139 *
1140 * Called by the user via ioctl.
1141 *
1142 * RETURNS:
1143 * Zero on success, errno on failure.
1144 */
1145int drm_mode_getresources(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
1147{
1148 struct drm_mode_card_res *card_res = data;
1149 struct list_head *lh;
1150 struct drm_framebuffer *fb;
1151 struct drm_connector *connector;
1152 struct drm_crtc *crtc;
1153 struct drm_encoder *encoder;
1154 int ret = 0;
1155 int connector_count = 0;
1156 int crtc_count = 0;
1157 int fb_count = 0;
1158 int encoder_count = 0;
1159 int copied = 0, i;
1160 uint32_t __user *fb_id;
1161 uint32_t __user *crtc_id;
1162 uint32_t __user *connector_id;
1163 uint32_t __user *encoder_id;
1164 struct drm_mode_group *mode_group;
1165
Dave Airliefb3b06c2011-02-08 13:55:21 +10001166 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1167 return -EINVAL;
1168
Dave Airlief453ba02008-11-07 14:05:41 -08001169 mutex_lock(&dev->mode_config.mutex);
1170
1171 /*
1172 * For the non-control nodes we need to limit the list of resources
1173 * by IDs in the group list for this node
1174 */
1175 list_for_each(lh, &file_priv->fbs)
1176 fb_count++;
1177
1178 mode_group = &file_priv->master->minor->mode_group;
1179 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1180
1181 list_for_each(lh, &dev->mode_config.crtc_list)
1182 crtc_count++;
1183
1184 list_for_each(lh, &dev->mode_config.connector_list)
1185 connector_count++;
1186
1187 list_for_each(lh, &dev->mode_config.encoder_list)
1188 encoder_count++;
1189 } else {
1190
1191 crtc_count = mode_group->num_crtcs;
1192 connector_count = mode_group->num_connectors;
1193 encoder_count = mode_group->num_encoders;
1194 }
1195
1196 card_res->max_height = dev->mode_config.max_height;
1197 card_res->min_height = dev->mode_config.min_height;
1198 card_res->max_width = dev->mode_config.max_width;
1199 card_res->min_width = dev->mode_config.min_width;
1200
1201 /* handle this in 4 parts */
1202 /* FBs */
1203 if (card_res->count_fbs >= fb_count) {
1204 copied = 0;
1205 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001206 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001207 if (put_user(fb->base.id, fb_id + copied)) {
1208 ret = -EFAULT;
1209 goto out;
1210 }
1211 copied++;
1212 }
1213 }
1214 card_res->count_fbs = fb_count;
1215
1216 /* CRTCs */
1217 if (card_res->count_crtcs >= crtc_count) {
1218 copied = 0;
1219 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1220 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1221 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1222 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001223 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001224 if (put_user(crtc->base.id, crtc_id + copied)) {
1225 ret = -EFAULT;
1226 goto out;
1227 }
1228 copied++;
1229 }
1230 } else {
1231 for (i = 0; i < mode_group->num_crtcs; i++) {
1232 if (put_user(mode_group->id_list[i],
1233 crtc_id + copied)) {
1234 ret = -EFAULT;
1235 goto out;
1236 }
1237 copied++;
1238 }
1239 }
1240 }
1241 card_res->count_crtcs = crtc_count;
1242
1243 /* Encoders */
1244 if (card_res->count_encoders >= encoder_count) {
1245 copied = 0;
1246 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1247 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1248 list_for_each_entry(encoder,
1249 &dev->mode_config.encoder_list,
1250 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001251 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1252 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001253 if (put_user(encoder->base.id, encoder_id +
1254 copied)) {
1255 ret = -EFAULT;
1256 goto out;
1257 }
1258 copied++;
1259 }
1260 } else {
1261 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1262 if (put_user(mode_group->id_list[i],
1263 encoder_id + copied)) {
1264 ret = -EFAULT;
1265 goto out;
1266 }
1267 copied++;
1268 }
1269
1270 }
1271 }
1272 card_res->count_encoders = encoder_count;
1273
1274 /* Connectors */
1275 if (card_res->count_connectors >= connector_count) {
1276 copied = 0;
1277 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1278 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1279 list_for_each_entry(connector,
1280 &dev->mode_config.connector_list,
1281 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001282 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1283 connector->base.id,
1284 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001285 if (put_user(connector->base.id,
1286 connector_id + copied)) {
1287 ret = -EFAULT;
1288 goto out;
1289 }
1290 copied++;
1291 }
1292 } else {
1293 int start = mode_group->num_crtcs +
1294 mode_group->num_encoders;
1295 for (i = start; i < start + mode_group->num_connectors; i++) {
1296 if (put_user(mode_group->id_list[i],
1297 connector_id + copied)) {
1298 ret = -EFAULT;
1299 goto out;
1300 }
1301 copied++;
1302 }
1303 }
1304 }
1305 card_res->count_connectors = connector_count;
1306
Jerome Glisse94401062010-07-15 15:43:25 -04001307 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001308 card_res->count_connectors, card_res->count_encoders);
1309
1310out:
1311 mutex_unlock(&dev->mode_config.mutex);
1312 return ret;
1313}
1314
1315/**
1316 * drm_mode_getcrtc - get CRTC configuration
1317 * @inode: inode from the ioctl
1318 * @filp: file * from the ioctl
1319 * @cmd: cmd from ioctl
1320 * @arg: arg from ioctl
1321 *
1322 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001323 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001324 *
1325 * Construct a CRTC configuration structure to return to the user.
1326 *
1327 * Called by the user via ioctl.
1328 *
1329 * RETURNS:
1330 * Zero on success, errno on failure.
1331 */
1332int drm_mode_getcrtc(struct drm_device *dev,
1333 void *data, struct drm_file *file_priv)
1334{
1335 struct drm_mode_crtc *crtc_resp = data;
1336 struct drm_crtc *crtc;
1337 struct drm_mode_object *obj;
1338 int ret = 0;
1339
Dave Airliefb3b06c2011-02-08 13:55:21 +10001340 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1341 return -EINVAL;
1342
Dave Airlief453ba02008-11-07 14:05:41 -08001343 mutex_lock(&dev->mode_config.mutex);
1344
1345 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1346 DRM_MODE_OBJECT_CRTC);
1347 if (!obj) {
1348 ret = -EINVAL;
1349 goto out;
1350 }
1351 crtc = obj_to_crtc(obj);
1352
1353 crtc_resp->x = crtc->x;
1354 crtc_resp->y = crtc->y;
1355 crtc_resp->gamma_size = crtc->gamma_size;
1356 if (crtc->fb)
1357 crtc_resp->fb_id = crtc->fb->base.id;
1358 else
1359 crtc_resp->fb_id = 0;
1360
1361 if (crtc->enabled) {
1362
1363 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1364 crtc_resp->mode_valid = 1;
1365
1366 } else {
1367 crtc_resp->mode_valid = 0;
1368 }
1369
1370out:
1371 mutex_unlock(&dev->mode_config.mutex);
1372 return ret;
1373}
1374
1375/**
1376 * drm_mode_getconnector - get connector configuration
1377 * @inode: inode from the ioctl
1378 * @filp: file * from the ioctl
1379 * @cmd: cmd from ioctl
1380 * @arg: arg from ioctl
1381 *
1382 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001383 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001384 *
1385 * Construct a connector configuration structure to return to the user.
1386 *
1387 * Called by the user via ioctl.
1388 *
1389 * RETURNS:
1390 * Zero on success, errno on failure.
1391 */
1392int drm_mode_getconnector(struct drm_device *dev, void *data,
1393 struct drm_file *file_priv)
1394{
1395 struct drm_mode_get_connector *out_resp = data;
1396 struct drm_mode_object *obj;
1397 struct drm_connector *connector;
1398 struct drm_display_mode *mode;
1399 int mode_count = 0;
1400 int props_count = 0;
1401 int encoders_count = 0;
1402 int ret = 0;
1403 int copied = 0;
1404 int i;
1405 struct drm_mode_modeinfo u_mode;
1406 struct drm_mode_modeinfo __user *mode_ptr;
1407 uint32_t __user *prop_ptr;
1408 uint64_t __user *prop_values;
1409 uint32_t __user *encoder_ptr;
1410
Dave Airliefb3b06c2011-02-08 13:55:21 +10001411 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1412 return -EINVAL;
1413
Dave Airlief453ba02008-11-07 14:05:41 -08001414 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1415
Jerome Glisse94401062010-07-15 15:43:25 -04001416 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001417
1418 mutex_lock(&dev->mode_config.mutex);
1419
1420 obj = drm_mode_object_find(dev, out_resp->connector_id,
1421 DRM_MODE_OBJECT_CONNECTOR);
1422 if (!obj) {
1423 ret = -EINVAL;
1424 goto out;
1425 }
1426 connector = obj_to_connector(obj);
1427
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001428 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001429
1430 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1431 if (connector->encoder_ids[i] != 0) {
1432 encoders_count++;
1433 }
1434 }
1435
1436 if (out_resp->count_modes == 0) {
1437 connector->funcs->fill_modes(connector,
1438 dev->mode_config.max_width,
1439 dev->mode_config.max_height);
1440 }
1441
1442 /* delayed so we get modes regardless of pre-fill_modes state */
1443 list_for_each_entry(mode, &connector->modes, head)
1444 mode_count++;
1445
1446 out_resp->connector_id = connector->base.id;
1447 out_resp->connector_type = connector->connector_type;
1448 out_resp->connector_type_id = connector->connector_type_id;
1449 out_resp->mm_width = connector->display_info.width_mm;
1450 out_resp->mm_height = connector->display_info.height_mm;
1451 out_resp->subpixel = connector->display_info.subpixel_order;
1452 out_resp->connection = connector->status;
1453 if (connector->encoder)
1454 out_resp->encoder_id = connector->encoder->base.id;
1455 else
1456 out_resp->encoder_id = 0;
1457
1458 /*
1459 * This ioctl is called twice, once to determine how much space is
1460 * needed, and the 2nd time to fill it.
1461 */
1462 if ((out_resp->count_modes >= mode_count) && mode_count) {
1463 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001464 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001465 list_for_each_entry(mode, &connector->modes, head) {
1466 drm_crtc_convert_to_umode(&u_mode, mode);
1467 if (copy_to_user(mode_ptr + copied,
1468 &u_mode, sizeof(u_mode))) {
1469 ret = -EFAULT;
1470 goto out;
1471 }
1472 copied++;
1473 }
1474 }
1475 out_resp->count_modes = mode_count;
1476
1477 if ((out_resp->count_props >= props_count) && props_count) {
1478 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001479 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1480 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001481 for (i = 0; i < connector->properties.count; i++) {
1482 if (put_user(connector->properties.ids[i],
1483 prop_ptr + copied)) {
1484 ret = -EFAULT;
1485 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001486 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001487
1488 if (put_user(connector->properties.values[i],
1489 prop_values + copied)) {
1490 ret = -EFAULT;
1491 goto out;
1492 }
1493 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001494 }
1495 }
1496 out_resp->count_props = props_count;
1497
1498 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1499 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001500 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001501 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1502 if (connector->encoder_ids[i] != 0) {
1503 if (put_user(connector->encoder_ids[i],
1504 encoder_ptr + copied)) {
1505 ret = -EFAULT;
1506 goto out;
1507 }
1508 copied++;
1509 }
1510 }
1511 }
1512 out_resp->count_encoders = encoders_count;
1513
1514out:
1515 mutex_unlock(&dev->mode_config.mutex);
1516 return ret;
1517}
1518
1519int drm_mode_getencoder(struct drm_device *dev, void *data,
1520 struct drm_file *file_priv)
1521{
1522 struct drm_mode_get_encoder *enc_resp = data;
1523 struct drm_mode_object *obj;
1524 struct drm_encoder *encoder;
1525 int ret = 0;
1526
Dave Airliefb3b06c2011-02-08 13:55:21 +10001527 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1528 return -EINVAL;
1529
Dave Airlief453ba02008-11-07 14:05:41 -08001530 mutex_lock(&dev->mode_config.mutex);
1531 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1532 DRM_MODE_OBJECT_ENCODER);
1533 if (!obj) {
1534 ret = -EINVAL;
1535 goto out;
1536 }
1537 encoder = obj_to_encoder(obj);
1538
1539 if (encoder->crtc)
1540 enc_resp->crtc_id = encoder->crtc->base.id;
1541 else
1542 enc_resp->crtc_id = 0;
1543 enc_resp->encoder_type = encoder->encoder_type;
1544 enc_resp->encoder_id = encoder->base.id;
1545 enc_resp->possible_crtcs = encoder->possible_crtcs;
1546 enc_resp->possible_clones = encoder->possible_clones;
1547
1548out:
1549 mutex_unlock(&dev->mode_config.mutex);
1550 return ret;
1551}
1552
1553/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001554 * drm_mode_getplane_res - get plane info
1555 * @dev: DRM device
1556 * @data: ioctl data
1557 * @file_priv: DRM file info
1558 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001559 * LOCKING:
1560 * Takes mode config lock.
1561 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001562 * Return an plane count and set of IDs.
1563 */
1564int drm_mode_getplane_res(struct drm_device *dev, void *data,
1565 struct drm_file *file_priv)
1566{
1567 struct drm_mode_get_plane_res *plane_resp = data;
1568 struct drm_mode_config *config;
1569 struct drm_plane *plane;
1570 uint32_t __user *plane_ptr;
1571 int copied = 0, ret = 0;
1572
1573 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1574 return -EINVAL;
1575
1576 mutex_lock(&dev->mode_config.mutex);
1577 config = &dev->mode_config;
1578
1579 /*
1580 * This ioctl is called twice, once to determine how much space is
1581 * needed, and the 2nd time to fill it.
1582 */
1583 if (config->num_plane &&
1584 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001585 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001586
1587 list_for_each_entry(plane, &config->plane_list, head) {
1588 if (put_user(plane->base.id, plane_ptr + copied)) {
1589 ret = -EFAULT;
1590 goto out;
1591 }
1592 copied++;
1593 }
1594 }
1595 plane_resp->count_planes = config->num_plane;
1596
1597out:
1598 mutex_unlock(&dev->mode_config.mutex);
1599 return ret;
1600}
1601
1602/**
1603 * drm_mode_getplane - get plane info
1604 * @dev: DRM device
1605 * @data: ioctl data
1606 * @file_priv: DRM file info
1607 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001608 * LOCKING:
1609 * Takes mode config lock.
1610 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001611 * Return plane info, including formats supported, gamma size, any
1612 * current fb, etc.
1613 */
1614int drm_mode_getplane(struct drm_device *dev, void *data,
1615 struct drm_file *file_priv)
1616{
1617 struct drm_mode_get_plane *plane_resp = data;
1618 struct drm_mode_object *obj;
1619 struct drm_plane *plane;
1620 uint32_t __user *format_ptr;
1621 int ret = 0;
1622
1623 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1624 return -EINVAL;
1625
1626 mutex_lock(&dev->mode_config.mutex);
1627 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1628 DRM_MODE_OBJECT_PLANE);
1629 if (!obj) {
1630 ret = -ENOENT;
1631 goto out;
1632 }
1633 plane = obj_to_plane(obj);
1634
1635 if (plane->crtc)
1636 plane_resp->crtc_id = plane->crtc->base.id;
1637 else
1638 plane_resp->crtc_id = 0;
1639
1640 if (plane->fb)
1641 plane_resp->fb_id = plane->fb->base.id;
1642 else
1643 plane_resp->fb_id = 0;
1644
1645 plane_resp->plane_id = plane->base.id;
1646 plane_resp->possible_crtcs = plane->possible_crtcs;
1647 plane_resp->gamma_size = plane->gamma_size;
1648
1649 /*
1650 * This ioctl is called twice, once to determine how much space is
1651 * needed, and the 2nd time to fill it.
1652 */
1653 if (plane->format_count &&
1654 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001655 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001656 if (copy_to_user(format_ptr,
1657 plane->format_types,
1658 sizeof(uint32_t) * plane->format_count)) {
1659 ret = -EFAULT;
1660 goto out;
1661 }
1662 }
1663 plane_resp->count_format_types = plane->format_count;
1664
1665out:
1666 mutex_unlock(&dev->mode_config.mutex);
1667 return ret;
1668}
1669
1670/**
1671 * drm_mode_setplane - set up or tear down an plane
1672 * @dev: DRM device
1673 * @data: ioctl data*
1674 * @file_prive: DRM file info
1675 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001676 * LOCKING:
1677 * Takes mode config lock.
1678 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001679 * Set plane info, including placement, fb, scaling, and other factors.
1680 * Or pass a NULL fb to disable.
1681 */
1682int drm_mode_setplane(struct drm_device *dev, void *data,
1683 struct drm_file *file_priv)
1684{
1685 struct drm_mode_set_plane *plane_req = data;
1686 struct drm_mode_object *obj;
1687 struct drm_plane *plane;
1688 struct drm_crtc *crtc;
1689 struct drm_framebuffer *fb;
1690 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001691 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001692 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001693
1694 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1695 return -EINVAL;
1696
1697 mutex_lock(&dev->mode_config.mutex);
1698
1699 /*
1700 * First, find the plane, crtc, and fb objects. If not available,
1701 * we don't bother to call the driver.
1702 */
1703 obj = drm_mode_object_find(dev, plane_req->plane_id,
1704 DRM_MODE_OBJECT_PLANE);
1705 if (!obj) {
1706 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1707 plane_req->plane_id);
1708 ret = -ENOENT;
1709 goto out;
1710 }
1711 plane = obj_to_plane(obj);
1712
1713 /* No fb means shut it down */
1714 if (!plane_req->fb_id) {
1715 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001716 plane->crtc = NULL;
1717 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001718 goto out;
1719 }
1720
1721 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1722 DRM_MODE_OBJECT_CRTC);
1723 if (!obj) {
1724 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1725 plane_req->crtc_id);
1726 ret = -ENOENT;
1727 goto out;
1728 }
1729 crtc = obj_to_crtc(obj);
1730
1731 obj = drm_mode_object_find(dev, plane_req->fb_id,
1732 DRM_MODE_OBJECT_FB);
1733 if (!obj) {
1734 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1735 plane_req->fb_id);
1736 ret = -ENOENT;
1737 goto out;
1738 }
1739 fb = obj_to_fb(obj);
1740
Ville Syrjälä62443be2011-12-20 00:06:47 +02001741 /* Check whether this plane supports the fb pixel format. */
1742 for (i = 0; i < plane->format_count; i++)
1743 if (fb->pixel_format == plane->format_types[i])
1744 break;
1745 if (i == plane->format_count) {
1746 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1747 ret = -EINVAL;
1748 goto out;
1749 }
1750
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001751 fb_width = fb->width << 16;
1752 fb_height = fb->height << 16;
1753
1754 /* Make sure source coordinates are inside the fb. */
1755 if (plane_req->src_w > fb_width ||
1756 plane_req->src_x > fb_width - plane_req->src_w ||
1757 plane_req->src_h > fb_height ||
1758 plane_req->src_y > fb_height - plane_req->src_h) {
1759 DRM_DEBUG_KMS("Invalid source coordinates "
1760 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1761 plane_req->src_w >> 16,
1762 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1763 plane_req->src_h >> 16,
1764 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1765 plane_req->src_x >> 16,
1766 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1767 plane_req->src_y >> 16,
1768 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1769 ret = -ENOSPC;
1770 goto out;
1771 }
1772
Ville Syrjälä687a0402011-12-20 00:06:45 +02001773 /* Give drivers some help against integer overflows */
1774 if (plane_req->crtc_w > INT_MAX ||
1775 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1776 plane_req->crtc_h > INT_MAX ||
1777 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1778 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1779 plane_req->crtc_w, plane_req->crtc_h,
1780 plane_req->crtc_x, plane_req->crtc_y);
1781 ret = -ERANGE;
1782 goto out;
1783 }
1784
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001785 ret = plane->funcs->update_plane(plane, crtc, fb,
1786 plane_req->crtc_x, plane_req->crtc_y,
1787 plane_req->crtc_w, plane_req->crtc_h,
1788 plane_req->src_x, plane_req->src_y,
1789 plane_req->src_w, plane_req->src_h);
1790 if (!ret) {
1791 plane->crtc = crtc;
1792 plane->fb = fb;
1793 }
1794
1795out:
1796 mutex_unlock(&dev->mode_config.mutex);
1797
1798 return ret;
1799}
1800
1801/**
Dave Airlief453ba02008-11-07 14:05:41 -08001802 * drm_mode_setcrtc - set CRTC configuration
1803 * @inode: inode from the ioctl
1804 * @filp: file * from the ioctl
1805 * @cmd: cmd from ioctl
1806 * @arg: arg from ioctl
1807 *
1808 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001809 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001810 *
1811 * Build a new CRTC configuration based on user request.
1812 *
1813 * Called by the user via ioctl.
1814 *
1815 * RETURNS:
1816 * Zero on success, errno on failure.
1817 */
1818int drm_mode_setcrtc(struct drm_device *dev, void *data,
1819 struct drm_file *file_priv)
1820{
1821 struct drm_mode_config *config = &dev->mode_config;
1822 struct drm_mode_crtc *crtc_req = data;
1823 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001824 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001825 struct drm_connector **connector_set = NULL, *connector;
1826 struct drm_framebuffer *fb = NULL;
1827 struct drm_display_mode *mode = NULL;
1828 struct drm_mode_set set;
1829 uint32_t __user *set_connectors_ptr;
1830 int ret = 0;
1831 int i;
1832
Dave Airliefb3b06c2011-02-08 13:55:21 +10001833 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1834 return -EINVAL;
1835
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001836 /* For some reason crtc x/y offsets are signed internally. */
1837 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1838 return -ERANGE;
1839
Dave Airlief453ba02008-11-07 14:05:41 -08001840 mutex_lock(&dev->mode_config.mutex);
1841 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1842 DRM_MODE_OBJECT_CRTC);
1843 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001844 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001845 ret = -EINVAL;
1846 goto out;
1847 }
1848 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001849 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001850
1851 if (crtc_req->mode_valid) {
1852 /* If we have a mode we need a framebuffer. */
1853 /* If we pass -1, set the mode with the currently bound fb */
1854 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001855 if (!crtc->fb) {
1856 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1857 ret = -EINVAL;
1858 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001859 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001860 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001861 } else {
1862 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1863 DRM_MODE_OBJECT_FB);
1864 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001865 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1866 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001867 ret = -EINVAL;
1868 goto out;
1869 }
1870 fb = obj_to_fb(obj);
1871 }
1872
1873 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001874 if (!mode) {
1875 ret = -ENOMEM;
1876 goto out;
1877 }
1878
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001879 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1880 if (ret) {
1881 DRM_DEBUG_KMS("Invalid mode\n");
1882 goto out;
1883 }
1884
Dave Airlief453ba02008-11-07 14:05:41 -08001885 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001886
1887 if (mode->hdisplay > fb->width ||
1888 mode->vdisplay > fb->height ||
1889 crtc_req->x > fb->width - mode->hdisplay ||
1890 crtc_req->y > fb->height - mode->vdisplay) {
1891 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1892 mode->hdisplay, mode->vdisplay,
1893 crtc_req->x, crtc_req->y,
1894 fb->width, fb->height);
1895 ret = -ENOSPC;
1896 goto out;
1897 }
Dave Airlief453ba02008-11-07 14:05:41 -08001898 }
1899
1900 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001901 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001902 ret = -EINVAL;
1903 goto out;
1904 }
1905
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001906 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001907 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001908 crtc_req->count_connectors);
1909 ret = -EINVAL;
1910 goto out;
1911 }
1912
1913 if (crtc_req->count_connectors > 0) {
1914 u32 out_id;
1915
1916 /* Avoid unbounded kernel memory allocation */
1917 if (crtc_req->count_connectors > config->num_connector) {
1918 ret = -EINVAL;
1919 goto out;
1920 }
1921
1922 connector_set = kmalloc(crtc_req->count_connectors *
1923 sizeof(struct drm_connector *),
1924 GFP_KERNEL);
1925 if (!connector_set) {
1926 ret = -ENOMEM;
1927 goto out;
1928 }
1929
1930 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001931 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001932 if (get_user(out_id, &set_connectors_ptr[i])) {
1933 ret = -EFAULT;
1934 goto out;
1935 }
1936
1937 obj = drm_mode_object_find(dev, out_id,
1938 DRM_MODE_OBJECT_CONNECTOR);
1939 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001940 DRM_DEBUG_KMS("Connector id %d unknown\n",
1941 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001942 ret = -EINVAL;
1943 goto out;
1944 }
1945 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001946 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1947 connector->base.id,
1948 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001949
1950 connector_set[i] = connector;
1951 }
1952 }
1953
1954 set.crtc = crtc;
1955 set.x = crtc_req->x;
1956 set.y = crtc_req->y;
1957 set.mode = mode;
1958 set.connectors = connector_set;
1959 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001960 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001961 ret = crtc->funcs->set_config(&set);
1962
1963out:
1964 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001965 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001966 mutex_unlock(&dev->mode_config.mutex);
1967 return ret;
1968}
1969
1970int drm_mode_cursor_ioctl(struct drm_device *dev,
1971 void *data, struct drm_file *file_priv)
1972{
1973 struct drm_mode_cursor *req = data;
1974 struct drm_mode_object *obj;
1975 struct drm_crtc *crtc;
1976 int ret = 0;
1977
Dave Airliefb3b06c2011-02-08 13:55:21 +10001978 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1979 return -EINVAL;
1980
Jesse Barnesacb4b992011-11-07 12:03:23 -08001981 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001982 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001983
1984 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001985 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001986 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001987 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001988 ret = -EINVAL;
1989 goto out;
1990 }
1991 crtc = obj_to_crtc(obj);
1992
1993 if (req->flags & DRM_MODE_CURSOR_BO) {
1994 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001995 ret = -ENXIO;
1996 goto out;
1997 }
1998 /* Turns off the cursor if handle is 0 */
1999 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2000 req->width, req->height);
2001 }
2002
2003 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2004 if (crtc->funcs->cursor_move) {
2005 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2006 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002007 ret = -EFAULT;
2008 goto out;
2009 }
2010 }
2011out:
2012 mutex_unlock(&dev->mode_config.mutex);
2013 return ret;
2014}
2015
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002016/* Original addfb only supported RGB formats, so figure out which one */
2017uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2018{
2019 uint32_t fmt;
2020
2021 switch (bpp) {
2022 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002023 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002024 break;
2025 case 16:
2026 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002027 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002028 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002029 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002030 break;
2031 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 break;
2034 case 32:
2035 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002036 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002037 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002038 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002039 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002040 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002041 break;
2042 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2044 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002045 break;
2046 }
2047
2048 return fmt;
2049}
2050EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2051
Dave Airlief453ba02008-11-07 14:05:41 -08002052/**
2053 * drm_mode_addfb - add an FB to the graphics configuration
2054 * @inode: inode from the ioctl
2055 * @filp: file * from the ioctl
2056 * @cmd: cmd from ioctl
2057 * @arg: arg from ioctl
2058 *
2059 * LOCKING:
2060 * Takes mode config lock.
2061 *
2062 * Add a new FB to the specified CRTC, given a user request.
2063 *
2064 * Called by the user via ioctl.
2065 *
2066 * RETURNS:
2067 * Zero on success, errno on failure.
2068 */
2069int drm_mode_addfb(struct drm_device *dev,
2070 void *data, struct drm_file *file_priv)
2071{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002072 struct drm_mode_fb_cmd *or = data;
2073 struct drm_mode_fb_cmd2 r = {};
2074 struct drm_mode_config *config = &dev->mode_config;
2075 struct drm_framebuffer *fb;
2076 int ret = 0;
2077
2078 /* Use new struct with format internally */
2079 r.fb_id = or->fb_id;
2080 r.width = or->width;
2081 r.height = or->height;
2082 r.pitches[0] = or->pitch;
2083 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2084 r.handles[0] = or->handle;
2085
2086 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2087 return -EINVAL;
2088
Jesse Barnesacb4b992011-11-07 12:03:23 -08002089 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002090 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002091
2092 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002093 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002094
2095 mutex_lock(&dev->mode_config.mutex);
2096
2097 /* TODO check buffer is sufficiently large */
2098 /* TODO setup destructor callback */
2099
2100 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2101 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002102 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002103 ret = PTR_ERR(fb);
2104 goto out;
2105 }
2106
2107 or->fb_id = fb->base.id;
2108 list_add(&fb->filp_head, &file_priv->fbs);
2109 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2110
2111out:
2112 mutex_unlock(&dev->mode_config.mutex);
2113 return ret;
2114}
2115
Ville Syrjälä935b5972011-12-20 00:06:48 +02002116static int format_check(struct drm_mode_fb_cmd2 *r)
2117{
2118 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2119
2120 switch (format) {
2121 case DRM_FORMAT_C8:
2122 case DRM_FORMAT_RGB332:
2123 case DRM_FORMAT_BGR233:
2124 case DRM_FORMAT_XRGB4444:
2125 case DRM_FORMAT_XBGR4444:
2126 case DRM_FORMAT_RGBX4444:
2127 case DRM_FORMAT_BGRX4444:
2128 case DRM_FORMAT_ARGB4444:
2129 case DRM_FORMAT_ABGR4444:
2130 case DRM_FORMAT_RGBA4444:
2131 case DRM_FORMAT_BGRA4444:
2132 case DRM_FORMAT_XRGB1555:
2133 case DRM_FORMAT_XBGR1555:
2134 case DRM_FORMAT_RGBX5551:
2135 case DRM_FORMAT_BGRX5551:
2136 case DRM_FORMAT_ARGB1555:
2137 case DRM_FORMAT_ABGR1555:
2138 case DRM_FORMAT_RGBA5551:
2139 case DRM_FORMAT_BGRA5551:
2140 case DRM_FORMAT_RGB565:
2141 case DRM_FORMAT_BGR565:
2142 case DRM_FORMAT_RGB888:
2143 case DRM_FORMAT_BGR888:
2144 case DRM_FORMAT_XRGB8888:
2145 case DRM_FORMAT_XBGR8888:
2146 case DRM_FORMAT_RGBX8888:
2147 case DRM_FORMAT_BGRX8888:
2148 case DRM_FORMAT_ARGB8888:
2149 case DRM_FORMAT_ABGR8888:
2150 case DRM_FORMAT_RGBA8888:
2151 case DRM_FORMAT_BGRA8888:
2152 case DRM_FORMAT_XRGB2101010:
2153 case DRM_FORMAT_XBGR2101010:
2154 case DRM_FORMAT_RGBX1010102:
2155 case DRM_FORMAT_BGRX1010102:
2156 case DRM_FORMAT_ARGB2101010:
2157 case DRM_FORMAT_ABGR2101010:
2158 case DRM_FORMAT_RGBA1010102:
2159 case DRM_FORMAT_BGRA1010102:
2160 case DRM_FORMAT_YUYV:
2161 case DRM_FORMAT_YVYU:
2162 case DRM_FORMAT_UYVY:
2163 case DRM_FORMAT_VYUY:
2164 case DRM_FORMAT_AYUV:
2165 case DRM_FORMAT_NV12:
2166 case DRM_FORMAT_NV21:
2167 case DRM_FORMAT_NV16:
2168 case DRM_FORMAT_NV61:
2169 case DRM_FORMAT_YUV410:
2170 case DRM_FORMAT_YVU410:
2171 case DRM_FORMAT_YUV411:
2172 case DRM_FORMAT_YVU411:
2173 case DRM_FORMAT_YUV420:
2174 case DRM_FORMAT_YVU420:
2175 case DRM_FORMAT_YUV422:
2176 case DRM_FORMAT_YVU422:
2177 case DRM_FORMAT_YUV444:
2178 case DRM_FORMAT_YVU444:
2179 return 0;
2180 default:
2181 return -EINVAL;
2182 }
2183}
2184
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002185static int framebuffer_check(struct drm_mode_fb_cmd2 *r)
2186{
2187 int ret, hsub, vsub, num_planes, i;
2188
2189 ret = format_check(r);
2190 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002191 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002192 return ret;
2193 }
2194
2195 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2196 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2197 num_planes = drm_format_num_planes(r->pixel_format);
2198
2199 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002200 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002201 return -EINVAL;
2202 }
2203
2204 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002205 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002206 return -EINVAL;
2207 }
2208
2209 for (i = 0; i < num_planes; i++) {
2210 unsigned int width = r->width / (i != 0 ? hsub : 1);
2211
2212 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002213 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002214 return -EINVAL;
2215 }
2216
2217 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002218 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002219 return -EINVAL;
2220 }
2221 }
2222
2223 return 0;
2224}
2225
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002226/**
2227 * drm_mode_addfb2 - add an FB to the graphics configuration
2228 * @inode: inode from the ioctl
2229 * @filp: file * from the ioctl
2230 * @cmd: cmd from ioctl
2231 * @arg: arg from ioctl
2232 *
2233 * LOCKING:
2234 * Takes mode config lock.
2235 *
2236 * Add a new FB to the specified CRTC, given a user request with format.
2237 *
2238 * Called by the user via ioctl.
2239 *
2240 * RETURNS:
2241 * Zero on success, errno on failure.
2242 */
2243int drm_mode_addfb2(struct drm_device *dev,
2244 void *data, struct drm_file *file_priv)
2245{
2246 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002247 struct drm_mode_config *config = &dev->mode_config;
2248 struct drm_framebuffer *fb;
2249 int ret = 0;
2250
Dave Airliefb3b06c2011-02-08 13:55:21 +10002251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2252 return -EINVAL;
2253
Dave Airlief453ba02008-11-07 14:05:41 -08002254 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002255 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002256 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002257 return -EINVAL;
2258 }
2259 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002260 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002261 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002262 return -EINVAL;
2263 }
2264
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002265 ret = framebuffer_check(r);
2266 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002267 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002268
Dave Airlief453ba02008-11-07 14:05:41 -08002269 mutex_lock(&dev->mode_config.mutex);
2270
Dave Airlief453ba02008-11-07 14:05:41 -08002271 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002272 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002273 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002274 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002275 goto out;
2276 }
2277
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002278 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002279 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002280 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002281
2282out:
2283 mutex_unlock(&dev->mode_config.mutex);
2284 return ret;
2285}
2286
2287/**
2288 * drm_mode_rmfb - remove an FB from the configuration
2289 * @inode: inode from the ioctl
2290 * @filp: file * from the ioctl
2291 * @cmd: cmd from ioctl
2292 * @arg: arg from ioctl
2293 *
2294 * LOCKING:
2295 * Takes mode config lock.
2296 *
2297 * Remove the FB specified by the user.
2298 *
2299 * Called by the user via ioctl.
2300 *
2301 * RETURNS:
2302 * Zero on success, errno on failure.
2303 */
2304int drm_mode_rmfb(struct drm_device *dev,
2305 void *data, struct drm_file *file_priv)
2306{
2307 struct drm_mode_object *obj;
2308 struct drm_framebuffer *fb = NULL;
2309 struct drm_framebuffer *fbl = NULL;
2310 uint32_t *id = data;
2311 int ret = 0;
2312 int found = 0;
2313
Dave Airliefb3b06c2011-02-08 13:55:21 +10002314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2315 return -EINVAL;
2316
Dave Airlief453ba02008-11-07 14:05:41 -08002317 mutex_lock(&dev->mode_config.mutex);
2318 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002319 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002320 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002321 ret = -EINVAL;
2322 goto out;
2323 }
2324 fb = obj_to_fb(obj);
2325
2326 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2327 if (fb == fbl)
2328 found = 1;
2329
2330 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002331 ret = -EINVAL;
2332 goto out;
2333 }
2334
2335 /* TODO release all crtc connected to the framebuffer */
2336 /* TODO unhock the destructor from the buffer object */
2337
2338 list_del(&fb->filp_head);
2339 fb->funcs->destroy(fb);
2340
2341out:
2342 mutex_unlock(&dev->mode_config.mutex);
2343 return ret;
2344}
2345
2346/**
2347 * drm_mode_getfb - get FB info
2348 * @inode: inode from the ioctl
2349 * @filp: file * from the ioctl
2350 * @cmd: cmd from ioctl
2351 * @arg: arg from ioctl
2352 *
2353 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002354 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002355 *
2356 * Lookup the FB given its ID and return info about it.
2357 *
2358 * Called by the user via ioctl.
2359 *
2360 * RETURNS:
2361 * Zero on success, errno on failure.
2362 */
2363int drm_mode_getfb(struct drm_device *dev,
2364 void *data, struct drm_file *file_priv)
2365{
2366 struct drm_mode_fb_cmd *r = data;
2367 struct drm_mode_object *obj;
2368 struct drm_framebuffer *fb;
2369 int ret = 0;
2370
Dave Airliefb3b06c2011-02-08 13:55:21 +10002371 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2372 return -EINVAL;
2373
Dave Airlief453ba02008-11-07 14:05:41 -08002374 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002375 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002376 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002377 ret = -EINVAL;
2378 goto out;
2379 }
2380 fb = obj_to_fb(obj);
2381
2382 r->height = fb->height;
2383 r->width = fb->width;
2384 r->depth = fb->depth;
2385 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002386 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002387 fb->funcs->create_handle(fb, file_priv, &r->handle);
2388
2389out:
2390 mutex_unlock(&dev->mode_config.mutex);
2391 return ret;
2392}
2393
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002394int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2395 void *data, struct drm_file *file_priv)
2396{
2397 struct drm_clip_rect __user *clips_ptr;
2398 struct drm_clip_rect *clips = NULL;
2399 struct drm_mode_fb_dirty_cmd *r = data;
2400 struct drm_mode_object *obj;
2401 struct drm_framebuffer *fb;
2402 unsigned flags;
2403 int num_clips;
2404 int ret = 0;
2405
Dave Airliefb3b06c2011-02-08 13:55:21 +10002406 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2407 return -EINVAL;
2408
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002409 mutex_lock(&dev->mode_config.mutex);
2410 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2411 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002412 ret = -EINVAL;
2413 goto out_err1;
2414 }
2415 fb = obj_to_fb(obj);
2416
2417 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002418 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002419
2420 if (!num_clips != !clips_ptr) {
2421 ret = -EINVAL;
2422 goto out_err1;
2423 }
2424
2425 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2426
2427 /* If userspace annotates copy, clips must come in pairs */
2428 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2429 ret = -EINVAL;
2430 goto out_err1;
2431 }
2432
2433 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002434 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2435 ret = -EINVAL;
2436 goto out_err1;
2437 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002438 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2439 if (!clips) {
2440 ret = -ENOMEM;
2441 goto out_err1;
2442 }
2443
2444 ret = copy_from_user(clips, clips_ptr,
2445 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002446 if (ret) {
2447 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002448 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002449 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002450 }
2451
2452 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002453 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2454 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002455 } else {
2456 ret = -ENOSYS;
2457 goto out_err2;
2458 }
2459
2460out_err2:
2461 kfree(clips);
2462out_err1:
2463 mutex_unlock(&dev->mode_config.mutex);
2464 return ret;
2465}
2466
2467
Dave Airlief453ba02008-11-07 14:05:41 -08002468/**
2469 * drm_fb_release - remove and free the FBs on this file
2470 * @filp: file * from the ioctl
2471 *
2472 * LOCKING:
2473 * Takes mode config lock.
2474 *
2475 * Destroy all the FBs associated with @filp.
2476 *
2477 * Called by the user via ioctl.
2478 *
2479 * RETURNS:
2480 * Zero on success, errno on failure.
2481 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002482void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002483{
Dave Airlief453ba02008-11-07 14:05:41 -08002484 struct drm_device *dev = priv->minor->dev;
2485 struct drm_framebuffer *fb, *tfb;
2486
2487 mutex_lock(&dev->mode_config.mutex);
2488 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2489 list_del(&fb->filp_head);
2490 fb->funcs->destroy(fb);
2491 }
2492 mutex_unlock(&dev->mode_config.mutex);
2493}
2494
2495/**
2496 * drm_mode_attachmode - add a mode to the user mode list
2497 * @dev: DRM device
2498 * @connector: connector to add the mode to
2499 * @mode: mode to add
2500 *
2501 * Add @mode to @connector's user mode list.
2502 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002503static void drm_mode_attachmode(struct drm_device *dev,
2504 struct drm_connector *connector,
2505 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002506{
Dave Airlief453ba02008-11-07 14:05:41 -08002507 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002508}
2509
2510int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002511 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002512{
2513 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002514 int ret = 0;
2515 struct drm_display_mode *dup_mode, *next;
2516 LIST_HEAD(list);
2517
Dave Airlief453ba02008-11-07 14:05:41 -08002518 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2519 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002520 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002521 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002522 dup_mode = drm_mode_duplicate(dev, mode);
2523 if (!dup_mode) {
2524 ret = -ENOMEM;
2525 goto out;
2526 }
2527 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002528 }
2529 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002530
2531 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2532 if (!connector->encoder)
2533 continue;
2534 if (connector->encoder->crtc == crtc)
2535 list_move_tail(list.next, &connector->user_modes);
2536 }
2537
2538 WARN_ON(!list_empty(&list));
2539
2540 out:
2541 list_for_each_entry_safe(dup_mode, next, &list, head)
2542 drm_mode_destroy(dev, dup_mode);
2543
2544 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002545}
2546EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2547
2548static int drm_mode_detachmode(struct drm_device *dev,
2549 struct drm_connector *connector,
2550 struct drm_display_mode *mode)
2551{
2552 int found = 0;
2553 int ret = 0;
2554 struct drm_display_mode *match_mode, *t;
2555
2556 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2557 if (drm_mode_equal(match_mode, mode)) {
2558 list_del(&match_mode->head);
2559 drm_mode_destroy(dev, match_mode);
2560 found = 1;
2561 break;
2562 }
2563 }
2564
2565 if (!found)
2566 ret = -EINVAL;
2567
2568 return ret;
2569}
2570
2571int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2572{
2573 struct drm_connector *connector;
2574
2575 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2576 drm_mode_detachmode(dev, connector, mode);
2577 }
2578 return 0;
2579}
2580EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2581
2582/**
2583 * drm_fb_attachmode - Attach a user mode to an connector
2584 * @inode: inode from the ioctl
2585 * @filp: file * from the ioctl
2586 * @cmd: cmd from ioctl
2587 * @arg: arg from ioctl
2588 *
2589 * This attaches a user specified mode to an connector.
2590 * Called by the user via ioctl.
2591 *
2592 * RETURNS:
2593 * Zero on success, errno on failure.
2594 */
2595int drm_mode_attachmode_ioctl(struct drm_device *dev,
2596 void *data, struct drm_file *file_priv)
2597{
2598 struct drm_mode_mode_cmd *mode_cmd = data;
2599 struct drm_connector *connector;
2600 struct drm_display_mode *mode;
2601 struct drm_mode_object *obj;
2602 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2603 int ret = 0;
2604
Dave Airliefb3b06c2011-02-08 13:55:21 +10002605 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2606 return -EINVAL;
2607
Dave Airlief453ba02008-11-07 14:05:41 -08002608 mutex_lock(&dev->mode_config.mutex);
2609
2610 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2611 if (!obj) {
2612 ret = -EINVAL;
2613 goto out;
2614 }
2615 connector = obj_to_connector(obj);
2616
2617 mode = drm_mode_create(dev);
2618 if (!mode) {
2619 ret = -ENOMEM;
2620 goto out;
2621 }
2622
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002623 ret = drm_crtc_convert_umode(mode, umode);
2624 if (ret) {
2625 DRM_DEBUG_KMS("Invalid mode\n");
2626 drm_mode_destroy(dev, mode);
2627 goto out;
2628 }
Dave Airlief453ba02008-11-07 14:05:41 -08002629
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002630 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002631out:
2632 mutex_unlock(&dev->mode_config.mutex);
2633 return ret;
2634}
2635
2636
2637/**
2638 * drm_fb_detachmode - Detach a user specified mode from an connector
2639 * @inode: inode from the ioctl
2640 * @filp: file * from the ioctl
2641 * @cmd: cmd from ioctl
2642 * @arg: arg from ioctl
2643 *
2644 * Called by the user via ioctl.
2645 *
2646 * RETURNS:
2647 * Zero on success, errno on failure.
2648 */
2649int drm_mode_detachmode_ioctl(struct drm_device *dev,
2650 void *data, struct drm_file *file_priv)
2651{
2652 struct drm_mode_object *obj;
2653 struct drm_mode_mode_cmd *mode_cmd = data;
2654 struct drm_connector *connector;
2655 struct drm_display_mode mode;
2656 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2657 int ret = 0;
2658
Dave Airliefb3b06c2011-02-08 13:55:21 +10002659 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2660 return -EINVAL;
2661
Dave Airlief453ba02008-11-07 14:05:41 -08002662 mutex_lock(&dev->mode_config.mutex);
2663
2664 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2665 if (!obj) {
2666 ret = -EINVAL;
2667 goto out;
2668 }
2669 connector = obj_to_connector(obj);
2670
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002671 ret = drm_crtc_convert_umode(&mode, umode);
2672 if (ret) {
2673 DRM_DEBUG_KMS("Invalid mode\n");
2674 goto out;
2675 }
2676
Dave Airlief453ba02008-11-07 14:05:41 -08002677 ret = drm_mode_detachmode(dev, connector, &mode);
2678out:
2679 mutex_unlock(&dev->mode_config.mutex);
2680 return ret;
2681}
2682
2683struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2684 const char *name, int num_values)
2685{
2686 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002687 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002688
2689 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2690 if (!property)
2691 return NULL;
2692
2693 if (num_values) {
2694 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2695 if (!property->values)
2696 goto fail;
2697 }
2698
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002699 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2700 if (ret)
2701 goto fail;
2702
Dave Airlief453ba02008-11-07 14:05:41 -08002703 property->flags = flags;
2704 property->num_values = num_values;
2705 INIT_LIST_HEAD(&property->enum_blob_list);
2706
Vinson Lee471dd2e2011-11-10 11:55:40 -08002707 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002708 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002709 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2710 }
Dave Airlief453ba02008-11-07 14:05:41 -08002711
2712 list_add_tail(&property->head, &dev->mode_config.property_list);
2713 return property;
2714fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002715 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002716 kfree(property);
2717 return NULL;
2718}
2719EXPORT_SYMBOL(drm_property_create);
2720
Sascha Hauer4a67d392012-02-06 10:58:17 +01002721struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2722 const char *name,
2723 const struct drm_prop_enum_list *props,
2724 int num_values)
2725{
2726 struct drm_property *property;
2727 int i, ret;
2728
2729 flags |= DRM_MODE_PROP_ENUM;
2730
2731 property = drm_property_create(dev, flags, name, num_values);
2732 if (!property)
2733 return NULL;
2734
2735 for (i = 0; i < num_values; i++) {
2736 ret = drm_property_add_enum(property, i,
2737 props[i].type,
2738 props[i].name);
2739 if (ret) {
2740 drm_property_destroy(dev, property);
2741 return NULL;
2742 }
2743 }
2744
2745 return property;
2746}
2747EXPORT_SYMBOL(drm_property_create_enum);
2748
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002749struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2750 const char *name,
2751 uint64_t min, uint64_t max)
2752{
2753 struct drm_property *property;
2754
2755 flags |= DRM_MODE_PROP_RANGE;
2756
2757 property = drm_property_create(dev, flags, name, 2);
2758 if (!property)
2759 return NULL;
2760
2761 property->values[0] = min;
2762 property->values[1] = max;
2763
2764 return property;
2765}
2766EXPORT_SYMBOL(drm_property_create_range);
2767
Dave Airlief453ba02008-11-07 14:05:41 -08002768int drm_property_add_enum(struct drm_property *property, int index,
2769 uint64_t value, const char *name)
2770{
2771 struct drm_property_enum *prop_enum;
2772
2773 if (!(property->flags & DRM_MODE_PROP_ENUM))
2774 return -EINVAL;
2775
2776 if (!list_empty(&property->enum_blob_list)) {
2777 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2778 if (prop_enum->value == value) {
2779 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2780 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2781 return 0;
2782 }
2783 }
2784 }
2785
2786 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2787 if (!prop_enum)
2788 return -ENOMEM;
2789
2790 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2791 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2792 prop_enum->value = value;
2793
2794 property->values[index] = value;
2795 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2796 return 0;
2797}
2798EXPORT_SYMBOL(drm_property_add_enum);
2799
2800void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2801{
2802 struct drm_property_enum *prop_enum, *pt;
2803
2804 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2805 list_del(&prop_enum->head);
2806 kfree(prop_enum);
2807 }
2808
2809 if (property->num_values)
2810 kfree(property->values);
2811 drm_mode_object_put(dev, &property->base);
2812 list_del(&property->head);
2813 kfree(property);
2814}
2815EXPORT_SYMBOL(drm_property_destroy);
2816
Paulo Zanoniafea2ad2012-05-15 18:09:00 -03002817void drm_connector_attach_property(struct drm_connector *connector,
Dave Airlief453ba02008-11-07 14:05:41 -08002818 struct drm_property *property, uint64_t init_val)
2819{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002820 drm_object_attach_property(&connector->base, property, init_val);
Dave Airlief453ba02008-11-07 14:05:41 -08002821}
2822EXPORT_SYMBOL(drm_connector_attach_property);
2823
2824int drm_connector_property_set_value(struct drm_connector *connector,
2825 struct drm_property *property, uint64_t value)
2826{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002827 return drm_object_property_set_value(&connector->base, property, value);
Dave Airlief453ba02008-11-07 14:05:41 -08002828}
2829EXPORT_SYMBOL(drm_connector_property_set_value);
2830
2831int drm_connector_property_get_value(struct drm_connector *connector,
2832 struct drm_property *property, uint64_t *val)
2833{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002834 return drm_object_property_get_value(&connector->base, property, val);
Dave Airlief453ba02008-11-07 14:05:41 -08002835}
2836EXPORT_SYMBOL(drm_connector_property_get_value);
2837
Paulo Zanonic5431882012-05-15 18:09:02 -03002838void drm_object_attach_property(struct drm_mode_object *obj,
2839 struct drm_property *property,
2840 uint64_t init_val)
2841{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002842 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002843
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002844 if (count == DRM_OBJECT_MAX_PROPERTY) {
2845 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2846 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2847 "you see this message on the same object type.\n",
2848 obj->type);
2849 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002850 }
2851
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002852 obj->properties->ids[count] = property->base.id;
2853 obj->properties->values[count] = init_val;
2854 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002855}
2856EXPORT_SYMBOL(drm_object_attach_property);
2857
2858int drm_object_property_set_value(struct drm_mode_object *obj,
2859 struct drm_property *property, uint64_t val)
2860{
2861 int i;
2862
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002863 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002864 if (obj->properties->ids[i] == property->base.id) {
2865 obj->properties->values[i] = val;
2866 return 0;
2867 }
2868 }
2869
2870 return -EINVAL;
2871}
2872EXPORT_SYMBOL(drm_object_property_set_value);
2873
2874int drm_object_property_get_value(struct drm_mode_object *obj,
2875 struct drm_property *property, uint64_t *val)
2876{
2877 int i;
2878
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002879 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002880 if (obj->properties->ids[i] == property->base.id) {
2881 *val = obj->properties->values[i];
2882 return 0;
2883 }
2884 }
2885
2886 return -EINVAL;
2887}
2888EXPORT_SYMBOL(drm_object_property_get_value);
2889
Dave Airlief453ba02008-11-07 14:05:41 -08002890int drm_mode_getproperty_ioctl(struct drm_device *dev,
2891 void *data, struct drm_file *file_priv)
2892{
2893 struct drm_mode_object *obj;
2894 struct drm_mode_get_property *out_resp = data;
2895 struct drm_property *property;
2896 int enum_count = 0;
2897 int blob_count = 0;
2898 int value_count = 0;
2899 int ret = 0, i;
2900 int copied;
2901 struct drm_property_enum *prop_enum;
2902 struct drm_mode_property_enum __user *enum_ptr;
2903 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002904 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002905 uint64_t __user *values_ptr;
2906 uint32_t __user *blob_length_ptr;
2907
Dave Airliefb3b06c2011-02-08 13:55:21 +10002908 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2909 return -EINVAL;
2910
Dave Airlief453ba02008-11-07 14:05:41 -08002911 mutex_lock(&dev->mode_config.mutex);
2912 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2913 if (!obj) {
2914 ret = -EINVAL;
2915 goto done;
2916 }
2917 property = obj_to_property(obj);
2918
2919 if (property->flags & DRM_MODE_PROP_ENUM) {
2920 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2921 enum_count++;
2922 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2923 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2924 blob_count++;
2925 }
2926
2927 value_count = property->num_values;
2928
2929 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2930 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2931 out_resp->flags = property->flags;
2932
2933 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002934 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002935 for (i = 0; i < value_count; i++) {
2936 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2937 ret = -EFAULT;
2938 goto done;
2939 }
2940 }
2941 }
2942 out_resp->count_values = value_count;
2943
2944 if (property->flags & DRM_MODE_PROP_ENUM) {
2945 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2946 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002947 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002948 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2949
2950 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2951 ret = -EFAULT;
2952 goto done;
2953 }
2954
2955 if (copy_to_user(&enum_ptr[copied].name,
2956 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2957 ret = -EFAULT;
2958 goto done;
2959 }
2960 copied++;
2961 }
2962 }
2963 out_resp->count_enum_blobs = enum_count;
2964 }
2965
2966 if (property->flags & DRM_MODE_PROP_BLOB) {
2967 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2968 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002969 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2970 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002971
2972 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2973 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2974 ret = -EFAULT;
2975 goto done;
2976 }
2977
2978 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2979 ret = -EFAULT;
2980 goto done;
2981 }
2982
2983 copied++;
2984 }
2985 }
2986 out_resp->count_enum_blobs = blob_count;
2987 }
2988done:
2989 mutex_unlock(&dev->mode_config.mutex);
2990 return ret;
2991}
2992
2993static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2994 void *data)
2995{
2996 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002997 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002998
2999 if (!length || !data)
3000 return NULL;
3001
3002 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3003 if (!blob)
3004 return NULL;
3005
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003006 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3007 if (ret) {
3008 kfree(blob);
3009 return NULL;
3010 }
3011
Dave Airlief453ba02008-11-07 14:05:41 -08003012 blob->length = length;
3013
3014 memcpy(blob->data, data, length);
3015
Dave Airlief453ba02008-11-07 14:05:41 -08003016 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3017 return blob;
3018}
3019
3020static void drm_property_destroy_blob(struct drm_device *dev,
3021 struct drm_property_blob *blob)
3022{
3023 drm_mode_object_put(dev, &blob->base);
3024 list_del(&blob->head);
3025 kfree(blob);
3026}
3027
3028int drm_mode_getblob_ioctl(struct drm_device *dev,
3029 void *data, struct drm_file *file_priv)
3030{
3031 struct drm_mode_object *obj;
3032 struct drm_mode_get_blob *out_resp = data;
3033 struct drm_property_blob *blob;
3034 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003035 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003036
Dave Airliefb3b06c2011-02-08 13:55:21 +10003037 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3038 return -EINVAL;
3039
Dave Airlief453ba02008-11-07 14:05:41 -08003040 mutex_lock(&dev->mode_config.mutex);
3041 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3042 if (!obj) {
3043 ret = -EINVAL;
3044 goto done;
3045 }
3046 blob = obj_to_blob(obj);
3047
3048 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003049 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003050 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3051 ret = -EFAULT;
3052 goto done;
3053 }
3054 }
3055 out_resp->length = blob->length;
3056
3057done:
3058 mutex_unlock(&dev->mode_config.mutex);
3059 return ret;
3060}
3061
3062int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3063 struct edid *edid)
3064{
3065 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00003066 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003067
3068 if (connector->edid_blob_ptr)
3069 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3070
3071 /* Delete edid, when there is none. */
3072 if (!edid) {
3073 connector->edid_blob_ptr = NULL;
3074 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3075 return ret;
3076 }
3077
Adam Jackson7466f4c2010-03-29 21:43:23 +00003078 size = EDID_LENGTH * (1 + edid->extensions);
3079 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3080 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003081
3082 ret = drm_connector_property_set_value(connector,
3083 dev->mode_config.edid_property,
3084 connector->edid_blob_ptr->base.id);
3085
3086 return ret;
3087}
3088EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3089
Paulo Zanoni26a34812012-05-15 18:08:59 -03003090static bool drm_property_change_is_valid(struct drm_property *property,
3091 __u64 value)
3092{
3093 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3094 return false;
3095 if (property->flags & DRM_MODE_PROP_RANGE) {
3096 if (value < property->values[0] || value > property->values[1])
3097 return false;
3098 return true;
3099 } else {
3100 int i;
3101 for (i = 0; i < property->num_values; i++)
3102 if (property->values[i] == value)
3103 return true;
3104 return false;
3105 }
3106}
3107
Dave Airlief453ba02008-11-07 14:05:41 -08003108int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3109 void *data, struct drm_file *file_priv)
3110{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003111 struct drm_mode_connector_set_property *conn_set_prop = data;
3112 struct drm_mode_obj_set_property obj_set_prop = {
3113 .value = conn_set_prop->value,
3114 .prop_id = conn_set_prop->prop_id,
3115 .obj_id = conn_set_prop->connector_id,
3116 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3117 };
Dave Airlief453ba02008-11-07 14:05:41 -08003118
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003119 /* It does all the locking and checking we need */
3120 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003121}
3122
Paulo Zanonic5431882012-05-15 18:09:02 -03003123static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3124 struct drm_property *property,
3125 uint64_t value)
3126{
3127 int ret = -EINVAL;
3128 struct drm_connector *connector = obj_to_connector(obj);
3129
3130 /* Do DPMS ourselves */
3131 if (property == connector->dev->mode_config.dpms_property) {
3132 if (connector->funcs->dpms)
3133 (*connector->funcs->dpms)(connector, (int)value);
3134 ret = 0;
3135 } else if (connector->funcs->set_property)
3136 ret = connector->funcs->set_property(connector, property, value);
3137
3138 /* store the property value if successful */
3139 if (!ret)
3140 drm_connector_property_set_value(connector, property, value);
3141 return ret;
3142}
3143
3144int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3145 struct drm_file *file_priv)
3146{
3147 struct drm_mode_obj_get_properties *arg = data;
3148 struct drm_mode_object *obj;
3149 int ret = 0;
3150 int i;
3151 int copied = 0;
3152 int props_count = 0;
3153 uint32_t __user *props_ptr;
3154 uint64_t __user *prop_values_ptr;
3155
3156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3157 return -EINVAL;
3158
3159 mutex_lock(&dev->mode_config.mutex);
3160
3161 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3162 if (!obj) {
3163 ret = -EINVAL;
3164 goto out;
3165 }
3166 if (!obj->properties) {
3167 ret = -EINVAL;
3168 goto out;
3169 }
3170
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003171 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003172
3173 /* This ioctl is called twice, once to determine how much space is
3174 * needed, and the 2nd time to fill it. */
3175 if ((arg->count_props >= props_count) && props_count) {
3176 copied = 0;
3177 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3178 prop_values_ptr = (uint64_t __user *)(unsigned long)
3179 (arg->prop_values_ptr);
3180 for (i = 0; i < props_count; i++) {
3181 if (put_user(obj->properties->ids[i],
3182 props_ptr + copied)) {
3183 ret = -EFAULT;
3184 goto out;
3185 }
3186 if (put_user(obj->properties->values[i],
3187 prop_values_ptr + copied)) {
3188 ret = -EFAULT;
3189 goto out;
3190 }
3191 copied++;
3192 }
3193 }
3194 arg->count_props = props_count;
3195out:
3196 mutex_unlock(&dev->mode_config.mutex);
3197 return ret;
3198}
3199
3200int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3201 struct drm_file *file_priv)
3202{
3203 struct drm_mode_obj_set_property *arg = data;
3204 struct drm_mode_object *arg_obj;
3205 struct drm_mode_object *prop_obj;
3206 struct drm_property *property;
3207 int ret = -EINVAL;
3208 int i;
3209
3210 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3211 return -EINVAL;
3212
3213 mutex_lock(&dev->mode_config.mutex);
3214
3215 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3216 if (!arg_obj)
3217 goto out;
3218 if (!arg_obj->properties)
3219 goto out;
3220
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003221 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003222 if (arg_obj->properties->ids[i] == arg->prop_id)
3223 break;
3224
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003225 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003226 goto out;
3227
3228 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3229 DRM_MODE_OBJECT_PROPERTY);
3230 if (!prop_obj)
3231 goto out;
3232 property = obj_to_property(prop_obj);
3233
3234 if (!drm_property_change_is_valid(property, arg->value))
3235 goto out;
3236
3237 switch (arg_obj->type) {
3238 case DRM_MODE_OBJECT_CONNECTOR:
3239 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3240 arg->value);
3241 break;
3242 }
3243
3244out:
3245 mutex_unlock(&dev->mode_config.mutex);
3246 return ret;
3247}
3248
Dave Airlief453ba02008-11-07 14:05:41 -08003249int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3250 struct drm_encoder *encoder)
3251{
3252 int i;
3253
3254 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3255 if (connector->encoder_ids[i] == 0) {
3256 connector->encoder_ids[i] = encoder->base.id;
3257 return 0;
3258 }
3259 }
3260 return -ENOMEM;
3261}
3262EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3263
3264void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3265 struct drm_encoder *encoder)
3266{
3267 int i;
3268 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3269 if (connector->encoder_ids[i] == encoder->base.id) {
3270 connector->encoder_ids[i] = 0;
3271 if (connector->encoder == encoder)
3272 connector->encoder = NULL;
3273 break;
3274 }
3275 }
3276}
3277EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3278
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003279int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003280 int gamma_size)
3281{
3282 crtc->gamma_size = gamma_size;
3283
3284 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3285 if (!crtc->gamma_store) {
3286 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003287 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003288 }
3289
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003290 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003291}
3292EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3293
3294int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3295 void *data, struct drm_file *file_priv)
3296{
3297 struct drm_mode_crtc_lut *crtc_lut = data;
3298 struct drm_mode_object *obj;
3299 struct drm_crtc *crtc;
3300 void *r_base, *g_base, *b_base;
3301 int size;
3302 int ret = 0;
3303
Dave Airliefb3b06c2011-02-08 13:55:21 +10003304 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3305 return -EINVAL;
3306
Dave Airlief453ba02008-11-07 14:05:41 -08003307 mutex_lock(&dev->mode_config.mutex);
3308 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3309 if (!obj) {
3310 ret = -EINVAL;
3311 goto out;
3312 }
3313 crtc = obj_to_crtc(obj);
3314
3315 /* memcpy into gamma store */
3316 if (crtc_lut->gamma_size != crtc->gamma_size) {
3317 ret = -EINVAL;
3318 goto out;
3319 }
3320
3321 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3322 r_base = crtc->gamma_store;
3323 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3324 ret = -EFAULT;
3325 goto out;
3326 }
3327
3328 g_base = r_base + size;
3329 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3330 ret = -EFAULT;
3331 goto out;
3332 }
3333
3334 b_base = g_base + size;
3335 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3336 ret = -EFAULT;
3337 goto out;
3338 }
3339
James Simmons72034252010-08-03 01:33:19 +01003340 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003341
3342out:
3343 mutex_unlock(&dev->mode_config.mutex);
3344 return ret;
3345
3346}
3347
3348int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3349 void *data, struct drm_file *file_priv)
3350{
3351 struct drm_mode_crtc_lut *crtc_lut = data;
3352 struct drm_mode_object *obj;
3353 struct drm_crtc *crtc;
3354 void *r_base, *g_base, *b_base;
3355 int size;
3356 int ret = 0;
3357
Dave Airliefb3b06c2011-02-08 13:55:21 +10003358 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3359 return -EINVAL;
3360
Dave Airlief453ba02008-11-07 14:05:41 -08003361 mutex_lock(&dev->mode_config.mutex);
3362 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3363 if (!obj) {
3364 ret = -EINVAL;
3365 goto out;
3366 }
3367 crtc = obj_to_crtc(obj);
3368
3369 /* memcpy into gamma store */
3370 if (crtc_lut->gamma_size != crtc->gamma_size) {
3371 ret = -EINVAL;
3372 goto out;
3373 }
3374
3375 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3376 r_base = crtc->gamma_store;
3377 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3378 ret = -EFAULT;
3379 goto out;
3380 }
3381
3382 g_base = r_base + size;
3383 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3384 ret = -EFAULT;
3385 goto out;
3386 }
3387
3388 b_base = g_base + size;
3389 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3390 ret = -EFAULT;
3391 goto out;
3392 }
3393out:
3394 mutex_unlock(&dev->mode_config.mutex);
3395 return ret;
3396}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003397
3398int drm_mode_page_flip_ioctl(struct drm_device *dev,
3399 void *data, struct drm_file *file_priv)
3400{
3401 struct drm_mode_crtc_page_flip *page_flip = data;
3402 struct drm_mode_object *obj;
3403 struct drm_crtc *crtc;
3404 struct drm_framebuffer *fb;
3405 struct drm_pending_vblank_event *e = NULL;
3406 unsigned long flags;
3407 int ret = -EINVAL;
3408
3409 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3410 page_flip->reserved != 0)
3411 return -EINVAL;
3412
3413 mutex_lock(&dev->mode_config.mutex);
3414 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3415 if (!obj)
3416 goto out;
3417 crtc = obj_to_crtc(obj);
3418
Chris Wilson90c1efd2010-07-17 20:23:26 +01003419 if (crtc->fb == NULL) {
3420 /* The framebuffer is currently unbound, presumably
3421 * due to a hotplug event, that userspace has not
3422 * yet discovered.
3423 */
3424 ret = -EBUSY;
3425 goto out;
3426 }
3427
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003428 if (crtc->funcs->page_flip == NULL)
3429 goto out;
3430
3431 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3432 if (!obj)
3433 goto out;
3434 fb = obj_to_fb(obj);
3435
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003436 if (crtc->mode.hdisplay > fb->width ||
3437 crtc->mode.vdisplay > fb->height ||
3438 crtc->x > fb->width - crtc->mode.hdisplay ||
3439 crtc->y > fb->height - crtc->mode.vdisplay) {
3440 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3441 fb->width, fb->height,
3442 crtc->mode.hdisplay, crtc->mode.vdisplay,
3443 crtc->x, crtc->y);
3444 ret = -ENOSPC;
3445 goto out;
3446 }
3447
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003448 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3449 ret = -ENOMEM;
3450 spin_lock_irqsave(&dev->event_lock, flags);
3451 if (file_priv->event_space < sizeof e->event) {
3452 spin_unlock_irqrestore(&dev->event_lock, flags);
3453 goto out;
3454 }
3455 file_priv->event_space -= sizeof e->event;
3456 spin_unlock_irqrestore(&dev->event_lock, flags);
3457
3458 e = kzalloc(sizeof *e, GFP_KERNEL);
3459 if (e == NULL) {
3460 spin_lock_irqsave(&dev->event_lock, flags);
3461 file_priv->event_space += sizeof e->event;
3462 spin_unlock_irqrestore(&dev->event_lock, flags);
3463 goto out;
3464 }
3465
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003466 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003467 e->event.base.length = sizeof e->event;
3468 e->event.user_data = page_flip->user_data;
3469 e->base.event = &e->event.base;
3470 e->base.file_priv = file_priv;
3471 e->base.destroy =
3472 (void (*) (struct drm_pending_event *)) kfree;
3473 }
3474
3475 ret = crtc->funcs->page_flip(crtc, fb, e);
3476 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003477 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3478 spin_lock_irqsave(&dev->event_lock, flags);
3479 file_priv->event_space += sizeof e->event;
3480 spin_unlock_irqrestore(&dev->event_lock, flags);
3481 kfree(e);
3482 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003483 }
3484
3485out:
3486 mutex_unlock(&dev->mode_config.mutex);
3487 return ret;
3488}
Chris Wilsoneb033552011-01-24 15:11:08 +00003489
3490void drm_mode_config_reset(struct drm_device *dev)
3491{
3492 struct drm_crtc *crtc;
3493 struct drm_encoder *encoder;
3494 struct drm_connector *connector;
3495
3496 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3497 if (crtc->funcs->reset)
3498 crtc->funcs->reset(crtc);
3499
3500 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3501 if (encoder->funcs->reset)
3502 encoder->funcs->reset(encoder);
3503
3504 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3505 if (connector->funcs->reset)
3506 connector->funcs->reset(connector);
3507}
3508EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003509
3510int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3511 void *data, struct drm_file *file_priv)
3512{
3513 struct drm_mode_create_dumb *args = data;
3514
3515 if (!dev->driver->dumb_create)
3516 return -ENOSYS;
3517 return dev->driver->dumb_create(file_priv, dev, args);
3518}
3519
3520int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3521 void *data, struct drm_file *file_priv)
3522{
3523 struct drm_mode_map_dumb *args = data;
3524
3525 /* call driver ioctl to get mmap offset */
3526 if (!dev->driver->dumb_map_offset)
3527 return -ENOSYS;
3528
3529 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3530}
3531
3532int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3533 void *data, struct drm_file *file_priv)
3534{
3535 struct drm_mode_destroy_dumb *args = data;
3536
3537 if (!dev->driver->dumb_destroy)
3538 return -ENOSYS;
3539
3540 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3541}
Dave Airlie248dbc22011-11-29 20:02:54 +00003542
3543/*
3544 * Just need to support RGB formats here for compat with code that doesn't
3545 * use pixel formats directly yet.
3546 */
3547void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3548 int *bpp)
3549{
3550 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003551 case DRM_FORMAT_RGB332:
3552 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003553 *depth = 8;
3554 *bpp = 8;
3555 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003556 case DRM_FORMAT_XRGB1555:
3557 case DRM_FORMAT_XBGR1555:
3558 case DRM_FORMAT_RGBX5551:
3559 case DRM_FORMAT_BGRX5551:
3560 case DRM_FORMAT_ARGB1555:
3561 case DRM_FORMAT_ABGR1555:
3562 case DRM_FORMAT_RGBA5551:
3563 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003564 *depth = 15;
3565 *bpp = 16;
3566 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003567 case DRM_FORMAT_RGB565:
3568 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003569 *depth = 16;
3570 *bpp = 16;
3571 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003572 case DRM_FORMAT_RGB888:
3573 case DRM_FORMAT_BGR888:
3574 *depth = 24;
3575 *bpp = 24;
3576 break;
3577 case DRM_FORMAT_XRGB8888:
3578 case DRM_FORMAT_XBGR8888:
3579 case DRM_FORMAT_RGBX8888:
3580 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003581 *depth = 24;
3582 *bpp = 32;
3583 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003584 case DRM_FORMAT_XRGB2101010:
3585 case DRM_FORMAT_XBGR2101010:
3586 case DRM_FORMAT_RGBX1010102:
3587 case DRM_FORMAT_BGRX1010102:
3588 case DRM_FORMAT_ARGB2101010:
3589 case DRM_FORMAT_ABGR2101010:
3590 case DRM_FORMAT_RGBA1010102:
3591 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003592 *depth = 30;
3593 *bpp = 32;
3594 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003595 case DRM_FORMAT_ARGB8888:
3596 case DRM_FORMAT_ABGR8888:
3597 case DRM_FORMAT_RGBA8888:
3598 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003599 *depth = 32;
3600 *bpp = 32;
3601 break;
3602 default:
3603 DRM_DEBUG_KMS("unsupported pixel format\n");
3604 *depth = 0;
3605 *bpp = 0;
3606 break;
3607 }
3608}
3609EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003610
3611/**
3612 * drm_format_num_planes - get the number of planes for format
3613 * @format: pixel format (DRM_FORMAT_*)
3614 *
3615 * RETURNS:
3616 * The number of planes used by the specified pixel format.
3617 */
3618int drm_format_num_planes(uint32_t format)
3619{
3620 switch (format) {
3621 case DRM_FORMAT_YUV410:
3622 case DRM_FORMAT_YVU410:
3623 case DRM_FORMAT_YUV411:
3624 case DRM_FORMAT_YVU411:
3625 case DRM_FORMAT_YUV420:
3626 case DRM_FORMAT_YVU420:
3627 case DRM_FORMAT_YUV422:
3628 case DRM_FORMAT_YVU422:
3629 case DRM_FORMAT_YUV444:
3630 case DRM_FORMAT_YVU444:
3631 return 3;
3632 case DRM_FORMAT_NV12:
3633 case DRM_FORMAT_NV21:
3634 case DRM_FORMAT_NV16:
3635 case DRM_FORMAT_NV61:
3636 return 2;
3637 default:
3638 return 1;
3639 }
3640}
3641EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003642
3643/**
3644 * drm_format_plane_cpp - determine the bytes per pixel value
3645 * @format: pixel format (DRM_FORMAT_*)
3646 * @plane: plane index
3647 *
3648 * RETURNS:
3649 * The bytes per pixel value for the specified plane.
3650 */
3651int drm_format_plane_cpp(uint32_t format, int plane)
3652{
3653 unsigned int depth;
3654 int bpp;
3655
3656 if (plane >= drm_format_num_planes(format))
3657 return 0;
3658
3659 switch (format) {
3660 case DRM_FORMAT_YUYV:
3661 case DRM_FORMAT_YVYU:
3662 case DRM_FORMAT_UYVY:
3663 case DRM_FORMAT_VYUY:
3664 return 2;
3665 case DRM_FORMAT_NV12:
3666 case DRM_FORMAT_NV21:
3667 case DRM_FORMAT_NV16:
3668 case DRM_FORMAT_NV61:
3669 return plane ? 2 : 1;
3670 case DRM_FORMAT_YUV410:
3671 case DRM_FORMAT_YVU410:
3672 case DRM_FORMAT_YUV411:
3673 case DRM_FORMAT_YVU411:
3674 case DRM_FORMAT_YUV420:
3675 case DRM_FORMAT_YVU420:
3676 case DRM_FORMAT_YUV422:
3677 case DRM_FORMAT_YVU422:
3678 case DRM_FORMAT_YUV444:
3679 case DRM_FORMAT_YVU444:
3680 return 1;
3681 default:
3682 drm_fb_get_bpp_depth(format, &depth, &bpp);
3683 return bpp >> 3;
3684 }
3685}
3686EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003687
3688/**
3689 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3690 * @format: pixel format (DRM_FORMAT_*)
3691 *
3692 * RETURNS:
3693 * The horizontal chroma subsampling factor for the
3694 * specified pixel format.
3695 */
3696int drm_format_horz_chroma_subsampling(uint32_t format)
3697{
3698 switch (format) {
3699 case DRM_FORMAT_YUV411:
3700 case DRM_FORMAT_YVU411:
3701 case DRM_FORMAT_YUV410:
3702 case DRM_FORMAT_YVU410:
3703 return 4;
3704 case DRM_FORMAT_YUYV:
3705 case DRM_FORMAT_YVYU:
3706 case DRM_FORMAT_UYVY:
3707 case DRM_FORMAT_VYUY:
3708 case DRM_FORMAT_NV12:
3709 case DRM_FORMAT_NV21:
3710 case DRM_FORMAT_NV16:
3711 case DRM_FORMAT_NV61:
3712 case DRM_FORMAT_YUV422:
3713 case DRM_FORMAT_YVU422:
3714 case DRM_FORMAT_YUV420:
3715 case DRM_FORMAT_YVU420:
3716 return 2;
3717 default:
3718 return 1;
3719 }
3720}
3721EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3722
3723/**
3724 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3725 * @format: pixel format (DRM_FORMAT_*)
3726 *
3727 * RETURNS:
3728 * The vertical chroma subsampling factor for the
3729 * specified pixel format.
3730 */
3731int drm_format_vert_chroma_subsampling(uint32_t format)
3732{
3733 switch (format) {
3734 case DRM_FORMAT_YUV410:
3735 case DRM_FORMAT_YVU410:
3736 return 4;
3737 case DRM_FORMAT_YUV420:
3738 case DRM_FORMAT_YVU420:
3739 case DRM_FORMAT_NV12:
3740 case DRM_FORMAT_NV21:
3741 return 2;
3742 default:
3743 return 1;
3744 }
3745}
3746EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);