[go: nahoru, domu]

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