blob: 9ccb92fdd7b26f7d49008d564f19fa4f006ab0bb [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");
230 return -EINVAL;
231 }
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;
238
239 obj->id = new_id;
240 obj->type = obj_type;
241 return 0;
242}
243
244/**
245 * drm_mode_object_put - free an identifer
246 * @dev: DRM device
247 * @id: ID to free
248 *
249 * LOCKING:
250 * Caller must hold DRM mode_config lock.
251 *
252 * Free @id from @dev's unique identifier pool.
253 */
254static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
256{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000257 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800258 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260}
261
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200262struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800264{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000265 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800266
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000270 obj = NULL;
271 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800272
273 return obj;
274}
275EXPORT_SYMBOL(drm_mode_object_find);
276
277/**
Dave Airlief453ba02008-11-07 14:05:41 -0800278 * drm_framebuffer_init - initialize a framebuffer
279 * @dev: DRM device
280 *
281 * LOCKING:
282 * Caller must hold mode config lock.
283 *
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
286 *
287 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200288 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800289 */
290int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
292{
293 int ret;
294
295 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
296 if (ret) {
297 return ret;
298 }
299
300 fb->dev = dev;
301 fb->funcs = funcs;
302 dev->mode_config.num_fb++;
303 list_add(&fb->head, &dev->mode_config.fb_list);
304
305 return 0;
306}
307EXPORT_SYMBOL(drm_framebuffer_init);
308
309/**
310 * drm_framebuffer_cleanup - remove a framebuffer object
311 * @fb: framebuffer to remove
312 *
313 * LOCKING:
314 * Caller must hold mode config lock.
315 *
316 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
317 * it, setting it to NULL.
318 */
319void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
320{
321 struct drm_device *dev = fb->dev;
322 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800323 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000324 struct drm_mode_set set;
325 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800326
327 /* remove from any CRTC */
328 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000329 if (crtc->fb == fb) {
330 /* should turn off the crtc */
331 memset(&set, 0, sizeof(struct drm_mode_set));
332 set.crtc = crtc;
333 set.fb = NULL;
334 ret = crtc->funcs->set_config(&set);
335 if (ret)
336 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
337 }
Dave Airlief453ba02008-11-07 14:05:41 -0800338 }
339
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800340 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
341 if (plane->fb == fb) {
342 /* should turn off the crtc */
343 ret = plane->funcs->disable_plane(plane);
344 if (ret)
345 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600346 /* disconnect the plane from the fb and crtc: */
347 plane->fb = NULL;
348 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800349 }
350 }
351
Dave Airlief453ba02008-11-07 14:05:41 -0800352 drm_mode_object_put(dev, &fb->base);
353 list_del(&fb->head);
354 dev->mode_config.num_fb--;
355}
356EXPORT_SYMBOL(drm_framebuffer_cleanup);
357
358/**
359 * drm_crtc_init - Initialise a new CRTC object
360 * @dev: DRM device
361 * @crtc: CRTC object to init
362 * @funcs: callbacks for the new CRTC
363 *
364 * LOCKING:
365 * Caller must hold mode config lock.
366 *
367 * Inits a new object created as base part of an driver crtc object.
368 */
369void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
370 const struct drm_crtc_funcs *funcs)
371{
372 crtc->dev = dev;
373 crtc->funcs = funcs;
374
375 mutex_lock(&dev->mode_config.mutex);
376 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
377
378 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
379 dev->mode_config.num_crtc++;
380 mutex_unlock(&dev->mode_config.mutex);
381}
382EXPORT_SYMBOL(drm_crtc_init);
383
384/**
385 * drm_crtc_cleanup - Cleans up the core crtc usage.
386 * @crtc: CRTC to cleanup
387 *
388 * LOCKING:
389 * Caller must hold mode config lock.
390 *
391 * Cleanup @crtc. Removes from drm modesetting space
392 * does NOT free object, caller does that.
393 */
394void drm_crtc_cleanup(struct drm_crtc *crtc)
395{
396 struct drm_device *dev = crtc->dev;
397
398 if (crtc->gamma_store) {
399 kfree(crtc->gamma_store);
400 crtc->gamma_store = NULL;
401 }
402
403 drm_mode_object_put(dev, &crtc->base);
404 list_del(&crtc->head);
405 dev->mode_config.num_crtc--;
406}
407EXPORT_SYMBOL(drm_crtc_cleanup);
408
409/**
410 * drm_mode_probed_add - add a mode to a connector's probed mode list
411 * @connector: connector the new mode
412 * @mode: mode data
413 *
414 * LOCKING:
415 * Caller must hold mode config lock.
416 *
417 * Add @mode to @connector's mode list for later use.
418 */
419void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421{
422 list_add(&mode->head, &connector->probed_modes);
423}
424EXPORT_SYMBOL(drm_mode_probed_add);
425
426/**
427 * drm_mode_remove - remove and free a mode
428 * @connector: connector list to modify
429 * @mode: mode to remove
430 *
431 * LOCKING:
432 * Caller must hold mode config lock.
433 *
434 * Remove @mode from @connector's mode list, then free it.
435 */
436void drm_mode_remove(struct drm_connector *connector,
437 struct drm_display_mode *mode)
438{
439 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100440 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800441}
442EXPORT_SYMBOL(drm_mode_remove);
443
444/**
445 * drm_connector_init - Init a preallocated connector
446 * @dev: DRM device
447 * @connector: the connector to init
448 * @funcs: callbacks for this connector
449 * @name: user visible name of the connector
450 *
451 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100452 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800453 *
454 * Initialises a preallocated connector. Connectors should be
455 * subclassed as part of driver connector objects.
456 */
457void drm_connector_init(struct drm_device *dev,
458 struct drm_connector *connector,
459 const struct drm_connector_funcs *funcs,
460 int connector_type)
461{
462 mutex_lock(&dev->mode_config.mutex);
463
464 connector->dev = dev;
465 connector->funcs = funcs;
466 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
467 connector->connector_type = connector_type;
468 connector->connector_type_id =
469 ++drm_connector_enum_list[connector_type].count; /* TODO */
470 INIT_LIST_HEAD(&connector->user_modes);
471 INIT_LIST_HEAD(&connector->probed_modes);
472 INIT_LIST_HEAD(&connector->modes);
473 connector->edid_blob_ptr = NULL;
474
475 list_add_tail(&connector->head, &dev->mode_config.connector_list);
476 dev->mode_config.num_connector++;
477
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200478 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
479 drm_connector_attach_property(connector,
480 dev->mode_config.edid_property,
481 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800482
483 drm_connector_attach_property(connector,
484 dev->mode_config.dpms_property, 0);
485
486 mutex_unlock(&dev->mode_config.mutex);
487}
488EXPORT_SYMBOL(drm_connector_init);
489
490/**
491 * drm_connector_cleanup - cleans up an initialised connector
492 * @connector: connector to cleanup
493 *
494 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100495 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800496 *
497 * Cleans up the connector but doesn't free the object.
498 */
499void drm_connector_cleanup(struct drm_connector *connector)
500{
501 struct drm_device *dev = connector->dev;
502 struct drm_display_mode *mode, *t;
503
504 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
505 drm_mode_remove(connector, mode);
506
507 list_for_each_entry_safe(mode, t, &connector->modes, head)
508 drm_mode_remove(connector, mode);
509
510 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
511 drm_mode_remove(connector, mode);
512
513 mutex_lock(&dev->mode_config.mutex);
514 drm_mode_object_put(dev, &connector->base);
515 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000516 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800517 mutex_unlock(&dev->mode_config.mutex);
518}
519EXPORT_SYMBOL(drm_connector_cleanup);
520
521void drm_encoder_init(struct drm_device *dev,
522 struct drm_encoder *encoder,
523 const struct drm_encoder_funcs *funcs,
524 int encoder_type)
525{
526 mutex_lock(&dev->mode_config.mutex);
527
528 encoder->dev = dev;
529
530 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
531 encoder->encoder_type = encoder_type;
532 encoder->funcs = funcs;
533
534 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
535 dev->mode_config.num_encoder++;
536
537 mutex_unlock(&dev->mode_config.mutex);
538}
539EXPORT_SYMBOL(drm_encoder_init);
540
541void drm_encoder_cleanup(struct drm_encoder *encoder)
542{
543 struct drm_device *dev = encoder->dev;
544 mutex_lock(&dev->mode_config.mutex);
545 drm_mode_object_put(dev, &encoder->base);
546 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000547 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800548 mutex_unlock(&dev->mode_config.mutex);
549}
550EXPORT_SYMBOL(drm_encoder_cleanup);
551
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800552int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
553 unsigned long possible_crtcs,
554 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600555 const uint32_t *formats, uint32_t format_count,
556 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800557{
558 mutex_lock(&dev->mode_config.mutex);
559
560 plane->dev = dev;
561 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
562 plane->funcs = funcs;
563 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
564 GFP_KERNEL);
565 if (!plane->format_types) {
566 DRM_DEBUG_KMS("out of memory when allocating plane\n");
567 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä10bf5732011-12-20 00:06:41 +0200568 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800569 return -ENOMEM;
570 }
571
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
575
Rob Clark0a7eb242011-12-13 20:19:36 -0600576 /* private planes are not exposed to userspace, but depending on
577 * display hardware, might be convenient to allow sharing programming
578 * for the scanout engine with the crtc implementation.
579 */
580 if (!priv) {
581 list_add_tail(&plane->head, &dev->mode_config.plane_list);
582 dev->mode_config.num_plane++;
583 } else {
584 INIT_LIST_HEAD(&plane->head);
585 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800586
587 mutex_unlock(&dev->mode_config.mutex);
588
589 return 0;
590}
591EXPORT_SYMBOL(drm_plane_init);
592
593void drm_plane_cleanup(struct drm_plane *plane)
594{
595 struct drm_device *dev = plane->dev;
596
597 mutex_lock(&dev->mode_config.mutex);
598 kfree(plane->format_types);
599 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600600 /* if not added to a list, it must be a private plane */
601 if (!list_empty(&plane->head)) {
602 list_del(&plane->head);
603 dev->mode_config.num_plane--;
604 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800605 mutex_unlock(&dev->mode_config.mutex);
606}
607EXPORT_SYMBOL(drm_plane_cleanup);
608
Dave Airlief453ba02008-11-07 14:05:41 -0800609/**
610 * drm_mode_create - create a new display mode
611 * @dev: DRM device
612 *
613 * LOCKING:
614 * Caller must hold DRM mode_config lock.
615 *
616 * Create a new drm_display_mode, give it an ID, and return it.
617 *
618 * RETURNS:
619 * Pointer to new mode on success, NULL on error.
620 */
621struct drm_display_mode *drm_mode_create(struct drm_device *dev)
622{
623 struct drm_display_mode *nmode;
624
625 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
626 if (!nmode)
627 return NULL;
628
629 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
630 return nmode;
631}
632EXPORT_SYMBOL(drm_mode_create);
633
634/**
635 * drm_mode_destroy - remove a mode
636 * @dev: DRM device
637 * @mode: mode to remove
638 *
639 * LOCKING:
640 * Caller must hold mode config lock.
641 *
642 * Free @mode's unique identifier, then free it.
643 */
644void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
645{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200646 if (!mode)
647 return;
648
Dave Airlief453ba02008-11-07 14:05:41 -0800649 drm_mode_object_put(dev, &mode->base);
650
651 kfree(mode);
652}
653EXPORT_SYMBOL(drm_mode_destroy);
654
655static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
656{
657 struct drm_property *edid;
658 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800659
660 /*
661 * Standard properties (apply to all connectors)
662 */
663 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
664 DRM_MODE_PROP_IMMUTABLE,
665 "EDID", 0);
666 dev->mode_config.edid_property = edid;
667
Sascha Hauer4a67d392012-02-06 10:58:17 +0100668 dpms = drm_property_create_enum(dev, 0,
669 "DPMS", drm_dpms_enum_list,
670 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800671 dev->mode_config.dpms_property = dpms;
672
673 return 0;
674}
675
676/**
677 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
678 * @dev: DRM device
679 *
680 * Called by a driver the first time a DVI-I connector is made.
681 */
682int drm_mode_create_dvi_i_properties(struct drm_device *dev)
683{
684 struct drm_property *dvi_i_selector;
685 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800686
687 if (dev->mode_config.dvi_i_select_subconnector_property)
688 return 0;
689
690 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100691 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800692 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100693 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800694 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800695 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
696
Sascha Hauer4a67d392012-02-06 10:58:17 +0100697 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800698 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100699 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800700 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800701 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
702
703 return 0;
704}
705EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
706
707/**
708 * drm_create_tv_properties - create TV specific connector properties
709 * @dev: DRM device
710 * @num_modes: number of different TV formats (modes) supported
711 * @modes: array of pointers to strings containing name of each format
712 *
713 * Called by a driver's TV initialization routine, this function creates
714 * the TV specific connector properties for a given device. Caller is
715 * responsible for allocating a list of format names and passing them to
716 * this routine.
717 */
718int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
719 char *modes[])
720{
721 struct drm_property *tv_selector;
722 struct drm_property *tv_subconnector;
723 int i;
724
725 if (dev->mode_config.tv_select_subconnector_property)
726 return 0;
727
728 /*
729 * Basic connector properties
730 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100731 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800732 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100733 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800734 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800735 dev->mode_config.tv_select_subconnector_property = tv_selector;
736
737 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100738 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
739 "subconnector",
740 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800741 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800742 dev->mode_config.tv_subconnector_property = tv_subconnector;
743
744 /*
745 * Other, TV specific properties: margins & TV modes.
746 */
747 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100748 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800749
750 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100751 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800752
753 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100754 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800755
756 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100757 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800758
759 dev->mode_config.tv_mode_property =
760 drm_property_create(dev, DRM_MODE_PROP_ENUM,
761 "mode", num_modes);
762 for (i = 0; i < num_modes; i++)
763 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
764 i, modes[i]);
765
Francisco Jerezb6b79022009-08-02 04:19:20 +0200766 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100767 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200768
769 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100770 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200771
772 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100773 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200774
Francisco Jereza75f0232009-08-12 02:30:10 +0200775 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100776 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200777
778 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100779 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200780
781 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100782 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200783
Dave Airlief453ba02008-11-07 14:05:41 -0800784 return 0;
785}
786EXPORT_SYMBOL(drm_mode_create_tv_properties);
787
788/**
789 * drm_mode_create_scaling_mode_property - create scaling mode property
790 * @dev: DRM device
791 *
792 * Called by a driver the first time it's needed, must be attached to desired
793 * connectors.
794 */
795int drm_mode_create_scaling_mode_property(struct drm_device *dev)
796{
797 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800798
799 if (dev->mode_config.scaling_mode_property)
800 return 0;
801
802 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100803 drm_property_create_enum(dev, 0, "scaling mode",
804 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800805 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800806
807 dev->mode_config.scaling_mode_property = scaling_mode;
808
809 return 0;
810}
811EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
812
813/**
814 * drm_mode_create_dithering_property - create dithering property
815 * @dev: DRM device
816 *
817 * Called by a driver the first time it's needed, must be attached to desired
818 * connectors.
819 */
820int drm_mode_create_dithering_property(struct drm_device *dev)
821{
822 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800823
824 if (dev->mode_config.dithering_mode_property)
825 return 0;
826
827 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100828 drm_property_create_enum(dev, 0, "dithering",
829 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800830 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800831 dev->mode_config.dithering_mode_property = dithering_mode;
832
833 return 0;
834}
835EXPORT_SYMBOL(drm_mode_create_dithering_property);
836
837/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000838 * drm_mode_create_dirty_property - create dirty property
839 * @dev: DRM device
840 *
841 * Called by a driver the first time it's needed, must be attached to desired
842 * connectors.
843 */
844int drm_mode_create_dirty_info_property(struct drm_device *dev)
845{
846 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000847
848 if (dev->mode_config.dirty_info_property)
849 return 0;
850
851 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100852 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000853 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100854 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000855 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000856 dev->mode_config.dirty_info_property = dirty_info;
857
858 return 0;
859}
860EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
861
862/**
Dave Airlief453ba02008-11-07 14:05:41 -0800863 * drm_mode_config_init - initialize DRM mode_configuration structure
864 * @dev: DRM device
865 *
866 * LOCKING:
867 * None, should happen single threaded at init time.
868 *
869 * Initialize @dev's mode_config structure, used for tracking the graphics
870 * configuration of @dev.
871 */
872void drm_mode_config_init(struct drm_device *dev)
873{
874 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000875 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800876 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800877 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
878 INIT_LIST_HEAD(&dev->mode_config.connector_list);
879 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
880 INIT_LIST_HEAD(&dev->mode_config.property_list);
881 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800882 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800883 idr_init(&dev->mode_config.crtc_idr);
884
885 mutex_lock(&dev->mode_config.mutex);
886 drm_mode_create_standard_connector_properties(dev);
887 mutex_unlock(&dev->mode_config.mutex);
888
889 /* Just to be sure */
890 dev->mode_config.num_fb = 0;
891 dev->mode_config.num_connector = 0;
892 dev->mode_config.num_crtc = 0;
893 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800894}
895EXPORT_SYMBOL(drm_mode_config_init);
896
897int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
898{
899 uint32_t total_objects = 0;
900
901 total_objects += dev->mode_config.num_crtc;
902 total_objects += dev->mode_config.num_connector;
903 total_objects += dev->mode_config.num_encoder;
904
Dave Airlief453ba02008-11-07 14:05:41 -0800905 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
906 if (!group->id_list)
907 return -ENOMEM;
908
909 group->num_crtcs = 0;
910 group->num_connectors = 0;
911 group->num_encoders = 0;
912 return 0;
913}
914
915int drm_mode_group_init_legacy_group(struct drm_device *dev,
916 struct drm_mode_group *group)
917{
918 struct drm_crtc *crtc;
919 struct drm_encoder *encoder;
920 struct drm_connector *connector;
921 int ret;
922
923 if ((ret = drm_mode_group_init(dev, group)))
924 return ret;
925
926 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
927 group->id_list[group->num_crtcs++] = crtc->base.id;
928
929 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
930 group->id_list[group->num_crtcs + group->num_encoders++] =
931 encoder->base.id;
932
933 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
934 group->id_list[group->num_crtcs + group->num_encoders +
935 group->num_connectors++] = connector->base.id;
936
937 return 0;
938}
939
940/**
941 * drm_mode_config_cleanup - free up DRM mode_config info
942 * @dev: DRM device
943 *
944 * LOCKING:
945 * Caller must hold mode config lock.
946 *
947 * Free up all the connectors and CRTCs associated with this DRM device, then
948 * free up the framebuffers and associated buffer objects.
949 *
950 * FIXME: cleanup any dangling user buffer objects too
951 */
952void drm_mode_config_cleanup(struct drm_device *dev)
953{
954 struct drm_connector *connector, *ot;
955 struct drm_crtc *crtc, *ct;
956 struct drm_encoder *encoder, *enct;
957 struct drm_framebuffer *fb, *fbt;
958 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800959 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -0800960
961 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
962 head) {
963 encoder->funcs->destroy(encoder);
964 }
965
966 list_for_each_entry_safe(connector, ot,
967 &dev->mode_config.connector_list, head) {
968 connector->funcs->destroy(connector);
969 }
970
971 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
972 head) {
973 drm_property_destroy(dev, property);
974 }
975
976 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
977 fb->funcs->destroy(fb);
978 }
979
980 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
981 crtc->funcs->destroy(crtc);
982 }
983
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800984 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
985 head) {
986 plane->funcs->destroy(plane);
987 }
Sascha Hauer59ce0622012-02-01 11:38:20 +0100988
989 idr_remove_all(&dev->mode_config.crtc_idr);
990 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -0800991}
992EXPORT_SYMBOL(drm_mode_config_cleanup);
993
Dave Airlief453ba02008-11-07 14:05:41 -0800994/**
995 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
996 * @out: drm_mode_modeinfo struct to return to the user
997 * @in: drm_display_mode to use
998 *
999 * LOCKING:
1000 * None.
1001 *
1002 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1003 * the user.
1004 */
1005void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1006 struct drm_display_mode *in)
1007{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001008 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1009 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1010 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1011 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1012 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1013 "timing values too large for mode info\n");
1014
Dave Airlief453ba02008-11-07 14:05:41 -08001015 out->clock = in->clock;
1016 out->hdisplay = in->hdisplay;
1017 out->hsync_start = in->hsync_start;
1018 out->hsync_end = in->hsync_end;
1019 out->htotal = in->htotal;
1020 out->hskew = in->hskew;
1021 out->vdisplay = in->vdisplay;
1022 out->vsync_start = in->vsync_start;
1023 out->vsync_end = in->vsync_end;
1024 out->vtotal = in->vtotal;
1025 out->vscan = in->vscan;
1026 out->vrefresh = in->vrefresh;
1027 out->flags = in->flags;
1028 out->type = in->type;
1029 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1030 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1031}
1032
1033/**
1034 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1035 * @out: drm_display_mode to return to the user
1036 * @in: drm_mode_modeinfo to use
1037 *
1038 * LOCKING:
1039 * None.
1040 *
1041 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1042 * the caller.
1043 */
1044void drm_crtc_convert_umode(struct drm_display_mode *out,
1045 struct drm_mode_modeinfo *in)
1046{
1047 out->clock = in->clock;
1048 out->hdisplay = in->hdisplay;
1049 out->hsync_start = in->hsync_start;
1050 out->hsync_end = in->hsync_end;
1051 out->htotal = in->htotal;
1052 out->hskew = in->hskew;
1053 out->vdisplay = in->vdisplay;
1054 out->vsync_start = in->vsync_start;
1055 out->vsync_end = in->vsync_end;
1056 out->vtotal = in->vtotal;
1057 out->vscan = in->vscan;
1058 out->vrefresh = in->vrefresh;
1059 out->flags = in->flags;
1060 out->type = in->type;
1061 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1062 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1063}
1064
1065/**
1066 * drm_mode_getresources - get graphics configuration
1067 * @inode: inode from the ioctl
1068 * @filp: file * from the ioctl
1069 * @cmd: cmd from ioctl
1070 * @arg: arg from ioctl
1071 *
1072 * LOCKING:
1073 * Takes mode config lock.
1074 *
1075 * Construct a set of configuration description structures and return
1076 * them to the user, including CRTC, connector and framebuffer configuration.
1077 *
1078 * Called by the user via ioctl.
1079 *
1080 * RETURNS:
1081 * Zero on success, errno on failure.
1082 */
1083int drm_mode_getresources(struct drm_device *dev, void *data,
1084 struct drm_file *file_priv)
1085{
1086 struct drm_mode_card_res *card_res = data;
1087 struct list_head *lh;
1088 struct drm_framebuffer *fb;
1089 struct drm_connector *connector;
1090 struct drm_crtc *crtc;
1091 struct drm_encoder *encoder;
1092 int ret = 0;
1093 int connector_count = 0;
1094 int crtc_count = 0;
1095 int fb_count = 0;
1096 int encoder_count = 0;
1097 int copied = 0, i;
1098 uint32_t __user *fb_id;
1099 uint32_t __user *crtc_id;
1100 uint32_t __user *connector_id;
1101 uint32_t __user *encoder_id;
1102 struct drm_mode_group *mode_group;
1103
Dave Airliefb3b06c2011-02-08 13:55:21 +10001104 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1105 return -EINVAL;
1106
Dave Airlief453ba02008-11-07 14:05:41 -08001107 mutex_lock(&dev->mode_config.mutex);
1108
1109 /*
1110 * For the non-control nodes we need to limit the list of resources
1111 * by IDs in the group list for this node
1112 */
1113 list_for_each(lh, &file_priv->fbs)
1114 fb_count++;
1115
1116 mode_group = &file_priv->master->minor->mode_group;
1117 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1118
1119 list_for_each(lh, &dev->mode_config.crtc_list)
1120 crtc_count++;
1121
1122 list_for_each(lh, &dev->mode_config.connector_list)
1123 connector_count++;
1124
1125 list_for_each(lh, &dev->mode_config.encoder_list)
1126 encoder_count++;
1127 } else {
1128
1129 crtc_count = mode_group->num_crtcs;
1130 connector_count = mode_group->num_connectors;
1131 encoder_count = mode_group->num_encoders;
1132 }
1133
1134 card_res->max_height = dev->mode_config.max_height;
1135 card_res->min_height = dev->mode_config.min_height;
1136 card_res->max_width = dev->mode_config.max_width;
1137 card_res->min_width = dev->mode_config.min_width;
1138
1139 /* handle this in 4 parts */
1140 /* FBs */
1141 if (card_res->count_fbs >= fb_count) {
1142 copied = 0;
1143 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001144 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001145 if (put_user(fb->base.id, fb_id + copied)) {
1146 ret = -EFAULT;
1147 goto out;
1148 }
1149 copied++;
1150 }
1151 }
1152 card_res->count_fbs = fb_count;
1153
1154 /* CRTCs */
1155 if (card_res->count_crtcs >= crtc_count) {
1156 copied = 0;
1157 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1158 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1159 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1160 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001161 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001162 if (put_user(crtc->base.id, crtc_id + copied)) {
1163 ret = -EFAULT;
1164 goto out;
1165 }
1166 copied++;
1167 }
1168 } else {
1169 for (i = 0; i < mode_group->num_crtcs; i++) {
1170 if (put_user(mode_group->id_list[i],
1171 crtc_id + copied)) {
1172 ret = -EFAULT;
1173 goto out;
1174 }
1175 copied++;
1176 }
1177 }
1178 }
1179 card_res->count_crtcs = crtc_count;
1180
1181 /* Encoders */
1182 if (card_res->count_encoders >= encoder_count) {
1183 copied = 0;
1184 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1185 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1186 list_for_each_entry(encoder,
1187 &dev->mode_config.encoder_list,
1188 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001189 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1190 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001191 if (put_user(encoder->base.id, encoder_id +
1192 copied)) {
1193 ret = -EFAULT;
1194 goto out;
1195 }
1196 copied++;
1197 }
1198 } else {
1199 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1200 if (put_user(mode_group->id_list[i],
1201 encoder_id + copied)) {
1202 ret = -EFAULT;
1203 goto out;
1204 }
1205 copied++;
1206 }
1207
1208 }
1209 }
1210 card_res->count_encoders = encoder_count;
1211
1212 /* Connectors */
1213 if (card_res->count_connectors >= connector_count) {
1214 copied = 0;
1215 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1216 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1217 list_for_each_entry(connector,
1218 &dev->mode_config.connector_list,
1219 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001220 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1221 connector->base.id,
1222 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001223 if (put_user(connector->base.id,
1224 connector_id + copied)) {
1225 ret = -EFAULT;
1226 goto out;
1227 }
1228 copied++;
1229 }
1230 } else {
1231 int start = mode_group->num_crtcs +
1232 mode_group->num_encoders;
1233 for (i = start; i < start + mode_group->num_connectors; i++) {
1234 if (put_user(mode_group->id_list[i],
1235 connector_id + copied)) {
1236 ret = -EFAULT;
1237 goto out;
1238 }
1239 copied++;
1240 }
1241 }
1242 }
1243 card_res->count_connectors = connector_count;
1244
Jerome Glisse94401062010-07-15 15:43:25 -04001245 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001246 card_res->count_connectors, card_res->count_encoders);
1247
1248out:
1249 mutex_unlock(&dev->mode_config.mutex);
1250 return ret;
1251}
1252
1253/**
1254 * drm_mode_getcrtc - get CRTC configuration
1255 * @inode: inode from the ioctl
1256 * @filp: file * from the ioctl
1257 * @cmd: cmd from ioctl
1258 * @arg: arg from ioctl
1259 *
1260 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001261 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001262 *
1263 * Construct a CRTC configuration structure to return to the user.
1264 *
1265 * Called by the user via ioctl.
1266 *
1267 * RETURNS:
1268 * Zero on success, errno on failure.
1269 */
1270int drm_mode_getcrtc(struct drm_device *dev,
1271 void *data, struct drm_file *file_priv)
1272{
1273 struct drm_mode_crtc *crtc_resp = data;
1274 struct drm_crtc *crtc;
1275 struct drm_mode_object *obj;
1276 int ret = 0;
1277
Dave Airliefb3b06c2011-02-08 13:55:21 +10001278 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1279 return -EINVAL;
1280
Dave Airlief453ba02008-11-07 14:05:41 -08001281 mutex_lock(&dev->mode_config.mutex);
1282
1283 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1284 DRM_MODE_OBJECT_CRTC);
1285 if (!obj) {
1286 ret = -EINVAL;
1287 goto out;
1288 }
1289 crtc = obj_to_crtc(obj);
1290
1291 crtc_resp->x = crtc->x;
1292 crtc_resp->y = crtc->y;
1293 crtc_resp->gamma_size = crtc->gamma_size;
1294 if (crtc->fb)
1295 crtc_resp->fb_id = crtc->fb->base.id;
1296 else
1297 crtc_resp->fb_id = 0;
1298
1299 if (crtc->enabled) {
1300
1301 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1302 crtc_resp->mode_valid = 1;
1303
1304 } else {
1305 crtc_resp->mode_valid = 0;
1306 }
1307
1308out:
1309 mutex_unlock(&dev->mode_config.mutex);
1310 return ret;
1311}
1312
1313/**
1314 * drm_mode_getconnector - get connector configuration
1315 * @inode: inode from the ioctl
1316 * @filp: file * from the ioctl
1317 * @cmd: cmd from ioctl
1318 * @arg: arg from ioctl
1319 *
1320 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001321 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001322 *
1323 * Construct a connector configuration structure to return to the user.
1324 *
1325 * Called by the user via ioctl.
1326 *
1327 * RETURNS:
1328 * Zero on success, errno on failure.
1329 */
1330int drm_mode_getconnector(struct drm_device *dev, void *data,
1331 struct drm_file *file_priv)
1332{
1333 struct drm_mode_get_connector *out_resp = data;
1334 struct drm_mode_object *obj;
1335 struct drm_connector *connector;
1336 struct drm_display_mode *mode;
1337 int mode_count = 0;
1338 int props_count = 0;
1339 int encoders_count = 0;
1340 int ret = 0;
1341 int copied = 0;
1342 int i;
1343 struct drm_mode_modeinfo u_mode;
1344 struct drm_mode_modeinfo __user *mode_ptr;
1345 uint32_t __user *prop_ptr;
1346 uint64_t __user *prop_values;
1347 uint32_t __user *encoder_ptr;
1348
Dave Airliefb3b06c2011-02-08 13:55:21 +10001349 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1350 return -EINVAL;
1351
Dave Airlief453ba02008-11-07 14:05:41 -08001352 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1353
Jerome Glisse94401062010-07-15 15:43:25 -04001354 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001355
1356 mutex_lock(&dev->mode_config.mutex);
1357
1358 obj = drm_mode_object_find(dev, out_resp->connector_id,
1359 DRM_MODE_OBJECT_CONNECTOR);
1360 if (!obj) {
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364 connector = obj_to_connector(obj);
1365
1366 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1367 if (connector->property_ids[i] != 0) {
1368 props_count++;
1369 }
1370 }
1371
1372 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1373 if (connector->encoder_ids[i] != 0) {
1374 encoders_count++;
1375 }
1376 }
1377
1378 if (out_resp->count_modes == 0) {
1379 connector->funcs->fill_modes(connector,
1380 dev->mode_config.max_width,
1381 dev->mode_config.max_height);
1382 }
1383
1384 /* delayed so we get modes regardless of pre-fill_modes state */
1385 list_for_each_entry(mode, &connector->modes, head)
1386 mode_count++;
1387
1388 out_resp->connector_id = connector->base.id;
1389 out_resp->connector_type = connector->connector_type;
1390 out_resp->connector_type_id = connector->connector_type_id;
1391 out_resp->mm_width = connector->display_info.width_mm;
1392 out_resp->mm_height = connector->display_info.height_mm;
1393 out_resp->subpixel = connector->display_info.subpixel_order;
1394 out_resp->connection = connector->status;
1395 if (connector->encoder)
1396 out_resp->encoder_id = connector->encoder->base.id;
1397 else
1398 out_resp->encoder_id = 0;
1399
1400 /*
1401 * This ioctl is called twice, once to determine how much space is
1402 * needed, and the 2nd time to fill it.
1403 */
1404 if ((out_resp->count_modes >= mode_count) && mode_count) {
1405 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001406 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001407 list_for_each_entry(mode, &connector->modes, head) {
1408 drm_crtc_convert_to_umode(&u_mode, mode);
1409 if (copy_to_user(mode_ptr + copied,
1410 &u_mode, sizeof(u_mode))) {
1411 ret = -EFAULT;
1412 goto out;
1413 }
1414 copied++;
1415 }
1416 }
1417 out_resp->count_modes = mode_count;
1418
1419 if ((out_resp->count_props >= props_count) && props_count) {
1420 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001421 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1422 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001423 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1424 if (connector->property_ids[i] != 0) {
1425 if (put_user(connector->property_ids[i],
1426 prop_ptr + copied)) {
1427 ret = -EFAULT;
1428 goto out;
1429 }
1430
1431 if (put_user(connector->property_values[i],
1432 prop_values + copied)) {
1433 ret = -EFAULT;
1434 goto out;
1435 }
1436 copied++;
1437 }
1438 }
1439 }
1440 out_resp->count_props = props_count;
1441
1442 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1443 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001444 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001445 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1446 if (connector->encoder_ids[i] != 0) {
1447 if (put_user(connector->encoder_ids[i],
1448 encoder_ptr + copied)) {
1449 ret = -EFAULT;
1450 goto out;
1451 }
1452 copied++;
1453 }
1454 }
1455 }
1456 out_resp->count_encoders = encoders_count;
1457
1458out:
1459 mutex_unlock(&dev->mode_config.mutex);
1460 return ret;
1461}
1462
1463int drm_mode_getencoder(struct drm_device *dev, void *data,
1464 struct drm_file *file_priv)
1465{
1466 struct drm_mode_get_encoder *enc_resp = data;
1467 struct drm_mode_object *obj;
1468 struct drm_encoder *encoder;
1469 int ret = 0;
1470
Dave Airliefb3b06c2011-02-08 13:55:21 +10001471 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1472 return -EINVAL;
1473
Dave Airlief453ba02008-11-07 14:05:41 -08001474 mutex_lock(&dev->mode_config.mutex);
1475 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1476 DRM_MODE_OBJECT_ENCODER);
1477 if (!obj) {
1478 ret = -EINVAL;
1479 goto out;
1480 }
1481 encoder = obj_to_encoder(obj);
1482
1483 if (encoder->crtc)
1484 enc_resp->crtc_id = encoder->crtc->base.id;
1485 else
1486 enc_resp->crtc_id = 0;
1487 enc_resp->encoder_type = encoder->encoder_type;
1488 enc_resp->encoder_id = encoder->base.id;
1489 enc_resp->possible_crtcs = encoder->possible_crtcs;
1490 enc_resp->possible_clones = encoder->possible_clones;
1491
1492out:
1493 mutex_unlock(&dev->mode_config.mutex);
1494 return ret;
1495}
1496
1497/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001498 * drm_mode_getplane_res - get plane info
1499 * @dev: DRM device
1500 * @data: ioctl data
1501 * @file_priv: DRM file info
1502 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001503 * LOCKING:
1504 * Takes mode config lock.
1505 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001506 * Return an plane count and set of IDs.
1507 */
1508int drm_mode_getplane_res(struct drm_device *dev, void *data,
1509 struct drm_file *file_priv)
1510{
1511 struct drm_mode_get_plane_res *plane_resp = data;
1512 struct drm_mode_config *config;
1513 struct drm_plane *plane;
1514 uint32_t __user *plane_ptr;
1515 int copied = 0, ret = 0;
1516
1517 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1518 return -EINVAL;
1519
1520 mutex_lock(&dev->mode_config.mutex);
1521 config = &dev->mode_config;
1522
1523 /*
1524 * This ioctl is called twice, once to determine how much space is
1525 * needed, and the 2nd time to fill it.
1526 */
1527 if (config->num_plane &&
1528 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001529 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001530
1531 list_for_each_entry(plane, &config->plane_list, head) {
1532 if (put_user(plane->base.id, plane_ptr + copied)) {
1533 ret = -EFAULT;
1534 goto out;
1535 }
1536 copied++;
1537 }
1538 }
1539 plane_resp->count_planes = config->num_plane;
1540
1541out:
1542 mutex_unlock(&dev->mode_config.mutex);
1543 return ret;
1544}
1545
1546/**
1547 * drm_mode_getplane - get plane info
1548 * @dev: DRM device
1549 * @data: ioctl data
1550 * @file_priv: DRM file info
1551 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001552 * LOCKING:
1553 * Takes mode config lock.
1554 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001555 * Return plane info, including formats supported, gamma size, any
1556 * current fb, etc.
1557 */
1558int drm_mode_getplane(struct drm_device *dev, void *data,
1559 struct drm_file *file_priv)
1560{
1561 struct drm_mode_get_plane *plane_resp = data;
1562 struct drm_mode_object *obj;
1563 struct drm_plane *plane;
1564 uint32_t __user *format_ptr;
1565 int ret = 0;
1566
1567 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1568 return -EINVAL;
1569
1570 mutex_lock(&dev->mode_config.mutex);
1571 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1572 DRM_MODE_OBJECT_PLANE);
1573 if (!obj) {
1574 ret = -ENOENT;
1575 goto out;
1576 }
1577 plane = obj_to_plane(obj);
1578
1579 if (plane->crtc)
1580 plane_resp->crtc_id = plane->crtc->base.id;
1581 else
1582 plane_resp->crtc_id = 0;
1583
1584 if (plane->fb)
1585 plane_resp->fb_id = plane->fb->base.id;
1586 else
1587 plane_resp->fb_id = 0;
1588
1589 plane_resp->plane_id = plane->base.id;
1590 plane_resp->possible_crtcs = plane->possible_crtcs;
1591 plane_resp->gamma_size = plane->gamma_size;
1592
1593 /*
1594 * This ioctl is called twice, once to determine how much space is
1595 * needed, and the 2nd time to fill it.
1596 */
1597 if (plane->format_count &&
1598 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001599 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001600 if (copy_to_user(format_ptr,
1601 plane->format_types,
1602 sizeof(uint32_t) * plane->format_count)) {
1603 ret = -EFAULT;
1604 goto out;
1605 }
1606 }
1607 plane_resp->count_format_types = plane->format_count;
1608
1609out:
1610 mutex_unlock(&dev->mode_config.mutex);
1611 return ret;
1612}
1613
1614/**
1615 * drm_mode_setplane - set up or tear down an plane
1616 * @dev: DRM device
1617 * @data: ioctl data*
1618 * @file_prive: DRM file info
1619 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001620 * LOCKING:
1621 * Takes mode config lock.
1622 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001623 * Set plane info, including placement, fb, scaling, and other factors.
1624 * Or pass a NULL fb to disable.
1625 */
1626int drm_mode_setplane(struct drm_device *dev, void *data,
1627 struct drm_file *file_priv)
1628{
1629 struct drm_mode_set_plane *plane_req = data;
1630 struct drm_mode_object *obj;
1631 struct drm_plane *plane;
1632 struct drm_crtc *crtc;
1633 struct drm_framebuffer *fb;
1634 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001635 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001636 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001637
1638 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1639 return -EINVAL;
1640
1641 mutex_lock(&dev->mode_config.mutex);
1642
1643 /*
1644 * First, find the plane, crtc, and fb objects. If not available,
1645 * we don't bother to call the driver.
1646 */
1647 obj = drm_mode_object_find(dev, plane_req->plane_id,
1648 DRM_MODE_OBJECT_PLANE);
1649 if (!obj) {
1650 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1651 plane_req->plane_id);
1652 ret = -ENOENT;
1653 goto out;
1654 }
1655 plane = obj_to_plane(obj);
1656
1657 /* No fb means shut it down */
1658 if (!plane_req->fb_id) {
1659 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001660 plane->crtc = NULL;
1661 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001662 goto out;
1663 }
1664
1665 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1666 DRM_MODE_OBJECT_CRTC);
1667 if (!obj) {
1668 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1669 plane_req->crtc_id);
1670 ret = -ENOENT;
1671 goto out;
1672 }
1673 crtc = obj_to_crtc(obj);
1674
1675 obj = drm_mode_object_find(dev, plane_req->fb_id,
1676 DRM_MODE_OBJECT_FB);
1677 if (!obj) {
1678 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1679 plane_req->fb_id);
1680 ret = -ENOENT;
1681 goto out;
1682 }
1683 fb = obj_to_fb(obj);
1684
Ville Syrjälä62443be2011-12-20 00:06:47 +02001685 /* Check whether this plane supports the fb pixel format. */
1686 for (i = 0; i < plane->format_count; i++)
1687 if (fb->pixel_format == plane->format_types[i])
1688 break;
1689 if (i == plane->format_count) {
1690 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1691 ret = -EINVAL;
1692 goto out;
1693 }
1694
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001695 fb_width = fb->width << 16;
1696 fb_height = fb->height << 16;
1697
1698 /* Make sure source coordinates are inside the fb. */
1699 if (plane_req->src_w > fb_width ||
1700 plane_req->src_x > fb_width - plane_req->src_w ||
1701 plane_req->src_h > fb_height ||
1702 plane_req->src_y > fb_height - plane_req->src_h) {
1703 DRM_DEBUG_KMS("Invalid source coordinates "
1704 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1705 plane_req->src_w >> 16,
1706 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1707 plane_req->src_h >> 16,
1708 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1709 plane_req->src_x >> 16,
1710 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1711 plane_req->src_y >> 16,
1712 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1713 ret = -ENOSPC;
1714 goto out;
1715 }
1716
Ville Syrjälä687a0402011-12-20 00:06:45 +02001717 /* Give drivers some help against integer overflows */
1718 if (plane_req->crtc_w > INT_MAX ||
1719 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1720 plane_req->crtc_h > INT_MAX ||
1721 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1722 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1723 plane_req->crtc_w, plane_req->crtc_h,
1724 plane_req->crtc_x, plane_req->crtc_y);
1725 ret = -ERANGE;
1726 goto out;
1727 }
1728
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001729 ret = plane->funcs->update_plane(plane, crtc, fb,
1730 plane_req->crtc_x, plane_req->crtc_y,
1731 plane_req->crtc_w, plane_req->crtc_h,
1732 plane_req->src_x, plane_req->src_y,
1733 plane_req->src_w, plane_req->src_h);
1734 if (!ret) {
1735 plane->crtc = crtc;
1736 plane->fb = fb;
1737 }
1738
1739out:
1740 mutex_unlock(&dev->mode_config.mutex);
1741
1742 return ret;
1743}
1744
1745/**
Dave Airlief453ba02008-11-07 14:05:41 -08001746 * drm_mode_setcrtc - set CRTC configuration
1747 * @inode: inode from the ioctl
1748 * @filp: file * from the ioctl
1749 * @cmd: cmd from ioctl
1750 * @arg: arg from ioctl
1751 *
1752 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001753 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001754 *
1755 * Build a new CRTC configuration based on user request.
1756 *
1757 * Called by the user via ioctl.
1758 *
1759 * RETURNS:
1760 * Zero on success, errno on failure.
1761 */
1762int drm_mode_setcrtc(struct drm_device *dev, void *data,
1763 struct drm_file *file_priv)
1764{
1765 struct drm_mode_config *config = &dev->mode_config;
1766 struct drm_mode_crtc *crtc_req = data;
1767 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001768 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001769 struct drm_connector **connector_set = NULL, *connector;
1770 struct drm_framebuffer *fb = NULL;
1771 struct drm_display_mode *mode = NULL;
1772 struct drm_mode_set set;
1773 uint32_t __user *set_connectors_ptr;
1774 int ret = 0;
1775 int i;
1776
Dave Airliefb3b06c2011-02-08 13:55:21 +10001777 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1778 return -EINVAL;
1779
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001780 /* For some reason crtc x/y offsets are signed internally. */
1781 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1782 return -ERANGE;
1783
Dave Airlief453ba02008-11-07 14:05:41 -08001784 mutex_lock(&dev->mode_config.mutex);
1785 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1786 DRM_MODE_OBJECT_CRTC);
1787 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001788 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001789 ret = -EINVAL;
1790 goto out;
1791 }
1792 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001793 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001794
1795 if (crtc_req->mode_valid) {
1796 /* If we have a mode we need a framebuffer. */
1797 /* If we pass -1, set the mode with the currently bound fb */
1798 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001799 if (!crtc->fb) {
1800 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1801 ret = -EINVAL;
1802 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001803 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001804 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001805 } else {
1806 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1807 DRM_MODE_OBJECT_FB);
1808 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001809 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1810 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001811 ret = -EINVAL;
1812 goto out;
1813 }
1814 fb = obj_to_fb(obj);
1815 }
1816
1817 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001818 if (!mode) {
1819 ret = -ENOMEM;
1820 goto out;
1821 }
1822
Dave Airlief453ba02008-11-07 14:05:41 -08001823 drm_crtc_convert_umode(mode, &crtc_req->mode);
1824 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1825 }
1826
1827 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001828 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001829 ret = -EINVAL;
1830 goto out;
1831 }
1832
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001833 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001834 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001835 crtc_req->count_connectors);
1836 ret = -EINVAL;
1837 goto out;
1838 }
1839
1840 if (crtc_req->count_connectors > 0) {
1841 u32 out_id;
1842
1843 /* Avoid unbounded kernel memory allocation */
1844 if (crtc_req->count_connectors > config->num_connector) {
1845 ret = -EINVAL;
1846 goto out;
1847 }
1848
1849 connector_set = kmalloc(crtc_req->count_connectors *
1850 sizeof(struct drm_connector *),
1851 GFP_KERNEL);
1852 if (!connector_set) {
1853 ret = -ENOMEM;
1854 goto out;
1855 }
1856
1857 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001858 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001859 if (get_user(out_id, &set_connectors_ptr[i])) {
1860 ret = -EFAULT;
1861 goto out;
1862 }
1863
1864 obj = drm_mode_object_find(dev, out_id,
1865 DRM_MODE_OBJECT_CONNECTOR);
1866 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001867 DRM_DEBUG_KMS("Connector id %d unknown\n",
1868 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001869 ret = -EINVAL;
1870 goto out;
1871 }
1872 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001873 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1874 connector->base.id,
1875 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001876
1877 connector_set[i] = connector;
1878 }
1879 }
1880
1881 set.crtc = crtc;
1882 set.x = crtc_req->x;
1883 set.y = crtc_req->y;
1884 set.mode = mode;
1885 set.connectors = connector_set;
1886 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001887 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001888 ret = crtc->funcs->set_config(&set);
1889
1890out:
1891 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001892 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001893 mutex_unlock(&dev->mode_config.mutex);
1894 return ret;
1895}
1896
1897int drm_mode_cursor_ioctl(struct drm_device *dev,
1898 void *data, struct drm_file *file_priv)
1899{
1900 struct drm_mode_cursor *req = data;
1901 struct drm_mode_object *obj;
1902 struct drm_crtc *crtc;
1903 int ret = 0;
1904
Dave Airliefb3b06c2011-02-08 13:55:21 +10001905 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1906 return -EINVAL;
1907
Jesse Barnesacb4b992011-11-07 12:03:23 -08001908 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001909 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001910
1911 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001912 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001913 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001914 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001915 ret = -EINVAL;
1916 goto out;
1917 }
1918 crtc = obj_to_crtc(obj);
1919
1920 if (req->flags & DRM_MODE_CURSOR_BO) {
1921 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001922 ret = -ENXIO;
1923 goto out;
1924 }
1925 /* Turns off the cursor if handle is 0 */
1926 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1927 req->width, req->height);
1928 }
1929
1930 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1931 if (crtc->funcs->cursor_move) {
1932 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1933 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08001934 ret = -EFAULT;
1935 goto out;
1936 }
1937 }
1938out:
1939 mutex_unlock(&dev->mode_config.mutex);
1940 return ret;
1941}
1942
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001943/* Original addfb only supported RGB formats, so figure out which one */
1944uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1945{
1946 uint32_t fmt;
1947
1948 switch (bpp) {
1949 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001950 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001951 break;
1952 case 16:
1953 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001954 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001955 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001956 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001957 break;
1958 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001959 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001960 break;
1961 case 32:
1962 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001963 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001964 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001965 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001966 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001967 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001968 break;
1969 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001970 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1971 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001972 break;
1973 }
1974
1975 return fmt;
1976}
1977EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1978
Dave Airlief453ba02008-11-07 14:05:41 -08001979/**
1980 * drm_mode_addfb - add an FB to the graphics configuration
1981 * @inode: inode from the ioctl
1982 * @filp: file * from the ioctl
1983 * @cmd: cmd from ioctl
1984 * @arg: arg from ioctl
1985 *
1986 * LOCKING:
1987 * Takes mode config lock.
1988 *
1989 * Add a new FB to the specified CRTC, given a user request.
1990 *
1991 * Called by the user via ioctl.
1992 *
1993 * RETURNS:
1994 * Zero on success, errno on failure.
1995 */
1996int drm_mode_addfb(struct drm_device *dev,
1997 void *data, struct drm_file *file_priv)
1998{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001999 struct drm_mode_fb_cmd *or = data;
2000 struct drm_mode_fb_cmd2 r = {};
2001 struct drm_mode_config *config = &dev->mode_config;
2002 struct drm_framebuffer *fb;
2003 int ret = 0;
2004
2005 /* Use new struct with format internally */
2006 r.fb_id = or->fb_id;
2007 r.width = or->width;
2008 r.height = or->height;
2009 r.pitches[0] = or->pitch;
2010 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2011 r.handles[0] = or->handle;
2012
2013 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2014 return -EINVAL;
2015
Jesse Barnesacb4b992011-11-07 12:03:23 -08002016 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002017 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002018
2019 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002020 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002021
2022 mutex_lock(&dev->mode_config.mutex);
2023
2024 /* TODO check buffer is sufficiently large */
2025 /* TODO setup destructor callback */
2026
2027 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2028 if (IS_ERR(fb)) {
2029 DRM_ERROR("could not create framebuffer\n");
2030 ret = PTR_ERR(fb);
2031 goto out;
2032 }
2033
2034 or->fb_id = fb->base.id;
2035 list_add(&fb->filp_head, &file_priv->fbs);
2036 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2037
2038out:
2039 mutex_unlock(&dev->mode_config.mutex);
2040 return ret;
2041}
2042
Ville Syrjälä935b5972011-12-20 00:06:48 +02002043static int format_check(struct drm_mode_fb_cmd2 *r)
2044{
2045 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2046
2047 switch (format) {
2048 case DRM_FORMAT_C8:
2049 case DRM_FORMAT_RGB332:
2050 case DRM_FORMAT_BGR233:
2051 case DRM_FORMAT_XRGB4444:
2052 case DRM_FORMAT_XBGR4444:
2053 case DRM_FORMAT_RGBX4444:
2054 case DRM_FORMAT_BGRX4444:
2055 case DRM_FORMAT_ARGB4444:
2056 case DRM_FORMAT_ABGR4444:
2057 case DRM_FORMAT_RGBA4444:
2058 case DRM_FORMAT_BGRA4444:
2059 case DRM_FORMAT_XRGB1555:
2060 case DRM_FORMAT_XBGR1555:
2061 case DRM_FORMAT_RGBX5551:
2062 case DRM_FORMAT_BGRX5551:
2063 case DRM_FORMAT_ARGB1555:
2064 case DRM_FORMAT_ABGR1555:
2065 case DRM_FORMAT_RGBA5551:
2066 case DRM_FORMAT_BGRA5551:
2067 case DRM_FORMAT_RGB565:
2068 case DRM_FORMAT_BGR565:
2069 case DRM_FORMAT_RGB888:
2070 case DRM_FORMAT_BGR888:
2071 case DRM_FORMAT_XRGB8888:
2072 case DRM_FORMAT_XBGR8888:
2073 case DRM_FORMAT_RGBX8888:
2074 case DRM_FORMAT_BGRX8888:
2075 case DRM_FORMAT_ARGB8888:
2076 case DRM_FORMAT_ABGR8888:
2077 case DRM_FORMAT_RGBA8888:
2078 case DRM_FORMAT_BGRA8888:
2079 case DRM_FORMAT_XRGB2101010:
2080 case DRM_FORMAT_XBGR2101010:
2081 case DRM_FORMAT_RGBX1010102:
2082 case DRM_FORMAT_BGRX1010102:
2083 case DRM_FORMAT_ARGB2101010:
2084 case DRM_FORMAT_ABGR2101010:
2085 case DRM_FORMAT_RGBA1010102:
2086 case DRM_FORMAT_BGRA1010102:
2087 case DRM_FORMAT_YUYV:
2088 case DRM_FORMAT_YVYU:
2089 case DRM_FORMAT_UYVY:
2090 case DRM_FORMAT_VYUY:
2091 case DRM_FORMAT_AYUV:
2092 case DRM_FORMAT_NV12:
2093 case DRM_FORMAT_NV21:
2094 case DRM_FORMAT_NV16:
2095 case DRM_FORMAT_NV61:
2096 case DRM_FORMAT_YUV410:
2097 case DRM_FORMAT_YVU410:
2098 case DRM_FORMAT_YUV411:
2099 case DRM_FORMAT_YVU411:
2100 case DRM_FORMAT_YUV420:
2101 case DRM_FORMAT_YVU420:
2102 case DRM_FORMAT_YUV422:
2103 case DRM_FORMAT_YVU422:
2104 case DRM_FORMAT_YUV444:
2105 case DRM_FORMAT_YVU444:
2106 return 0;
2107 default:
2108 return -EINVAL;
2109 }
2110}
2111
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002112/**
2113 * drm_mode_addfb2 - add an FB to the graphics configuration
2114 * @inode: inode from the ioctl
2115 * @filp: file * from the ioctl
2116 * @cmd: cmd from ioctl
2117 * @arg: arg from ioctl
2118 *
2119 * LOCKING:
2120 * Takes mode config lock.
2121 *
2122 * Add a new FB to the specified CRTC, given a user request with format.
2123 *
2124 * Called by the user via ioctl.
2125 *
2126 * RETURNS:
2127 * Zero on success, errno on failure.
2128 */
2129int drm_mode_addfb2(struct drm_device *dev,
2130 void *data, struct drm_file *file_priv)
2131{
2132 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002133 struct drm_mode_config *config = &dev->mode_config;
2134 struct drm_framebuffer *fb;
2135 int ret = 0;
2136
Dave Airliefb3b06c2011-02-08 13:55:21 +10002137 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2138 return -EINVAL;
2139
Dave Airlief453ba02008-11-07 14:05:41 -08002140 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002141 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2142 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002143 return -EINVAL;
2144 }
2145 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002146 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2147 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002148 return -EINVAL;
2149 }
2150
Ville Syrjälä935b5972011-12-20 00:06:48 +02002151 ret = format_check(r);
2152 if (ret) {
2153 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2154 return ret;
2155 }
2156
Dave Airlief453ba02008-11-07 14:05:41 -08002157 mutex_lock(&dev->mode_config.mutex);
2158
Dave Airlief453ba02008-11-07 14:05:41 -08002159 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002160 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002161 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002162 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002163 goto out;
2164 }
2165
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002166 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002167 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002168 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002169
2170out:
2171 mutex_unlock(&dev->mode_config.mutex);
2172 return ret;
2173}
2174
2175/**
2176 * drm_mode_rmfb - remove an FB from the configuration
2177 * @inode: inode from the ioctl
2178 * @filp: file * from the ioctl
2179 * @cmd: cmd from ioctl
2180 * @arg: arg from ioctl
2181 *
2182 * LOCKING:
2183 * Takes mode config lock.
2184 *
2185 * Remove the FB specified by the user.
2186 *
2187 * Called by the user via ioctl.
2188 *
2189 * RETURNS:
2190 * Zero on success, errno on failure.
2191 */
2192int drm_mode_rmfb(struct drm_device *dev,
2193 void *data, struct drm_file *file_priv)
2194{
2195 struct drm_mode_object *obj;
2196 struct drm_framebuffer *fb = NULL;
2197 struct drm_framebuffer *fbl = NULL;
2198 uint32_t *id = data;
2199 int ret = 0;
2200 int found = 0;
2201
Dave Airliefb3b06c2011-02-08 13:55:21 +10002202 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2203 return -EINVAL;
2204
Dave Airlief453ba02008-11-07 14:05:41 -08002205 mutex_lock(&dev->mode_config.mutex);
2206 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002207 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002208 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002209 ret = -EINVAL;
2210 goto out;
2211 }
2212 fb = obj_to_fb(obj);
2213
2214 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2215 if (fb == fbl)
2216 found = 1;
2217
2218 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002219 ret = -EINVAL;
2220 goto out;
2221 }
2222
2223 /* TODO release all crtc connected to the framebuffer */
2224 /* TODO unhock the destructor from the buffer object */
2225
2226 list_del(&fb->filp_head);
2227 fb->funcs->destroy(fb);
2228
2229out:
2230 mutex_unlock(&dev->mode_config.mutex);
2231 return ret;
2232}
2233
2234/**
2235 * drm_mode_getfb - get FB info
2236 * @inode: inode from the ioctl
2237 * @filp: file * from the ioctl
2238 * @cmd: cmd from ioctl
2239 * @arg: arg from ioctl
2240 *
2241 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002242 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002243 *
2244 * Lookup the FB given its ID and return info about it.
2245 *
2246 * Called by the user via ioctl.
2247 *
2248 * RETURNS:
2249 * Zero on success, errno on failure.
2250 */
2251int drm_mode_getfb(struct drm_device *dev,
2252 void *data, struct drm_file *file_priv)
2253{
2254 struct drm_mode_fb_cmd *r = data;
2255 struct drm_mode_object *obj;
2256 struct drm_framebuffer *fb;
2257 int ret = 0;
2258
Dave Airliefb3b06c2011-02-08 13:55:21 +10002259 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2260 return -EINVAL;
2261
Dave Airlief453ba02008-11-07 14:05:41 -08002262 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002263 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002264 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002265 ret = -EINVAL;
2266 goto out;
2267 }
2268 fb = obj_to_fb(obj);
2269
2270 r->height = fb->height;
2271 r->width = fb->width;
2272 r->depth = fb->depth;
2273 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002274 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002275 fb->funcs->create_handle(fb, file_priv, &r->handle);
2276
2277out:
2278 mutex_unlock(&dev->mode_config.mutex);
2279 return ret;
2280}
2281
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002282int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2283 void *data, struct drm_file *file_priv)
2284{
2285 struct drm_clip_rect __user *clips_ptr;
2286 struct drm_clip_rect *clips = NULL;
2287 struct drm_mode_fb_dirty_cmd *r = data;
2288 struct drm_mode_object *obj;
2289 struct drm_framebuffer *fb;
2290 unsigned flags;
2291 int num_clips;
2292 int ret = 0;
2293
Dave Airliefb3b06c2011-02-08 13:55:21 +10002294 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2295 return -EINVAL;
2296
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002297 mutex_lock(&dev->mode_config.mutex);
2298 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2299 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002300 ret = -EINVAL;
2301 goto out_err1;
2302 }
2303 fb = obj_to_fb(obj);
2304
2305 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002306 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002307
2308 if (!num_clips != !clips_ptr) {
2309 ret = -EINVAL;
2310 goto out_err1;
2311 }
2312
2313 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2314
2315 /* If userspace annotates copy, clips must come in pairs */
2316 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2317 ret = -EINVAL;
2318 goto out_err1;
2319 }
2320
2321 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002322 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2323 ret = -EINVAL;
2324 goto out_err1;
2325 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002326 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2327 if (!clips) {
2328 ret = -ENOMEM;
2329 goto out_err1;
2330 }
2331
2332 ret = copy_from_user(clips, clips_ptr,
2333 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002334 if (ret) {
2335 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002336 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002337 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002338 }
2339
2340 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002341 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2342 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002343 } else {
2344 ret = -ENOSYS;
2345 goto out_err2;
2346 }
2347
2348out_err2:
2349 kfree(clips);
2350out_err1:
2351 mutex_unlock(&dev->mode_config.mutex);
2352 return ret;
2353}
2354
2355
Dave Airlief453ba02008-11-07 14:05:41 -08002356/**
2357 * drm_fb_release - remove and free the FBs on this file
2358 * @filp: file * from the ioctl
2359 *
2360 * LOCKING:
2361 * Takes mode config lock.
2362 *
2363 * Destroy all the FBs associated with @filp.
2364 *
2365 * Called by the user via ioctl.
2366 *
2367 * RETURNS:
2368 * Zero on success, errno on failure.
2369 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002370void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002371{
Dave Airlief453ba02008-11-07 14:05:41 -08002372 struct drm_device *dev = priv->minor->dev;
2373 struct drm_framebuffer *fb, *tfb;
2374
2375 mutex_lock(&dev->mode_config.mutex);
2376 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2377 list_del(&fb->filp_head);
2378 fb->funcs->destroy(fb);
2379 }
2380 mutex_unlock(&dev->mode_config.mutex);
2381}
2382
2383/**
2384 * drm_mode_attachmode - add a mode to the user mode list
2385 * @dev: DRM device
2386 * @connector: connector to add the mode to
2387 * @mode: mode to add
2388 *
2389 * Add @mode to @connector's user mode list.
2390 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002391static void drm_mode_attachmode(struct drm_device *dev,
2392 struct drm_connector *connector,
2393 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002394{
Dave Airlief453ba02008-11-07 14:05:41 -08002395 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002396}
2397
2398int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2399 struct drm_display_mode *mode)
2400{
2401 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -08002402 struct drm_display_mode *dup_mode;
2403 int need_dup = 0;
2404 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2405 if (!connector->encoder)
2406 break;
2407 if (connector->encoder->crtc == crtc) {
2408 if (need_dup)
2409 dup_mode = drm_mode_duplicate(dev, mode);
2410 else
2411 dup_mode = mode;
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002412 drm_mode_attachmode(dev, connector, dup_mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002413 need_dup = 1;
2414 }
2415 }
2416 return 0;
2417}
2418EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2419
2420static int drm_mode_detachmode(struct drm_device *dev,
2421 struct drm_connector *connector,
2422 struct drm_display_mode *mode)
2423{
2424 int found = 0;
2425 int ret = 0;
2426 struct drm_display_mode *match_mode, *t;
2427
2428 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2429 if (drm_mode_equal(match_mode, mode)) {
2430 list_del(&match_mode->head);
2431 drm_mode_destroy(dev, match_mode);
2432 found = 1;
2433 break;
2434 }
2435 }
2436
2437 if (!found)
2438 ret = -EINVAL;
2439
2440 return ret;
2441}
2442
2443int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2444{
2445 struct drm_connector *connector;
2446
2447 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2448 drm_mode_detachmode(dev, connector, mode);
2449 }
2450 return 0;
2451}
2452EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2453
2454/**
2455 * drm_fb_attachmode - Attach a user mode to an connector
2456 * @inode: inode from the ioctl
2457 * @filp: file * from the ioctl
2458 * @cmd: cmd from ioctl
2459 * @arg: arg from ioctl
2460 *
2461 * This attaches a user specified mode to an connector.
2462 * Called by the user via ioctl.
2463 *
2464 * RETURNS:
2465 * Zero on success, errno on failure.
2466 */
2467int drm_mode_attachmode_ioctl(struct drm_device *dev,
2468 void *data, struct drm_file *file_priv)
2469{
2470 struct drm_mode_mode_cmd *mode_cmd = data;
2471 struct drm_connector *connector;
2472 struct drm_display_mode *mode;
2473 struct drm_mode_object *obj;
2474 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2475 int ret = 0;
2476
Dave Airliefb3b06c2011-02-08 13:55:21 +10002477 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2478 return -EINVAL;
2479
Dave Airlief453ba02008-11-07 14:05:41 -08002480 mutex_lock(&dev->mode_config.mutex);
2481
2482 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2483 if (!obj) {
2484 ret = -EINVAL;
2485 goto out;
2486 }
2487 connector = obj_to_connector(obj);
2488
2489 mode = drm_mode_create(dev);
2490 if (!mode) {
2491 ret = -ENOMEM;
2492 goto out;
2493 }
2494
2495 drm_crtc_convert_umode(mode, umode);
2496
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002497 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002498out:
2499 mutex_unlock(&dev->mode_config.mutex);
2500 return ret;
2501}
2502
2503
2504/**
2505 * drm_fb_detachmode - Detach a user specified mode from an connector
2506 * @inode: inode from the ioctl
2507 * @filp: file * from the ioctl
2508 * @cmd: cmd from ioctl
2509 * @arg: arg from ioctl
2510 *
2511 * Called by the user via ioctl.
2512 *
2513 * RETURNS:
2514 * Zero on success, errno on failure.
2515 */
2516int drm_mode_detachmode_ioctl(struct drm_device *dev,
2517 void *data, struct drm_file *file_priv)
2518{
2519 struct drm_mode_object *obj;
2520 struct drm_mode_mode_cmd *mode_cmd = data;
2521 struct drm_connector *connector;
2522 struct drm_display_mode mode;
2523 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2524 int ret = 0;
2525
Dave Airliefb3b06c2011-02-08 13:55:21 +10002526 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2527 return -EINVAL;
2528
Dave Airlief453ba02008-11-07 14:05:41 -08002529 mutex_lock(&dev->mode_config.mutex);
2530
2531 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2532 if (!obj) {
2533 ret = -EINVAL;
2534 goto out;
2535 }
2536 connector = obj_to_connector(obj);
2537
2538 drm_crtc_convert_umode(&mode, umode);
2539 ret = drm_mode_detachmode(dev, connector, &mode);
2540out:
2541 mutex_unlock(&dev->mode_config.mutex);
2542 return ret;
2543}
2544
2545struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2546 const char *name, int num_values)
2547{
2548 struct drm_property *property = NULL;
2549
2550 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2551 if (!property)
2552 return NULL;
2553
2554 if (num_values) {
2555 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2556 if (!property->values)
2557 goto fail;
2558 }
2559
2560 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2561 property->flags = flags;
2562 property->num_values = num_values;
2563 INIT_LIST_HEAD(&property->enum_blob_list);
2564
Vinson Lee471dd2e2011-11-10 11:55:40 -08002565 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002566 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002567 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2568 }
Dave Airlief453ba02008-11-07 14:05:41 -08002569
2570 list_add_tail(&property->head, &dev->mode_config.property_list);
2571 return property;
2572fail:
2573 kfree(property);
2574 return NULL;
2575}
2576EXPORT_SYMBOL(drm_property_create);
2577
Sascha Hauer4a67d392012-02-06 10:58:17 +01002578struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2579 const char *name,
2580 const struct drm_prop_enum_list *props,
2581 int num_values)
2582{
2583 struct drm_property *property;
2584 int i, ret;
2585
2586 flags |= DRM_MODE_PROP_ENUM;
2587
2588 property = drm_property_create(dev, flags, name, num_values);
2589 if (!property)
2590 return NULL;
2591
2592 for (i = 0; i < num_values; i++) {
2593 ret = drm_property_add_enum(property, i,
2594 props[i].type,
2595 props[i].name);
2596 if (ret) {
2597 drm_property_destroy(dev, property);
2598 return NULL;
2599 }
2600 }
2601
2602 return property;
2603}
2604EXPORT_SYMBOL(drm_property_create_enum);
2605
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002606struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2607 const char *name,
2608 uint64_t min, uint64_t max)
2609{
2610 struct drm_property *property;
2611
2612 flags |= DRM_MODE_PROP_RANGE;
2613
2614 property = drm_property_create(dev, flags, name, 2);
2615 if (!property)
2616 return NULL;
2617
2618 property->values[0] = min;
2619 property->values[1] = max;
2620
2621 return property;
2622}
2623EXPORT_SYMBOL(drm_property_create_range);
2624
Dave Airlief453ba02008-11-07 14:05:41 -08002625int drm_property_add_enum(struct drm_property *property, int index,
2626 uint64_t value, const char *name)
2627{
2628 struct drm_property_enum *prop_enum;
2629
2630 if (!(property->flags & DRM_MODE_PROP_ENUM))
2631 return -EINVAL;
2632
2633 if (!list_empty(&property->enum_blob_list)) {
2634 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2635 if (prop_enum->value == value) {
2636 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2637 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2638 return 0;
2639 }
2640 }
2641 }
2642
2643 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2644 if (!prop_enum)
2645 return -ENOMEM;
2646
2647 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2648 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2649 prop_enum->value = value;
2650
2651 property->values[index] = value;
2652 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2653 return 0;
2654}
2655EXPORT_SYMBOL(drm_property_add_enum);
2656
2657void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2658{
2659 struct drm_property_enum *prop_enum, *pt;
2660
2661 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2662 list_del(&prop_enum->head);
2663 kfree(prop_enum);
2664 }
2665
2666 if (property->num_values)
2667 kfree(property->values);
2668 drm_mode_object_put(dev, &property->base);
2669 list_del(&property->head);
2670 kfree(property);
2671}
2672EXPORT_SYMBOL(drm_property_destroy);
2673
2674int drm_connector_attach_property(struct drm_connector *connector,
2675 struct drm_property *property, uint64_t init_val)
2676{
2677 int i;
2678
2679 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2680 if (connector->property_ids[i] == 0) {
2681 connector->property_ids[i] = property->base.id;
2682 connector->property_values[i] = init_val;
2683 break;
2684 }
2685 }
2686
2687 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2688 return -EINVAL;
2689 return 0;
2690}
2691EXPORT_SYMBOL(drm_connector_attach_property);
2692
2693int drm_connector_property_set_value(struct drm_connector *connector,
2694 struct drm_property *property, uint64_t value)
2695{
2696 int i;
2697
2698 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2699 if (connector->property_ids[i] == property->base.id) {
2700 connector->property_values[i] = value;
2701 break;
2702 }
2703 }
2704
2705 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2706 return -EINVAL;
2707 return 0;
2708}
2709EXPORT_SYMBOL(drm_connector_property_set_value);
2710
2711int drm_connector_property_get_value(struct drm_connector *connector,
2712 struct drm_property *property, uint64_t *val)
2713{
2714 int i;
2715
2716 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2717 if (connector->property_ids[i] == property->base.id) {
2718 *val = connector->property_values[i];
2719 break;
2720 }
2721 }
2722
2723 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2724 return -EINVAL;
2725 return 0;
2726}
2727EXPORT_SYMBOL(drm_connector_property_get_value);
2728
2729int drm_mode_getproperty_ioctl(struct drm_device *dev,
2730 void *data, struct drm_file *file_priv)
2731{
2732 struct drm_mode_object *obj;
2733 struct drm_mode_get_property *out_resp = data;
2734 struct drm_property *property;
2735 int enum_count = 0;
2736 int blob_count = 0;
2737 int value_count = 0;
2738 int ret = 0, i;
2739 int copied;
2740 struct drm_property_enum *prop_enum;
2741 struct drm_mode_property_enum __user *enum_ptr;
2742 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002743 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002744 uint64_t __user *values_ptr;
2745 uint32_t __user *blob_length_ptr;
2746
Dave Airliefb3b06c2011-02-08 13:55:21 +10002747 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2748 return -EINVAL;
2749
Dave Airlief453ba02008-11-07 14:05:41 -08002750 mutex_lock(&dev->mode_config.mutex);
2751 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2752 if (!obj) {
2753 ret = -EINVAL;
2754 goto done;
2755 }
2756 property = obj_to_property(obj);
2757
2758 if (property->flags & DRM_MODE_PROP_ENUM) {
2759 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2760 enum_count++;
2761 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2762 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2763 blob_count++;
2764 }
2765
2766 value_count = property->num_values;
2767
2768 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2769 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2770 out_resp->flags = property->flags;
2771
2772 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002773 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002774 for (i = 0; i < value_count; i++) {
2775 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2776 ret = -EFAULT;
2777 goto done;
2778 }
2779 }
2780 }
2781 out_resp->count_values = value_count;
2782
2783 if (property->flags & DRM_MODE_PROP_ENUM) {
2784 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2785 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002786 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002787 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2788
2789 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2790 ret = -EFAULT;
2791 goto done;
2792 }
2793
2794 if (copy_to_user(&enum_ptr[copied].name,
2795 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2796 ret = -EFAULT;
2797 goto done;
2798 }
2799 copied++;
2800 }
2801 }
2802 out_resp->count_enum_blobs = enum_count;
2803 }
2804
2805 if (property->flags & DRM_MODE_PROP_BLOB) {
2806 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2807 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002808 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2809 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002810
2811 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2812 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2813 ret = -EFAULT;
2814 goto done;
2815 }
2816
2817 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2818 ret = -EFAULT;
2819 goto done;
2820 }
2821
2822 copied++;
2823 }
2824 }
2825 out_resp->count_enum_blobs = blob_count;
2826 }
2827done:
2828 mutex_unlock(&dev->mode_config.mutex);
2829 return ret;
2830}
2831
2832static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2833 void *data)
2834{
2835 struct drm_property_blob *blob;
2836
2837 if (!length || !data)
2838 return NULL;
2839
2840 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2841 if (!blob)
2842 return NULL;
2843
2844 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2845 blob->length = length;
2846
2847 memcpy(blob->data, data, length);
2848
2849 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2850
2851 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2852 return blob;
2853}
2854
2855static void drm_property_destroy_blob(struct drm_device *dev,
2856 struct drm_property_blob *blob)
2857{
2858 drm_mode_object_put(dev, &blob->base);
2859 list_del(&blob->head);
2860 kfree(blob);
2861}
2862
2863int drm_mode_getblob_ioctl(struct drm_device *dev,
2864 void *data, struct drm_file *file_priv)
2865{
2866 struct drm_mode_object *obj;
2867 struct drm_mode_get_blob *out_resp = data;
2868 struct drm_property_blob *blob;
2869 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002870 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002871
Dave Airliefb3b06c2011-02-08 13:55:21 +10002872 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2873 return -EINVAL;
2874
Dave Airlief453ba02008-11-07 14:05:41 -08002875 mutex_lock(&dev->mode_config.mutex);
2876 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2877 if (!obj) {
2878 ret = -EINVAL;
2879 goto done;
2880 }
2881 blob = obj_to_blob(obj);
2882
2883 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002884 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002885 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2886 ret = -EFAULT;
2887 goto done;
2888 }
2889 }
2890 out_resp->length = blob->length;
2891
2892done:
2893 mutex_unlock(&dev->mode_config.mutex);
2894 return ret;
2895}
2896
2897int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2898 struct edid *edid)
2899{
2900 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002901 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002902
2903 if (connector->edid_blob_ptr)
2904 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2905
2906 /* Delete edid, when there is none. */
2907 if (!edid) {
2908 connector->edid_blob_ptr = NULL;
2909 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2910 return ret;
2911 }
2912
Adam Jackson7466f4c2010-03-29 21:43:23 +00002913 size = EDID_LENGTH * (1 + edid->extensions);
2914 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2915 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002916
2917 ret = drm_connector_property_set_value(connector,
2918 dev->mode_config.edid_property,
2919 connector->edid_blob_ptr->base.id);
2920
2921 return ret;
2922}
2923EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2924
2925int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2926 void *data, struct drm_file *file_priv)
2927{
2928 struct drm_mode_connector_set_property *out_resp = data;
2929 struct drm_mode_object *obj;
2930 struct drm_property *property;
2931 struct drm_connector *connector;
2932 int ret = -EINVAL;
2933 int i;
2934
Dave Airliefb3b06c2011-02-08 13:55:21 +10002935 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2936 return -EINVAL;
2937
Dave Airlief453ba02008-11-07 14:05:41 -08002938 mutex_lock(&dev->mode_config.mutex);
2939
2940 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2941 if (!obj) {
2942 goto out;
2943 }
2944 connector = obj_to_connector(obj);
2945
2946 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2947 if (connector->property_ids[i] == out_resp->prop_id)
2948 break;
2949 }
2950
2951 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2952 goto out;
2953 }
2954
2955 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2956 if (!obj) {
2957 goto out;
2958 }
2959 property = obj_to_property(obj);
2960
2961 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2962 goto out;
2963
2964 if (property->flags & DRM_MODE_PROP_RANGE) {
2965 if (out_resp->value < property->values[0])
2966 goto out;
2967
2968 if (out_resp->value > property->values[1])
2969 goto out;
2970 } else {
2971 int found = 0;
2972 for (i = 0; i < property->num_values; i++) {
2973 if (property->values[i] == out_resp->value) {
2974 found = 1;
2975 break;
2976 }
2977 }
2978 if (!found) {
2979 goto out;
2980 }
2981 }
2982
Keith Packardc9fb15f2009-05-30 20:42:28 -07002983 /* Do DPMS ourselves */
2984 if (property == connector->dev->mode_config.dpms_property) {
2985 if (connector->funcs->dpms)
2986 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2987 ret = 0;
2988 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002989 ret = connector->funcs->set_property(connector, property, out_resp->value);
2990
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002991 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002992 if (!ret)
2993 drm_connector_property_set_value(connector, property, out_resp->value);
2994out:
2995 mutex_unlock(&dev->mode_config.mutex);
2996 return ret;
2997}
2998
Dave Airlief453ba02008-11-07 14:05:41 -08002999int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3000 struct drm_encoder *encoder)
3001{
3002 int i;
3003
3004 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3005 if (connector->encoder_ids[i] == 0) {
3006 connector->encoder_ids[i] = encoder->base.id;
3007 return 0;
3008 }
3009 }
3010 return -ENOMEM;
3011}
3012EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3013
3014void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3015 struct drm_encoder *encoder)
3016{
3017 int i;
3018 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3019 if (connector->encoder_ids[i] == encoder->base.id) {
3020 connector->encoder_ids[i] = 0;
3021 if (connector->encoder == encoder)
3022 connector->encoder = NULL;
3023 break;
3024 }
3025 }
3026}
3027EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3028
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003029int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003030 int gamma_size)
3031{
3032 crtc->gamma_size = gamma_size;
3033
3034 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3035 if (!crtc->gamma_store) {
3036 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003037 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003038 }
3039
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003040 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003041}
3042EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3043
3044int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3045 void *data, struct drm_file *file_priv)
3046{
3047 struct drm_mode_crtc_lut *crtc_lut = data;
3048 struct drm_mode_object *obj;
3049 struct drm_crtc *crtc;
3050 void *r_base, *g_base, *b_base;
3051 int size;
3052 int ret = 0;
3053
Dave Airliefb3b06c2011-02-08 13:55:21 +10003054 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3055 return -EINVAL;
3056
Dave Airlief453ba02008-11-07 14:05:41 -08003057 mutex_lock(&dev->mode_config.mutex);
3058 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3059 if (!obj) {
3060 ret = -EINVAL;
3061 goto out;
3062 }
3063 crtc = obj_to_crtc(obj);
3064
3065 /* memcpy into gamma store */
3066 if (crtc_lut->gamma_size != crtc->gamma_size) {
3067 ret = -EINVAL;
3068 goto out;
3069 }
3070
3071 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3072 r_base = crtc->gamma_store;
3073 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3074 ret = -EFAULT;
3075 goto out;
3076 }
3077
3078 g_base = r_base + size;
3079 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3080 ret = -EFAULT;
3081 goto out;
3082 }
3083
3084 b_base = g_base + size;
3085 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3086 ret = -EFAULT;
3087 goto out;
3088 }
3089
James Simmons72034252010-08-03 01:33:19 +01003090 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003091
3092out:
3093 mutex_unlock(&dev->mode_config.mutex);
3094 return ret;
3095
3096}
3097
3098int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3099 void *data, struct drm_file *file_priv)
3100{
3101 struct drm_mode_crtc_lut *crtc_lut = data;
3102 struct drm_mode_object *obj;
3103 struct drm_crtc *crtc;
3104 void *r_base, *g_base, *b_base;
3105 int size;
3106 int ret = 0;
3107
Dave Airliefb3b06c2011-02-08 13:55:21 +10003108 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3109 return -EINVAL;
3110
Dave Airlief453ba02008-11-07 14:05:41 -08003111 mutex_lock(&dev->mode_config.mutex);
3112 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3113 if (!obj) {
3114 ret = -EINVAL;
3115 goto out;
3116 }
3117 crtc = obj_to_crtc(obj);
3118
3119 /* memcpy into gamma store */
3120 if (crtc_lut->gamma_size != crtc->gamma_size) {
3121 ret = -EINVAL;
3122 goto out;
3123 }
3124
3125 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3126 r_base = crtc->gamma_store;
3127 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3128 ret = -EFAULT;
3129 goto out;
3130 }
3131
3132 g_base = r_base + size;
3133 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3134 ret = -EFAULT;
3135 goto out;
3136 }
3137
3138 b_base = g_base + size;
3139 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3140 ret = -EFAULT;
3141 goto out;
3142 }
3143out:
3144 mutex_unlock(&dev->mode_config.mutex);
3145 return ret;
3146}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003147
3148int drm_mode_page_flip_ioctl(struct drm_device *dev,
3149 void *data, struct drm_file *file_priv)
3150{
3151 struct drm_mode_crtc_page_flip *page_flip = data;
3152 struct drm_mode_object *obj;
3153 struct drm_crtc *crtc;
3154 struct drm_framebuffer *fb;
3155 struct drm_pending_vblank_event *e = NULL;
3156 unsigned long flags;
3157 int ret = -EINVAL;
3158
3159 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3160 page_flip->reserved != 0)
3161 return -EINVAL;
3162
3163 mutex_lock(&dev->mode_config.mutex);
3164 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3165 if (!obj)
3166 goto out;
3167 crtc = obj_to_crtc(obj);
3168
Chris Wilson90c1efd2010-07-17 20:23:26 +01003169 if (crtc->fb == NULL) {
3170 /* The framebuffer is currently unbound, presumably
3171 * due to a hotplug event, that userspace has not
3172 * yet discovered.
3173 */
3174 ret = -EBUSY;
3175 goto out;
3176 }
3177
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003178 if (crtc->funcs->page_flip == NULL)
3179 goto out;
3180
3181 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3182 if (!obj)
3183 goto out;
3184 fb = obj_to_fb(obj);
3185
3186 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3187 ret = -ENOMEM;
3188 spin_lock_irqsave(&dev->event_lock, flags);
3189 if (file_priv->event_space < sizeof e->event) {
3190 spin_unlock_irqrestore(&dev->event_lock, flags);
3191 goto out;
3192 }
3193 file_priv->event_space -= sizeof e->event;
3194 spin_unlock_irqrestore(&dev->event_lock, flags);
3195
3196 e = kzalloc(sizeof *e, GFP_KERNEL);
3197 if (e == NULL) {
3198 spin_lock_irqsave(&dev->event_lock, flags);
3199 file_priv->event_space += sizeof e->event;
3200 spin_unlock_irqrestore(&dev->event_lock, flags);
3201 goto out;
3202 }
3203
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003204 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003205 e->event.base.length = sizeof e->event;
3206 e->event.user_data = page_flip->user_data;
3207 e->base.event = &e->event.base;
3208 e->base.file_priv = file_priv;
3209 e->base.destroy =
3210 (void (*) (struct drm_pending_event *)) kfree;
3211 }
3212
3213 ret = crtc->funcs->page_flip(crtc, fb, e);
3214 if (ret) {
3215 spin_lock_irqsave(&dev->event_lock, flags);
3216 file_priv->event_space += sizeof e->event;
3217 spin_unlock_irqrestore(&dev->event_lock, flags);
3218 kfree(e);
3219 }
3220
3221out:
3222 mutex_unlock(&dev->mode_config.mutex);
3223 return ret;
3224}
Chris Wilsoneb033552011-01-24 15:11:08 +00003225
3226void drm_mode_config_reset(struct drm_device *dev)
3227{
3228 struct drm_crtc *crtc;
3229 struct drm_encoder *encoder;
3230 struct drm_connector *connector;
3231
3232 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3233 if (crtc->funcs->reset)
3234 crtc->funcs->reset(crtc);
3235
3236 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3237 if (encoder->funcs->reset)
3238 encoder->funcs->reset(encoder);
3239
3240 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3241 if (connector->funcs->reset)
3242 connector->funcs->reset(connector);
3243}
3244EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003245
3246int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3247 void *data, struct drm_file *file_priv)
3248{
3249 struct drm_mode_create_dumb *args = data;
3250
3251 if (!dev->driver->dumb_create)
3252 return -ENOSYS;
3253 return dev->driver->dumb_create(file_priv, dev, args);
3254}
3255
3256int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3257 void *data, struct drm_file *file_priv)
3258{
3259 struct drm_mode_map_dumb *args = data;
3260
3261 /* call driver ioctl to get mmap offset */
3262 if (!dev->driver->dumb_map_offset)
3263 return -ENOSYS;
3264
3265 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3266}
3267
3268int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3269 void *data, struct drm_file *file_priv)
3270{
3271 struct drm_mode_destroy_dumb *args = data;
3272
3273 if (!dev->driver->dumb_destroy)
3274 return -ENOSYS;
3275
3276 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3277}
Dave Airlie248dbc22011-11-29 20:02:54 +00003278
3279/*
3280 * Just need to support RGB formats here for compat with code that doesn't
3281 * use pixel formats directly yet.
3282 */
3283void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3284 int *bpp)
3285{
3286 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003287 case DRM_FORMAT_RGB332:
3288 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003289 *depth = 8;
3290 *bpp = 8;
3291 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003292 case DRM_FORMAT_XRGB1555:
3293 case DRM_FORMAT_XBGR1555:
3294 case DRM_FORMAT_RGBX5551:
3295 case DRM_FORMAT_BGRX5551:
3296 case DRM_FORMAT_ARGB1555:
3297 case DRM_FORMAT_ABGR1555:
3298 case DRM_FORMAT_RGBA5551:
3299 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003300 *depth = 15;
3301 *bpp = 16;
3302 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003303 case DRM_FORMAT_RGB565:
3304 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003305 *depth = 16;
3306 *bpp = 16;
3307 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003308 case DRM_FORMAT_RGB888:
3309 case DRM_FORMAT_BGR888:
3310 *depth = 24;
3311 *bpp = 24;
3312 break;
3313 case DRM_FORMAT_XRGB8888:
3314 case DRM_FORMAT_XBGR8888:
3315 case DRM_FORMAT_RGBX8888:
3316 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003317 *depth = 24;
3318 *bpp = 32;
3319 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003320 case DRM_FORMAT_XRGB2101010:
3321 case DRM_FORMAT_XBGR2101010:
3322 case DRM_FORMAT_RGBX1010102:
3323 case DRM_FORMAT_BGRX1010102:
3324 case DRM_FORMAT_ARGB2101010:
3325 case DRM_FORMAT_ABGR2101010:
3326 case DRM_FORMAT_RGBA1010102:
3327 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003328 *depth = 30;
3329 *bpp = 32;
3330 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003331 case DRM_FORMAT_ARGB8888:
3332 case DRM_FORMAT_ABGR8888:
3333 case DRM_FORMAT_RGBA8888:
3334 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003335 *depth = 32;
3336 *bpp = 32;
3337 break;
3338 default:
3339 DRM_DEBUG_KMS("unsupported pixel format\n");
3340 *depth = 0;
3341 *bpp = 0;
3342 break;
3343 }
3344}
3345EXPORT_SYMBOL(drm_fb_get_bpp_depth);