blob: 793f51b0f7a480551608f322e095c7e7c1a9c60d [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 Zanoni7e3bdf42012-05-15 18:09:01 -03001428 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++) {
1429 if (connector->properties.ids[i] != 0) {
Dave Airlief453ba02008-11-07 14:05:41 -08001430 props_count++;
1431 }
1432 }
1433
1434 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1435 if (connector->encoder_ids[i] != 0) {
1436 encoders_count++;
1437 }
1438 }
1439
1440 if (out_resp->count_modes == 0) {
1441 connector->funcs->fill_modes(connector,
1442 dev->mode_config.max_width,
1443 dev->mode_config.max_height);
1444 }
1445
1446 /* delayed so we get modes regardless of pre-fill_modes state */
1447 list_for_each_entry(mode, &connector->modes, head)
1448 mode_count++;
1449
1450 out_resp->connector_id = connector->base.id;
1451 out_resp->connector_type = connector->connector_type;
1452 out_resp->connector_type_id = connector->connector_type_id;
1453 out_resp->mm_width = connector->display_info.width_mm;
1454 out_resp->mm_height = connector->display_info.height_mm;
1455 out_resp->subpixel = connector->display_info.subpixel_order;
1456 out_resp->connection = connector->status;
1457 if (connector->encoder)
1458 out_resp->encoder_id = connector->encoder->base.id;
1459 else
1460 out_resp->encoder_id = 0;
1461
1462 /*
1463 * This ioctl is called twice, once to determine how much space is
1464 * needed, and the 2nd time to fill it.
1465 */
1466 if ((out_resp->count_modes >= mode_count) && mode_count) {
1467 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001468 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001469 list_for_each_entry(mode, &connector->modes, head) {
1470 drm_crtc_convert_to_umode(&u_mode, mode);
1471 if (copy_to_user(mode_ptr + copied,
1472 &u_mode, sizeof(u_mode))) {
1473 ret = -EFAULT;
1474 goto out;
1475 }
1476 copied++;
1477 }
1478 }
1479 out_resp->count_modes = mode_count;
1480
1481 if ((out_resp->count_props >= props_count) && props_count) {
1482 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001483 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1484 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -03001485 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++) {
1486 if (connector->properties.ids[i] != 0) {
1487 if (put_user(connector->properties.ids[i],
Dave Airlief453ba02008-11-07 14:05:41 -08001488 prop_ptr + copied)) {
1489 ret = -EFAULT;
1490 goto out;
1491 }
1492
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -03001493 if (put_user(connector->properties.values[i],
Dave Airlief453ba02008-11-07 14:05:41 -08001494 prop_values + copied)) {
1495 ret = -EFAULT;
1496 goto out;
1497 }
1498 copied++;
1499 }
1500 }
1501 }
1502 out_resp->count_props = props_count;
1503
1504 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1505 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001506 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001507 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1508 if (connector->encoder_ids[i] != 0) {
1509 if (put_user(connector->encoder_ids[i],
1510 encoder_ptr + copied)) {
1511 ret = -EFAULT;
1512 goto out;
1513 }
1514 copied++;
1515 }
1516 }
1517 }
1518 out_resp->count_encoders = encoders_count;
1519
1520out:
1521 mutex_unlock(&dev->mode_config.mutex);
1522 return ret;
1523}
1524
1525int drm_mode_getencoder(struct drm_device *dev, void *data,
1526 struct drm_file *file_priv)
1527{
1528 struct drm_mode_get_encoder *enc_resp = data;
1529 struct drm_mode_object *obj;
1530 struct drm_encoder *encoder;
1531 int ret = 0;
1532
Dave Airliefb3b06c2011-02-08 13:55:21 +10001533 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1534 return -EINVAL;
1535
Dave Airlief453ba02008-11-07 14:05:41 -08001536 mutex_lock(&dev->mode_config.mutex);
1537 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1538 DRM_MODE_OBJECT_ENCODER);
1539 if (!obj) {
1540 ret = -EINVAL;
1541 goto out;
1542 }
1543 encoder = obj_to_encoder(obj);
1544
1545 if (encoder->crtc)
1546 enc_resp->crtc_id = encoder->crtc->base.id;
1547 else
1548 enc_resp->crtc_id = 0;
1549 enc_resp->encoder_type = encoder->encoder_type;
1550 enc_resp->encoder_id = encoder->base.id;
1551 enc_resp->possible_crtcs = encoder->possible_crtcs;
1552 enc_resp->possible_clones = encoder->possible_clones;
1553
1554out:
1555 mutex_unlock(&dev->mode_config.mutex);
1556 return ret;
1557}
1558
1559/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001560 * drm_mode_getplane_res - get plane info
1561 * @dev: DRM device
1562 * @data: ioctl data
1563 * @file_priv: DRM file info
1564 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001565 * LOCKING:
1566 * Takes mode config lock.
1567 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001568 * Return an plane count and set of IDs.
1569 */
1570int drm_mode_getplane_res(struct drm_device *dev, void *data,
1571 struct drm_file *file_priv)
1572{
1573 struct drm_mode_get_plane_res *plane_resp = data;
1574 struct drm_mode_config *config;
1575 struct drm_plane *plane;
1576 uint32_t __user *plane_ptr;
1577 int copied = 0, ret = 0;
1578
1579 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1580 return -EINVAL;
1581
1582 mutex_lock(&dev->mode_config.mutex);
1583 config = &dev->mode_config;
1584
1585 /*
1586 * This ioctl is called twice, once to determine how much space is
1587 * needed, and the 2nd time to fill it.
1588 */
1589 if (config->num_plane &&
1590 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001591 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001592
1593 list_for_each_entry(plane, &config->plane_list, head) {
1594 if (put_user(plane->base.id, plane_ptr + copied)) {
1595 ret = -EFAULT;
1596 goto out;
1597 }
1598 copied++;
1599 }
1600 }
1601 plane_resp->count_planes = config->num_plane;
1602
1603out:
1604 mutex_unlock(&dev->mode_config.mutex);
1605 return ret;
1606}
1607
1608/**
1609 * drm_mode_getplane - get plane info
1610 * @dev: DRM device
1611 * @data: ioctl data
1612 * @file_priv: DRM file info
1613 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001614 * LOCKING:
1615 * Takes mode config lock.
1616 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001617 * Return plane info, including formats supported, gamma size, any
1618 * current fb, etc.
1619 */
1620int drm_mode_getplane(struct drm_device *dev, void *data,
1621 struct drm_file *file_priv)
1622{
1623 struct drm_mode_get_plane *plane_resp = data;
1624 struct drm_mode_object *obj;
1625 struct drm_plane *plane;
1626 uint32_t __user *format_ptr;
1627 int ret = 0;
1628
1629 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1630 return -EINVAL;
1631
1632 mutex_lock(&dev->mode_config.mutex);
1633 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1634 DRM_MODE_OBJECT_PLANE);
1635 if (!obj) {
1636 ret = -ENOENT;
1637 goto out;
1638 }
1639 plane = obj_to_plane(obj);
1640
1641 if (plane->crtc)
1642 plane_resp->crtc_id = plane->crtc->base.id;
1643 else
1644 plane_resp->crtc_id = 0;
1645
1646 if (plane->fb)
1647 plane_resp->fb_id = plane->fb->base.id;
1648 else
1649 plane_resp->fb_id = 0;
1650
1651 plane_resp->plane_id = plane->base.id;
1652 plane_resp->possible_crtcs = plane->possible_crtcs;
1653 plane_resp->gamma_size = plane->gamma_size;
1654
1655 /*
1656 * This ioctl is called twice, once to determine how much space is
1657 * needed, and the 2nd time to fill it.
1658 */
1659 if (plane->format_count &&
1660 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001661 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001662 if (copy_to_user(format_ptr,
1663 plane->format_types,
1664 sizeof(uint32_t) * plane->format_count)) {
1665 ret = -EFAULT;
1666 goto out;
1667 }
1668 }
1669 plane_resp->count_format_types = plane->format_count;
1670
1671out:
1672 mutex_unlock(&dev->mode_config.mutex);
1673 return ret;
1674}
1675
1676/**
1677 * drm_mode_setplane - set up or tear down an plane
1678 * @dev: DRM device
1679 * @data: ioctl data*
1680 * @file_prive: DRM file info
1681 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001682 * LOCKING:
1683 * Takes mode config lock.
1684 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001685 * Set plane info, including placement, fb, scaling, and other factors.
1686 * Or pass a NULL fb to disable.
1687 */
1688int drm_mode_setplane(struct drm_device *dev, void *data,
1689 struct drm_file *file_priv)
1690{
1691 struct drm_mode_set_plane *plane_req = data;
1692 struct drm_mode_object *obj;
1693 struct drm_plane *plane;
1694 struct drm_crtc *crtc;
1695 struct drm_framebuffer *fb;
1696 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001697 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001698 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001699
1700 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1701 return -EINVAL;
1702
1703 mutex_lock(&dev->mode_config.mutex);
1704
1705 /*
1706 * First, find the plane, crtc, and fb objects. If not available,
1707 * we don't bother to call the driver.
1708 */
1709 obj = drm_mode_object_find(dev, plane_req->plane_id,
1710 DRM_MODE_OBJECT_PLANE);
1711 if (!obj) {
1712 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1713 plane_req->plane_id);
1714 ret = -ENOENT;
1715 goto out;
1716 }
1717 plane = obj_to_plane(obj);
1718
1719 /* No fb means shut it down */
1720 if (!plane_req->fb_id) {
1721 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001722 plane->crtc = NULL;
1723 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001724 goto out;
1725 }
1726
1727 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1728 DRM_MODE_OBJECT_CRTC);
1729 if (!obj) {
1730 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1731 plane_req->crtc_id);
1732 ret = -ENOENT;
1733 goto out;
1734 }
1735 crtc = obj_to_crtc(obj);
1736
1737 obj = drm_mode_object_find(dev, plane_req->fb_id,
1738 DRM_MODE_OBJECT_FB);
1739 if (!obj) {
1740 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1741 plane_req->fb_id);
1742 ret = -ENOENT;
1743 goto out;
1744 }
1745 fb = obj_to_fb(obj);
1746
Ville Syrjälä62443be2011-12-20 00:06:47 +02001747 /* Check whether this plane supports the fb pixel format. */
1748 for (i = 0; i < plane->format_count; i++)
1749 if (fb->pixel_format == plane->format_types[i])
1750 break;
1751 if (i == plane->format_count) {
1752 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1753 ret = -EINVAL;
1754 goto out;
1755 }
1756
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001757 fb_width = fb->width << 16;
1758 fb_height = fb->height << 16;
1759
1760 /* Make sure source coordinates are inside the fb. */
1761 if (plane_req->src_w > fb_width ||
1762 plane_req->src_x > fb_width - plane_req->src_w ||
1763 plane_req->src_h > fb_height ||
1764 plane_req->src_y > fb_height - plane_req->src_h) {
1765 DRM_DEBUG_KMS("Invalid source coordinates "
1766 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1767 plane_req->src_w >> 16,
1768 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1769 plane_req->src_h >> 16,
1770 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1771 plane_req->src_x >> 16,
1772 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1773 plane_req->src_y >> 16,
1774 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1775 ret = -ENOSPC;
1776 goto out;
1777 }
1778
Ville Syrjälä687a0402011-12-20 00:06:45 +02001779 /* Give drivers some help against integer overflows */
1780 if (plane_req->crtc_w > INT_MAX ||
1781 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1782 plane_req->crtc_h > INT_MAX ||
1783 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1784 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1785 plane_req->crtc_w, plane_req->crtc_h,
1786 plane_req->crtc_x, plane_req->crtc_y);
1787 ret = -ERANGE;
1788 goto out;
1789 }
1790
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001791 ret = plane->funcs->update_plane(plane, crtc, fb,
1792 plane_req->crtc_x, plane_req->crtc_y,
1793 plane_req->crtc_w, plane_req->crtc_h,
1794 plane_req->src_x, plane_req->src_y,
1795 plane_req->src_w, plane_req->src_h);
1796 if (!ret) {
1797 plane->crtc = crtc;
1798 plane->fb = fb;
1799 }
1800
1801out:
1802 mutex_unlock(&dev->mode_config.mutex);
1803
1804 return ret;
1805}
1806
1807/**
Dave Airlief453ba02008-11-07 14:05:41 -08001808 * drm_mode_setcrtc - set CRTC configuration
1809 * @inode: inode from the ioctl
1810 * @filp: file * from the ioctl
1811 * @cmd: cmd from ioctl
1812 * @arg: arg from ioctl
1813 *
1814 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001815 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001816 *
1817 * Build a new CRTC configuration based on user request.
1818 *
1819 * Called by the user via ioctl.
1820 *
1821 * RETURNS:
1822 * Zero on success, errno on failure.
1823 */
1824int drm_mode_setcrtc(struct drm_device *dev, void *data,
1825 struct drm_file *file_priv)
1826{
1827 struct drm_mode_config *config = &dev->mode_config;
1828 struct drm_mode_crtc *crtc_req = data;
1829 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001830 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001831 struct drm_connector **connector_set = NULL, *connector;
1832 struct drm_framebuffer *fb = NULL;
1833 struct drm_display_mode *mode = NULL;
1834 struct drm_mode_set set;
1835 uint32_t __user *set_connectors_ptr;
1836 int ret = 0;
1837 int i;
1838
Dave Airliefb3b06c2011-02-08 13:55:21 +10001839 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1840 return -EINVAL;
1841
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001842 /* For some reason crtc x/y offsets are signed internally. */
1843 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1844 return -ERANGE;
1845
Dave Airlief453ba02008-11-07 14:05:41 -08001846 mutex_lock(&dev->mode_config.mutex);
1847 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1848 DRM_MODE_OBJECT_CRTC);
1849 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001850 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001851 ret = -EINVAL;
1852 goto out;
1853 }
1854 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001855 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001856
1857 if (crtc_req->mode_valid) {
1858 /* If we have a mode we need a framebuffer. */
1859 /* If we pass -1, set the mode with the currently bound fb */
1860 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001861 if (!crtc->fb) {
1862 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1863 ret = -EINVAL;
1864 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001865 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001866 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001867 } else {
1868 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1869 DRM_MODE_OBJECT_FB);
1870 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001871 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1872 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001873 ret = -EINVAL;
1874 goto out;
1875 }
1876 fb = obj_to_fb(obj);
1877 }
1878
1879 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001880 if (!mode) {
1881 ret = -ENOMEM;
1882 goto out;
1883 }
1884
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001885 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1886 if (ret) {
1887 DRM_DEBUG_KMS("Invalid mode\n");
1888 goto out;
1889 }
1890
Dave Airlief453ba02008-11-07 14:05:41 -08001891 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001892
1893 if (mode->hdisplay > fb->width ||
1894 mode->vdisplay > fb->height ||
1895 crtc_req->x > fb->width - mode->hdisplay ||
1896 crtc_req->y > fb->height - mode->vdisplay) {
1897 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1898 mode->hdisplay, mode->vdisplay,
1899 crtc_req->x, crtc_req->y,
1900 fb->width, fb->height);
1901 ret = -ENOSPC;
1902 goto out;
1903 }
Dave Airlief453ba02008-11-07 14:05:41 -08001904 }
1905
1906 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001907 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001908 ret = -EINVAL;
1909 goto out;
1910 }
1911
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001912 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001913 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001914 crtc_req->count_connectors);
1915 ret = -EINVAL;
1916 goto out;
1917 }
1918
1919 if (crtc_req->count_connectors > 0) {
1920 u32 out_id;
1921
1922 /* Avoid unbounded kernel memory allocation */
1923 if (crtc_req->count_connectors > config->num_connector) {
1924 ret = -EINVAL;
1925 goto out;
1926 }
1927
1928 connector_set = kmalloc(crtc_req->count_connectors *
1929 sizeof(struct drm_connector *),
1930 GFP_KERNEL);
1931 if (!connector_set) {
1932 ret = -ENOMEM;
1933 goto out;
1934 }
1935
1936 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001937 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001938 if (get_user(out_id, &set_connectors_ptr[i])) {
1939 ret = -EFAULT;
1940 goto out;
1941 }
1942
1943 obj = drm_mode_object_find(dev, out_id,
1944 DRM_MODE_OBJECT_CONNECTOR);
1945 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001946 DRM_DEBUG_KMS("Connector id %d unknown\n",
1947 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001948 ret = -EINVAL;
1949 goto out;
1950 }
1951 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001952 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1953 connector->base.id,
1954 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001955
1956 connector_set[i] = connector;
1957 }
1958 }
1959
1960 set.crtc = crtc;
1961 set.x = crtc_req->x;
1962 set.y = crtc_req->y;
1963 set.mode = mode;
1964 set.connectors = connector_set;
1965 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001966 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001967 ret = crtc->funcs->set_config(&set);
1968
1969out:
1970 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001971 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001972 mutex_unlock(&dev->mode_config.mutex);
1973 return ret;
1974}
1975
1976int drm_mode_cursor_ioctl(struct drm_device *dev,
1977 void *data, struct drm_file *file_priv)
1978{
1979 struct drm_mode_cursor *req = data;
1980 struct drm_mode_object *obj;
1981 struct drm_crtc *crtc;
1982 int ret = 0;
1983
Dave Airliefb3b06c2011-02-08 13:55:21 +10001984 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1985 return -EINVAL;
1986
Jesse Barnesacb4b992011-11-07 12:03:23 -08001987 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001988 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001989
1990 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001991 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001992 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001993 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001994 ret = -EINVAL;
1995 goto out;
1996 }
1997 crtc = obj_to_crtc(obj);
1998
1999 if (req->flags & DRM_MODE_CURSOR_BO) {
2000 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002001 ret = -ENXIO;
2002 goto out;
2003 }
2004 /* Turns off the cursor if handle is 0 */
2005 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2006 req->width, req->height);
2007 }
2008
2009 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2010 if (crtc->funcs->cursor_move) {
2011 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2012 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002013 ret = -EFAULT;
2014 goto out;
2015 }
2016 }
2017out:
2018 mutex_unlock(&dev->mode_config.mutex);
2019 return ret;
2020}
2021
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002022/* Original addfb only supported RGB formats, so figure out which one */
2023uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2024{
2025 uint32_t fmt;
2026
2027 switch (bpp) {
2028 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002029 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002030 break;
2031 case 16:
2032 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002033 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002034 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002035 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002036 break;
2037 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002038 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002039 break;
2040 case 32:
2041 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002042 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002043 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002044 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002045 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002046 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002047 break;
2048 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002049 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2050 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002051 break;
2052 }
2053
2054 return fmt;
2055}
2056EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2057
Dave Airlief453ba02008-11-07 14:05:41 -08002058/**
2059 * drm_mode_addfb - add an FB to the graphics configuration
2060 * @inode: inode from the ioctl
2061 * @filp: file * from the ioctl
2062 * @cmd: cmd from ioctl
2063 * @arg: arg from ioctl
2064 *
2065 * LOCKING:
2066 * Takes mode config lock.
2067 *
2068 * Add a new FB to the specified CRTC, given a user request.
2069 *
2070 * Called by the user via ioctl.
2071 *
2072 * RETURNS:
2073 * Zero on success, errno on failure.
2074 */
2075int drm_mode_addfb(struct drm_device *dev,
2076 void *data, struct drm_file *file_priv)
2077{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002078 struct drm_mode_fb_cmd *or = data;
2079 struct drm_mode_fb_cmd2 r = {};
2080 struct drm_mode_config *config = &dev->mode_config;
2081 struct drm_framebuffer *fb;
2082 int ret = 0;
2083
2084 /* Use new struct with format internally */
2085 r.fb_id = or->fb_id;
2086 r.width = or->width;
2087 r.height = or->height;
2088 r.pitches[0] = or->pitch;
2089 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2090 r.handles[0] = or->handle;
2091
2092 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2093 return -EINVAL;
2094
Jesse Barnesacb4b992011-11-07 12:03:23 -08002095 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002096 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002097
2098 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002099 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002100
2101 mutex_lock(&dev->mode_config.mutex);
2102
2103 /* TODO check buffer is sufficiently large */
2104 /* TODO setup destructor callback */
2105
2106 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2107 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002108 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002109 ret = PTR_ERR(fb);
2110 goto out;
2111 }
2112
2113 or->fb_id = fb->base.id;
2114 list_add(&fb->filp_head, &file_priv->fbs);
2115 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2116
2117out:
2118 mutex_unlock(&dev->mode_config.mutex);
2119 return ret;
2120}
2121
Ville Syrjälä935b5972011-12-20 00:06:48 +02002122static int format_check(struct drm_mode_fb_cmd2 *r)
2123{
2124 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2125
2126 switch (format) {
2127 case DRM_FORMAT_C8:
2128 case DRM_FORMAT_RGB332:
2129 case DRM_FORMAT_BGR233:
2130 case DRM_FORMAT_XRGB4444:
2131 case DRM_FORMAT_XBGR4444:
2132 case DRM_FORMAT_RGBX4444:
2133 case DRM_FORMAT_BGRX4444:
2134 case DRM_FORMAT_ARGB4444:
2135 case DRM_FORMAT_ABGR4444:
2136 case DRM_FORMAT_RGBA4444:
2137 case DRM_FORMAT_BGRA4444:
2138 case DRM_FORMAT_XRGB1555:
2139 case DRM_FORMAT_XBGR1555:
2140 case DRM_FORMAT_RGBX5551:
2141 case DRM_FORMAT_BGRX5551:
2142 case DRM_FORMAT_ARGB1555:
2143 case DRM_FORMAT_ABGR1555:
2144 case DRM_FORMAT_RGBA5551:
2145 case DRM_FORMAT_BGRA5551:
2146 case DRM_FORMAT_RGB565:
2147 case DRM_FORMAT_BGR565:
2148 case DRM_FORMAT_RGB888:
2149 case DRM_FORMAT_BGR888:
2150 case DRM_FORMAT_XRGB8888:
2151 case DRM_FORMAT_XBGR8888:
2152 case DRM_FORMAT_RGBX8888:
2153 case DRM_FORMAT_BGRX8888:
2154 case DRM_FORMAT_ARGB8888:
2155 case DRM_FORMAT_ABGR8888:
2156 case DRM_FORMAT_RGBA8888:
2157 case DRM_FORMAT_BGRA8888:
2158 case DRM_FORMAT_XRGB2101010:
2159 case DRM_FORMAT_XBGR2101010:
2160 case DRM_FORMAT_RGBX1010102:
2161 case DRM_FORMAT_BGRX1010102:
2162 case DRM_FORMAT_ARGB2101010:
2163 case DRM_FORMAT_ABGR2101010:
2164 case DRM_FORMAT_RGBA1010102:
2165 case DRM_FORMAT_BGRA1010102:
2166 case DRM_FORMAT_YUYV:
2167 case DRM_FORMAT_YVYU:
2168 case DRM_FORMAT_UYVY:
2169 case DRM_FORMAT_VYUY:
2170 case DRM_FORMAT_AYUV:
2171 case DRM_FORMAT_NV12:
2172 case DRM_FORMAT_NV21:
2173 case DRM_FORMAT_NV16:
2174 case DRM_FORMAT_NV61:
2175 case DRM_FORMAT_YUV410:
2176 case DRM_FORMAT_YVU410:
2177 case DRM_FORMAT_YUV411:
2178 case DRM_FORMAT_YVU411:
2179 case DRM_FORMAT_YUV420:
2180 case DRM_FORMAT_YVU420:
2181 case DRM_FORMAT_YUV422:
2182 case DRM_FORMAT_YVU422:
2183 case DRM_FORMAT_YUV444:
2184 case DRM_FORMAT_YVU444:
2185 return 0;
2186 default:
2187 return -EINVAL;
2188 }
2189}
2190
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002191static int framebuffer_check(struct drm_mode_fb_cmd2 *r)
2192{
2193 int ret, hsub, vsub, num_planes, i;
2194
2195 ret = format_check(r);
2196 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002197 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002198 return ret;
2199 }
2200
2201 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2202 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2203 num_planes = drm_format_num_planes(r->pixel_format);
2204
2205 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002206 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002207 return -EINVAL;
2208 }
2209
2210 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002211 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002212 return -EINVAL;
2213 }
2214
2215 for (i = 0; i < num_planes; i++) {
2216 unsigned int width = r->width / (i != 0 ? hsub : 1);
2217
2218 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002219 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002220 return -EINVAL;
2221 }
2222
2223 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002224 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002225 return -EINVAL;
2226 }
2227 }
2228
2229 return 0;
2230}
2231
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002232/**
2233 * drm_mode_addfb2 - add an FB to the graphics configuration
2234 * @inode: inode from the ioctl
2235 * @filp: file * from the ioctl
2236 * @cmd: cmd from ioctl
2237 * @arg: arg from ioctl
2238 *
2239 * LOCKING:
2240 * Takes mode config lock.
2241 *
2242 * Add a new FB to the specified CRTC, given a user request with format.
2243 *
2244 * Called by the user via ioctl.
2245 *
2246 * RETURNS:
2247 * Zero on success, errno on failure.
2248 */
2249int drm_mode_addfb2(struct drm_device *dev,
2250 void *data, struct drm_file *file_priv)
2251{
2252 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002253 struct drm_mode_config *config = &dev->mode_config;
2254 struct drm_framebuffer *fb;
2255 int ret = 0;
2256
Dave Airliefb3b06c2011-02-08 13:55:21 +10002257 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2258 return -EINVAL;
2259
Dave Airlief453ba02008-11-07 14:05:41 -08002260 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002261 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002262 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002263 return -EINVAL;
2264 }
2265 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002266 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002267 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002268 return -EINVAL;
2269 }
2270
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002271 ret = framebuffer_check(r);
2272 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002273 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002274
Dave Airlief453ba02008-11-07 14:05:41 -08002275 mutex_lock(&dev->mode_config.mutex);
2276
Dave Airlief453ba02008-11-07 14:05:41 -08002277 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002278 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002279 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002280 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002281 goto out;
2282 }
2283
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002284 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002285 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002286 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002287
2288out:
2289 mutex_unlock(&dev->mode_config.mutex);
2290 return ret;
2291}
2292
2293/**
2294 * drm_mode_rmfb - remove an FB from the configuration
2295 * @inode: inode from the ioctl
2296 * @filp: file * from the ioctl
2297 * @cmd: cmd from ioctl
2298 * @arg: arg from ioctl
2299 *
2300 * LOCKING:
2301 * Takes mode config lock.
2302 *
2303 * Remove the FB specified by the user.
2304 *
2305 * Called by the user via ioctl.
2306 *
2307 * RETURNS:
2308 * Zero on success, errno on failure.
2309 */
2310int drm_mode_rmfb(struct drm_device *dev,
2311 void *data, struct drm_file *file_priv)
2312{
2313 struct drm_mode_object *obj;
2314 struct drm_framebuffer *fb = NULL;
2315 struct drm_framebuffer *fbl = NULL;
2316 uint32_t *id = data;
2317 int ret = 0;
2318 int found = 0;
2319
Dave Airliefb3b06c2011-02-08 13:55:21 +10002320 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2321 return -EINVAL;
2322
Dave Airlief453ba02008-11-07 14:05:41 -08002323 mutex_lock(&dev->mode_config.mutex);
2324 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002325 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002326 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002327 ret = -EINVAL;
2328 goto out;
2329 }
2330 fb = obj_to_fb(obj);
2331
2332 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2333 if (fb == fbl)
2334 found = 1;
2335
2336 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002337 ret = -EINVAL;
2338 goto out;
2339 }
2340
2341 /* TODO release all crtc connected to the framebuffer */
2342 /* TODO unhock the destructor from the buffer object */
2343
2344 list_del(&fb->filp_head);
2345 fb->funcs->destroy(fb);
2346
2347out:
2348 mutex_unlock(&dev->mode_config.mutex);
2349 return ret;
2350}
2351
2352/**
2353 * drm_mode_getfb - get FB info
2354 * @inode: inode from the ioctl
2355 * @filp: file * from the ioctl
2356 * @cmd: cmd from ioctl
2357 * @arg: arg from ioctl
2358 *
2359 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002360 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002361 *
2362 * Lookup the FB given its ID and return info about it.
2363 *
2364 * Called by the user via ioctl.
2365 *
2366 * RETURNS:
2367 * Zero on success, errno on failure.
2368 */
2369int drm_mode_getfb(struct drm_device *dev,
2370 void *data, struct drm_file *file_priv)
2371{
2372 struct drm_mode_fb_cmd *r = data;
2373 struct drm_mode_object *obj;
2374 struct drm_framebuffer *fb;
2375 int ret = 0;
2376
Dave Airliefb3b06c2011-02-08 13:55:21 +10002377 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2378 return -EINVAL;
2379
Dave Airlief453ba02008-11-07 14:05:41 -08002380 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002381 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002382 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002383 ret = -EINVAL;
2384 goto out;
2385 }
2386 fb = obj_to_fb(obj);
2387
2388 r->height = fb->height;
2389 r->width = fb->width;
2390 r->depth = fb->depth;
2391 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002392 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002393 fb->funcs->create_handle(fb, file_priv, &r->handle);
2394
2395out:
2396 mutex_unlock(&dev->mode_config.mutex);
2397 return ret;
2398}
2399
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002400int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2401 void *data, struct drm_file *file_priv)
2402{
2403 struct drm_clip_rect __user *clips_ptr;
2404 struct drm_clip_rect *clips = NULL;
2405 struct drm_mode_fb_dirty_cmd *r = data;
2406 struct drm_mode_object *obj;
2407 struct drm_framebuffer *fb;
2408 unsigned flags;
2409 int num_clips;
2410 int ret = 0;
2411
Dave Airliefb3b06c2011-02-08 13:55:21 +10002412 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2413 return -EINVAL;
2414
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002415 mutex_lock(&dev->mode_config.mutex);
2416 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2417 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002418 ret = -EINVAL;
2419 goto out_err1;
2420 }
2421 fb = obj_to_fb(obj);
2422
2423 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002424 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002425
2426 if (!num_clips != !clips_ptr) {
2427 ret = -EINVAL;
2428 goto out_err1;
2429 }
2430
2431 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2432
2433 /* If userspace annotates copy, clips must come in pairs */
2434 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2435 ret = -EINVAL;
2436 goto out_err1;
2437 }
2438
2439 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002440 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2441 ret = -EINVAL;
2442 goto out_err1;
2443 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002444 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2445 if (!clips) {
2446 ret = -ENOMEM;
2447 goto out_err1;
2448 }
2449
2450 ret = copy_from_user(clips, clips_ptr,
2451 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002452 if (ret) {
2453 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002454 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002455 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002456 }
2457
2458 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002459 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2460 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002461 } else {
2462 ret = -ENOSYS;
2463 goto out_err2;
2464 }
2465
2466out_err2:
2467 kfree(clips);
2468out_err1:
2469 mutex_unlock(&dev->mode_config.mutex);
2470 return ret;
2471}
2472
2473
Dave Airlief453ba02008-11-07 14:05:41 -08002474/**
2475 * drm_fb_release - remove and free the FBs on this file
2476 * @filp: file * from the ioctl
2477 *
2478 * LOCKING:
2479 * Takes mode config lock.
2480 *
2481 * Destroy all the FBs associated with @filp.
2482 *
2483 * Called by the user via ioctl.
2484 *
2485 * RETURNS:
2486 * Zero on success, errno on failure.
2487 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002488void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002489{
Dave Airlief453ba02008-11-07 14:05:41 -08002490 struct drm_device *dev = priv->minor->dev;
2491 struct drm_framebuffer *fb, *tfb;
2492
2493 mutex_lock(&dev->mode_config.mutex);
2494 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2495 list_del(&fb->filp_head);
2496 fb->funcs->destroy(fb);
2497 }
2498 mutex_unlock(&dev->mode_config.mutex);
2499}
2500
2501/**
2502 * drm_mode_attachmode - add a mode to the user mode list
2503 * @dev: DRM device
2504 * @connector: connector to add the mode to
2505 * @mode: mode to add
2506 *
2507 * Add @mode to @connector's user mode list.
2508 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002509static void drm_mode_attachmode(struct drm_device *dev,
2510 struct drm_connector *connector,
2511 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002512{
Dave Airlief453ba02008-11-07 14:05:41 -08002513 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002514}
2515
2516int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002517 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002518{
2519 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002520 int ret = 0;
2521 struct drm_display_mode *dup_mode, *next;
2522 LIST_HEAD(list);
2523
Dave Airlief453ba02008-11-07 14:05:41 -08002524 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2525 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002526 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002527 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002528 dup_mode = drm_mode_duplicate(dev, mode);
2529 if (!dup_mode) {
2530 ret = -ENOMEM;
2531 goto out;
2532 }
2533 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002534 }
2535 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002536
2537 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2538 if (!connector->encoder)
2539 continue;
2540 if (connector->encoder->crtc == crtc)
2541 list_move_tail(list.next, &connector->user_modes);
2542 }
2543
2544 WARN_ON(!list_empty(&list));
2545
2546 out:
2547 list_for_each_entry_safe(dup_mode, next, &list, head)
2548 drm_mode_destroy(dev, dup_mode);
2549
2550 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002551}
2552EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2553
2554static int drm_mode_detachmode(struct drm_device *dev,
2555 struct drm_connector *connector,
2556 struct drm_display_mode *mode)
2557{
2558 int found = 0;
2559 int ret = 0;
2560 struct drm_display_mode *match_mode, *t;
2561
2562 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2563 if (drm_mode_equal(match_mode, mode)) {
2564 list_del(&match_mode->head);
2565 drm_mode_destroy(dev, match_mode);
2566 found = 1;
2567 break;
2568 }
2569 }
2570
2571 if (!found)
2572 ret = -EINVAL;
2573
2574 return ret;
2575}
2576
2577int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2578{
2579 struct drm_connector *connector;
2580
2581 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2582 drm_mode_detachmode(dev, connector, mode);
2583 }
2584 return 0;
2585}
2586EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2587
2588/**
2589 * drm_fb_attachmode - Attach a user mode to an connector
2590 * @inode: inode from the ioctl
2591 * @filp: file * from the ioctl
2592 * @cmd: cmd from ioctl
2593 * @arg: arg from ioctl
2594 *
2595 * This attaches a user specified mode to an connector.
2596 * Called by the user via ioctl.
2597 *
2598 * RETURNS:
2599 * Zero on success, errno on failure.
2600 */
2601int drm_mode_attachmode_ioctl(struct drm_device *dev,
2602 void *data, struct drm_file *file_priv)
2603{
2604 struct drm_mode_mode_cmd *mode_cmd = data;
2605 struct drm_connector *connector;
2606 struct drm_display_mode *mode;
2607 struct drm_mode_object *obj;
2608 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2609 int ret = 0;
2610
Dave Airliefb3b06c2011-02-08 13:55:21 +10002611 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2612 return -EINVAL;
2613
Dave Airlief453ba02008-11-07 14:05:41 -08002614 mutex_lock(&dev->mode_config.mutex);
2615
2616 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2617 if (!obj) {
2618 ret = -EINVAL;
2619 goto out;
2620 }
2621 connector = obj_to_connector(obj);
2622
2623 mode = drm_mode_create(dev);
2624 if (!mode) {
2625 ret = -ENOMEM;
2626 goto out;
2627 }
2628
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002629 ret = drm_crtc_convert_umode(mode, umode);
2630 if (ret) {
2631 DRM_DEBUG_KMS("Invalid mode\n");
2632 drm_mode_destroy(dev, mode);
2633 goto out;
2634 }
Dave Airlief453ba02008-11-07 14:05:41 -08002635
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002636 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002637out:
2638 mutex_unlock(&dev->mode_config.mutex);
2639 return ret;
2640}
2641
2642
2643/**
2644 * drm_fb_detachmode - Detach a user specified mode from an connector
2645 * @inode: inode from the ioctl
2646 * @filp: file * from the ioctl
2647 * @cmd: cmd from ioctl
2648 * @arg: arg from ioctl
2649 *
2650 * Called by the user via ioctl.
2651 *
2652 * RETURNS:
2653 * Zero on success, errno on failure.
2654 */
2655int drm_mode_detachmode_ioctl(struct drm_device *dev,
2656 void *data, struct drm_file *file_priv)
2657{
2658 struct drm_mode_object *obj;
2659 struct drm_mode_mode_cmd *mode_cmd = data;
2660 struct drm_connector *connector;
2661 struct drm_display_mode mode;
2662 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2663 int ret = 0;
2664
Dave Airliefb3b06c2011-02-08 13:55:21 +10002665 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2666 return -EINVAL;
2667
Dave Airlief453ba02008-11-07 14:05:41 -08002668 mutex_lock(&dev->mode_config.mutex);
2669
2670 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2671 if (!obj) {
2672 ret = -EINVAL;
2673 goto out;
2674 }
2675 connector = obj_to_connector(obj);
2676
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002677 ret = drm_crtc_convert_umode(&mode, umode);
2678 if (ret) {
2679 DRM_DEBUG_KMS("Invalid mode\n");
2680 goto out;
2681 }
2682
Dave Airlief453ba02008-11-07 14:05:41 -08002683 ret = drm_mode_detachmode(dev, connector, &mode);
2684out:
2685 mutex_unlock(&dev->mode_config.mutex);
2686 return ret;
2687}
2688
2689struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2690 const char *name, int num_values)
2691{
2692 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002693 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002694
2695 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2696 if (!property)
2697 return NULL;
2698
2699 if (num_values) {
2700 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2701 if (!property->values)
2702 goto fail;
2703 }
2704
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002705 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2706 if (ret)
2707 goto fail;
2708
Dave Airlief453ba02008-11-07 14:05:41 -08002709 property->flags = flags;
2710 property->num_values = num_values;
2711 INIT_LIST_HEAD(&property->enum_blob_list);
2712
Vinson Lee471dd2e2011-11-10 11:55:40 -08002713 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002714 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002715 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2716 }
Dave Airlief453ba02008-11-07 14:05:41 -08002717
2718 list_add_tail(&property->head, &dev->mode_config.property_list);
2719 return property;
2720fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002721 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002722 kfree(property);
2723 return NULL;
2724}
2725EXPORT_SYMBOL(drm_property_create);
2726
Sascha Hauer4a67d392012-02-06 10:58:17 +01002727struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2728 const char *name,
2729 const struct drm_prop_enum_list *props,
2730 int num_values)
2731{
2732 struct drm_property *property;
2733 int i, ret;
2734
2735 flags |= DRM_MODE_PROP_ENUM;
2736
2737 property = drm_property_create(dev, flags, name, num_values);
2738 if (!property)
2739 return NULL;
2740
2741 for (i = 0; i < num_values; i++) {
2742 ret = drm_property_add_enum(property, i,
2743 props[i].type,
2744 props[i].name);
2745 if (ret) {
2746 drm_property_destroy(dev, property);
2747 return NULL;
2748 }
2749 }
2750
2751 return property;
2752}
2753EXPORT_SYMBOL(drm_property_create_enum);
2754
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002755struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2756 const char *name,
2757 uint64_t min, uint64_t max)
2758{
2759 struct drm_property *property;
2760
2761 flags |= DRM_MODE_PROP_RANGE;
2762
2763 property = drm_property_create(dev, flags, name, 2);
2764 if (!property)
2765 return NULL;
2766
2767 property->values[0] = min;
2768 property->values[1] = max;
2769
2770 return property;
2771}
2772EXPORT_SYMBOL(drm_property_create_range);
2773
Dave Airlief453ba02008-11-07 14:05:41 -08002774int drm_property_add_enum(struct drm_property *property, int index,
2775 uint64_t value, const char *name)
2776{
2777 struct drm_property_enum *prop_enum;
2778
2779 if (!(property->flags & DRM_MODE_PROP_ENUM))
2780 return -EINVAL;
2781
2782 if (!list_empty(&property->enum_blob_list)) {
2783 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2784 if (prop_enum->value == value) {
2785 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2786 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2787 return 0;
2788 }
2789 }
2790 }
2791
2792 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2793 if (!prop_enum)
2794 return -ENOMEM;
2795
2796 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2797 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2798 prop_enum->value = value;
2799
2800 property->values[index] = value;
2801 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2802 return 0;
2803}
2804EXPORT_SYMBOL(drm_property_add_enum);
2805
2806void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2807{
2808 struct drm_property_enum *prop_enum, *pt;
2809
2810 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2811 list_del(&prop_enum->head);
2812 kfree(prop_enum);
2813 }
2814
2815 if (property->num_values)
2816 kfree(property->values);
2817 drm_mode_object_put(dev, &property->base);
2818 list_del(&property->head);
2819 kfree(property);
2820}
2821EXPORT_SYMBOL(drm_property_destroy);
2822
Paulo Zanoniafea2ad2012-05-15 18:09:00 -03002823void drm_connector_attach_property(struct drm_connector *connector,
Dave Airlief453ba02008-11-07 14:05:41 -08002824 struct drm_property *property, uint64_t init_val)
2825{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002826 drm_object_attach_property(&connector->base, property, init_val);
Dave Airlief453ba02008-11-07 14:05:41 -08002827}
2828EXPORT_SYMBOL(drm_connector_attach_property);
2829
2830int drm_connector_property_set_value(struct drm_connector *connector,
2831 struct drm_property *property, uint64_t value)
2832{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002833 return drm_object_property_set_value(&connector->base, property, value);
Dave Airlief453ba02008-11-07 14:05:41 -08002834}
2835EXPORT_SYMBOL(drm_connector_property_set_value);
2836
2837int drm_connector_property_get_value(struct drm_connector *connector,
2838 struct drm_property *property, uint64_t *val)
2839{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002840 return drm_object_property_get_value(&connector->base, property, val);
Dave Airlief453ba02008-11-07 14:05:41 -08002841}
2842EXPORT_SYMBOL(drm_connector_property_get_value);
2843
Paulo Zanonic5431882012-05-15 18:09:02 -03002844void drm_object_attach_property(struct drm_mode_object *obj,
2845 struct drm_property *property,
2846 uint64_t init_val)
2847{
2848 int i;
2849
2850 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++) {
2851 if (obj->properties->ids[i] == 0) {
2852 obj->properties->ids[i] = property->base.id;
2853 obj->properties->values[i] = init_val;
2854 return;
2855 }
2856 }
2857
2858 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2859 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time you see "
2860 "this message on the same object type.\n", obj->type);
2861}
2862EXPORT_SYMBOL(drm_object_attach_property);
2863
2864int drm_object_property_set_value(struct drm_mode_object *obj,
2865 struct drm_property *property, uint64_t val)
2866{
2867 int i;
2868
2869 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++) {
2870 if (obj->properties->ids[i] == property->base.id) {
2871 obj->properties->values[i] = val;
2872 return 0;
2873 }
2874 }
2875
2876 return -EINVAL;
2877}
2878EXPORT_SYMBOL(drm_object_property_set_value);
2879
2880int drm_object_property_get_value(struct drm_mode_object *obj,
2881 struct drm_property *property, uint64_t *val)
2882{
2883 int i;
2884
2885 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++) {
2886 if (obj->properties->ids[i] == property->base.id) {
2887 *val = obj->properties->values[i];
2888 return 0;
2889 }
2890 }
2891
2892 return -EINVAL;
2893}
2894EXPORT_SYMBOL(drm_object_property_get_value);
2895
Dave Airlief453ba02008-11-07 14:05:41 -08002896int drm_mode_getproperty_ioctl(struct drm_device *dev,
2897 void *data, struct drm_file *file_priv)
2898{
2899 struct drm_mode_object *obj;
2900 struct drm_mode_get_property *out_resp = data;
2901 struct drm_property *property;
2902 int enum_count = 0;
2903 int blob_count = 0;
2904 int value_count = 0;
2905 int ret = 0, i;
2906 int copied;
2907 struct drm_property_enum *prop_enum;
2908 struct drm_mode_property_enum __user *enum_ptr;
2909 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002910 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002911 uint64_t __user *values_ptr;
2912 uint32_t __user *blob_length_ptr;
2913
Dave Airliefb3b06c2011-02-08 13:55:21 +10002914 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2915 return -EINVAL;
2916
Dave Airlief453ba02008-11-07 14:05:41 -08002917 mutex_lock(&dev->mode_config.mutex);
2918 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2919 if (!obj) {
2920 ret = -EINVAL;
2921 goto done;
2922 }
2923 property = obj_to_property(obj);
2924
2925 if (property->flags & DRM_MODE_PROP_ENUM) {
2926 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2927 enum_count++;
2928 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2929 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2930 blob_count++;
2931 }
2932
2933 value_count = property->num_values;
2934
2935 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2936 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2937 out_resp->flags = property->flags;
2938
2939 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002940 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002941 for (i = 0; i < value_count; i++) {
2942 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2943 ret = -EFAULT;
2944 goto done;
2945 }
2946 }
2947 }
2948 out_resp->count_values = value_count;
2949
2950 if (property->flags & DRM_MODE_PROP_ENUM) {
2951 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2952 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002953 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002954 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2955
2956 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2957 ret = -EFAULT;
2958 goto done;
2959 }
2960
2961 if (copy_to_user(&enum_ptr[copied].name,
2962 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2963 ret = -EFAULT;
2964 goto done;
2965 }
2966 copied++;
2967 }
2968 }
2969 out_resp->count_enum_blobs = enum_count;
2970 }
2971
2972 if (property->flags & DRM_MODE_PROP_BLOB) {
2973 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2974 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002975 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2976 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002977
2978 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2979 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2980 ret = -EFAULT;
2981 goto done;
2982 }
2983
2984 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2985 ret = -EFAULT;
2986 goto done;
2987 }
2988
2989 copied++;
2990 }
2991 }
2992 out_resp->count_enum_blobs = blob_count;
2993 }
2994done:
2995 mutex_unlock(&dev->mode_config.mutex);
2996 return ret;
2997}
2998
2999static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3000 void *data)
3001{
3002 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003003 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003004
3005 if (!length || !data)
3006 return NULL;
3007
3008 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3009 if (!blob)
3010 return NULL;
3011
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003012 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3013 if (ret) {
3014 kfree(blob);
3015 return NULL;
3016 }
3017
Dave Airlief453ba02008-11-07 14:05:41 -08003018 blob->length = length;
3019
3020 memcpy(blob->data, data, length);
3021
Dave Airlief453ba02008-11-07 14:05:41 -08003022 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3023 return blob;
3024}
3025
3026static void drm_property_destroy_blob(struct drm_device *dev,
3027 struct drm_property_blob *blob)
3028{
3029 drm_mode_object_put(dev, &blob->base);
3030 list_del(&blob->head);
3031 kfree(blob);
3032}
3033
3034int drm_mode_getblob_ioctl(struct drm_device *dev,
3035 void *data, struct drm_file *file_priv)
3036{
3037 struct drm_mode_object *obj;
3038 struct drm_mode_get_blob *out_resp = data;
3039 struct drm_property_blob *blob;
3040 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003041 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003042
Dave Airliefb3b06c2011-02-08 13:55:21 +10003043 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3044 return -EINVAL;
3045
Dave Airlief453ba02008-11-07 14:05:41 -08003046 mutex_lock(&dev->mode_config.mutex);
3047 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3048 if (!obj) {
3049 ret = -EINVAL;
3050 goto done;
3051 }
3052 blob = obj_to_blob(obj);
3053
3054 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003055 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003056 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3057 ret = -EFAULT;
3058 goto done;
3059 }
3060 }
3061 out_resp->length = blob->length;
3062
3063done:
3064 mutex_unlock(&dev->mode_config.mutex);
3065 return ret;
3066}
3067
3068int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3069 struct edid *edid)
3070{
3071 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00003072 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003073
3074 if (connector->edid_blob_ptr)
3075 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3076
3077 /* Delete edid, when there is none. */
3078 if (!edid) {
3079 connector->edid_blob_ptr = NULL;
3080 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3081 return ret;
3082 }
3083
Adam Jackson7466f4c2010-03-29 21:43:23 +00003084 size = EDID_LENGTH * (1 + edid->extensions);
3085 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3086 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003087
3088 ret = drm_connector_property_set_value(connector,
3089 dev->mode_config.edid_property,
3090 connector->edid_blob_ptr->base.id);
3091
3092 return ret;
3093}
3094EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3095
Paulo Zanoni26a34812012-05-15 18:08:59 -03003096static bool drm_property_change_is_valid(struct drm_property *property,
3097 __u64 value)
3098{
3099 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3100 return false;
3101 if (property->flags & DRM_MODE_PROP_RANGE) {
3102 if (value < property->values[0] || value > property->values[1])
3103 return false;
3104 return true;
3105 } else {
3106 int i;
3107 for (i = 0; i < property->num_values; i++)
3108 if (property->values[i] == value)
3109 return true;
3110 return false;
3111 }
3112}
3113
Dave Airlief453ba02008-11-07 14:05:41 -08003114int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3115 void *data, struct drm_file *file_priv)
3116{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003117 struct drm_mode_connector_set_property *conn_set_prop = data;
3118 struct drm_mode_obj_set_property obj_set_prop = {
3119 .value = conn_set_prop->value,
3120 .prop_id = conn_set_prop->prop_id,
3121 .obj_id = conn_set_prop->connector_id,
3122 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3123 };
Dave Airlief453ba02008-11-07 14:05:41 -08003124
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003125 /* It does all the locking and checking we need */
3126 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003127}
3128
Paulo Zanonic5431882012-05-15 18:09:02 -03003129static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3130 struct drm_property *property,
3131 uint64_t value)
3132{
3133 int ret = -EINVAL;
3134 struct drm_connector *connector = obj_to_connector(obj);
3135
3136 /* Do DPMS ourselves */
3137 if (property == connector->dev->mode_config.dpms_property) {
3138 if (connector->funcs->dpms)
3139 (*connector->funcs->dpms)(connector, (int)value);
3140 ret = 0;
3141 } else if (connector->funcs->set_property)
3142 ret = connector->funcs->set_property(connector, property, value);
3143
3144 /* store the property value if successful */
3145 if (!ret)
3146 drm_connector_property_set_value(connector, property, value);
3147 return ret;
3148}
3149
3150int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3151 struct drm_file *file_priv)
3152{
3153 struct drm_mode_obj_get_properties *arg = data;
3154 struct drm_mode_object *obj;
3155 int ret = 0;
3156 int i;
3157 int copied = 0;
3158 int props_count = 0;
3159 uint32_t __user *props_ptr;
3160 uint64_t __user *prop_values_ptr;
3161
3162 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3163 return -EINVAL;
3164
3165 mutex_lock(&dev->mode_config.mutex);
3166
3167 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3168 if (!obj) {
3169 ret = -EINVAL;
3170 goto out;
3171 }
3172 if (!obj->properties) {
3173 ret = -EINVAL;
3174 goto out;
3175 }
3176
3177 /* Assume [ prop, 0, prop ] won't happen (if we ever delete properties,
3178 * we need to remove the gap inside the array). */
3179 for (props_count = 0; props_count < DRM_OBJECT_MAX_PROPERTY &&
3180 obj->properties->ids[props_count] != 0; props_count++)
3181 ;
3182
3183 /* This ioctl is called twice, once to determine how much space is
3184 * needed, and the 2nd time to fill it. */
3185 if ((arg->count_props >= props_count) && props_count) {
3186 copied = 0;
3187 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3188 prop_values_ptr = (uint64_t __user *)(unsigned long)
3189 (arg->prop_values_ptr);
3190 for (i = 0; i < props_count; i++) {
3191 if (put_user(obj->properties->ids[i],
3192 props_ptr + copied)) {
3193 ret = -EFAULT;
3194 goto out;
3195 }
3196 if (put_user(obj->properties->values[i],
3197 prop_values_ptr + copied)) {
3198 ret = -EFAULT;
3199 goto out;
3200 }
3201 copied++;
3202 }
3203 }
3204 arg->count_props = props_count;
3205out:
3206 mutex_unlock(&dev->mode_config.mutex);
3207 return ret;
3208}
3209
3210int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3211 struct drm_file *file_priv)
3212{
3213 struct drm_mode_obj_set_property *arg = data;
3214 struct drm_mode_object *arg_obj;
3215 struct drm_mode_object *prop_obj;
3216 struct drm_property *property;
3217 int ret = -EINVAL;
3218 int i;
3219
3220 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3221 return -EINVAL;
3222
3223 mutex_lock(&dev->mode_config.mutex);
3224
3225 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3226 if (!arg_obj)
3227 goto out;
3228 if (!arg_obj->properties)
3229 goto out;
3230
3231 for (i = 0; i < DRM_OBJECT_MAX_PROPERTY; i++)
3232 if (arg_obj->properties->ids[i] == arg->prop_id)
3233 break;
3234
3235 if (i == DRM_OBJECT_MAX_PROPERTY)
3236 goto out;
3237
3238 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3239 DRM_MODE_OBJECT_PROPERTY);
3240 if (!prop_obj)
3241 goto out;
3242 property = obj_to_property(prop_obj);
3243
3244 if (!drm_property_change_is_valid(property, arg->value))
3245 goto out;
3246
3247 switch (arg_obj->type) {
3248 case DRM_MODE_OBJECT_CONNECTOR:
3249 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3250 arg->value);
3251 break;
3252 }
3253
3254out:
3255 mutex_unlock(&dev->mode_config.mutex);
3256 return ret;
3257}
3258
Dave Airlief453ba02008-11-07 14:05:41 -08003259int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3260 struct drm_encoder *encoder)
3261{
3262 int i;
3263
3264 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3265 if (connector->encoder_ids[i] == 0) {
3266 connector->encoder_ids[i] = encoder->base.id;
3267 return 0;
3268 }
3269 }
3270 return -ENOMEM;
3271}
3272EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3273
3274void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3275 struct drm_encoder *encoder)
3276{
3277 int i;
3278 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3279 if (connector->encoder_ids[i] == encoder->base.id) {
3280 connector->encoder_ids[i] = 0;
3281 if (connector->encoder == encoder)
3282 connector->encoder = NULL;
3283 break;
3284 }
3285 }
3286}
3287EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3288
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003289int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003290 int gamma_size)
3291{
3292 crtc->gamma_size = gamma_size;
3293
3294 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3295 if (!crtc->gamma_store) {
3296 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003297 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003298 }
3299
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003300 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003301}
3302EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3303
3304int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3305 void *data, struct drm_file *file_priv)
3306{
3307 struct drm_mode_crtc_lut *crtc_lut = data;
3308 struct drm_mode_object *obj;
3309 struct drm_crtc *crtc;
3310 void *r_base, *g_base, *b_base;
3311 int size;
3312 int ret = 0;
3313
Dave Airliefb3b06c2011-02-08 13:55:21 +10003314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3315 return -EINVAL;
3316
Dave Airlief453ba02008-11-07 14:05:41 -08003317 mutex_lock(&dev->mode_config.mutex);
3318 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3319 if (!obj) {
3320 ret = -EINVAL;
3321 goto out;
3322 }
3323 crtc = obj_to_crtc(obj);
3324
3325 /* memcpy into gamma store */
3326 if (crtc_lut->gamma_size != crtc->gamma_size) {
3327 ret = -EINVAL;
3328 goto out;
3329 }
3330
3331 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3332 r_base = crtc->gamma_store;
3333 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3334 ret = -EFAULT;
3335 goto out;
3336 }
3337
3338 g_base = r_base + size;
3339 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3340 ret = -EFAULT;
3341 goto out;
3342 }
3343
3344 b_base = g_base + size;
3345 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3346 ret = -EFAULT;
3347 goto out;
3348 }
3349
James Simmons72034252010-08-03 01:33:19 +01003350 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003351
3352out:
3353 mutex_unlock(&dev->mode_config.mutex);
3354 return ret;
3355
3356}
3357
3358int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3359 void *data, struct drm_file *file_priv)
3360{
3361 struct drm_mode_crtc_lut *crtc_lut = data;
3362 struct drm_mode_object *obj;
3363 struct drm_crtc *crtc;
3364 void *r_base, *g_base, *b_base;
3365 int size;
3366 int ret = 0;
3367
Dave Airliefb3b06c2011-02-08 13:55:21 +10003368 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3369 return -EINVAL;
3370
Dave Airlief453ba02008-11-07 14:05:41 -08003371 mutex_lock(&dev->mode_config.mutex);
3372 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3373 if (!obj) {
3374 ret = -EINVAL;
3375 goto out;
3376 }
3377 crtc = obj_to_crtc(obj);
3378
3379 /* memcpy into gamma store */
3380 if (crtc_lut->gamma_size != crtc->gamma_size) {
3381 ret = -EINVAL;
3382 goto out;
3383 }
3384
3385 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3386 r_base = crtc->gamma_store;
3387 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3388 ret = -EFAULT;
3389 goto out;
3390 }
3391
3392 g_base = r_base + size;
3393 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3394 ret = -EFAULT;
3395 goto out;
3396 }
3397
3398 b_base = g_base + size;
3399 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3400 ret = -EFAULT;
3401 goto out;
3402 }
3403out:
3404 mutex_unlock(&dev->mode_config.mutex);
3405 return ret;
3406}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003407
3408int drm_mode_page_flip_ioctl(struct drm_device *dev,
3409 void *data, struct drm_file *file_priv)
3410{
3411 struct drm_mode_crtc_page_flip *page_flip = data;
3412 struct drm_mode_object *obj;
3413 struct drm_crtc *crtc;
3414 struct drm_framebuffer *fb;
3415 struct drm_pending_vblank_event *e = NULL;
3416 unsigned long flags;
3417 int ret = -EINVAL;
3418
3419 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3420 page_flip->reserved != 0)
3421 return -EINVAL;
3422
3423 mutex_lock(&dev->mode_config.mutex);
3424 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3425 if (!obj)
3426 goto out;
3427 crtc = obj_to_crtc(obj);
3428
Chris Wilson90c1efd2010-07-17 20:23:26 +01003429 if (crtc->fb == NULL) {
3430 /* The framebuffer is currently unbound, presumably
3431 * due to a hotplug event, that userspace has not
3432 * yet discovered.
3433 */
3434 ret = -EBUSY;
3435 goto out;
3436 }
3437
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003438 if (crtc->funcs->page_flip == NULL)
3439 goto out;
3440
3441 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3442 if (!obj)
3443 goto out;
3444 fb = obj_to_fb(obj);
3445
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003446 if (crtc->mode.hdisplay > fb->width ||
3447 crtc->mode.vdisplay > fb->height ||
3448 crtc->x > fb->width - crtc->mode.hdisplay ||
3449 crtc->y > fb->height - crtc->mode.vdisplay) {
3450 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3451 fb->width, fb->height,
3452 crtc->mode.hdisplay, crtc->mode.vdisplay,
3453 crtc->x, crtc->y);
3454 ret = -ENOSPC;
3455 goto out;
3456 }
3457
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003458 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3459 ret = -ENOMEM;
3460 spin_lock_irqsave(&dev->event_lock, flags);
3461 if (file_priv->event_space < sizeof e->event) {
3462 spin_unlock_irqrestore(&dev->event_lock, flags);
3463 goto out;
3464 }
3465 file_priv->event_space -= sizeof e->event;
3466 spin_unlock_irqrestore(&dev->event_lock, flags);
3467
3468 e = kzalloc(sizeof *e, GFP_KERNEL);
3469 if (e == NULL) {
3470 spin_lock_irqsave(&dev->event_lock, flags);
3471 file_priv->event_space += sizeof e->event;
3472 spin_unlock_irqrestore(&dev->event_lock, flags);
3473 goto out;
3474 }
3475
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003476 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003477 e->event.base.length = sizeof e->event;
3478 e->event.user_data = page_flip->user_data;
3479 e->base.event = &e->event.base;
3480 e->base.file_priv = file_priv;
3481 e->base.destroy =
3482 (void (*) (struct drm_pending_event *)) kfree;
3483 }
3484
3485 ret = crtc->funcs->page_flip(crtc, fb, e);
3486 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003487 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3488 spin_lock_irqsave(&dev->event_lock, flags);
3489 file_priv->event_space += sizeof e->event;
3490 spin_unlock_irqrestore(&dev->event_lock, flags);
3491 kfree(e);
3492 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003493 }
3494
3495out:
3496 mutex_unlock(&dev->mode_config.mutex);
3497 return ret;
3498}
Chris Wilsoneb033552011-01-24 15:11:08 +00003499
3500void drm_mode_config_reset(struct drm_device *dev)
3501{
3502 struct drm_crtc *crtc;
3503 struct drm_encoder *encoder;
3504 struct drm_connector *connector;
3505
3506 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3507 if (crtc->funcs->reset)
3508 crtc->funcs->reset(crtc);
3509
3510 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3511 if (encoder->funcs->reset)
3512 encoder->funcs->reset(encoder);
3513
3514 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3515 if (connector->funcs->reset)
3516 connector->funcs->reset(connector);
3517}
3518EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003519
3520int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3521 void *data, struct drm_file *file_priv)
3522{
3523 struct drm_mode_create_dumb *args = data;
3524
3525 if (!dev->driver->dumb_create)
3526 return -ENOSYS;
3527 return dev->driver->dumb_create(file_priv, dev, args);
3528}
3529
3530int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3531 void *data, struct drm_file *file_priv)
3532{
3533 struct drm_mode_map_dumb *args = data;
3534
3535 /* call driver ioctl to get mmap offset */
3536 if (!dev->driver->dumb_map_offset)
3537 return -ENOSYS;
3538
3539 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3540}
3541
3542int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3543 void *data, struct drm_file *file_priv)
3544{
3545 struct drm_mode_destroy_dumb *args = data;
3546
3547 if (!dev->driver->dumb_destroy)
3548 return -ENOSYS;
3549
3550 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3551}
Dave Airlie248dbc22011-11-29 20:02:54 +00003552
3553/*
3554 * Just need to support RGB formats here for compat with code that doesn't
3555 * use pixel formats directly yet.
3556 */
3557void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3558 int *bpp)
3559{
3560 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003561 case DRM_FORMAT_RGB332:
3562 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003563 *depth = 8;
3564 *bpp = 8;
3565 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003566 case DRM_FORMAT_XRGB1555:
3567 case DRM_FORMAT_XBGR1555:
3568 case DRM_FORMAT_RGBX5551:
3569 case DRM_FORMAT_BGRX5551:
3570 case DRM_FORMAT_ARGB1555:
3571 case DRM_FORMAT_ABGR1555:
3572 case DRM_FORMAT_RGBA5551:
3573 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003574 *depth = 15;
3575 *bpp = 16;
3576 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003577 case DRM_FORMAT_RGB565:
3578 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003579 *depth = 16;
3580 *bpp = 16;
3581 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003582 case DRM_FORMAT_RGB888:
3583 case DRM_FORMAT_BGR888:
3584 *depth = 24;
3585 *bpp = 24;
3586 break;
3587 case DRM_FORMAT_XRGB8888:
3588 case DRM_FORMAT_XBGR8888:
3589 case DRM_FORMAT_RGBX8888:
3590 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003591 *depth = 24;
3592 *bpp = 32;
3593 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003594 case DRM_FORMAT_XRGB2101010:
3595 case DRM_FORMAT_XBGR2101010:
3596 case DRM_FORMAT_RGBX1010102:
3597 case DRM_FORMAT_BGRX1010102:
3598 case DRM_FORMAT_ARGB2101010:
3599 case DRM_FORMAT_ABGR2101010:
3600 case DRM_FORMAT_RGBA1010102:
3601 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003602 *depth = 30;
3603 *bpp = 32;
3604 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003605 case DRM_FORMAT_ARGB8888:
3606 case DRM_FORMAT_ABGR8888:
3607 case DRM_FORMAT_RGBA8888:
3608 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003609 *depth = 32;
3610 *bpp = 32;
3611 break;
3612 default:
3613 DRM_DEBUG_KMS("unsupported pixel format\n");
3614 *depth = 0;
3615 *bpp = 0;
3616 break;
3617 }
3618}
3619EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003620
3621/**
3622 * drm_format_num_planes - get the number of planes for format
3623 * @format: pixel format (DRM_FORMAT_*)
3624 *
3625 * RETURNS:
3626 * The number of planes used by the specified pixel format.
3627 */
3628int drm_format_num_planes(uint32_t format)
3629{
3630 switch (format) {
3631 case DRM_FORMAT_YUV410:
3632 case DRM_FORMAT_YVU410:
3633 case DRM_FORMAT_YUV411:
3634 case DRM_FORMAT_YVU411:
3635 case DRM_FORMAT_YUV420:
3636 case DRM_FORMAT_YVU420:
3637 case DRM_FORMAT_YUV422:
3638 case DRM_FORMAT_YVU422:
3639 case DRM_FORMAT_YUV444:
3640 case DRM_FORMAT_YVU444:
3641 return 3;
3642 case DRM_FORMAT_NV12:
3643 case DRM_FORMAT_NV21:
3644 case DRM_FORMAT_NV16:
3645 case DRM_FORMAT_NV61:
3646 return 2;
3647 default:
3648 return 1;
3649 }
3650}
3651EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003652
3653/**
3654 * drm_format_plane_cpp - determine the bytes per pixel value
3655 * @format: pixel format (DRM_FORMAT_*)
3656 * @plane: plane index
3657 *
3658 * RETURNS:
3659 * The bytes per pixel value for the specified plane.
3660 */
3661int drm_format_plane_cpp(uint32_t format, int plane)
3662{
3663 unsigned int depth;
3664 int bpp;
3665
3666 if (plane >= drm_format_num_planes(format))
3667 return 0;
3668
3669 switch (format) {
3670 case DRM_FORMAT_YUYV:
3671 case DRM_FORMAT_YVYU:
3672 case DRM_FORMAT_UYVY:
3673 case DRM_FORMAT_VYUY:
3674 return 2;
3675 case DRM_FORMAT_NV12:
3676 case DRM_FORMAT_NV21:
3677 case DRM_FORMAT_NV16:
3678 case DRM_FORMAT_NV61:
3679 return plane ? 2 : 1;
3680 case DRM_FORMAT_YUV410:
3681 case DRM_FORMAT_YVU410:
3682 case DRM_FORMAT_YUV411:
3683 case DRM_FORMAT_YVU411:
3684 case DRM_FORMAT_YUV420:
3685 case DRM_FORMAT_YVU420:
3686 case DRM_FORMAT_YUV422:
3687 case DRM_FORMAT_YVU422:
3688 case DRM_FORMAT_YUV444:
3689 case DRM_FORMAT_YVU444:
3690 return 1;
3691 default:
3692 drm_fb_get_bpp_depth(format, &depth, &bpp);
3693 return bpp >> 3;
3694 }
3695}
3696EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003697
3698/**
3699 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3700 * @format: pixel format (DRM_FORMAT_*)
3701 *
3702 * RETURNS:
3703 * The horizontal chroma subsampling factor for the
3704 * specified pixel format.
3705 */
3706int drm_format_horz_chroma_subsampling(uint32_t format)
3707{
3708 switch (format) {
3709 case DRM_FORMAT_YUV411:
3710 case DRM_FORMAT_YVU411:
3711 case DRM_FORMAT_YUV410:
3712 case DRM_FORMAT_YVU410:
3713 return 4;
3714 case DRM_FORMAT_YUYV:
3715 case DRM_FORMAT_YVYU:
3716 case DRM_FORMAT_UYVY:
3717 case DRM_FORMAT_VYUY:
3718 case DRM_FORMAT_NV12:
3719 case DRM_FORMAT_NV21:
3720 case DRM_FORMAT_NV16:
3721 case DRM_FORMAT_NV61:
3722 case DRM_FORMAT_YUV422:
3723 case DRM_FORMAT_YVU422:
3724 case DRM_FORMAT_YUV420:
3725 case DRM_FORMAT_YVU420:
3726 return 2;
3727 default:
3728 return 1;
3729 }
3730}
3731EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3732
3733/**
3734 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3735 * @format: pixel format (DRM_FORMAT_*)
3736 *
3737 * RETURNS:
3738 * The vertical chroma subsampling factor for the
3739 * specified pixel format.
3740 */
3741int drm_format_vert_chroma_subsampling(uint32_t format)
3742{
3743 switch (format) {
3744 case DRM_FORMAT_YUV410:
3745 case DRM_FORMAT_YVU410:
3746 return 4;
3747 case DRM_FORMAT_YUV420:
3748 case DRM_FORMAT_YVU420:
3749 case DRM_FORMAT_NV12:
3750 case DRM_FORMAT_NV21:
3751 return 2;
3752 default:
3753 return 1;
3754 }
3755}
3756EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);