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