xref: /linux/drivers/gpu/drm/drm_connector.c (revision 50f0033d1a0f3a8e9eed09ab68067fbb57b0669d)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <drm/drmP.h>
24 #include <drm/drm_connector.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_encoder.h>
27 
28 #include "drm_crtc_internal.h"
29 #include "drm_internal.h"
30 
31 /**
32  * DOC: overview
33  *
34  * In DRM connectors are the general abstraction for display sinks, and include
35  * als fixed panels or anything else that can display pixels in some form. As
36  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
37  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
38  * Hence they are reference-counted using drm_connector_reference() and
39  * drm_connector_unreference().
40  *
41  * KMS driver must create, initialize, register and attach at a struct
42  * &drm_connector for each such sink. The instance is created as other KMS
43  * objects and initialized by setting the following fields.
44  *
45  * The connector is then registered with a call to drm_connector_init() with a
46  * pointer to the connector functions and a connector type, and exposed through
47  * sysfs with a call to drm_connector_register().
48  *
49  * Connectors must be attached to an encoder to be used. For devices that map
50  * connectors to encoders 1:1, the connector should be attached at
51  * initialization time with a call to drm_mode_connector_attach_encoder(). The
52  * driver must also set the struct &drm_connector encoder field to point to the
53  * attached encoder.
54  *
55  * For connectors which are not fixed (like built-in panels) the driver needs to
56  * support hotplug notifications. The simplest way to do that is by using the
57  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
58  * hardware support for hotplug interrupts. Connectors with hardware hotplug
59  * support can instead use e.g. drm_helper_hpd_irq_event().
60  */
61 
62 struct drm_conn_prop_enum_list {
63 	int type;
64 	const char *name;
65 	struct ida ida;
66 };
67 
68 /*
69  * Connector and encoder types.
70  */
71 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
72 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
73 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
74 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
75 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
76 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
77 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
78 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
79 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
80 	{ DRM_MODE_CONNECTOR_Component, "Component" },
81 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
82 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
83 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
84 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
85 	{ DRM_MODE_CONNECTOR_TV, "TV" },
86 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
87 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
88 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
89 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
90 };
91 
92 void drm_connector_ida_init(void)
93 {
94 	int i;
95 
96 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
97 		ida_init(&drm_connector_enum_list[i].ida);
98 }
99 
100 void drm_connector_ida_destroy(void)
101 {
102 	int i;
103 
104 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
105 		ida_destroy(&drm_connector_enum_list[i].ida);
106 }
107 
108 /**
109  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
110  * @connector: connector to quwery
111  *
112  * The kernel supports per-connector configuration of its consoles through
113  * use of the video= parameter. This function parses that option and
114  * extracts the user's specified mode (or enable/disable status) for a
115  * particular connector. This is typically only used during the early fbdev
116  * setup.
117  */
118 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
119 {
120 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
121 	char *option = NULL;
122 
123 	if (fb_get_options(connector->name, &option))
124 		return;
125 
126 	if (!drm_mode_parse_command_line_for_connector(option,
127 						       connector,
128 						       mode))
129 		return;
130 
131 	if (mode->force) {
132 		const char *s;
133 
134 		switch (mode->force) {
135 		case DRM_FORCE_OFF:
136 			s = "OFF";
137 			break;
138 		case DRM_FORCE_ON_DIGITAL:
139 			s = "ON - dig";
140 			break;
141 		default:
142 		case DRM_FORCE_ON:
143 			s = "ON";
144 			break;
145 		}
146 
147 		DRM_INFO("forcing %s connector %s\n", connector->name, s);
148 		connector->force = mode->force;
149 	}
150 
151 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
152 		      connector->name,
153 		      mode->xres, mode->yres,
154 		      mode->refresh_specified ? mode->refresh : 60,
155 		      mode->rb ? " reduced blanking" : "",
156 		      mode->margins ? " with margins" : "",
157 		      mode->interlace ?  " interlaced" : "");
158 }
159 
160 static void drm_connector_free(struct kref *kref)
161 {
162 	struct drm_connector *connector =
163 		container_of(kref, struct drm_connector, base.refcount);
164 	struct drm_device *dev = connector->dev;
165 
166 	drm_mode_object_unregister(dev, &connector->base);
167 	connector->funcs->destroy(connector);
168 }
169 
170 /**
171  * drm_connector_init - Init a preallocated connector
172  * @dev: DRM device
173  * @connector: the connector to init
174  * @funcs: callbacks for this connector
175  * @connector_type: user visible type of the connector
176  *
177  * Initialises a preallocated connector. Connectors should be
178  * subclassed as part of driver connector objects.
179  *
180  * Returns:
181  * Zero on success, error code on failure.
182  */
183 int drm_connector_init(struct drm_device *dev,
184 		       struct drm_connector *connector,
185 		       const struct drm_connector_funcs *funcs,
186 		       int connector_type)
187 {
188 	struct drm_mode_config *config = &dev->mode_config;
189 	int ret;
190 	struct ida *connector_ida =
191 		&drm_connector_enum_list[connector_type].ida;
192 
193 	ret = drm_mode_object_get_reg(dev, &connector->base,
194 				      DRM_MODE_OBJECT_CONNECTOR,
195 				      false, drm_connector_free);
196 	if (ret)
197 		return ret;
198 
199 	connector->base.properties = &connector->properties;
200 	connector->dev = dev;
201 	connector->funcs = funcs;
202 
203 	ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
204 	if (ret < 0)
205 		goto out_put;
206 	connector->index = ret;
207 	ret = 0;
208 
209 	connector->connector_type = connector_type;
210 	connector->connector_type_id =
211 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
212 	if (connector->connector_type_id < 0) {
213 		ret = connector->connector_type_id;
214 		goto out_put_id;
215 	}
216 	connector->name =
217 		kasprintf(GFP_KERNEL, "%s-%d",
218 			  drm_connector_enum_list[connector_type].name,
219 			  connector->connector_type_id);
220 	if (!connector->name) {
221 		ret = -ENOMEM;
222 		goto out_put_type_id;
223 	}
224 
225 	INIT_LIST_HEAD(&connector->probed_modes);
226 	INIT_LIST_HEAD(&connector->modes);
227 	mutex_init(&connector->mutex);
228 	connector->edid_blob_ptr = NULL;
229 	connector->status = connector_status_unknown;
230 
231 	drm_connector_get_cmdline_mode(connector);
232 
233 	/* We should add connectors at the end to avoid upsetting the connector
234 	 * index too much. */
235 	spin_lock_irq(&config->connector_list_lock);
236 	list_add_tail(&connector->head, &config->connector_list);
237 	config->num_connector++;
238 	spin_unlock_irq(&config->connector_list_lock);
239 
240 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
241 		drm_object_attach_property(&connector->base,
242 					      config->edid_property,
243 					      0);
244 
245 	drm_object_attach_property(&connector->base,
246 				      config->dpms_property, 0);
247 
248 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
249 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
250 	}
251 
252 	connector->debugfs_entry = NULL;
253 out_put_type_id:
254 	if (ret)
255 		ida_simple_remove(connector_ida, connector->connector_type_id);
256 out_put_id:
257 	if (ret)
258 		ida_simple_remove(&config->connector_ida, connector->index);
259 out_put:
260 	if (ret)
261 		drm_mode_object_unregister(dev, &connector->base);
262 
263 	return ret;
264 }
265 EXPORT_SYMBOL(drm_connector_init);
266 
267 /**
268  * drm_mode_connector_attach_encoder - attach a connector to an encoder
269  * @connector: connector to attach
270  * @encoder: encoder to attach @connector to
271  *
272  * This function links up a connector to an encoder. Note that the routing
273  * restrictions between encoders and crtcs are exposed to userspace through the
274  * possible_clones and possible_crtcs bitmasks.
275  *
276  * Returns:
277  * Zero on success, negative errno on failure.
278  */
279 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
280 				      struct drm_encoder *encoder)
281 {
282 	int i;
283 
284 	/*
285 	 * In the past, drivers have attempted to model the static association
286 	 * of connector to encoder in simple connector/encoder devices using a
287 	 * direct assignment of connector->encoder = encoder. This connection
288 	 * is a logical one and the responsibility of the core, so drivers are
289 	 * expected not to mess with this.
290 	 *
291 	 * Note that the error return should've been enough here, but a large
292 	 * majority of drivers ignores the return value, so add in a big WARN
293 	 * to get people's attention.
294 	 */
295 	if (WARN_ON(connector->encoder))
296 		return -EINVAL;
297 
298 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
299 		if (connector->encoder_ids[i] == 0) {
300 			connector->encoder_ids[i] = encoder->base.id;
301 			return 0;
302 		}
303 	}
304 	return -ENOMEM;
305 }
306 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
307 
308 static void drm_mode_remove(struct drm_connector *connector,
309 			    struct drm_display_mode *mode)
310 {
311 	list_del(&mode->head);
312 	drm_mode_destroy(connector->dev, mode);
313 }
314 
315 /**
316  * drm_connector_cleanup - cleans up an initialised connector
317  * @connector: connector to cleanup
318  *
319  * Cleans up the connector but doesn't free the object.
320  */
321 void drm_connector_cleanup(struct drm_connector *connector)
322 {
323 	struct drm_device *dev = connector->dev;
324 	struct drm_display_mode *mode, *t;
325 
326 	/* The connector should have been removed from userspace long before
327 	 * it is finally destroyed.
328 	 */
329 	if (WARN_ON(connector->registered))
330 		drm_connector_unregister(connector);
331 
332 	if (connector->tile_group) {
333 		drm_mode_put_tile_group(dev, connector->tile_group);
334 		connector->tile_group = NULL;
335 	}
336 
337 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
338 		drm_mode_remove(connector, mode);
339 
340 	list_for_each_entry_safe(mode, t, &connector->modes, head)
341 		drm_mode_remove(connector, mode);
342 
343 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
344 			  connector->connector_type_id);
345 
346 	ida_simple_remove(&dev->mode_config.connector_ida,
347 			  connector->index);
348 
349 	kfree(connector->display_info.bus_formats);
350 	drm_mode_object_unregister(dev, &connector->base);
351 	kfree(connector->name);
352 	connector->name = NULL;
353 	spin_lock_irq(&dev->mode_config.connector_list_lock);
354 	list_del(&connector->head);
355 	dev->mode_config.num_connector--;
356 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
357 
358 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
359 	if (connector->state && connector->funcs->atomic_destroy_state)
360 		connector->funcs->atomic_destroy_state(connector,
361 						       connector->state);
362 
363 	mutex_destroy(&connector->mutex);
364 
365 	memset(connector, 0, sizeof(*connector));
366 }
367 EXPORT_SYMBOL(drm_connector_cleanup);
368 
369 /**
370  * drm_connector_register - register a connector
371  * @connector: the connector to register
372  *
373  * Register userspace interfaces for a connector
374  *
375  * Returns:
376  * Zero on success, error code on failure.
377  */
378 int drm_connector_register(struct drm_connector *connector)
379 {
380 	int ret = 0;
381 
382 	mutex_lock(&connector->mutex);
383 	if (connector->registered)
384 		goto unlock;
385 
386 	ret = drm_sysfs_connector_add(connector);
387 	if (ret)
388 		goto unlock;
389 
390 	ret = drm_debugfs_connector_add(connector);
391 	if (ret) {
392 		goto err_sysfs;
393 	}
394 
395 	if (connector->funcs->late_register) {
396 		ret = connector->funcs->late_register(connector);
397 		if (ret)
398 			goto err_debugfs;
399 	}
400 
401 	drm_mode_object_register(connector->dev, &connector->base);
402 
403 	connector->registered = true;
404 	goto unlock;
405 
406 err_debugfs:
407 	drm_debugfs_connector_remove(connector);
408 err_sysfs:
409 	drm_sysfs_connector_remove(connector);
410 unlock:
411 	mutex_unlock(&connector->mutex);
412 	return ret;
413 }
414 EXPORT_SYMBOL(drm_connector_register);
415 
416 /**
417  * drm_connector_unregister - unregister a connector
418  * @connector: the connector to unregister
419  *
420  * Unregister userspace interfaces for a connector
421  */
422 void drm_connector_unregister(struct drm_connector *connector)
423 {
424 	mutex_lock(&connector->mutex);
425 	if (!connector->registered) {
426 		mutex_unlock(&connector->mutex);
427 		return;
428 	}
429 
430 	if (connector->funcs->early_unregister)
431 		connector->funcs->early_unregister(connector);
432 
433 	drm_sysfs_connector_remove(connector);
434 	drm_debugfs_connector_remove(connector);
435 
436 	connector->registered = false;
437 	mutex_unlock(&connector->mutex);
438 }
439 EXPORT_SYMBOL(drm_connector_unregister);
440 
441 void drm_connector_unregister_all(struct drm_device *dev)
442 {
443 	struct drm_connector *connector;
444 	struct drm_connector_list_iter conn_iter;
445 
446 	drm_connector_list_iter_get(dev, &conn_iter);
447 	drm_for_each_connector_iter(connector, &conn_iter)
448 		drm_connector_unregister(connector);
449 	drm_connector_list_iter_put(&conn_iter);
450 }
451 
452 int drm_connector_register_all(struct drm_device *dev)
453 {
454 	struct drm_connector *connector;
455 	struct drm_connector_list_iter conn_iter;
456 	int ret = 0;
457 
458 	drm_connector_list_iter_get(dev, &conn_iter);
459 	drm_for_each_connector_iter(connector, &conn_iter) {
460 		ret = drm_connector_register(connector);
461 		if (ret)
462 			break;
463 	}
464 	drm_connector_list_iter_put(&conn_iter);
465 
466 	if (ret)
467 		drm_connector_unregister_all(dev);
468 	return ret;
469 }
470 
471 /**
472  * drm_get_connector_status_name - return a string for connector status
473  * @status: connector status to compute name of
474  *
475  * In contrast to the other drm_get_*_name functions this one here returns a
476  * const pointer and hence is threadsafe.
477  */
478 const char *drm_get_connector_status_name(enum drm_connector_status status)
479 {
480 	if (status == connector_status_connected)
481 		return "connected";
482 	else if (status == connector_status_disconnected)
483 		return "disconnected";
484 	else
485 		return "unknown";
486 }
487 EXPORT_SYMBOL(drm_get_connector_status_name);
488 
489 #ifdef CONFIG_LOCKDEP
490 static struct lockdep_map connector_list_iter_dep_map = {
491 	.name = "drm_connector_list_iter"
492 };
493 #endif
494 
495 /**
496  * drm_connector_list_iter_get - initialize a connector_list iterator
497  * @dev: DRM device
498  * @iter: connector_list iterator
499  *
500  * Sets @iter up to walk the connector list in &drm_mode_config of @dev. @iter
501  * must always be cleaned up again by calling drm_connector_list_iter_put().
502  * Iteration itself happens using drm_connector_list_iter_next() or
503  * drm_for_each_connector_iter().
504  */
505 void drm_connector_list_iter_get(struct drm_device *dev,
506 				 struct drm_connector_list_iter *iter)
507 {
508 	iter->dev = dev;
509 	iter->conn = NULL;
510 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
511 }
512 EXPORT_SYMBOL(drm_connector_list_iter_get);
513 
514 /**
515  * drm_connector_list_iter_next - return next connector
516  * @iter: connectr_list iterator
517  *
518  * Returns the next connector for @iter, or NULL when the list walk has
519  * completed.
520  */
521 struct drm_connector *
522 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
523 {
524 	struct drm_connector *old_conn = iter->conn;
525 	struct drm_mode_config *config = &iter->dev->mode_config;
526 	struct list_head *lhead;
527 	unsigned long flags;
528 
529 	spin_lock_irqsave(&config->connector_list_lock, flags);
530 	lhead = old_conn ? &old_conn->head : &config->connector_list;
531 
532 	do {
533 		if (lhead->next == &config->connector_list) {
534 			iter->conn = NULL;
535 			break;
536 		}
537 
538 		lhead = lhead->next;
539 		iter->conn = list_entry(lhead, struct drm_connector, head);
540 
541 		/* loop until it's not a zombie connector */
542 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
543 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
544 
545 	if (old_conn)
546 		drm_connector_unreference(old_conn);
547 
548 	return iter->conn;
549 }
550 EXPORT_SYMBOL(drm_connector_list_iter_next);
551 
552 /**
553  * drm_connector_list_iter_put - tear down a connector_list iterator
554  * @iter: connector_list iterator
555  *
556  * Tears down @iter and releases any resources (like &drm_connector references)
557  * acquired while walking the list. This must always be called, both when the
558  * iteration completes fully or when it was aborted without walking the entire
559  * list.
560  */
561 void drm_connector_list_iter_put(struct drm_connector_list_iter *iter)
562 {
563 	iter->dev = NULL;
564 	if (iter->conn)
565 		drm_connector_unreference(iter->conn);
566 	lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
567 }
568 EXPORT_SYMBOL(drm_connector_list_iter_put);
569 
570 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
571 	{ SubPixelUnknown, "Unknown" },
572 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
573 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
574 	{ SubPixelVerticalRGB, "Vertical RGB" },
575 	{ SubPixelVerticalBGR, "Vertical BGR" },
576 	{ SubPixelNone, "None" },
577 };
578 
579 /**
580  * drm_get_subpixel_order_name - return a string for a given subpixel enum
581  * @order: enum of subpixel_order
582  *
583  * Note you could abuse this and return something out of bounds, but that
584  * would be a caller error.  No unscrubbed user data should make it here.
585  */
586 const char *drm_get_subpixel_order_name(enum subpixel_order order)
587 {
588 	return drm_subpixel_enum_list[order].name;
589 }
590 EXPORT_SYMBOL(drm_get_subpixel_order_name);
591 
592 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
593 	{ DRM_MODE_DPMS_ON, "On" },
594 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
595 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
596 	{ DRM_MODE_DPMS_OFF, "Off" }
597 };
598 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
599 
600 /**
601  * drm_display_info_set_bus_formats - set the supported bus formats
602  * @info: display info to store bus formats in
603  * @formats: array containing the supported bus formats
604  * @num_formats: the number of entries in the fmts array
605  *
606  * Store the supported bus formats in display info structure.
607  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
608  * a full list of available formats.
609  */
610 int drm_display_info_set_bus_formats(struct drm_display_info *info,
611 				     const u32 *formats,
612 				     unsigned int num_formats)
613 {
614 	u32 *fmts = NULL;
615 
616 	if (!formats && num_formats)
617 		return -EINVAL;
618 
619 	if (formats && num_formats) {
620 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
621 			       GFP_KERNEL);
622 		if (!fmts)
623 			return -ENOMEM;
624 	}
625 
626 	kfree(info->bus_formats);
627 	info->bus_formats = fmts;
628 	info->num_bus_formats = num_formats;
629 
630 	return 0;
631 }
632 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
633 
634 /* Optional connector properties. */
635 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
636 	{ DRM_MODE_SCALE_NONE, "None" },
637 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
638 	{ DRM_MODE_SCALE_CENTER, "Center" },
639 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
640 };
641 
642 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
643 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
644 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
645 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
646 };
647 
648 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
649 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
650 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
651 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
652 };
653 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
654 
655 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
656 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
657 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
658 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
659 };
660 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
661 		 drm_dvi_i_subconnector_enum_list)
662 
663 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
664 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
665 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
666 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
667 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
668 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
669 };
670 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
671 
672 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
673 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
674 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
675 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
676 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
677 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
678 };
679 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
680 		 drm_tv_subconnector_enum_list)
681 
682 /**
683  * DOC: standard connector properties
684  *
685  * DRM connectors have a few standardized properties:
686  *
687  * EDID:
688  * 	Blob property which contains the current EDID read from the sink. This
689  * 	is useful to parse sink identification information like vendor, model
690  * 	and serial. Drivers should update this property by calling
691  * 	drm_mode_connector_update_edid_property(), usually after having parsed
692  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
693  * 	property.
694  * DPMS:
695  * 	Legacy property for setting the power state of the connector. For atomic
696  * 	drivers this is only provided for backwards compatibility with existing
697  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
698  * 	connector is linked to. Drivers should never set this property directly,
699  * 	it is handled by the DRM core by calling the ->dpms() callback in
700  * 	&drm_connector_funcs. Atomic drivers should implement this hook using
701  * 	drm_atomic_helper_connector_dpms(). This is the only property standard
702  * 	connector property that userspace can change.
703  * PATH:
704  * 	Connector path property to identify how this sink is physically
705  * 	connected. Used by DP MST. This should be set by calling
706  * 	drm_mode_connector_set_path_property(), in the case of DP MST with the
707  * 	path property the MST manager created. Userspace cannot change this
708  * 	property.
709  * TILE:
710  * 	Connector tile group property to indicate how a set of DRM connector
711  * 	compose together into one logical screen. This is used by both high-res
712  * 	external screens (often only using a single cable, but exposing multiple
713  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
714  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
715  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
716  * 	tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
717  * 	should update this value using drm_mode_connector_set_tile_property().
718  * 	Userspace cannot change this property.
719  *
720  * Connectors also have one standardized atomic property:
721  *
722  * CRTC_ID:
723  * 	Mode object ID of the &drm_crtc this connector should be connected to.
724  */
725 
726 int drm_connector_create_standard_properties(struct drm_device *dev)
727 {
728 	struct drm_property *prop;
729 
730 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
731 				   DRM_MODE_PROP_IMMUTABLE,
732 				   "EDID", 0);
733 	if (!prop)
734 		return -ENOMEM;
735 	dev->mode_config.edid_property = prop;
736 
737 	prop = drm_property_create_enum(dev, 0,
738 				   "DPMS", drm_dpms_enum_list,
739 				   ARRAY_SIZE(drm_dpms_enum_list));
740 	if (!prop)
741 		return -ENOMEM;
742 	dev->mode_config.dpms_property = prop;
743 
744 	prop = drm_property_create(dev,
745 				   DRM_MODE_PROP_BLOB |
746 				   DRM_MODE_PROP_IMMUTABLE,
747 				   "PATH", 0);
748 	if (!prop)
749 		return -ENOMEM;
750 	dev->mode_config.path_property = prop;
751 
752 	prop = drm_property_create(dev,
753 				   DRM_MODE_PROP_BLOB |
754 				   DRM_MODE_PROP_IMMUTABLE,
755 				   "TILE", 0);
756 	if (!prop)
757 		return -ENOMEM;
758 	dev->mode_config.tile_property = prop;
759 
760 	return 0;
761 }
762 
763 /**
764  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
765  * @dev: DRM device
766  *
767  * Called by a driver the first time a DVI-I connector is made.
768  */
769 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
770 {
771 	struct drm_property *dvi_i_selector;
772 	struct drm_property *dvi_i_subconnector;
773 
774 	if (dev->mode_config.dvi_i_select_subconnector_property)
775 		return 0;
776 
777 	dvi_i_selector =
778 		drm_property_create_enum(dev, 0,
779 				    "select subconnector",
780 				    drm_dvi_i_select_enum_list,
781 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
782 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
783 
784 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
785 				    "subconnector",
786 				    drm_dvi_i_subconnector_enum_list,
787 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
788 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
789 
790 	return 0;
791 }
792 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
793 
794 /**
795  * drm_create_tv_properties - create TV specific connector properties
796  * @dev: DRM device
797  * @num_modes: number of different TV formats (modes) supported
798  * @modes: array of pointers to strings containing name of each format
799  *
800  * Called by a driver's TV initialization routine, this function creates
801  * the TV specific connector properties for a given device.  Caller is
802  * responsible for allocating a list of format names and passing them to
803  * this routine.
804  */
805 int drm_mode_create_tv_properties(struct drm_device *dev,
806 				  unsigned int num_modes,
807 				  const char * const modes[])
808 {
809 	struct drm_property *tv_selector;
810 	struct drm_property *tv_subconnector;
811 	unsigned int i;
812 
813 	if (dev->mode_config.tv_select_subconnector_property)
814 		return 0;
815 
816 	/*
817 	 * Basic connector properties
818 	 */
819 	tv_selector = drm_property_create_enum(dev, 0,
820 					  "select subconnector",
821 					  drm_tv_select_enum_list,
822 					  ARRAY_SIZE(drm_tv_select_enum_list));
823 	if (!tv_selector)
824 		goto nomem;
825 
826 	dev->mode_config.tv_select_subconnector_property = tv_selector;
827 
828 	tv_subconnector =
829 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
830 				    "subconnector",
831 				    drm_tv_subconnector_enum_list,
832 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
833 	if (!tv_subconnector)
834 		goto nomem;
835 	dev->mode_config.tv_subconnector_property = tv_subconnector;
836 
837 	/*
838 	 * Other, TV specific properties: margins & TV modes.
839 	 */
840 	dev->mode_config.tv_left_margin_property =
841 		drm_property_create_range(dev, 0, "left margin", 0, 100);
842 	if (!dev->mode_config.tv_left_margin_property)
843 		goto nomem;
844 
845 	dev->mode_config.tv_right_margin_property =
846 		drm_property_create_range(dev, 0, "right margin", 0, 100);
847 	if (!dev->mode_config.tv_right_margin_property)
848 		goto nomem;
849 
850 	dev->mode_config.tv_top_margin_property =
851 		drm_property_create_range(dev, 0, "top margin", 0, 100);
852 	if (!dev->mode_config.tv_top_margin_property)
853 		goto nomem;
854 
855 	dev->mode_config.tv_bottom_margin_property =
856 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
857 	if (!dev->mode_config.tv_bottom_margin_property)
858 		goto nomem;
859 
860 	dev->mode_config.tv_mode_property =
861 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
862 				    "mode", num_modes);
863 	if (!dev->mode_config.tv_mode_property)
864 		goto nomem;
865 
866 	for (i = 0; i < num_modes; i++)
867 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
868 				      i, modes[i]);
869 
870 	dev->mode_config.tv_brightness_property =
871 		drm_property_create_range(dev, 0, "brightness", 0, 100);
872 	if (!dev->mode_config.tv_brightness_property)
873 		goto nomem;
874 
875 	dev->mode_config.tv_contrast_property =
876 		drm_property_create_range(dev, 0, "contrast", 0, 100);
877 	if (!dev->mode_config.tv_contrast_property)
878 		goto nomem;
879 
880 	dev->mode_config.tv_flicker_reduction_property =
881 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
882 	if (!dev->mode_config.tv_flicker_reduction_property)
883 		goto nomem;
884 
885 	dev->mode_config.tv_overscan_property =
886 		drm_property_create_range(dev, 0, "overscan", 0, 100);
887 	if (!dev->mode_config.tv_overscan_property)
888 		goto nomem;
889 
890 	dev->mode_config.tv_saturation_property =
891 		drm_property_create_range(dev, 0, "saturation", 0, 100);
892 	if (!dev->mode_config.tv_saturation_property)
893 		goto nomem;
894 
895 	dev->mode_config.tv_hue_property =
896 		drm_property_create_range(dev, 0, "hue", 0, 100);
897 	if (!dev->mode_config.tv_hue_property)
898 		goto nomem;
899 
900 	return 0;
901 nomem:
902 	return -ENOMEM;
903 }
904 EXPORT_SYMBOL(drm_mode_create_tv_properties);
905 
906 /**
907  * drm_mode_create_scaling_mode_property - create scaling mode property
908  * @dev: DRM device
909  *
910  * Called by a driver the first time it's needed, must be attached to desired
911  * connectors.
912  */
913 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
914 {
915 	struct drm_property *scaling_mode;
916 
917 	if (dev->mode_config.scaling_mode_property)
918 		return 0;
919 
920 	scaling_mode =
921 		drm_property_create_enum(dev, 0, "scaling mode",
922 				drm_scaling_mode_enum_list,
923 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
924 
925 	dev->mode_config.scaling_mode_property = scaling_mode;
926 
927 	return 0;
928 }
929 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
930 
931 /**
932  * drm_mode_create_aspect_ratio_property - create aspect ratio property
933  * @dev: DRM device
934  *
935  * Called by a driver the first time it's needed, must be attached to desired
936  * connectors.
937  *
938  * Returns:
939  * Zero on success, negative errno on failure.
940  */
941 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
942 {
943 	if (dev->mode_config.aspect_ratio_property)
944 		return 0;
945 
946 	dev->mode_config.aspect_ratio_property =
947 		drm_property_create_enum(dev, 0, "aspect ratio",
948 				drm_aspect_ratio_enum_list,
949 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
950 
951 	if (dev->mode_config.aspect_ratio_property == NULL)
952 		return -ENOMEM;
953 
954 	return 0;
955 }
956 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
957 
958 /**
959  * drm_mode_create_suggested_offset_properties - create suggests offset properties
960  * @dev: DRM device
961  *
962  * Create the the suggested x/y offset property for connectors.
963  */
964 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
965 {
966 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
967 		return 0;
968 
969 	dev->mode_config.suggested_x_property =
970 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
971 
972 	dev->mode_config.suggested_y_property =
973 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
974 
975 	if (dev->mode_config.suggested_x_property == NULL ||
976 	    dev->mode_config.suggested_y_property == NULL)
977 		return -ENOMEM;
978 	return 0;
979 }
980 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
981 
982 /**
983  * drm_mode_connector_set_path_property - set tile property on connector
984  * @connector: connector to set property on.
985  * @path: path to use for property; must not be NULL.
986  *
987  * This creates a property to expose to userspace to specify a
988  * connector path. This is mainly used for DisplayPort MST where
989  * connectors have a topology and we want to allow userspace to give
990  * them more meaningful names.
991  *
992  * Returns:
993  * Zero on success, negative errno on failure.
994  */
995 int drm_mode_connector_set_path_property(struct drm_connector *connector,
996 					 const char *path)
997 {
998 	struct drm_device *dev = connector->dev;
999 	int ret;
1000 
1001 	ret = drm_property_replace_global_blob(dev,
1002 	                                       &connector->path_blob_ptr,
1003 	                                       strlen(path) + 1,
1004 	                                       path,
1005 	                                       &connector->base,
1006 	                                       dev->mode_config.path_property);
1007 	return ret;
1008 }
1009 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1010 
1011 /**
1012  * drm_mode_connector_set_tile_property - set tile property on connector
1013  * @connector: connector to set property on.
1014  *
1015  * This looks up the tile information for a connector, and creates a
1016  * property for userspace to parse if it exists. The property is of
1017  * the form of 8 integers using ':' as a separator.
1018  *
1019  * Returns:
1020  * Zero on success, errno on failure.
1021  */
1022 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1023 {
1024 	struct drm_device *dev = connector->dev;
1025 	char tile[256];
1026 	int ret;
1027 
1028 	if (!connector->has_tile) {
1029 		ret  = drm_property_replace_global_blob(dev,
1030 		                                        &connector->tile_blob_ptr,
1031 		                                        0,
1032 		                                        NULL,
1033 		                                        &connector->base,
1034 		                                        dev->mode_config.tile_property);
1035 		return ret;
1036 	}
1037 
1038 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1039 		 connector->tile_group->id, connector->tile_is_single_monitor,
1040 		 connector->num_h_tile, connector->num_v_tile,
1041 		 connector->tile_h_loc, connector->tile_v_loc,
1042 		 connector->tile_h_size, connector->tile_v_size);
1043 
1044 	ret = drm_property_replace_global_blob(dev,
1045 	                                       &connector->tile_blob_ptr,
1046 	                                       strlen(tile) + 1,
1047 	                                       tile,
1048 	                                       &connector->base,
1049 	                                       dev->mode_config.tile_property);
1050 	return ret;
1051 }
1052 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1053 
1054 /**
1055  * drm_mode_connector_update_edid_property - update the edid property of a connector
1056  * @connector: drm connector
1057  * @edid: new value of the edid property
1058  *
1059  * This function creates a new blob modeset object and assigns its id to the
1060  * connector's edid property.
1061  *
1062  * Returns:
1063  * Zero on success, negative errno on failure.
1064  */
1065 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1066 					    const struct edid *edid)
1067 {
1068 	struct drm_device *dev = connector->dev;
1069 	size_t size = 0;
1070 	int ret;
1071 
1072 	/* ignore requests to set edid when overridden */
1073 	if (connector->override_edid)
1074 		return 0;
1075 
1076 	if (edid)
1077 		size = EDID_LENGTH * (1 + edid->extensions);
1078 
1079 	ret = drm_property_replace_global_blob(dev,
1080 					       &connector->edid_blob_ptr,
1081 	                                       size,
1082 	                                       edid,
1083 	                                       &connector->base,
1084 	                                       dev->mode_config.edid_property);
1085 	return ret;
1086 }
1087 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1088 
1089 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1090 				    struct drm_property *property,
1091 				    uint64_t value)
1092 {
1093 	int ret = -EINVAL;
1094 	struct drm_connector *connector = obj_to_connector(obj);
1095 
1096 	/* Do DPMS ourselves */
1097 	if (property == connector->dev->mode_config.dpms_property) {
1098 		ret = (*connector->funcs->dpms)(connector, (int)value);
1099 	} else if (connector->funcs->set_property)
1100 		ret = connector->funcs->set_property(connector, property, value);
1101 
1102 	/* store the property value if successful */
1103 	if (!ret)
1104 		drm_object_property_set_value(&connector->base, property, value);
1105 	return ret;
1106 }
1107 
1108 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1109 				       void *data, struct drm_file *file_priv)
1110 {
1111 	struct drm_mode_connector_set_property *conn_set_prop = data;
1112 	struct drm_mode_obj_set_property obj_set_prop = {
1113 		.value = conn_set_prop->value,
1114 		.prop_id = conn_set_prop->prop_id,
1115 		.obj_id = conn_set_prop->connector_id,
1116 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
1117 	};
1118 
1119 	/* It does all the locking and checking we need */
1120 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1121 }
1122 
1123 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1124 {
1125 	/* For atomic drivers only state objects are synchronously updated and
1126 	 * protected by modeset locks, so check those first. */
1127 	if (connector->state)
1128 		return connector->state->best_encoder;
1129 	return connector->encoder;
1130 }
1131 
1132 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1133 					 const struct drm_file *file_priv)
1134 {
1135 	/*
1136 	 * If user-space hasn't configured the driver to expose the stereo 3D
1137 	 * modes, don't expose them.
1138 	 */
1139 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1140 		return false;
1141 
1142 	return true;
1143 }
1144 
1145 int drm_mode_getconnector(struct drm_device *dev, void *data,
1146 			  struct drm_file *file_priv)
1147 {
1148 	struct drm_mode_get_connector *out_resp = data;
1149 	struct drm_connector *connector;
1150 	struct drm_encoder *encoder;
1151 	struct drm_display_mode *mode;
1152 	int mode_count = 0;
1153 	int encoders_count = 0;
1154 	int ret = 0;
1155 	int copied = 0;
1156 	int i;
1157 	struct drm_mode_modeinfo u_mode;
1158 	struct drm_mode_modeinfo __user *mode_ptr;
1159 	uint32_t __user *encoder_ptr;
1160 
1161 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1162 		return -EINVAL;
1163 
1164 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1165 
1166 	connector = drm_connector_lookup(dev, out_resp->connector_id);
1167 	if (!connector)
1168 		return -ENOENT;
1169 
1170 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1171 	encoder = drm_connector_get_encoder(connector);
1172 	if (encoder)
1173 		out_resp->encoder_id = encoder->base.id;
1174 	else
1175 		out_resp->encoder_id = 0;
1176 
1177 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1178 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1179 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1180 			&out_resp->count_props);
1181 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1182 	if (ret)
1183 		goto out_unref;
1184 
1185 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1186 		if (connector->encoder_ids[i] != 0)
1187 			encoders_count++;
1188 
1189 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1190 		copied = 0;
1191 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1192 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1193 			if (connector->encoder_ids[i] != 0) {
1194 				if (put_user(connector->encoder_ids[i],
1195 					     encoder_ptr + copied)) {
1196 					ret = -EFAULT;
1197 					goto out_unref;
1198 				}
1199 				copied++;
1200 			}
1201 		}
1202 	}
1203 	out_resp->count_encoders = encoders_count;
1204 
1205 	out_resp->connector_id = connector->base.id;
1206 	out_resp->connector_type = connector->connector_type;
1207 	out_resp->connector_type_id = connector->connector_type_id;
1208 
1209 	mutex_lock(&dev->mode_config.mutex);
1210 	if (out_resp->count_modes == 0) {
1211 		connector->funcs->fill_modes(connector,
1212 					     dev->mode_config.max_width,
1213 					     dev->mode_config.max_height);
1214 	}
1215 
1216 	out_resp->mm_width = connector->display_info.width_mm;
1217 	out_resp->mm_height = connector->display_info.height_mm;
1218 	out_resp->subpixel = connector->display_info.subpixel_order;
1219 	out_resp->connection = connector->status;
1220 
1221 	/* delayed so we get modes regardless of pre-fill_modes state */
1222 	list_for_each_entry(mode, &connector->modes, head)
1223 		if (drm_mode_expose_to_userspace(mode, file_priv))
1224 			mode_count++;
1225 
1226 	/*
1227 	 * This ioctl is called twice, once to determine how much space is
1228 	 * needed, and the 2nd time to fill it.
1229 	 */
1230 	if ((out_resp->count_modes >= mode_count) && mode_count) {
1231 		copied = 0;
1232 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1233 		list_for_each_entry(mode, &connector->modes, head) {
1234 			if (!drm_mode_expose_to_userspace(mode, file_priv))
1235 				continue;
1236 
1237 			drm_mode_convert_to_umode(&u_mode, mode);
1238 			if (copy_to_user(mode_ptr + copied,
1239 					 &u_mode, sizeof(u_mode))) {
1240 				ret = -EFAULT;
1241 				goto out;
1242 			}
1243 			copied++;
1244 		}
1245 	}
1246 	out_resp->count_modes = mode_count;
1247 out:
1248 	mutex_unlock(&dev->mode_config.mutex);
1249 out_unref:
1250 	drm_connector_unreference(connector);
1251 
1252 	return ret;
1253 }
1254 
1255 
1256 /**
1257  * DOC: Tile group
1258  *
1259  * Tile groups are used to represent tiled monitors with a unique integer
1260  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1261  * we store this in a tile group, so we have a common identifier for all tiles
1262  * in a monitor group. The property is called "TILE". Drivers can manage tile
1263  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1264  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1265  * the tile group information is exposed through a non-standard way.
1266  */
1267 
1268 static void drm_tile_group_free(struct kref *kref)
1269 {
1270 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1271 	struct drm_device *dev = tg->dev;
1272 	mutex_lock(&dev->mode_config.idr_mutex);
1273 	idr_remove(&dev->mode_config.tile_idr, tg->id);
1274 	mutex_unlock(&dev->mode_config.idr_mutex);
1275 	kfree(tg);
1276 }
1277 
1278 /**
1279  * drm_mode_put_tile_group - drop a reference to a tile group.
1280  * @dev: DRM device
1281  * @tg: tile group to drop reference to.
1282  *
1283  * drop reference to tile group and free if 0.
1284  */
1285 void drm_mode_put_tile_group(struct drm_device *dev,
1286 			     struct drm_tile_group *tg)
1287 {
1288 	kref_put(&tg->refcount, drm_tile_group_free);
1289 }
1290 EXPORT_SYMBOL(drm_mode_put_tile_group);
1291 
1292 /**
1293  * drm_mode_get_tile_group - get a reference to an existing tile group
1294  * @dev: DRM device
1295  * @topology: 8-bytes unique per monitor.
1296  *
1297  * Use the unique bytes to get a reference to an existing tile group.
1298  *
1299  * RETURNS:
1300  * tile group or NULL if not found.
1301  */
1302 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1303 					       char topology[8])
1304 {
1305 	struct drm_tile_group *tg;
1306 	int id;
1307 	mutex_lock(&dev->mode_config.idr_mutex);
1308 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1309 		if (!memcmp(tg->group_data, topology, 8)) {
1310 			if (!kref_get_unless_zero(&tg->refcount))
1311 				tg = NULL;
1312 			mutex_unlock(&dev->mode_config.idr_mutex);
1313 			return tg;
1314 		}
1315 	}
1316 	mutex_unlock(&dev->mode_config.idr_mutex);
1317 	return NULL;
1318 }
1319 EXPORT_SYMBOL(drm_mode_get_tile_group);
1320 
1321 /**
1322  * drm_mode_create_tile_group - create a tile group from a displayid description
1323  * @dev: DRM device
1324  * @topology: 8-bytes unique per monitor.
1325  *
1326  * Create a tile group for the unique monitor, and get a unique
1327  * identifier for the tile group.
1328  *
1329  * RETURNS:
1330  * new tile group or error.
1331  */
1332 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1333 						  char topology[8])
1334 {
1335 	struct drm_tile_group *tg;
1336 	int ret;
1337 
1338 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1339 	if (!tg)
1340 		return ERR_PTR(-ENOMEM);
1341 
1342 	kref_init(&tg->refcount);
1343 	memcpy(tg->group_data, topology, 8);
1344 	tg->dev = dev;
1345 
1346 	mutex_lock(&dev->mode_config.idr_mutex);
1347 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1348 	if (ret >= 0) {
1349 		tg->id = ret;
1350 	} else {
1351 		kfree(tg);
1352 		tg = ERR_PTR(ret);
1353 	}
1354 
1355 	mutex_unlock(&dev->mode_config.idr_mutex);
1356 	return tg;
1357 }
1358 EXPORT_SYMBOL(drm_mode_create_tile_group);
1359