xref: /linux/drivers/gpu/drm/drm_crtc.c (revision 5d4a2e29fba5b2bef95b96a46b338ec4d76fa4fd)
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 "drm.h"
35 #include "drmP.h"
36 #include "drm_crtc.h"
37 #include "drm_edid.h"
38 
39 struct drm_prop_enum_list {
40 	int type;
41 	char *name;
42 };
43 
44 /* Avoid boilerplate.  I'm tired of typing. */
45 #define DRM_ENUM_NAME_FN(fnname, list)				\
46 	char *fnname(int val)					\
47 	{							\
48 		int i;						\
49 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
50 			if (list[i].type == val)		\
51 				return list[i].name;		\
52 		}						\
53 		return "(unknown)";				\
54 	}
55 
56 /*
57  * Global properties
58  */
59 static struct drm_prop_enum_list drm_dpms_enum_list[] =
60 {	{ DRM_MODE_DPMS_ON, "On" },
61 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
62 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
63 	{ DRM_MODE_DPMS_OFF, "Off" }
64 };
65 
66 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
67 
68 /*
69  * Optional properties
70  */
71 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
72 {
73 	{ DRM_MODE_SCALE_NONE, "None" },
74 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
75 	{ DRM_MODE_SCALE_CENTER, "Center" },
76 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
77 };
78 
79 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
80 {
81 	{ DRM_MODE_DITHERING_OFF, "Off" },
82 	{ DRM_MODE_DITHERING_ON, "On" },
83 };
84 
85 /*
86  * Non-global properties, but "required" for certain connectors.
87  */
88 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
89 {
90 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
91 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
92 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
93 };
94 
95 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
96 
97 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
98 {
99 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
100 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
101 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
102 };
103 
104 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
105 		 drm_dvi_i_subconnector_enum_list)
106 
107 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
108 {
109 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
110 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
111 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
112 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
113 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
114 };
115 
116 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
117 
118 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
119 {
120 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
121 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
122 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
123 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
124 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
125 };
126 
127 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
128 		 drm_tv_subconnector_enum_list)
129 
130 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
131 	{ DRM_MODE_DIRTY_OFF,      "Off"      },
132 	{ DRM_MODE_DIRTY_ON,       "On"       },
133 	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
134 };
135 
136 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
137 		 drm_dirty_info_enum_list)
138 
139 struct drm_conn_prop_enum_list {
140 	int type;
141 	char *name;
142 	int count;
143 };
144 
145 /*
146  * Connector and encoder types.
147  */
148 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
149 {	{ DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
150 	{ DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
151 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
152 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
153 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
154 	{ DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
155 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
156 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
157 	{ DRM_MODE_CONNECTOR_Component, "Component", 0 },
158 	{ DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
159 	{ DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
160 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
161 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
162 	{ DRM_MODE_CONNECTOR_TV, "TV", 0 },
163 	{ DRM_MODE_CONNECTOR_eDP, "Embedded DisplayPort", 0 },
164 };
165 
166 static struct drm_prop_enum_list drm_encoder_enum_list[] =
167 {	{ DRM_MODE_ENCODER_NONE, "None" },
168 	{ DRM_MODE_ENCODER_DAC, "DAC" },
169 	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
170 	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
171 	{ DRM_MODE_ENCODER_TVDAC, "TV" },
172 };
173 
174 char *drm_get_encoder_name(struct drm_encoder *encoder)
175 {
176 	static char buf[32];
177 
178 	snprintf(buf, 32, "%s-%d",
179 		 drm_encoder_enum_list[encoder->encoder_type].name,
180 		 encoder->base.id);
181 	return buf;
182 }
183 EXPORT_SYMBOL(drm_get_encoder_name);
184 
185 char *drm_get_connector_name(struct drm_connector *connector)
186 {
187 	static char buf[32];
188 
189 	snprintf(buf, 32, "%s-%d",
190 		 drm_connector_enum_list[connector->connector_type].name,
191 		 connector->connector_type_id);
192 	return buf;
193 }
194 EXPORT_SYMBOL(drm_get_connector_name);
195 
196 char *drm_get_connector_status_name(enum drm_connector_status status)
197 {
198 	if (status == connector_status_connected)
199 		return "connected";
200 	else if (status == connector_status_disconnected)
201 		return "disconnected";
202 	else
203 		return "unknown";
204 }
205 
206 /**
207  * drm_mode_object_get - allocate a new identifier
208  * @dev: DRM device
209  * @ptr: object pointer, used to generate unique ID
210  * @type: object type
211  *
212  * LOCKING:
213  *
214  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
215  * for tracking modes, CRTCs and connectors.
216  *
217  * RETURNS:
218  * New unique (relative to other objects in @dev) integer identifier for the
219  * object.
220  */
221 static int drm_mode_object_get(struct drm_device *dev,
222 			       struct drm_mode_object *obj, uint32_t obj_type)
223 {
224 	int new_id = 0;
225 	int ret;
226 
227 again:
228 	if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 		DRM_ERROR("Ran out memory getting a mode number\n");
230 		return -EINVAL;
231 	}
232 
233 	mutex_lock(&dev->mode_config.idr_mutex);
234 	ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
235 	mutex_unlock(&dev->mode_config.idr_mutex);
236 	if (ret == -EAGAIN)
237 		goto again;
238 
239 	obj->id = new_id;
240 	obj->type = obj_type;
241 	return 0;
242 }
243 
244 /**
245  * drm_mode_object_put - free an identifer
246  * @dev: DRM device
247  * @id: ID to free
248  *
249  * LOCKING:
250  * Caller must hold DRM mode_config lock.
251  *
252  * Free @id from @dev's unique identifier pool.
253  */
254 static void drm_mode_object_put(struct drm_device *dev,
255 				struct drm_mode_object *object)
256 {
257 	mutex_lock(&dev->mode_config.idr_mutex);
258 	idr_remove(&dev->mode_config.crtc_idr, object->id);
259 	mutex_unlock(&dev->mode_config.idr_mutex);
260 }
261 
262 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 		uint32_t id, uint32_t type)
264 {
265 	struct drm_mode_object *obj = NULL;
266 
267 	mutex_lock(&dev->mode_config.idr_mutex);
268 	obj = idr_find(&dev->mode_config.crtc_idr, id);
269 	if (!obj || (obj->type != type) || (obj->id != id))
270 		obj = NULL;
271 	mutex_unlock(&dev->mode_config.idr_mutex);
272 
273 	return obj;
274 }
275 EXPORT_SYMBOL(drm_mode_object_find);
276 
277 /**
278  * drm_framebuffer_init - initialize a framebuffer
279  * @dev: DRM device
280  *
281  * LOCKING:
282  * Caller must hold mode config lock.
283  *
284  * Allocates an ID for the framebuffer's parent mode object, sets its mode
285  * functions & device file and adds it to the master fd list.
286  *
287  * RETURNS:
288  * Zero on success, error code on failure.
289  */
290 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 			 const struct drm_framebuffer_funcs *funcs)
292 {
293 	int ret;
294 
295 	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
296 	if (ret) {
297 		return ret;
298 	}
299 
300 	fb->dev = dev;
301 	fb->funcs = funcs;
302 	dev->mode_config.num_fb++;
303 	list_add(&fb->head, &dev->mode_config.fb_list);
304 
305 	return 0;
306 }
307 EXPORT_SYMBOL(drm_framebuffer_init);
308 
309 /**
310  * drm_framebuffer_cleanup - remove a framebuffer object
311  * @fb: framebuffer to remove
312  *
313  * LOCKING:
314  * Caller must hold mode config lock.
315  *
316  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
317  * it, setting it to NULL.
318  */
319 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
320 {
321 	struct drm_device *dev = fb->dev;
322 	struct drm_crtc *crtc;
323 	struct drm_mode_set set;
324 	int ret;
325 
326 	/* remove from any CRTC */
327 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
328 		if (crtc->fb == fb) {
329 			/* should turn off the crtc */
330 			memset(&set, 0, sizeof(struct drm_mode_set));
331 			set.crtc = crtc;
332 			set.fb = NULL;
333 			ret = crtc->funcs->set_config(&set);
334 			if (ret)
335 				DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
336 		}
337 	}
338 
339 	drm_mode_object_put(dev, &fb->base);
340 	list_del(&fb->head);
341 	dev->mode_config.num_fb--;
342 }
343 EXPORT_SYMBOL(drm_framebuffer_cleanup);
344 
345 /**
346  * drm_crtc_init - Initialise a new CRTC object
347  * @dev: DRM device
348  * @crtc: CRTC object to init
349  * @funcs: callbacks for the new CRTC
350  *
351  * LOCKING:
352  * Caller must hold mode config lock.
353  *
354  * Inits a new object created as base part of an driver crtc object.
355  */
356 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
357 		   const struct drm_crtc_funcs *funcs)
358 {
359 	crtc->dev = dev;
360 	crtc->funcs = funcs;
361 
362 	mutex_lock(&dev->mode_config.mutex);
363 	drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
364 
365 	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
366 	dev->mode_config.num_crtc++;
367 	mutex_unlock(&dev->mode_config.mutex);
368 }
369 EXPORT_SYMBOL(drm_crtc_init);
370 
371 /**
372  * drm_crtc_cleanup - Cleans up the core crtc usage.
373  * @crtc: CRTC to cleanup
374  *
375  * LOCKING:
376  * Caller must hold mode config lock.
377  *
378  * Cleanup @crtc. Removes from drm modesetting space
379  * does NOT free object, caller does that.
380  */
381 void drm_crtc_cleanup(struct drm_crtc *crtc)
382 {
383 	struct drm_device *dev = crtc->dev;
384 
385 	if (crtc->gamma_store) {
386 		kfree(crtc->gamma_store);
387 		crtc->gamma_store = NULL;
388 	}
389 
390 	drm_mode_object_put(dev, &crtc->base);
391 	list_del(&crtc->head);
392 	dev->mode_config.num_crtc--;
393 }
394 EXPORT_SYMBOL(drm_crtc_cleanup);
395 
396 /**
397  * drm_mode_probed_add - add a mode to a connector's probed mode list
398  * @connector: connector the new mode
399  * @mode: mode data
400  *
401  * LOCKING:
402  * Caller must hold mode config lock.
403  *
404  * Add @mode to @connector's mode list for later use.
405  */
406 void drm_mode_probed_add(struct drm_connector *connector,
407 			 struct drm_display_mode *mode)
408 {
409 	list_add(&mode->head, &connector->probed_modes);
410 }
411 EXPORT_SYMBOL(drm_mode_probed_add);
412 
413 /**
414  * drm_mode_remove - remove and free a mode
415  * @connector: connector list to modify
416  * @mode: mode to remove
417  *
418  * LOCKING:
419  * Caller must hold mode config lock.
420  *
421  * Remove @mode from @connector's mode list, then free it.
422  */
423 void drm_mode_remove(struct drm_connector *connector,
424 		     struct drm_display_mode *mode)
425 {
426 	list_del(&mode->head);
427 	kfree(mode);
428 }
429 EXPORT_SYMBOL(drm_mode_remove);
430 
431 /**
432  * drm_connector_init - Init a preallocated connector
433  * @dev: DRM device
434  * @connector: the connector to init
435  * @funcs: callbacks for this connector
436  * @name: user visible name of the connector
437  *
438  * LOCKING:
439  * Caller must hold @dev's mode_config lock.
440  *
441  * Initialises a preallocated connector. Connectors should be
442  * subclassed as part of driver connector objects.
443  */
444 void drm_connector_init(struct drm_device *dev,
445 		     struct drm_connector *connector,
446 		     const struct drm_connector_funcs *funcs,
447 		     int connector_type)
448 {
449 	mutex_lock(&dev->mode_config.mutex);
450 
451 	connector->dev = dev;
452 	connector->funcs = funcs;
453 	drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
454 	connector->connector_type = connector_type;
455 	connector->connector_type_id =
456 		++drm_connector_enum_list[connector_type].count; /* TODO */
457 	INIT_LIST_HEAD(&connector->user_modes);
458 	INIT_LIST_HEAD(&connector->probed_modes);
459 	INIT_LIST_HEAD(&connector->modes);
460 	connector->edid_blob_ptr = NULL;
461 
462 	list_add_tail(&connector->head, &dev->mode_config.connector_list);
463 	dev->mode_config.num_connector++;
464 
465 	drm_connector_attach_property(connector,
466 				      dev->mode_config.edid_property, 0);
467 
468 	drm_connector_attach_property(connector,
469 				      dev->mode_config.dpms_property, 0);
470 
471 	mutex_unlock(&dev->mode_config.mutex);
472 }
473 EXPORT_SYMBOL(drm_connector_init);
474 
475 /**
476  * drm_connector_cleanup - cleans up an initialised connector
477  * @connector: connector to cleanup
478  *
479  * LOCKING:
480  * Caller must hold @dev's mode_config lock.
481  *
482  * Cleans up the connector but doesn't free the object.
483  */
484 void drm_connector_cleanup(struct drm_connector *connector)
485 {
486 	struct drm_device *dev = connector->dev;
487 	struct drm_display_mode *mode, *t;
488 
489 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
490 		drm_mode_remove(connector, mode);
491 
492 	list_for_each_entry_safe(mode, t, &connector->modes, head)
493 		drm_mode_remove(connector, mode);
494 
495 	list_for_each_entry_safe(mode, t, &connector->user_modes, head)
496 		drm_mode_remove(connector, mode);
497 
498 	mutex_lock(&dev->mode_config.mutex);
499 	drm_mode_object_put(dev, &connector->base);
500 	list_del(&connector->head);
501 	mutex_unlock(&dev->mode_config.mutex);
502 }
503 EXPORT_SYMBOL(drm_connector_cleanup);
504 
505 void drm_encoder_init(struct drm_device *dev,
506 		      struct drm_encoder *encoder,
507 		      const struct drm_encoder_funcs *funcs,
508 		      int encoder_type)
509 {
510 	mutex_lock(&dev->mode_config.mutex);
511 
512 	encoder->dev = dev;
513 
514 	drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
515 	encoder->encoder_type = encoder_type;
516 	encoder->funcs = funcs;
517 
518 	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
519 	dev->mode_config.num_encoder++;
520 
521 	mutex_unlock(&dev->mode_config.mutex);
522 }
523 EXPORT_SYMBOL(drm_encoder_init);
524 
525 void drm_encoder_cleanup(struct drm_encoder *encoder)
526 {
527 	struct drm_device *dev = encoder->dev;
528 	mutex_lock(&dev->mode_config.mutex);
529 	drm_mode_object_put(dev, &encoder->base);
530 	list_del(&encoder->head);
531 	mutex_unlock(&dev->mode_config.mutex);
532 }
533 EXPORT_SYMBOL(drm_encoder_cleanup);
534 
535 /**
536  * drm_mode_create - create a new display mode
537  * @dev: DRM device
538  *
539  * LOCKING:
540  * Caller must hold DRM mode_config lock.
541  *
542  * Create a new drm_display_mode, give it an ID, and return it.
543  *
544  * RETURNS:
545  * Pointer to new mode on success, NULL on error.
546  */
547 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
548 {
549 	struct drm_display_mode *nmode;
550 
551 	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
552 	if (!nmode)
553 		return NULL;
554 
555 	drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
556 	return nmode;
557 }
558 EXPORT_SYMBOL(drm_mode_create);
559 
560 /**
561  * drm_mode_destroy - remove a mode
562  * @dev: DRM device
563  * @mode: mode to remove
564  *
565  * LOCKING:
566  * Caller must hold mode config lock.
567  *
568  * Free @mode's unique identifier, then free it.
569  */
570 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
571 {
572 	drm_mode_object_put(dev, &mode->base);
573 
574 	kfree(mode);
575 }
576 EXPORT_SYMBOL(drm_mode_destroy);
577 
578 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
579 {
580 	struct drm_property *edid;
581 	struct drm_property *dpms;
582 	int i;
583 
584 	/*
585 	 * Standard properties (apply to all connectors)
586 	 */
587 	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
588 				   DRM_MODE_PROP_IMMUTABLE,
589 				   "EDID", 0);
590 	dev->mode_config.edid_property = edid;
591 
592 	dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
593 				   "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
594 	for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
595 		drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
596 				      drm_dpms_enum_list[i].name);
597 	dev->mode_config.dpms_property = dpms;
598 
599 	return 0;
600 }
601 
602 /**
603  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
604  * @dev: DRM device
605  *
606  * Called by a driver the first time a DVI-I connector is made.
607  */
608 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
609 {
610 	struct drm_property *dvi_i_selector;
611 	struct drm_property *dvi_i_subconnector;
612 	int i;
613 
614 	if (dev->mode_config.dvi_i_select_subconnector_property)
615 		return 0;
616 
617 	dvi_i_selector =
618 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
619 				    "select subconnector",
620 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
621 	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
622 		drm_property_add_enum(dvi_i_selector, i,
623 				      drm_dvi_i_select_enum_list[i].type,
624 				      drm_dvi_i_select_enum_list[i].name);
625 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
626 
627 	dvi_i_subconnector =
628 		drm_property_create(dev, DRM_MODE_PROP_ENUM |
629 				    DRM_MODE_PROP_IMMUTABLE,
630 				    "subconnector",
631 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
632 	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
633 		drm_property_add_enum(dvi_i_subconnector, i,
634 				      drm_dvi_i_subconnector_enum_list[i].type,
635 				      drm_dvi_i_subconnector_enum_list[i].name);
636 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
637 
638 	return 0;
639 }
640 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
641 
642 /**
643  * drm_create_tv_properties - create TV specific connector properties
644  * @dev: DRM device
645  * @num_modes: number of different TV formats (modes) supported
646  * @modes: array of pointers to strings containing name of each format
647  *
648  * Called by a driver's TV initialization routine, this function creates
649  * the TV specific connector properties for a given device.  Caller is
650  * responsible for allocating a list of format names and passing them to
651  * this routine.
652  */
653 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
654 				  char *modes[])
655 {
656 	struct drm_property *tv_selector;
657 	struct drm_property *tv_subconnector;
658 	int i;
659 
660 	if (dev->mode_config.tv_select_subconnector_property)
661 		return 0;
662 
663 	/*
664 	 * Basic connector properties
665 	 */
666 	tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
667 					  "select subconnector",
668 					  ARRAY_SIZE(drm_tv_select_enum_list));
669 	for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
670 		drm_property_add_enum(tv_selector, i,
671 				      drm_tv_select_enum_list[i].type,
672 				      drm_tv_select_enum_list[i].name);
673 	dev->mode_config.tv_select_subconnector_property = tv_selector;
674 
675 	tv_subconnector =
676 		drm_property_create(dev, DRM_MODE_PROP_ENUM |
677 				    DRM_MODE_PROP_IMMUTABLE, "subconnector",
678 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
679 	for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
680 		drm_property_add_enum(tv_subconnector, i,
681 				      drm_tv_subconnector_enum_list[i].type,
682 				      drm_tv_subconnector_enum_list[i].name);
683 	dev->mode_config.tv_subconnector_property = tv_subconnector;
684 
685 	/*
686 	 * Other, TV specific properties: margins & TV modes.
687 	 */
688 	dev->mode_config.tv_left_margin_property =
689 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
690 				    "left margin", 2);
691 	dev->mode_config.tv_left_margin_property->values[0] = 0;
692 	dev->mode_config.tv_left_margin_property->values[1] = 100;
693 
694 	dev->mode_config.tv_right_margin_property =
695 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
696 				    "right margin", 2);
697 	dev->mode_config.tv_right_margin_property->values[0] = 0;
698 	dev->mode_config.tv_right_margin_property->values[1] = 100;
699 
700 	dev->mode_config.tv_top_margin_property =
701 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
702 				    "top margin", 2);
703 	dev->mode_config.tv_top_margin_property->values[0] = 0;
704 	dev->mode_config.tv_top_margin_property->values[1] = 100;
705 
706 	dev->mode_config.tv_bottom_margin_property =
707 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
708 				    "bottom margin", 2);
709 	dev->mode_config.tv_bottom_margin_property->values[0] = 0;
710 	dev->mode_config.tv_bottom_margin_property->values[1] = 100;
711 
712 	dev->mode_config.tv_mode_property =
713 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
714 				    "mode", num_modes);
715 	for (i = 0; i < num_modes; i++)
716 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
717 				      i, modes[i]);
718 
719 	dev->mode_config.tv_brightness_property =
720 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
721 				    "brightness", 2);
722 	dev->mode_config.tv_brightness_property->values[0] = 0;
723 	dev->mode_config.tv_brightness_property->values[1] = 100;
724 
725 	dev->mode_config.tv_contrast_property =
726 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
727 				    "contrast", 2);
728 	dev->mode_config.tv_contrast_property->values[0] = 0;
729 	dev->mode_config.tv_contrast_property->values[1] = 100;
730 
731 	dev->mode_config.tv_flicker_reduction_property =
732 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
733 				    "flicker reduction", 2);
734 	dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
735 	dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
736 
737 	dev->mode_config.tv_overscan_property =
738 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
739 				    "overscan", 2);
740 	dev->mode_config.tv_overscan_property->values[0] = 0;
741 	dev->mode_config.tv_overscan_property->values[1] = 100;
742 
743 	dev->mode_config.tv_saturation_property =
744 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
745 				    "saturation", 2);
746 	dev->mode_config.tv_saturation_property->values[0] = 0;
747 	dev->mode_config.tv_saturation_property->values[1] = 100;
748 
749 	dev->mode_config.tv_hue_property =
750 		drm_property_create(dev, DRM_MODE_PROP_RANGE,
751 				    "hue", 2);
752 	dev->mode_config.tv_hue_property->values[0] = 0;
753 	dev->mode_config.tv_hue_property->values[1] = 100;
754 
755 	return 0;
756 }
757 EXPORT_SYMBOL(drm_mode_create_tv_properties);
758 
759 /**
760  * drm_mode_create_scaling_mode_property - create scaling mode property
761  * @dev: DRM device
762  *
763  * Called by a driver the first time it's needed, must be attached to desired
764  * connectors.
765  */
766 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
767 {
768 	struct drm_property *scaling_mode;
769 	int i;
770 
771 	if (dev->mode_config.scaling_mode_property)
772 		return 0;
773 
774 	scaling_mode =
775 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
776 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
777 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
778 		drm_property_add_enum(scaling_mode, i,
779 				      drm_scaling_mode_enum_list[i].type,
780 				      drm_scaling_mode_enum_list[i].name);
781 
782 	dev->mode_config.scaling_mode_property = scaling_mode;
783 
784 	return 0;
785 }
786 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
787 
788 /**
789  * drm_mode_create_dithering_property - create dithering property
790  * @dev: DRM device
791  *
792  * Called by a driver the first time it's needed, must be attached to desired
793  * connectors.
794  */
795 int drm_mode_create_dithering_property(struct drm_device *dev)
796 {
797 	struct drm_property *dithering_mode;
798 	int i;
799 
800 	if (dev->mode_config.dithering_mode_property)
801 		return 0;
802 
803 	dithering_mode =
804 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
805 				    ARRAY_SIZE(drm_dithering_mode_enum_list));
806 	for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
807 		drm_property_add_enum(dithering_mode, i,
808 				      drm_dithering_mode_enum_list[i].type,
809 				      drm_dithering_mode_enum_list[i].name);
810 	dev->mode_config.dithering_mode_property = dithering_mode;
811 
812 	return 0;
813 }
814 EXPORT_SYMBOL(drm_mode_create_dithering_property);
815 
816 /**
817  * drm_mode_create_dirty_property - create dirty property
818  * @dev: DRM device
819  *
820  * Called by a driver the first time it's needed, must be attached to desired
821  * connectors.
822  */
823 int drm_mode_create_dirty_info_property(struct drm_device *dev)
824 {
825 	struct drm_property *dirty_info;
826 	int i;
827 
828 	if (dev->mode_config.dirty_info_property)
829 		return 0;
830 
831 	dirty_info =
832 		drm_property_create(dev, DRM_MODE_PROP_ENUM |
833 				    DRM_MODE_PROP_IMMUTABLE,
834 				    "dirty",
835 				    ARRAY_SIZE(drm_dirty_info_enum_list));
836 	for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
837 		drm_property_add_enum(dirty_info, i,
838 				      drm_dirty_info_enum_list[i].type,
839 				      drm_dirty_info_enum_list[i].name);
840 	dev->mode_config.dirty_info_property = dirty_info;
841 
842 	return 0;
843 }
844 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
845 
846 /**
847  * drm_mode_config_init - initialize DRM mode_configuration structure
848  * @dev: DRM device
849  *
850  * LOCKING:
851  * None, should happen single threaded at init time.
852  *
853  * Initialize @dev's mode_config structure, used for tracking the graphics
854  * configuration of @dev.
855  */
856 void drm_mode_config_init(struct drm_device *dev)
857 {
858 	mutex_init(&dev->mode_config.mutex);
859 	mutex_init(&dev->mode_config.idr_mutex);
860 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
861 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
862 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
863 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
864 	INIT_LIST_HEAD(&dev->mode_config.property_list);
865 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
866 	idr_init(&dev->mode_config.crtc_idr);
867 
868 	mutex_lock(&dev->mode_config.mutex);
869 	drm_mode_create_standard_connector_properties(dev);
870 	mutex_unlock(&dev->mode_config.mutex);
871 
872 	/* Just to be sure */
873 	dev->mode_config.num_fb = 0;
874 	dev->mode_config.num_connector = 0;
875 	dev->mode_config.num_crtc = 0;
876 	dev->mode_config.num_encoder = 0;
877 }
878 EXPORT_SYMBOL(drm_mode_config_init);
879 
880 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
881 {
882 	uint32_t total_objects = 0;
883 
884 	total_objects += dev->mode_config.num_crtc;
885 	total_objects += dev->mode_config.num_connector;
886 	total_objects += dev->mode_config.num_encoder;
887 
888 	if (total_objects == 0)
889 		return -EINVAL;
890 
891 	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
892 	if (!group->id_list)
893 		return -ENOMEM;
894 
895 	group->num_crtcs = 0;
896 	group->num_connectors = 0;
897 	group->num_encoders = 0;
898 	return 0;
899 }
900 
901 int drm_mode_group_init_legacy_group(struct drm_device *dev,
902 				     struct drm_mode_group *group)
903 {
904 	struct drm_crtc *crtc;
905 	struct drm_encoder *encoder;
906 	struct drm_connector *connector;
907 	int ret;
908 
909 	if ((ret = drm_mode_group_init(dev, group)))
910 		return ret;
911 
912 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
913 		group->id_list[group->num_crtcs++] = crtc->base.id;
914 
915 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
916 		group->id_list[group->num_crtcs + group->num_encoders++] =
917 		encoder->base.id;
918 
919 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
920 		group->id_list[group->num_crtcs + group->num_encoders +
921 			       group->num_connectors++] = connector->base.id;
922 
923 	return 0;
924 }
925 
926 /**
927  * drm_mode_config_cleanup - free up DRM mode_config info
928  * @dev: DRM device
929  *
930  * LOCKING:
931  * Caller must hold mode config lock.
932  *
933  * Free up all the connectors and CRTCs associated with this DRM device, then
934  * free up the framebuffers and associated buffer objects.
935  *
936  * FIXME: cleanup any dangling user buffer objects too
937  */
938 void drm_mode_config_cleanup(struct drm_device *dev)
939 {
940 	struct drm_connector *connector, *ot;
941 	struct drm_crtc *crtc, *ct;
942 	struct drm_encoder *encoder, *enct;
943 	struct drm_framebuffer *fb, *fbt;
944 	struct drm_property *property, *pt;
945 
946 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
947 				 head) {
948 		encoder->funcs->destroy(encoder);
949 	}
950 
951 	list_for_each_entry_safe(connector, ot,
952 				 &dev->mode_config.connector_list, head) {
953 		connector->funcs->destroy(connector);
954 	}
955 
956 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
957 				 head) {
958 		drm_property_destroy(dev, property);
959 	}
960 
961 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
962 		fb->funcs->destroy(fb);
963 	}
964 
965 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
966 		crtc->funcs->destroy(crtc);
967 	}
968 
969 }
970 EXPORT_SYMBOL(drm_mode_config_cleanup);
971 
972 /**
973  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
974  * @out: drm_mode_modeinfo struct to return to the user
975  * @in: drm_display_mode to use
976  *
977  * LOCKING:
978  * None.
979  *
980  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
981  * the user.
982  */
983 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
984 			       struct drm_display_mode *in)
985 {
986 	out->clock = in->clock;
987 	out->hdisplay = in->hdisplay;
988 	out->hsync_start = in->hsync_start;
989 	out->hsync_end = in->hsync_end;
990 	out->htotal = in->htotal;
991 	out->hskew = in->hskew;
992 	out->vdisplay = in->vdisplay;
993 	out->vsync_start = in->vsync_start;
994 	out->vsync_end = in->vsync_end;
995 	out->vtotal = in->vtotal;
996 	out->vscan = in->vscan;
997 	out->vrefresh = in->vrefresh;
998 	out->flags = in->flags;
999 	out->type = in->type;
1000 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1001 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1002 }
1003 
1004 /**
1005  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1006  * @out: drm_display_mode to return to the user
1007  * @in: drm_mode_modeinfo to use
1008  *
1009  * LOCKING:
1010  * None.
1011  *
1012  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1013  * the caller.
1014  */
1015 void drm_crtc_convert_umode(struct drm_display_mode *out,
1016 			    struct drm_mode_modeinfo *in)
1017 {
1018 	out->clock = in->clock;
1019 	out->hdisplay = in->hdisplay;
1020 	out->hsync_start = in->hsync_start;
1021 	out->hsync_end = in->hsync_end;
1022 	out->htotal = in->htotal;
1023 	out->hskew = in->hskew;
1024 	out->vdisplay = in->vdisplay;
1025 	out->vsync_start = in->vsync_start;
1026 	out->vsync_end = in->vsync_end;
1027 	out->vtotal = in->vtotal;
1028 	out->vscan = in->vscan;
1029 	out->vrefresh = in->vrefresh;
1030 	out->flags = in->flags;
1031 	out->type = in->type;
1032 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1033 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1034 }
1035 
1036 /**
1037  * drm_mode_getresources - get graphics configuration
1038  * @inode: inode from the ioctl
1039  * @filp: file * from the ioctl
1040  * @cmd: cmd from ioctl
1041  * @arg: arg from ioctl
1042  *
1043  * LOCKING:
1044  * Takes mode config lock.
1045  *
1046  * Construct a set of configuration description structures and return
1047  * them to the user, including CRTC, connector and framebuffer configuration.
1048  *
1049  * Called by the user via ioctl.
1050  *
1051  * RETURNS:
1052  * Zero on success, errno on failure.
1053  */
1054 int drm_mode_getresources(struct drm_device *dev, void *data,
1055 			  struct drm_file *file_priv)
1056 {
1057 	struct drm_mode_card_res *card_res = data;
1058 	struct list_head *lh;
1059 	struct drm_framebuffer *fb;
1060 	struct drm_connector *connector;
1061 	struct drm_crtc *crtc;
1062 	struct drm_encoder *encoder;
1063 	int ret = 0;
1064 	int connector_count = 0;
1065 	int crtc_count = 0;
1066 	int fb_count = 0;
1067 	int encoder_count = 0;
1068 	int copied = 0, i;
1069 	uint32_t __user *fb_id;
1070 	uint32_t __user *crtc_id;
1071 	uint32_t __user *connector_id;
1072 	uint32_t __user *encoder_id;
1073 	struct drm_mode_group *mode_group;
1074 
1075 	mutex_lock(&dev->mode_config.mutex);
1076 
1077 	/*
1078 	 * For the non-control nodes we need to limit the list of resources
1079 	 * by IDs in the group list for this node
1080 	 */
1081 	list_for_each(lh, &file_priv->fbs)
1082 		fb_count++;
1083 
1084 	mode_group = &file_priv->master->minor->mode_group;
1085 	if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1086 
1087 		list_for_each(lh, &dev->mode_config.crtc_list)
1088 			crtc_count++;
1089 
1090 		list_for_each(lh, &dev->mode_config.connector_list)
1091 			connector_count++;
1092 
1093 		list_for_each(lh, &dev->mode_config.encoder_list)
1094 			encoder_count++;
1095 	} else {
1096 
1097 		crtc_count = mode_group->num_crtcs;
1098 		connector_count = mode_group->num_connectors;
1099 		encoder_count = mode_group->num_encoders;
1100 	}
1101 
1102 	card_res->max_height = dev->mode_config.max_height;
1103 	card_res->min_height = dev->mode_config.min_height;
1104 	card_res->max_width = dev->mode_config.max_width;
1105 	card_res->min_width = dev->mode_config.min_width;
1106 
1107 	/* handle this in 4 parts */
1108 	/* FBs */
1109 	if (card_res->count_fbs >= fb_count) {
1110 		copied = 0;
1111 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1112 		list_for_each_entry(fb, &file_priv->fbs, head) {
1113 			if (put_user(fb->base.id, fb_id + copied)) {
1114 				ret = -EFAULT;
1115 				goto out;
1116 			}
1117 			copied++;
1118 		}
1119 	}
1120 	card_res->count_fbs = fb_count;
1121 
1122 	/* CRTCs */
1123 	if (card_res->count_crtcs >= crtc_count) {
1124 		copied = 0;
1125 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1126 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1127 			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1128 					    head) {
1129 				DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
1130 				if (put_user(crtc->base.id, crtc_id + copied)) {
1131 					ret = -EFAULT;
1132 					goto out;
1133 				}
1134 				copied++;
1135 			}
1136 		} else {
1137 			for (i = 0; i < mode_group->num_crtcs; i++) {
1138 				if (put_user(mode_group->id_list[i],
1139 					     crtc_id + copied)) {
1140 					ret = -EFAULT;
1141 					goto out;
1142 				}
1143 				copied++;
1144 			}
1145 		}
1146 	}
1147 	card_res->count_crtcs = crtc_count;
1148 
1149 	/* Encoders */
1150 	if (card_res->count_encoders >= encoder_count) {
1151 		copied = 0;
1152 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1153 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1154 			list_for_each_entry(encoder,
1155 					    &dev->mode_config.encoder_list,
1156 					    head) {
1157 				DRM_DEBUG_KMS("ENCODER ID is %d\n",
1158 					  encoder->base.id);
1159 				if (put_user(encoder->base.id, encoder_id +
1160 					     copied)) {
1161 					ret = -EFAULT;
1162 					goto out;
1163 				}
1164 				copied++;
1165 			}
1166 		} else {
1167 			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1168 				if (put_user(mode_group->id_list[i],
1169 					     encoder_id + copied)) {
1170 					ret = -EFAULT;
1171 					goto out;
1172 				}
1173 				copied++;
1174 			}
1175 
1176 		}
1177 	}
1178 	card_res->count_encoders = encoder_count;
1179 
1180 	/* Connectors */
1181 	if (card_res->count_connectors >= connector_count) {
1182 		copied = 0;
1183 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1184 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1185 			list_for_each_entry(connector,
1186 					    &dev->mode_config.connector_list,
1187 					    head) {
1188 				DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
1189 					  connector->base.id);
1190 				if (put_user(connector->base.id,
1191 					     connector_id + copied)) {
1192 					ret = -EFAULT;
1193 					goto out;
1194 				}
1195 				copied++;
1196 			}
1197 		} else {
1198 			int start = mode_group->num_crtcs +
1199 				mode_group->num_encoders;
1200 			for (i = start; i < start + mode_group->num_connectors; i++) {
1201 				if (put_user(mode_group->id_list[i],
1202 					     connector_id + copied)) {
1203 					ret = -EFAULT;
1204 					goto out;
1205 				}
1206 				copied++;
1207 			}
1208 		}
1209 	}
1210 	card_res->count_connectors = connector_count;
1211 
1212 	DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
1213 		  card_res->count_connectors, card_res->count_encoders);
1214 
1215 out:
1216 	mutex_unlock(&dev->mode_config.mutex);
1217 	return ret;
1218 }
1219 
1220 /**
1221  * drm_mode_getcrtc - get CRTC configuration
1222  * @inode: inode from the ioctl
1223  * @filp: file * from the ioctl
1224  * @cmd: cmd from ioctl
1225  * @arg: arg from ioctl
1226  *
1227  * LOCKING:
1228  * Caller? (FIXME)
1229  *
1230  * Construct a CRTC configuration structure to return to the user.
1231  *
1232  * Called by the user via ioctl.
1233  *
1234  * RETURNS:
1235  * Zero on success, errno on failure.
1236  */
1237 int drm_mode_getcrtc(struct drm_device *dev,
1238 		     void *data, struct drm_file *file_priv)
1239 {
1240 	struct drm_mode_crtc *crtc_resp = data;
1241 	struct drm_crtc *crtc;
1242 	struct drm_mode_object *obj;
1243 	int ret = 0;
1244 
1245 	mutex_lock(&dev->mode_config.mutex);
1246 
1247 	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1248 				   DRM_MODE_OBJECT_CRTC);
1249 	if (!obj) {
1250 		ret = -EINVAL;
1251 		goto out;
1252 	}
1253 	crtc = obj_to_crtc(obj);
1254 
1255 	crtc_resp->x = crtc->x;
1256 	crtc_resp->y = crtc->y;
1257 	crtc_resp->gamma_size = crtc->gamma_size;
1258 	if (crtc->fb)
1259 		crtc_resp->fb_id = crtc->fb->base.id;
1260 	else
1261 		crtc_resp->fb_id = 0;
1262 
1263 	if (crtc->enabled) {
1264 
1265 		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1266 		crtc_resp->mode_valid = 1;
1267 
1268 	} else {
1269 		crtc_resp->mode_valid = 0;
1270 	}
1271 
1272 out:
1273 	mutex_unlock(&dev->mode_config.mutex);
1274 	return ret;
1275 }
1276 
1277 /**
1278  * drm_mode_getconnector - get connector configuration
1279  * @inode: inode from the ioctl
1280  * @filp: file * from the ioctl
1281  * @cmd: cmd from ioctl
1282  * @arg: arg from ioctl
1283  *
1284  * LOCKING:
1285  * Caller? (FIXME)
1286  *
1287  * Construct a connector configuration structure to return to the user.
1288  *
1289  * Called by the user via ioctl.
1290  *
1291  * RETURNS:
1292  * Zero on success, errno on failure.
1293  */
1294 int drm_mode_getconnector(struct drm_device *dev, void *data,
1295 			  struct drm_file *file_priv)
1296 {
1297 	struct drm_mode_get_connector *out_resp = data;
1298 	struct drm_mode_object *obj;
1299 	struct drm_connector *connector;
1300 	struct drm_display_mode *mode;
1301 	int mode_count = 0;
1302 	int props_count = 0;
1303 	int encoders_count = 0;
1304 	int ret = 0;
1305 	int copied = 0;
1306 	int i;
1307 	struct drm_mode_modeinfo u_mode;
1308 	struct drm_mode_modeinfo __user *mode_ptr;
1309 	uint32_t __user *prop_ptr;
1310 	uint64_t __user *prop_values;
1311 	uint32_t __user *encoder_ptr;
1312 
1313 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1314 
1315 	DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
1316 
1317 	mutex_lock(&dev->mode_config.mutex);
1318 
1319 	obj = drm_mode_object_find(dev, out_resp->connector_id,
1320 				   DRM_MODE_OBJECT_CONNECTOR);
1321 	if (!obj) {
1322 		ret = -EINVAL;
1323 		goto out;
1324 	}
1325 	connector = obj_to_connector(obj);
1326 
1327 	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1328 		if (connector->property_ids[i] != 0) {
1329 			props_count++;
1330 		}
1331 	}
1332 
1333 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1334 		if (connector->encoder_ids[i] != 0) {
1335 			encoders_count++;
1336 		}
1337 	}
1338 
1339 	if (out_resp->count_modes == 0) {
1340 		connector->funcs->fill_modes(connector,
1341 					     dev->mode_config.max_width,
1342 					     dev->mode_config.max_height);
1343 	}
1344 
1345 	/* delayed so we get modes regardless of pre-fill_modes state */
1346 	list_for_each_entry(mode, &connector->modes, head)
1347 		mode_count++;
1348 
1349 	out_resp->connector_id = connector->base.id;
1350 	out_resp->connector_type = connector->connector_type;
1351 	out_resp->connector_type_id = connector->connector_type_id;
1352 	out_resp->mm_width = connector->display_info.width_mm;
1353 	out_resp->mm_height = connector->display_info.height_mm;
1354 	out_resp->subpixel = connector->display_info.subpixel_order;
1355 	out_resp->connection = connector->status;
1356 	if (connector->encoder)
1357 		out_resp->encoder_id = connector->encoder->base.id;
1358 	else
1359 		out_resp->encoder_id = 0;
1360 
1361 	/*
1362 	 * This ioctl is called twice, once to determine how much space is
1363 	 * needed, and the 2nd time to fill it.
1364 	 */
1365 	if ((out_resp->count_modes >= mode_count) && mode_count) {
1366 		copied = 0;
1367 		mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1368 		list_for_each_entry(mode, &connector->modes, head) {
1369 			drm_crtc_convert_to_umode(&u_mode, mode);
1370 			if (copy_to_user(mode_ptr + copied,
1371 					 &u_mode, sizeof(u_mode))) {
1372 				ret = -EFAULT;
1373 				goto out;
1374 			}
1375 			copied++;
1376 		}
1377 	}
1378 	out_resp->count_modes = mode_count;
1379 
1380 	if ((out_resp->count_props >= props_count) && props_count) {
1381 		copied = 0;
1382 		prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1383 		prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1384 		for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1385 			if (connector->property_ids[i] != 0) {
1386 				if (put_user(connector->property_ids[i],
1387 					     prop_ptr + copied)) {
1388 					ret = -EFAULT;
1389 					goto out;
1390 				}
1391 
1392 				if (put_user(connector->property_values[i],
1393 					     prop_values + copied)) {
1394 					ret = -EFAULT;
1395 					goto out;
1396 				}
1397 				copied++;
1398 			}
1399 		}
1400 	}
1401 	out_resp->count_props = props_count;
1402 
1403 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1404 		copied = 0;
1405 		encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1406 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1407 			if (connector->encoder_ids[i] != 0) {
1408 				if (put_user(connector->encoder_ids[i],
1409 					     encoder_ptr + copied)) {
1410 					ret = -EFAULT;
1411 					goto out;
1412 				}
1413 				copied++;
1414 			}
1415 		}
1416 	}
1417 	out_resp->count_encoders = encoders_count;
1418 
1419 out:
1420 	mutex_unlock(&dev->mode_config.mutex);
1421 	return ret;
1422 }
1423 
1424 int drm_mode_getencoder(struct drm_device *dev, void *data,
1425 			struct drm_file *file_priv)
1426 {
1427 	struct drm_mode_get_encoder *enc_resp = data;
1428 	struct drm_mode_object *obj;
1429 	struct drm_encoder *encoder;
1430 	int ret = 0;
1431 
1432 	mutex_lock(&dev->mode_config.mutex);
1433 	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1434 				   DRM_MODE_OBJECT_ENCODER);
1435 	if (!obj) {
1436 		ret = -EINVAL;
1437 		goto out;
1438 	}
1439 	encoder = obj_to_encoder(obj);
1440 
1441 	if (encoder->crtc)
1442 		enc_resp->crtc_id = encoder->crtc->base.id;
1443 	else
1444 		enc_resp->crtc_id = 0;
1445 	enc_resp->encoder_type = encoder->encoder_type;
1446 	enc_resp->encoder_id = encoder->base.id;
1447 	enc_resp->possible_crtcs = encoder->possible_crtcs;
1448 	enc_resp->possible_clones = encoder->possible_clones;
1449 
1450 out:
1451 	mutex_unlock(&dev->mode_config.mutex);
1452 	return ret;
1453 }
1454 
1455 /**
1456  * drm_mode_setcrtc - set CRTC configuration
1457  * @inode: inode from the ioctl
1458  * @filp: file * from the ioctl
1459  * @cmd: cmd from ioctl
1460  * @arg: arg from ioctl
1461  *
1462  * LOCKING:
1463  * Caller? (FIXME)
1464  *
1465  * Build a new CRTC configuration based on user request.
1466  *
1467  * Called by the user via ioctl.
1468  *
1469  * RETURNS:
1470  * Zero on success, errno on failure.
1471  */
1472 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1473 		     struct drm_file *file_priv)
1474 {
1475 	struct drm_mode_config *config = &dev->mode_config;
1476 	struct drm_mode_crtc *crtc_req = data;
1477 	struct drm_mode_object *obj;
1478 	struct drm_crtc *crtc, *crtcfb;
1479 	struct drm_connector **connector_set = NULL, *connector;
1480 	struct drm_framebuffer *fb = NULL;
1481 	struct drm_display_mode *mode = NULL;
1482 	struct drm_mode_set set;
1483 	uint32_t __user *set_connectors_ptr;
1484 	int ret = 0;
1485 	int i;
1486 
1487 	mutex_lock(&dev->mode_config.mutex);
1488 	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1489 				   DRM_MODE_OBJECT_CRTC);
1490 	if (!obj) {
1491 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1492 		ret = -EINVAL;
1493 		goto out;
1494 	}
1495 	crtc = obj_to_crtc(obj);
1496 
1497 	if (crtc_req->mode_valid) {
1498 		/* If we have a mode we need a framebuffer. */
1499 		/* If we pass -1, set the mode with the currently bound fb */
1500 		if (crtc_req->fb_id == -1) {
1501 			list_for_each_entry(crtcfb,
1502 					    &dev->mode_config.crtc_list, head) {
1503 				if (crtcfb == crtc) {
1504 					DRM_DEBUG_KMS("Using current fb for "
1505 							"setmode\n");
1506 					fb = crtc->fb;
1507 				}
1508 			}
1509 		} else {
1510 			obj = drm_mode_object_find(dev, crtc_req->fb_id,
1511 						   DRM_MODE_OBJECT_FB);
1512 			if (!obj) {
1513 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
1514 						crtc_req->fb_id);
1515 				ret = -EINVAL;
1516 				goto out;
1517 			}
1518 			fb = obj_to_fb(obj);
1519 		}
1520 
1521 		mode = drm_mode_create(dev);
1522 		drm_crtc_convert_umode(mode, &crtc_req->mode);
1523 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1524 	}
1525 
1526 	if (crtc_req->count_connectors == 0 && mode) {
1527 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1528 		ret = -EINVAL;
1529 		goto out;
1530 	}
1531 
1532 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1533 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1534 			  crtc_req->count_connectors);
1535 		ret = -EINVAL;
1536 		goto out;
1537 	}
1538 
1539 	if (crtc_req->count_connectors > 0) {
1540 		u32 out_id;
1541 
1542 		/* Avoid unbounded kernel memory allocation */
1543 		if (crtc_req->count_connectors > config->num_connector) {
1544 			ret = -EINVAL;
1545 			goto out;
1546 		}
1547 
1548 		connector_set = kmalloc(crtc_req->count_connectors *
1549 					sizeof(struct drm_connector *),
1550 					GFP_KERNEL);
1551 		if (!connector_set) {
1552 			ret = -ENOMEM;
1553 			goto out;
1554 		}
1555 
1556 		for (i = 0; i < crtc_req->count_connectors; i++) {
1557 			set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1558 			if (get_user(out_id, &set_connectors_ptr[i])) {
1559 				ret = -EFAULT;
1560 				goto out;
1561 			}
1562 
1563 			obj = drm_mode_object_find(dev, out_id,
1564 						   DRM_MODE_OBJECT_CONNECTOR);
1565 			if (!obj) {
1566 				DRM_DEBUG_KMS("Connector id %d unknown\n",
1567 						out_id);
1568 				ret = -EINVAL;
1569 				goto out;
1570 			}
1571 			connector = obj_to_connector(obj);
1572 
1573 			connector_set[i] = connector;
1574 		}
1575 	}
1576 
1577 	set.crtc = crtc;
1578 	set.x = crtc_req->x;
1579 	set.y = crtc_req->y;
1580 	set.mode = mode;
1581 	set.connectors = connector_set;
1582 	set.num_connectors = crtc_req->count_connectors;
1583 	set.fb = fb;
1584 	ret = crtc->funcs->set_config(&set);
1585 
1586 out:
1587 	kfree(connector_set);
1588 	mutex_unlock(&dev->mode_config.mutex);
1589 	return ret;
1590 }
1591 
1592 int drm_mode_cursor_ioctl(struct drm_device *dev,
1593 			void *data, struct drm_file *file_priv)
1594 {
1595 	struct drm_mode_cursor *req = data;
1596 	struct drm_mode_object *obj;
1597 	struct drm_crtc *crtc;
1598 	int ret = 0;
1599 
1600 	if (!req->flags) {
1601 		DRM_ERROR("no operation set\n");
1602 		return -EINVAL;
1603 	}
1604 
1605 	mutex_lock(&dev->mode_config.mutex);
1606 	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1607 	if (!obj) {
1608 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1609 		ret = -EINVAL;
1610 		goto out;
1611 	}
1612 	crtc = obj_to_crtc(obj);
1613 
1614 	if (req->flags & DRM_MODE_CURSOR_BO) {
1615 		if (!crtc->funcs->cursor_set) {
1616 			DRM_ERROR("crtc does not support cursor\n");
1617 			ret = -ENXIO;
1618 			goto out;
1619 		}
1620 		/* Turns off the cursor if handle is 0 */
1621 		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1622 					      req->width, req->height);
1623 	}
1624 
1625 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
1626 		if (crtc->funcs->cursor_move) {
1627 			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1628 		} else {
1629 			DRM_ERROR("crtc does not support cursor\n");
1630 			ret = -EFAULT;
1631 			goto out;
1632 		}
1633 	}
1634 out:
1635 	mutex_unlock(&dev->mode_config.mutex);
1636 	return ret;
1637 }
1638 
1639 /**
1640  * drm_mode_addfb - add an FB to the graphics configuration
1641  * @inode: inode from the ioctl
1642  * @filp: file * from the ioctl
1643  * @cmd: cmd from ioctl
1644  * @arg: arg from ioctl
1645  *
1646  * LOCKING:
1647  * Takes mode config lock.
1648  *
1649  * Add a new FB to the specified CRTC, given a user request.
1650  *
1651  * Called by the user via ioctl.
1652  *
1653  * RETURNS:
1654  * Zero on success, errno on failure.
1655  */
1656 int drm_mode_addfb(struct drm_device *dev,
1657 		   void *data, struct drm_file *file_priv)
1658 {
1659 	struct drm_mode_fb_cmd *r = data;
1660 	struct drm_mode_config *config = &dev->mode_config;
1661 	struct drm_framebuffer *fb;
1662 	int ret = 0;
1663 
1664 	if ((config->min_width > r->width) || (r->width > config->max_width)) {
1665 		DRM_ERROR("mode new framebuffer width not within limits\n");
1666 		return -EINVAL;
1667 	}
1668 	if ((config->min_height > r->height) || (r->height > config->max_height)) {
1669 		DRM_ERROR("mode new framebuffer height not within limits\n");
1670 		return -EINVAL;
1671 	}
1672 
1673 	mutex_lock(&dev->mode_config.mutex);
1674 
1675 	/* TODO check buffer is sufficently large */
1676 	/* TODO setup destructor callback */
1677 
1678 	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1679 	if (!fb) {
1680 		DRM_ERROR("could not create framebuffer\n");
1681 		ret = -EINVAL;
1682 		goto out;
1683 	}
1684 
1685 	r->fb_id = fb->base.id;
1686 	list_add(&fb->filp_head, &file_priv->fbs);
1687 
1688 out:
1689 	mutex_unlock(&dev->mode_config.mutex);
1690 	return ret;
1691 }
1692 
1693 /**
1694  * drm_mode_rmfb - remove an FB from the configuration
1695  * @inode: inode from the ioctl
1696  * @filp: file * from the ioctl
1697  * @cmd: cmd from ioctl
1698  * @arg: arg from ioctl
1699  *
1700  * LOCKING:
1701  * Takes mode config lock.
1702  *
1703  * Remove the FB specified by the user.
1704  *
1705  * Called by the user via ioctl.
1706  *
1707  * RETURNS:
1708  * Zero on success, errno on failure.
1709  */
1710 int drm_mode_rmfb(struct drm_device *dev,
1711 		   void *data, struct drm_file *file_priv)
1712 {
1713 	struct drm_mode_object *obj;
1714 	struct drm_framebuffer *fb = NULL;
1715 	struct drm_framebuffer *fbl = NULL;
1716 	uint32_t *id = data;
1717 	int ret = 0;
1718 	int found = 0;
1719 
1720 	mutex_lock(&dev->mode_config.mutex);
1721 	obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1722 	/* TODO check that we realy get a framebuffer back. */
1723 	if (!obj) {
1724 		DRM_ERROR("mode invalid framebuffer id\n");
1725 		ret = -EINVAL;
1726 		goto out;
1727 	}
1728 	fb = obj_to_fb(obj);
1729 
1730 	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1731 		if (fb == fbl)
1732 			found = 1;
1733 
1734 	if (!found) {
1735 		DRM_ERROR("tried to remove a fb that we didn't own\n");
1736 		ret = -EINVAL;
1737 		goto out;
1738 	}
1739 
1740 	/* TODO release all crtc connected to the framebuffer */
1741 	/* TODO unhock the destructor from the buffer object */
1742 
1743 	list_del(&fb->filp_head);
1744 	fb->funcs->destroy(fb);
1745 
1746 out:
1747 	mutex_unlock(&dev->mode_config.mutex);
1748 	return ret;
1749 }
1750 
1751 /**
1752  * drm_mode_getfb - get FB info
1753  * @inode: inode from the ioctl
1754  * @filp: file * from the ioctl
1755  * @cmd: cmd from ioctl
1756  * @arg: arg from ioctl
1757  *
1758  * LOCKING:
1759  * Caller? (FIXME)
1760  *
1761  * Lookup the FB given its ID and return info about it.
1762  *
1763  * Called by the user via ioctl.
1764  *
1765  * RETURNS:
1766  * Zero on success, errno on failure.
1767  */
1768 int drm_mode_getfb(struct drm_device *dev,
1769 		   void *data, struct drm_file *file_priv)
1770 {
1771 	struct drm_mode_fb_cmd *r = data;
1772 	struct drm_mode_object *obj;
1773 	struct drm_framebuffer *fb;
1774 	int ret = 0;
1775 
1776 	mutex_lock(&dev->mode_config.mutex);
1777 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1778 	if (!obj) {
1779 		DRM_ERROR("invalid framebuffer id\n");
1780 		ret = -EINVAL;
1781 		goto out;
1782 	}
1783 	fb = obj_to_fb(obj);
1784 
1785 	r->height = fb->height;
1786 	r->width = fb->width;
1787 	r->depth = fb->depth;
1788 	r->bpp = fb->bits_per_pixel;
1789 	r->pitch = fb->pitch;
1790 	fb->funcs->create_handle(fb, file_priv, &r->handle);
1791 
1792 out:
1793 	mutex_unlock(&dev->mode_config.mutex);
1794 	return ret;
1795 }
1796 
1797 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1798 			   void *data, struct drm_file *file_priv)
1799 {
1800 	struct drm_clip_rect __user *clips_ptr;
1801 	struct drm_clip_rect *clips = NULL;
1802 	struct drm_mode_fb_dirty_cmd *r = data;
1803 	struct drm_mode_object *obj;
1804 	struct drm_framebuffer *fb;
1805 	unsigned flags;
1806 	int num_clips;
1807 	int ret = 0;
1808 
1809 	mutex_lock(&dev->mode_config.mutex);
1810 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1811 	if (!obj) {
1812 		DRM_ERROR("invalid framebuffer id\n");
1813 		ret = -EINVAL;
1814 		goto out_err1;
1815 	}
1816 	fb = obj_to_fb(obj);
1817 
1818 	num_clips = r->num_clips;
1819 	clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1820 
1821 	if (!num_clips != !clips_ptr) {
1822 		ret = -EINVAL;
1823 		goto out_err1;
1824 	}
1825 
1826 	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1827 
1828 	/* If userspace annotates copy, clips must come in pairs */
1829 	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1830 		ret = -EINVAL;
1831 		goto out_err1;
1832 	}
1833 
1834 	if (num_clips && clips_ptr) {
1835 		clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1836 		if (!clips) {
1837 			ret = -ENOMEM;
1838 			goto out_err1;
1839 		}
1840 
1841 		ret = copy_from_user(clips, clips_ptr,
1842 				     num_clips * sizeof(*clips));
1843 		if (ret) {
1844 			ret = -EFAULT;
1845 			goto out_err2;
1846 		}
1847 	}
1848 
1849 	if (fb->funcs->dirty) {
1850 		ret = fb->funcs->dirty(fb, flags, r->color, clips, num_clips);
1851 	} else {
1852 		ret = -ENOSYS;
1853 		goto out_err2;
1854 	}
1855 
1856 out_err2:
1857 	kfree(clips);
1858 out_err1:
1859 	mutex_unlock(&dev->mode_config.mutex);
1860 	return ret;
1861 }
1862 
1863 
1864 /**
1865  * drm_fb_release - remove and free the FBs on this file
1866  * @filp: file * from the ioctl
1867  *
1868  * LOCKING:
1869  * Takes mode config lock.
1870  *
1871  * Destroy all the FBs associated with @filp.
1872  *
1873  * Called by the user via ioctl.
1874  *
1875  * RETURNS:
1876  * Zero on success, errno on failure.
1877  */
1878 void drm_fb_release(struct drm_file *priv)
1879 {
1880 	struct drm_device *dev = priv->minor->dev;
1881 	struct drm_framebuffer *fb, *tfb;
1882 
1883 	mutex_lock(&dev->mode_config.mutex);
1884 	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1885 		list_del(&fb->filp_head);
1886 		fb->funcs->destroy(fb);
1887 	}
1888 	mutex_unlock(&dev->mode_config.mutex);
1889 }
1890 
1891 /**
1892  * drm_mode_attachmode - add a mode to the user mode list
1893  * @dev: DRM device
1894  * @connector: connector to add the mode to
1895  * @mode: mode to add
1896  *
1897  * Add @mode to @connector's user mode list.
1898  */
1899 static int drm_mode_attachmode(struct drm_device *dev,
1900 			       struct drm_connector *connector,
1901 			       struct drm_display_mode *mode)
1902 {
1903 	int ret = 0;
1904 
1905 	list_add_tail(&mode->head, &connector->user_modes);
1906 	return ret;
1907 }
1908 
1909 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1910 			     struct drm_display_mode *mode)
1911 {
1912 	struct drm_connector *connector;
1913 	int ret = 0;
1914 	struct drm_display_mode *dup_mode;
1915 	int need_dup = 0;
1916 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1917 		if (!connector->encoder)
1918 			break;
1919 		if (connector->encoder->crtc == crtc) {
1920 			if (need_dup)
1921 				dup_mode = drm_mode_duplicate(dev, mode);
1922 			else
1923 				dup_mode = mode;
1924 			ret = drm_mode_attachmode(dev, connector, dup_mode);
1925 			if (ret)
1926 				return ret;
1927 			need_dup = 1;
1928 		}
1929 	}
1930 	return 0;
1931 }
1932 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1933 
1934 static int drm_mode_detachmode(struct drm_device *dev,
1935 			       struct drm_connector *connector,
1936 			       struct drm_display_mode *mode)
1937 {
1938 	int found = 0;
1939 	int ret = 0;
1940 	struct drm_display_mode *match_mode, *t;
1941 
1942 	list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1943 		if (drm_mode_equal(match_mode, mode)) {
1944 			list_del(&match_mode->head);
1945 			drm_mode_destroy(dev, match_mode);
1946 			found = 1;
1947 			break;
1948 		}
1949 	}
1950 
1951 	if (!found)
1952 		ret = -EINVAL;
1953 
1954 	return ret;
1955 }
1956 
1957 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1958 {
1959 	struct drm_connector *connector;
1960 
1961 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1962 		drm_mode_detachmode(dev, connector, mode);
1963 	}
1964 	return 0;
1965 }
1966 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1967 
1968 /**
1969  * drm_fb_attachmode - Attach a user mode to an connector
1970  * @inode: inode from the ioctl
1971  * @filp: file * from the ioctl
1972  * @cmd: cmd from ioctl
1973  * @arg: arg from ioctl
1974  *
1975  * This attaches a user specified mode to an connector.
1976  * Called by the user via ioctl.
1977  *
1978  * RETURNS:
1979  * Zero on success, errno on failure.
1980  */
1981 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1982 			      void *data, struct drm_file *file_priv)
1983 {
1984 	struct drm_mode_mode_cmd *mode_cmd = data;
1985 	struct drm_connector *connector;
1986 	struct drm_display_mode *mode;
1987 	struct drm_mode_object *obj;
1988 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1989 	int ret = 0;
1990 
1991 	mutex_lock(&dev->mode_config.mutex);
1992 
1993 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1994 	if (!obj) {
1995 		ret = -EINVAL;
1996 		goto out;
1997 	}
1998 	connector = obj_to_connector(obj);
1999 
2000 	mode = drm_mode_create(dev);
2001 	if (!mode) {
2002 		ret = -ENOMEM;
2003 		goto out;
2004 	}
2005 
2006 	drm_crtc_convert_umode(mode, umode);
2007 
2008 	ret = drm_mode_attachmode(dev, connector, mode);
2009 out:
2010 	mutex_unlock(&dev->mode_config.mutex);
2011 	return ret;
2012 }
2013 
2014 
2015 /**
2016  * drm_fb_detachmode - Detach a user specified mode from an connector
2017  * @inode: inode from the ioctl
2018  * @filp: file * from the ioctl
2019  * @cmd: cmd from ioctl
2020  * @arg: arg from ioctl
2021  *
2022  * Called by the user via ioctl.
2023  *
2024  * RETURNS:
2025  * Zero on success, errno on failure.
2026  */
2027 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2028 			      void *data, struct drm_file *file_priv)
2029 {
2030 	struct drm_mode_object *obj;
2031 	struct drm_mode_mode_cmd *mode_cmd = data;
2032 	struct drm_connector *connector;
2033 	struct drm_display_mode mode;
2034 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2035 	int ret = 0;
2036 
2037 	mutex_lock(&dev->mode_config.mutex);
2038 
2039 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2040 	if (!obj) {
2041 		ret = -EINVAL;
2042 		goto out;
2043 	}
2044 	connector = obj_to_connector(obj);
2045 
2046 	drm_crtc_convert_umode(&mode, umode);
2047 	ret = drm_mode_detachmode(dev, connector, &mode);
2048 out:
2049 	mutex_unlock(&dev->mode_config.mutex);
2050 	return ret;
2051 }
2052 
2053 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2054 					 const char *name, int num_values)
2055 {
2056 	struct drm_property *property = NULL;
2057 
2058 	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2059 	if (!property)
2060 		return NULL;
2061 
2062 	if (num_values) {
2063 		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2064 		if (!property->values)
2065 			goto fail;
2066 	}
2067 
2068 	drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2069 	property->flags = flags;
2070 	property->num_values = num_values;
2071 	INIT_LIST_HEAD(&property->enum_blob_list);
2072 
2073 	if (name)
2074 		strncpy(property->name, name, DRM_PROP_NAME_LEN);
2075 
2076 	list_add_tail(&property->head, &dev->mode_config.property_list);
2077 	return property;
2078 fail:
2079 	kfree(property);
2080 	return NULL;
2081 }
2082 EXPORT_SYMBOL(drm_property_create);
2083 
2084 int drm_property_add_enum(struct drm_property *property, int index,
2085 			  uint64_t value, const char *name)
2086 {
2087 	struct drm_property_enum *prop_enum;
2088 
2089 	if (!(property->flags & DRM_MODE_PROP_ENUM))
2090 		return -EINVAL;
2091 
2092 	if (!list_empty(&property->enum_blob_list)) {
2093 		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2094 			if (prop_enum->value == value) {
2095 				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2096 				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2097 				return 0;
2098 			}
2099 		}
2100 	}
2101 
2102 	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2103 	if (!prop_enum)
2104 		return -ENOMEM;
2105 
2106 	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2107 	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2108 	prop_enum->value = value;
2109 
2110 	property->values[index] = value;
2111 	list_add_tail(&prop_enum->head, &property->enum_blob_list);
2112 	return 0;
2113 }
2114 EXPORT_SYMBOL(drm_property_add_enum);
2115 
2116 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2117 {
2118 	struct drm_property_enum *prop_enum, *pt;
2119 
2120 	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2121 		list_del(&prop_enum->head);
2122 		kfree(prop_enum);
2123 	}
2124 
2125 	if (property->num_values)
2126 		kfree(property->values);
2127 	drm_mode_object_put(dev, &property->base);
2128 	list_del(&property->head);
2129 	kfree(property);
2130 }
2131 EXPORT_SYMBOL(drm_property_destroy);
2132 
2133 int drm_connector_attach_property(struct drm_connector *connector,
2134 			       struct drm_property *property, uint64_t init_val)
2135 {
2136 	int i;
2137 
2138 	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2139 		if (connector->property_ids[i] == 0) {
2140 			connector->property_ids[i] = property->base.id;
2141 			connector->property_values[i] = init_val;
2142 			break;
2143 		}
2144 	}
2145 
2146 	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2147 		return -EINVAL;
2148 	return 0;
2149 }
2150 EXPORT_SYMBOL(drm_connector_attach_property);
2151 
2152 int drm_connector_property_set_value(struct drm_connector *connector,
2153 				  struct drm_property *property, uint64_t value)
2154 {
2155 	int i;
2156 
2157 	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2158 		if (connector->property_ids[i] == property->base.id) {
2159 			connector->property_values[i] = value;
2160 			break;
2161 		}
2162 	}
2163 
2164 	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2165 		return -EINVAL;
2166 	return 0;
2167 }
2168 EXPORT_SYMBOL(drm_connector_property_set_value);
2169 
2170 int drm_connector_property_get_value(struct drm_connector *connector,
2171 				  struct drm_property *property, uint64_t *val)
2172 {
2173 	int i;
2174 
2175 	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2176 		if (connector->property_ids[i] == property->base.id) {
2177 			*val = connector->property_values[i];
2178 			break;
2179 		}
2180 	}
2181 
2182 	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2183 		return -EINVAL;
2184 	return 0;
2185 }
2186 EXPORT_SYMBOL(drm_connector_property_get_value);
2187 
2188 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2189 			       void *data, struct drm_file *file_priv)
2190 {
2191 	struct drm_mode_object *obj;
2192 	struct drm_mode_get_property *out_resp = data;
2193 	struct drm_property *property;
2194 	int enum_count = 0;
2195 	int blob_count = 0;
2196 	int value_count = 0;
2197 	int ret = 0, i;
2198 	int copied;
2199 	struct drm_property_enum *prop_enum;
2200 	struct drm_mode_property_enum __user *enum_ptr;
2201 	struct drm_property_blob *prop_blob;
2202 	uint32_t *blob_id_ptr;
2203 	uint64_t __user *values_ptr;
2204 	uint32_t __user *blob_length_ptr;
2205 
2206 	mutex_lock(&dev->mode_config.mutex);
2207 	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2208 	if (!obj) {
2209 		ret = -EINVAL;
2210 		goto done;
2211 	}
2212 	property = obj_to_property(obj);
2213 
2214 	if (property->flags & DRM_MODE_PROP_ENUM) {
2215 		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2216 			enum_count++;
2217 	} else if (property->flags & DRM_MODE_PROP_BLOB) {
2218 		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2219 			blob_count++;
2220 	}
2221 
2222 	value_count = property->num_values;
2223 
2224 	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2225 	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2226 	out_resp->flags = property->flags;
2227 
2228 	if ((out_resp->count_values >= value_count) && value_count) {
2229 		values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2230 		for (i = 0; i < value_count; i++) {
2231 			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2232 				ret = -EFAULT;
2233 				goto done;
2234 			}
2235 		}
2236 	}
2237 	out_resp->count_values = value_count;
2238 
2239 	if (property->flags & DRM_MODE_PROP_ENUM) {
2240 		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2241 			copied = 0;
2242 			enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2243 			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2244 
2245 				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2246 					ret = -EFAULT;
2247 					goto done;
2248 				}
2249 
2250 				if (copy_to_user(&enum_ptr[copied].name,
2251 						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2252 					ret = -EFAULT;
2253 					goto done;
2254 				}
2255 				copied++;
2256 			}
2257 		}
2258 		out_resp->count_enum_blobs = enum_count;
2259 	}
2260 
2261 	if (property->flags & DRM_MODE_PROP_BLOB) {
2262 		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2263 			copied = 0;
2264 			blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2265 			blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2266 
2267 			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2268 				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2269 					ret = -EFAULT;
2270 					goto done;
2271 				}
2272 
2273 				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2274 					ret = -EFAULT;
2275 					goto done;
2276 				}
2277 
2278 				copied++;
2279 			}
2280 		}
2281 		out_resp->count_enum_blobs = blob_count;
2282 	}
2283 done:
2284 	mutex_unlock(&dev->mode_config.mutex);
2285 	return ret;
2286 }
2287 
2288 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2289 							  void *data)
2290 {
2291 	struct drm_property_blob *blob;
2292 
2293 	if (!length || !data)
2294 		return NULL;
2295 
2296 	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2297 	if (!blob)
2298 		return NULL;
2299 
2300 	blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2301 	blob->length = length;
2302 
2303 	memcpy(blob->data, data, length);
2304 
2305 	drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2306 
2307 	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2308 	return blob;
2309 }
2310 
2311 static void drm_property_destroy_blob(struct drm_device *dev,
2312 			       struct drm_property_blob *blob)
2313 {
2314 	drm_mode_object_put(dev, &blob->base);
2315 	list_del(&blob->head);
2316 	kfree(blob);
2317 }
2318 
2319 int drm_mode_getblob_ioctl(struct drm_device *dev,
2320 			   void *data, struct drm_file *file_priv)
2321 {
2322 	struct drm_mode_object *obj;
2323 	struct drm_mode_get_blob *out_resp = data;
2324 	struct drm_property_blob *blob;
2325 	int ret = 0;
2326 	void *blob_ptr;
2327 
2328 	mutex_lock(&dev->mode_config.mutex);
2329 	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2330 	if (!obj) {
2331 		ret = -EINVAL;
2332 		goto done;
2333 	}
2334 	blob = obj_to_blob(obj);
2335 
2336 	if (out_resp->length == blob->length) {
2337 		blob_ptr = (void *)(unsigned long)out_resp->data;
2338 		if (copy_to_user(blob_ptr, blob->data, blob->length)){
2339 			ret = -EFAULT;
2340 			goto done;
2341 		}
2342 	}
2343 	out_resp->length = blob->length;
2344 
2345 done:
2346 	mutex_unlock(&dev->mode_config.mutex);
2347 	return ret;
2348 }
2349 
2350 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2351 					    struct edid *edid)
2352 {
2353 	struct drm_device *dev = connector->dev;
2354 	int ret = 0, size;
2355 
2356 	if (connector->edid_blob_ptr)
2357 		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2358 
2359 	/* Delete edid, when there is none. */
2360 	if (!edid) {
2361 		connector->edid_blob_ptr = NULL;
2362 		ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2363 		return ret;
2364 	}
2365 
2366 	size = EDID_LENGTH * (1 + edid->extensions);
2367 	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2368 							    size, edid);
2369 
2370 	ret = drm_connector_property_set_value(connector,
2371 					       dev->mode_config.edid_property,
2372 					       connector->edid_blob_ptr->base.id);
2373 
2374 	return ret;
2375 }
2376 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2377 
2378 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2379 				       void *data, struct drm_file *file_priv)
2380 {
2381 	struct drm_mode_connector_set_property *out_resp = data;
2382 	struct drm_mode_object *obj;
2383 	struct drm_property *property;
2384 	struct drm_connector *connector;
2385 	int ret = -EINVAL;
2386 	int i;
2387 
2388 	mutex_lock(&dev->mode_config.mutex);
2389 
2390 	obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2391 	if (!obj) {
2392 		goto out;
2393 	}
2394 	connector = obj_to_connector(obj);
2395 
2396 	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2397 		if (connector->property_ids[i] == out_resp->prop_id)
2398 			break;
2399 	}
2400 
2401 	if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2402 		goto out;
2403 	}
2404 
2405 	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2406 	if (!obj) {
2407 		goto out;
2408 	}
2409 	property = obj_to_property(obj);
2410 
2411 	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2412 		goto out;
2413 
2414 	if (property->flags & DRM_MODE_PROP_RANGE) {
2415 		if (out_resp->value < property->values[0])
2416 			goto out;
2417 
2418 		if (out_resp->value > property->values[1])
2419 			goto out;
2420 	} else {
2421 		int found = 0;
2422 		for (i = 0; i < property->num_values; i++) {
2423 			if (property->values[i] == out_resp->value) {
2424 				found = 1;
2425 				break;
2426 			}
2427 		}
2428 		if (!found) {
2429 			goto out;
2430 		}
2431 	}
2432 
2433 	/* Do DPMS ourselves */
2434 	if (property == connector->dev->mode_config.dpms_property) {
2435 		if (connector->funcs->dpms)
2436 			(*connector->funcs->dpms)(connector, (int) out_resp->value);
2437 		ret = 0;
2438 	} else if (connector->funcs->set_property)
2439 		ret = connector->funcs->set_property(connector, property, out_resp->value);
2440 
2441 	/* store the property value if successful */
2442 	if (!ret)
2443 		drm_connector_property_set_value(connector, property, out_resp->value);
2444 out:
2445 	mutex_unlock(&dev->mode_config.mutex);
2446 	return ret;
2447 }
2448 
2449 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2450 				      struct drm_encoder *encoder)
2451 {
2452 	int i;
2453 
2454 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2455 		if (connector->encoder_ids[i] == 0) {
2456 			connector->encoder_ids[i] = encoder->base.id;
2457 			return 0;
2458 		}
2459 	}
2460 	return -ENOMEM;
2461 }
2462 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2463 
2464 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2465 				    struct drm_encoder *encoder)
2466 {
2467 	int i;
2468 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2469 		if (connector->encoder_ids[i] == encoder->base.id) {
2470 			connector->encoder_ids[i] = 0;
2471 			if (connector->encoder == encoder)
2472 				connector->encoder = NULL;
2473 			break;
2474 		}
2475 	}
2476 }
2477 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2478 
2479 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2480 				  int gamma_size)
2481 {
2482 	crtc->gamma_size = gamma_size;
2483 
2484 	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2485 	if (!crtc->gamma_store) {
2486 		crtc->gamma_size = 0;
2487 		return false;
2488 	}
2489 
2490 	return true;
2491 }
2492 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2493 
2494 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2495 			     void *data, struct drm_file *file_priv)
2496 {
2497 	struct drm_mode_crtc_lut *crtc_lut = data;
2498 	struct drm_mode_object *obj;
2499 	struct drm_crtc *crtc;
2500 	void *r_base, *g_base, *b_base;
2501 	int size;
2502 	int ret = 0;
2503 
2504 	mutex_lock(&dev->mode_config.mutex);
2505 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2506 	if (!obj) {
2507 		ret = -EINVAL;
2508 		goto out;
2509 	}
2510 	crtc = obj_to_crtc(obj);
2511 
2512 	/* memcpy into gamma store */
2513 	if (crtc_lut->gamma_size != crtc->gamma_size) {
2514 		ret = -EINVAL;
2515 		goto out;
2516 	}
2517 
2518 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2519 	r_base = crtc->gamma_store;
2520 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2521 		ret = -EFAULT;
2522 		goto out;
2523 	}
2524 
2525 	g_base = r_base + size;
2526 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2527 		ret = -EFAULT;
2528 		goto out;
2529 	}
2530 
2531 	b_base = g_base + size;
2532 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2533 		ret = -EFAULT;
2534 		goto out;
2535 	}
2536 
2537 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2538 
2539 out:
2540 	mutex_unlock(&dev->mode_config.mutex);
2541 	return ret;
2542 
2543 }
2544 
2545 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2546 			     void *data, struct drm_file *file_priv)
2547 {
2548 	struct drm_mode_crtc_lut *crtc_lut = data;
2549 	struct drm_mode_object *obj;
2550 	struct drm_crtc *crtc;
2551 	void *r_base, *g_base, *b_base;
2552 	int size;
2553 	int ret = 0;
2554 
2555 	mutex_lock(&dev->mode_config.mutex);
2556 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2557 	if (!obj) {
2558 		ret = -EINVAL;
2559 		goto out;
2560 	}
2561 	crtc = obj_to_crtc(obj);
2562 
2563 	/* memcpy into gamma store */
2564 	if (crtc_lut->gamma_size != crtc->gamma_size) {
2565 		ret = -EINVAL;
2566 		goto out;
2567 	}
2568 
2569 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2570 	r_base = crtc->gamma_store;
2571 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2572 		ret = -EFAULT;
2573 		goto out;
2574 	}
2575 
2576 	g_base = r_base + size;
2577 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2578 		ret = -EFAULT;
2579 		goto out;
2580 	}
2581 
2582 	b_base = g_base + size;
2583 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2584 		ret = -EFAULT;
2585 		goto out;
2586 	}
2587 out:
2588 	mutex_unlock(&dev->mode_config.mutex);
2589 	return ret;
2590 }
2591 
2592 int drm_mode_page_flip_ioctl(struct drm_device *dev,
2593 			     void *data, struct drm_file *file_priv)
2594 {
2595 	struct drm_mode_crtc_page_flip *page_flip = data;
2596 	struct drm_mode_object *obj;
2597 	struct drm_crtc *crtc;
2598 	struct drm_framebuffer *fb;
2599 	struct drm_pending_vblank_event *e = NULL;
2600 	unsigned long flags;
2601 	int ret = -EINVAL;
2602 
2603 	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2604 	    page_flip->reserved != 0)
2605 		return -EINVAL;
2606 
2607 	mutex_lock(&dev->mode_config.mutex);
2608 	obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2609 	if (!obj)
2610 		goto out;
2611 	crtc = obj_to_crtc(obj);
2612 
2613 	if (crtc->funcs->page_flip == NULL)
2614 		goto out;
2615 
2616 	obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2617 	if (!obj)
2618 		goto out;
2619 	fb = obj_to_fb(obj);
2620 
2621 	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2622 		ret = -ENOMEM;
2623 		spin_lock_irqsave(&dev->event_lock, flags);
2624 		if (file_priv->event_space < sizeof e->event) {
2625 			spin_unlock_irqrestore(&dev->event_lock, flags);
2626 			goto out;
2627 		}
2628 		file_priv->event_space -= sizeof e->event;
2629 		spin_unlock_irqrestore(&dev->event_lock, flags);
2630 
2631 		e = kzalloc(sizeof *e, GFP_KERNEL);
2632 		if (e == NULL) {
2633 			spin_lock_irqsave(&dev->event_lock, flags);
2634 			file_priv->event_space += sizeof e->event;
2635 			spin_unlock_irqrestore(&dev->event_lock, flags);
2636 			goto out;
2637 		}
2638 
2639 		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2640 		e->event.base.length = sizeof e->event;
2641 		e->event.user_data = page_flip->user_data;
2642 		e->base.event = &e->event.base;
2643 		e->base.file_priv = file_priv;
2644 		e->base.destroy =
2645 			(void (*) (struct drm_pending_event *)) kfree;
2646 	}
2647 
2648 	ret = crtc->funcs->page_flip(crtc, fb, e);
2649 	if (ret) {
2650 		spin_lock_irqsave(&dev->event_lock, flags);
2651 		file_priv->event_space += sizeof e->event;
2652 		spin_unlock_irqrestore(&dev->event_lock, flags);
2653 		kfree(e);
2654 	}
2655 
2656 out:
2657 	mutex_unlock(&dev->mode_config.mutex);
2658 	return ret;
2659 }
2660