• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 #include "drm_crtc_internal.h"
28 #include "drm_internal.h"
29 
30 /**
31  * DOC: overview
32  *
33  * In DRM connectors are the general abstraction for display sinks, and include
34  * als fixed panels or anything else that can display pixels in some form. As
35  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
36  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
37  * Hence they are reference-counted using drm_connector_reference() and
38  * drm_connector_unreference().
39  *
40  * KMS driver must create, initialize, register and attach at a struct
41  * &drm_connector for each such sink. The instance is created as other KMS
42  * objects and initialized by setting the following fields.
43  *
44  * The connector is then registered with a call to drm_connector_init() with a
45  * pointer to the connector functions and a connector type, and exposed through
46  * sysfs with a call to drm_connector_register().
47  *
48  * Connectors must be attached to an encoder to be used. For devices that map
49  * connectors to encoders 1:1, the connector should be attached at
50  * initialization time with a call to drm_mode_connector_attach_encoder(). The
51  * driver must also set the struct &drm_connector encoder field to point to the
52  * attached encoder.
53  *
54  * For connectors which are not fixed (like built-in panels) the driver needs to
55  * support hotplug notifications. The simplest way to do that is by using the
56  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
57  * hardware support for hotplug interrupts. Connectors with hardware hotplug
58  * support can instead use e.g. drm_helper_hpd_irq_event().
59  */
60 
61 struct drm_conn_prop_enum_list {
62 	int type;
63 	const char *name;
64 	struct ida ida;
65 };
66 
67 /*
68  * Connector and encoder types.
69  */
70 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
71 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
72 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
73 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
74 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
75 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
76 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
77 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
78 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
79 	{ DRM_MODE_CONNECTOR_Component, "Component" },
80 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
81 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
82 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
83 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
84 	{ DRM_MODE_CONNECTOR_TV, "TV" },
85 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
86 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
87 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
88 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
89 };
90 
drm_connector_ida_init(void)91 void drm_connector_ida_init(void)
92 {
93 	int i;
94 
95 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
96 		ida_init(&drm_connector_enum_list[i].ida);
97 }
98 
drm_connector_ida_destroy(void)99 void drm_connector_ida_destroy(void)
100 {
101 	int i;
102 
103 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
104 		ida_destroy(&drm_connector_enum_list[i].ida);
105 }
106 
107 /**
108  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
109  * @connector: connector to quwery
110  *
111  * The kernel supports per-connector configuration of its consoles through
112  * use of the video= parameter. This function parses that option and
113  * extracts the user's specified mode (or enable/disable status) for a
114  * particular connector. This is typically only used during the early fbdev
115  * setup.
116  */
drm_connector_get_cmdline_mode(struct drm_connector * connector)117 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
118 {
119 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
120 	char *option = NULL;
121 
122 	if (fb_get_options(connector->name, &option))
123 		return;
124 
125 	if (!drm_mode_parse_command_line_for_connector(option,
126 						       connector,
127 						       mode))
128 		return;
129 
130 	if (mode->force) {
131 		const char *s;
132 
133 		switch (mode->force) {
134 		case DRM_FORCE_OFF:
135 			s = "OFF";
136 			break;
137 		case DRM_FORCE_ON_DIGITAL:
138 			s = "ON - dig";
139 			break;
140 		default:
141 		case DRM_FORCE_ON:
142 			s = "ON";
143 			break;
144 		}
145 
146 		DRM_INFO("forcing %s connector %s\n", connector->name, s);
147 		connector->force = mode->force;
148 	}
149 
150 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
151 		      connector->name,
152 		      mode->xres, mode->yres,
153 		      mode->refresh_specified ? mode->refresh : 60,
154 		      mode->rb ? " reduced blanking" : "",
155 		      mode->margins ? " with margins" : "",
156 		      mode->interlace ?  " interlaced" : "");
157 }
158 
drm_connector_free(struct kref * kref)159 static void drm_connector_free(struct kref *kref)
160 {
161 	struct drm_connector *connector =
162 		container_of(kref, struct drm_connector, base.refcount);
163 	struct drm_device *dev = connector->dev;
164 
165 	drm_mode_object_unregister(dev, &connector->base);
166 	connector->funcs->destroy(connector);
167 }
168 
169 /**
170  * drm_connector_init - Init a preallocated connector
171  * @dev: DRM device
172  * @connector: the connector to init
173  * @funcs: callbacks for this connector
174  * @connector_type: user visible type of the connector
175  *
176  * Initialises a preallocated connector. Connectors should be
177  * subclassed as part of driver connector objects.
178  *
179  * Returns:
180  * Zero on success, error code on failure.
181  */
drm_connector_init(struct drm_device * dev,struct drm_connector * connector,const struct drm_connector_funcs * funcs,int connector_type)182 int drm_connector_init(struct drm_device *dev,
183 		       struct drm_connector *connector,
184 		       const struct drm_connector_funcs *funcs,
185 		       int connector_type)
186 {
187 	struct drm_mode_config *config = &dev->mode_config;
188 	int ret;
189 	struct ida *connector_ida =
190 		&drm_connector_enum_list[connector_type].ida;
191 
192 	drm_modeset_lock_all(dev);
193 
194 	ret = drm_mode_object_get_reg(dev, &connector->base,
195 				      DRM_MODE_OBJECT_CONNECTOR,
196 				      false, drm_connector_free);
197 	if (ret)
198 		goto out_unlock;
199 
200 	connector->base.properties = &connector->properties;
201 	connector->dev = dev;
202 	connector->funcs = funcs;
203 
204 	ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
205 	if (ret < 0)
206 		goto out_put;
207 	connector->index = ret;
208 	ret = 0;
209 
210 	connector->connector_type = connector_type;
211 	connector->connector_type_id =
212 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
213 	if (connector->connector_type_id < 0) {
214 		ret = connector->connector_type_id;
215 		goto out_put_id;
216 	}
217 	connector->name =
218 		kasprintf(GFP_KERNEL, "%s-%d",
219 			  drm_connector_enum_list[connector_type].name,
220 			  connector->connector_type_id);
221 	if (!connector->name) {
222 		ret = -ENOMEM;
223 		goto out_put_type_id;
224 	}
225 
226 	INIT_LIST_HEAD(&connector->probed_modes);
227 	INIT_LIST_HEAD(&connector->modes);
228 	mutex_init(&connector->mutex);
229 	connector->edid_blob_ptr = NULL;
230 	connector->status = connector_status_unknown;
231 
232 	drm_connector_get_cmdline_mode(connector);
233 
234 	/* We should add connectors at the end to avoid upsetting the connector
235 	 * index too much. */
236 	list_add_tail(&connector->head, &config->connector_list);
237 	config->num_connector++;
238 
239 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
240 		drm_object_attach_property(&connector->base,
241 					      config->edid_property,
242 					      0);
243 
244 	drm_object_attach_property(&connector->base,
245 				      config->dpms_property, 0);
246 
247 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
248 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
249 	}
250 
251 	connector->debugfs_entry = NULL;
252 out_put_type_id:
253 	if (ret)
254 		ida_simple_remove(connector_ida, connector->connector_type_id);
255 out_put_id:
256 	if (ret)
257 		ida_simple_remove(&config->connector_ida, connector->index);
258 out_put:
259 	if (ret)
260 		drm_mode_object_unregister(dev, &connector->base);
261 
262 out_unlock:
263 	drm_modeset_unlock_all(dev);
264 
265 	return ret;
266 }
267 EXPORT_SYMBOL(drm_connector_init);
268 
269 /**
270  * drm_mode_connector_attach_encoder - attach a connector to an encoder
271  * @connector: connector to attach
272  * @encoder: encoder to attach @connector to
273  *
274  * This function links up a connector to an encoder. Note that the routing
275  * restrictions between encoders and crtcs are exposed to userspace through the
276  * possible_clones and possible_crtcs bitmasks.
277  *
278  * Returns:
279  * Zero on success, negative errno on failure.
280  */
drm_mode_connector_attach_encoder(struct drm_connector * connector,struct drm_encoder * encoder)281 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
282 				      struct drm_encoder *encoder)
283 {
284 	int i;
285 
286 	/*
287 	 * In the past, drivers have attempted to model the static association
288 	 * of connector to encoder in simple connector/encoder devices using a
289 	 * direct assignment of connector->encoder = encoder. This connection
290 	 * is a logical one and the responsibility of the core, so drivers are
291 	 * expected not to mess with this.
292 	 *
293 	 * Note that the error return should've been enough here, but a large
294 	 * majority of drivers ignores the return value, so add in a big WARN
295 	 * to get people's attention.
296 	 */
297 	if (WARN_ON(connector->encoder))
298 		return -EINVAL;
299 
300 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
301 		if (connector->encoder_ids[i] == 0) {
302 			connector->encoder_ids[i] = encoder->base.id;
303 			return 0;
304 		}
305 	}
306 	return -ENOMEM;
307 }
308 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
309 
drm_mode_remove(struct drm_connector * connector,struct drm_display_mode * mode)310 static void drm_mode_remove(struct drm_connector *connector,
311 			    struct drm_display_mode *mode)
312 {
313 	list_del(&mode->head);
314 	drm_mode_destroy(connector->dev, mode);
315 }
316 
317 /**
318  * drm_connector_cleanup - cleans up an initialised connector
319  * @connector: connector to cleanup
320  *
321  * Cleans up the connector but doesn't free the object.
322  */
drm_connector_cleanup(struct drm_connector * connector)323 void drm_connector_cleanup(struct drm_connector *connector)
324 {
325 	struct drm_device *dev = connector->dev;
326 	struct drm_display_mode *mode, *t;
327 
328 	/* The connector should have been removed from userspace long before
329 	 * it is finally destroyed.
330 	 */
331 	if (WARN_ON(connector->registered))
332 		drm_connector_unregister(connector);
333 
334 	if (connector->tile_group) {
335 		drm_mode_put_tile_group(dev, connector->tile_group);
336 		connector->tile_group = NULL;
337 	}
338 
339 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
340 		drm_mode_remove(connector, mode);
341 
342 	list_for_each_entry_safe(mode, t, &connector->modes, head)
343 		drm_mode_remove(connector, mode);
344 
345 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
346 			  connector->connector_type_id);
347 
348 	ida_simple_remove(&dev->mode_config.connector_ida,
349 			  connector->index);
350 
351 	kfree(connector->display_info.bus_formats);
352 	drm_mode_object_unregister(dev, &connector->base);
353 	kfree(connector->name);
354 	connector->name = NULL;
355 	list_del(&connector->head);
356 	dev->mode_config.num_connector--;
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  */
drm_connector_register(struct drm_connector * connector)378 int drm_connector_register(struct drm_connector *connector)
379 {
380 	int ret = 0;
381 
382 	if (!connector->dev->registered)
383 		return 0;
384 
385 	mutex_lock(&connector->mutex);
386 	if (connector->registered)
387 		goto unlock;
388 
389 	ret = drm_sysfs_connector_add(connector);
390 	if (ret)
391 		goto unlock;
392 
393 	ret = drm_debugfs_connector_add(connector);
394 	if (ret) {
395 		goto err_sysfs;
396 	}
397 
398 	if (connector->funcs->late_register) {
399 		ret = connector->funcs->late_register(connector);
400 		if (ret)
401 			goto err_debugfs;
402 	}
403 
404 	drm_mode_object_register(connector->dev, &connector->base);
405 
406 	connector->registered = true;
407 	goto unlock;
408 
409 err_debugfs:
410 	drm_debugfs_connector_remove(connector);
411 err_sysfs:
412 	drm_sysfs_connector_remove(connector);
413 unlock:
414 	mutex_unlock(&connector->mutex);
415 	return ret;
416 }
417 EXPORT_SYMBOL(drm_connector_register);
418 
419 /**
420  * drm_connector_unregister - unregister a connector
421  * @connector: the connector to unregister
422  *
423  * Unregister userspace interfaces for a connector
424  */
drm_connector_unregister(struct drm_connector * connector)425 void drm_connector_unregister(struct drm_connector *connector)
426 {
427 	mutex_lock(&connector->mutex);
428 	if (!connector->registered) {
429 		mutex_unlock(&connector->mutex);
430 		return;
431 	}
432 
433 	if (connector->funcs->early_unregister)
434 		connector->funcs->early_unregister(connector);
435 
436 	drm_sysfs_connector_remove(connector);
437 	drm_debugfs_connector_remove(connector);
438 
439 	connector->registered = false;
440 	mutex_unlock(&connector->mutex);
441 }
442 EXPORT_SYMBOL(drm_connector_unregister);
443 
drm_connector_unregister_all(struct drm_device * dev)444 void drm_connector_unregister_all(struct drm_device *dev)
445 {
446 	struct drm_connector *connector;
447 
448 	/* FIXME: taking the mode config mutex ends up in a clash with sysfs */
449 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
450 		drm_connector_unregister(connector);
451 }
452 
drm_connector_register_all(struct drm_device * dev)453 int drm_connector_register_all(struct drm_device *dev)
454 {
455 	struct drm_connector *connector;
456 	int ret;
457 
458 	/* FIXME: taking the mode config mutex ends up in a clash with
459 	 * fbcon/backlight registration */
460 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
461 		ret = drm_connector_register(connector);
462 		if (ret)
463 			goto err;
464 	}
465 
466 	return 0;
467 
468 err:
469 	mutex_unlock(&dev->mode_config.mutex);
470 	drm_connector_unregister_all(dev);
471 	return ret;
472 }
473 
474 /**
475  * drm_get_connector_status_name - return a string for connector status
476  * @status: connector status to compute name of
477  *
478  * In contrast to the other drm_get_*_name functions this one here returns a
479  * const pointer and hence is threadsafe.
480  */
drm_get_connector_status_name(enum drm_connector_status status)481 const char *drm_get_connector_status_name(enum drm_connector_status status)
482 {
483 	if (status == connector_status_connected)
484 		return "connected";
485 	else if (status == connector_status_disconnected)
486 		return "disconnected";
487 	else
488 		return "unknown";
489 }
490 EXPORT_SYMBOL(drm_get_connector_status_name);
491 
492 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
493 	{ SubPixelUnknown, "Unknown" },
494 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
495 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
496 	{ SubPixelVerticalRGB, "Vertical RGB" },
497 	{ SubPixelVerticalBGR, "Vertical BGR" },
498 	{ SubPixelNone, "None" },
499 };
500 
501 /**
502  * drm_get_subpixel_order_name - return a string for a given subpixel enum
503  * @order: enum of subpixel_order
504  *
505  * Note you could abuse this and return something out of bounds, but that
506  * would be a caller error.  No unscrubbed user data should make it here.
507  */
drm_get_subpixel_order_name(enum subpixel_order order)508 const char *drm_get_subpixel_order_name(enum subpixel_order order)
509 {
510 	return drm_subpixel_enum_list[order].name;
511 }
512 EXPORT_SYMBOL(drm_get_subpixel_order_name);
513 
514 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
515 	{ DRM_MODE_DPMS_ON, "On" },
516 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
517 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
518 	{ DRM_MODE_DPMS_OFF, "Off" }
519 };
DRM_ENUM_NAME_FN(drm_get_dpms_name,drm_dpms_enum_list)520 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
521 
522 /**
523  * drm_display_info_set_bus_formats - set the supported bus formats
524  * @info: display info to store bus formats in
525  * @formats: array containing the supported bus formats
526  * @num_formats: the number of entries in the fmts array
527  *
528  * Store the supported bus formats in display info structure.
529  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
530  * a full list of available formats.
531  */
532 int drm_display_info_set_bus_formats(struct drm_display_info *info,
533 				     const u32 *formats,
534 				     unsigned int num_formats)
535 {
536 	u32 *fmts = NULL;
537 
538 	if (!formats && num_formats)
539 		return -EINVAL;
540 
541 	if (formats && num_formats) {
542 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
543 			       GFP_KERNEL);
544 		if (!fmts)
545 			return -ENOMEM;
546 	}
547 
548 	kfree(info->bus_formats);
549 	info->bus_formats = fmts;
550 	info->num_bus_formats = num_formats;
551 
552 	return 0;
553 }
554 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
555 
556 /* Optional connector properties. */
557 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
558 	{ DRM_MODE_SCALE_NONE, "None" },
559 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
560 	{ DRM_MODE_SCALE_CENTER, "Center" },
561 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
562 };
563 
564 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
565 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
566 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
567 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
568 };
569 
570 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
571 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
572 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
573 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
574 };
575 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
576 
577 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
578 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
579 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
580 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
581 };
582 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
583 		 drm_dvi_i_subconnector_enum_list)
584 
585 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
586 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
587 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
588 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
589 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
590 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
591 };
592 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
593 
594 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
595 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
596 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
597 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
598 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
599 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
600 };
DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,drm_tv_subconnector_enum_list)601 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
602 		 drm_tv_subconnector_enum_list)
603 
604 int drm_connector_create_standard_properties(struct drm_device *dev)
605 {
606 	struct drm_property *prop;
607 
608 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
609 				   DRM_MODE_PROP_IMMUTABLE,
610 				   "EDID", 0);
611 	if (!prop)
612 		return -ENOMEM;
613 	dev->mode_config.edid_property = prop;
614 
615 	prop = drm_property_create_enum(dev, 0,
616 				   "DPMS", drm_dpms_enum_list,
617 				   ARRAY_SIZE(drm_dpms_enum_list));
618 	if (!prop)
619 		return -ENOMEM;
620 	dev->mode_config.dpms_property = prop;
621 
622 	prop = drm_property_create(dev,
623 				   DRM_MODE_PROP_BLOB |
624 				   DRM_MODE_PROP_IMMUTABLE,
625 				   "PATH", 0);
626 	if (!prop)
627 		return -ENOMEM;
628 	dev->mode_config.path_property = prop;
629 
630 	prop = drm_property_create(dev,
631 				   DRM_MODE_PROP_BLOB |
632 				   DRM_MODE_PROP_IMMUTABLE,
633 				   "TILE", 0);
634 	if (!prop)
635 		return -ENOMEM;
636 	dev->mode_config.tile_property = prop;
637 
638 	return 0;
639 }
640 
641 /**
642  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
643  * @dev: DRM device
644  *
645  * Called by a driver the first time a DVI-I connector is made.
646  */
drm_mode_create_dvi_i_properties(struct drm_device * dev)647 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
648 {
649 	struct drm_property *dvi_i_selector;
650 	struct drm_property *dvi_i_subconnector;
651 
652 	if (dev->mode_config.dvi_i_select_subconnector_property)
653 		return 0;
654 
655 	dvi_i_selector =
656 		drm_property_create_enum(dev, 0,
657 				    "select subconnector",
658 				    drm_dvi_i_select_enum_list,
659 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
660 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
661 
662 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
663 				    "subconnector",
664 				    drm_dvi_i_subconnector_enum_list,
665 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
666 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
667 
668 	return 0;
669 }
670 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
671 
672 /**
673  * drm_create_tv_properties - create TV specific connector properties
674  * @dev: DRM device
675  * @num_modes: number of different TV formats (modes) supported
676  * @modes: array of pointers to strings containing name of each format
677  *
678  * Called by a driver's TV initialization routine, this function creates
679  * the TV specific connector properties for a given device.  Caller is
680  * responsible for allocating a list of format names and passing them to
681  * this routine.
682  */
drm_mode_create_tv_properties(struct drm_device * dev,unsigned int num_modes,const char * const modes[])683 int drm_mode_create_tv_properties(struct drm_device *dev,
684 				  unsigned int num_modes,
685 				  const char * const modes[])
686 {
687 	struct drm_property *tv_selector;
688 	struct drm_property *tv_subconnector;
689 	unsigned int i;
690 
691 	if (dev->mode_config.tv_select_subconnector_property)
692 		return 0;
693 
694 	/*
695 	 * Basic connector properties
696 	 */
697 	tv_selector = drm_property_create_enum(dev, 0,
698 					  "select subconnector",
699 					  drm_tv_select_enum_list,
700 					  ARRAY_SIZE(drm_tv_select_enum_list));
701 	if (!tv_selector)
702 		goto nomem;
703 
704 	dev->mode_config.tv_select_subconnector_property = tv_selector;
705 
706 	tv_subconnector =
707 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
708 				    "subconnector",
709 				    drm_tv_subconnector_enum_list,
710 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
711 	if (!tv_subconnector)
712 		goto nomem;
713 	dev->mode_config.tv_subconnector_property = tv_subconnector;
714 
715 	/*
716 	 * Other, TV specific properties: margins & TV modes.
717 	 */
718 	dev->mode_config.tv_left_margin_property =
719 		drm_property_create_range(dev, 0, "left margin", 0, 100);
720 	if (!dev->mode_config.tv_left_margin_property)
721 		goto nomem;
722 
723 	dev->mode_config.tv_right_margin_property =
724 		drm_property_create_range(dev, 0, "right margin", 0, 100);
725 	if (!dev->mode_config.tv_right_margin_property)
726 		goto nomem;
727 
728 	dev->mode_config.tv_top_margin_property =
729 		drm_property_create_range(dev, 0, "top margin", 0, 100);
730 	if (!dev->mode_config.tv_top_margin_property)
731 		goto nomem;
732 
733 	dev->mode_config.tv_bottom_margin_property =
734 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
735 	if (!dev->mode_config.tv_bottom_margin_property)
736 		goto nomem;
737 
738 	dev->mode_config.tv_mode_property =
739 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
740 				    "mode", num_modes);
741 	if (!dev->mode_config.tv_mode_property)
742 		goto nomem;
743 
744 	for (i = 0; i < num_modes; i++)
745 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
746 				      i, modes[i]);
747 
748 	dev->mode_config.tv_brightness_property =
749 		drm_property_create_range(dev, 0, "brightness", 0, 100);
750 	if (!dev->mode_config.tv_brightness_property)
751 		goto nomem;
752 
753 	dev->mode_config.tv_contrast_property =
754 		drm_property_create_range(dev, 0, "contrast", 0, 100);
755 	if (!dev->mode_config.tv_contrast_property)
756 		goto nomem;
757 
758 	dev->mode_config.tv_flicker_reduction_property =
759 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
760 	if (!dev->mode_config.tv_flicker_reduction_property)
761 		goto nomem;
762 
763 	dev->mode_config.tv_overscan_property =
764 		drm_property_create_range(dev, 0, "overscan", 0, 100);
765 	if (!dev->mode_config.tv_overscan_property)
766 		goto nomem;
767 
768 	dev->mode_config.tv_saturation_property =
769 		drm_property_create_range(dev, 0, "saturation", 0, 100);
770 	if (!dev->mode_config.tv_saturation_property)
771 		goto nomem;
772 
773 	dev->mode_config.tv_hue_property =
774 		drm_property_create_range(dev, 0, "hue", 0, 100);
775 	if (!dev->mode_config.tv_hue_property)
776 		goto nomem;
777 
778 	return 0;
779 nomem:
780 	return -ENOMEM;
781 }
782 EXPORT_SYMBOL(drm_mode_create_tv_properties);
783 
784 /**
785  * drm_mode_create_scaling_mode_property - create scaling mode property
786  * @dev: DRM device
787  *
788  * Called by a driver the first time it's needed, must be attached to desired
789  * connectors.
790  */
drm_mode_create_scaling_mode_property(struct drm_device * dev)791 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
792 {
793 	struct drm_property *scaling_mode;
794 
795 	if (dev->mode_config.scaling_mode_property)
796 		return 0;
797 
798 	scaling_mode =
799 		drm_property_create_enum(dev, 0, "scaling mode",
800 				drm_scaling_mode_enum_list,
801 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
802 
803 	dev->mode_config.scaling_mode_property = scaling_mode;
804 
805 	return 0;
806 }
807 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
808 
809 /**
810  * drm_mode_create_aspect_ratio_property - create aspect ratio property
811  * @dev: DRM device
812  *
813  * Called by a driver the first time it's needed, must be attached to desired
814  * connectors.
815  *
816  * Returns:
817  * Zero on success, negative errno on failure.
818  */
drm_mode_create_aspect_ratio_property(struct drm_device * dev)819 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
820 {
821 	if (dev->mode_config.aspect_ratio_property)
822 		return 0;
823 
824 	dev->mode_config.aspect_ratio_property =
825 		drm_property_create_enum(dev, 0, "aspect ratio",
826 				drm_aspect_ratio_enum_list,
827 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
828 
829 	if (dev->mode_config.aspect_ratio_property == NULL)
830 		return -ENOMEM;
831 
832 	return 0;
833 }
834 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
835 
836 /**
837  * drm_mode_create_suggested_offset_properties - create suggests offset properties
838  * @dev: DRM device
839  *
840  * Create the the suggested x/y offset property for connectors.
841  */
drm_mode_create_suggested_offset_properties(struct drm_device * dev)842 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
843 {
844 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
845 		return 0;
846 
847 	dev->mode_config.suggested_x_property =
848 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
849 
850 	dev->mode_config.suggested_y_property =
851 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
852 
853 	if (dev->mode_config.suggested_x_property == NULL ||
854 	    dev->mode_config.suggested_y_property == NULL)
855 		return -ENOMEM;
856 	return 0;
857 }
858 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
859 
860 /**
861  * drm_mode_connector_set_path_property - set tile property on connector
862  * @connector: connector to set property on.
863  * @path: path to use for property; must not be NULL.
864  *
865  * This creates a property to expose to userspace to specify a
866  * connector path. This is mainly used for DisplayPort MST where
867  * connectors have a topology and we want to allow userspace to give
868  * them more meaningful names.
869  *
870  * Returns:
871  * Zero on success, negative errno on failure.
872  */
drm_mode_connector_set_path_property(struct drm_connector * connector,const char * path)873 int drm_mode_connector_set_path_property(struct drm_connector *connector,
874 					 const char *path)
875 {
876 	struct drm_device *dev = connector->dev;
877 	int ret;
878 
879 	ret = drm_property_replace_global_blob(dev,
880 	                                       &connector->path_blob_ptr,
881 	                                       strlen(path) + 1,
882 	                                       path,
883 	                                       &connector->base,
884 	                                       dev->mode_config.path_property);
885 	return ret;
886 }
887 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
888 
889 /**
890  * drm_mode_connector_set_tile_property - set tile property on connector
891  * @connector: connector to set property on.
892  *
893  * This looks up the tile information for a connector, and creates a
894  * property for userspace to parse if it exists. The property is of
895  * the form of 8 integers using ':' as a separator.
896  *
897  * Returns:
898  * Zero on success, errno on failure.
899  */
drm_mode_connector_set_tile_property(struct drm_connector * connector)900 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
901 {
902 	struct drm_device *dev = connector->dev;
903 	char tile[256];
904 	int ret;
905 
906 	if (!connector->has_tile) {
907 		ret  = drm_property_replace_global_blob(dev,
908 		                                        &connector->tile_blob_ptr,
909 		                                        0,
910 		                                        NULL,
911 		                                        &connector->base,
912 		                                        dev->mode_config.tile_property);
913 		return ret;
914 	}
915 
916 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
917 		 connector->tile_group->id, connector->tile_is_single_monitor,
918 		 connector->num_h_tile, connector->num_v_tile,
919 		 connector->tile_h_loc, connector->tile_v_loc,
920 		 connector->tile_h_size, connector->tile_v_size);
921 
922 	ret = drm_property_replace_global_blob(dev,
923 	                                       &connector->tile_blob_ptr,
924 	                                       strlen(tile) + 1,
925 	                                       tile,
926 	                                       &connector->base,
927 	                                       dev->mode_config.tile_property);
928 	return ret;
929 }
930 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
931 
932 /**
933  * drm_mode_connector_update_edid_property - update the edid property of a connector
934  * @connector: drm connector
935  * @edid: new value of the edid property
936  *
937  * This function creates a new blob modeset object and assigns its id to the
938  * connector's edid property.
939  *
940  * Returns:
941  * Zero on success, negative errno on failure.
942  */
drm_mode_connector_update_edid_property(struct drm_connector * connector,const struct edid * edid)943 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
944 					    const struct edid *edid)
945 {
946 	struct drm_device *dev = connector->dev;
947 	size_t size = 0;
948 	int ret;
949 
950 	/* ignore requests to set edid when overridden */
951 	if (connector->override_edid)
952 		return 0;
953 
954 	if (edid)
955 		size = EDID_LENGTH * (1 + edid->extensions);
956 
957 	ret = drm_property_replace_global_blob(dev,
958 					       &connector->edid_blob_ptr,
959 	                                       size,
960 	                                       edid,
961 	                                       &connector->base,
962 	                                       dev->mode_config.edid_property);
963 	return ret;
964 }
965 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
966 
drm_mode_connector_set_obj_prop(struct drm_mode_object * obj,struct drm_property * property,uint64_t value)967 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
968 				    struct drm_property *property,
969 				    uint64_t value)
970 {
971 	int ret = -EINVAL;
972 	struct drm_connector *connector = obj_to_connector(obj);
973 
974 	/* Do DPMS ourselves */
975 	if (property == connector->dev->mode_config.dpms_property) {
976 		ret = (*connector->funcs->dpms)(connector, (int)value);
977 	} else if (connector->funcs->set_property)
978 		ret = connector->funcs->set_property(connector, property, value);
979 
980 	/* store the property value if successful */
981 	if (!ret)
982 		drm_object_property_set_value(&connector->base, property, value);
983 	return ret;
984 }
985 
drm_mode_connector_property_set_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)986 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
987 				       void *data, struct drm_file *file_priv)
988 {
989 	struct drm_mode_connector_set_property *conn_set_prop = data;
990 	struct drm_mode_obj_set_property obj_set_prop = {
991 		.value = conn_set_prop->value,
992 		.prop_id = conn_set_prop->prop_id,
993 		.obj_id = conn_set_prop->connector_id,
994 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
995 	};
996 
997 	/* It does all the locking and checking we need */
998 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
999 }
1000 
drm_connector_get_encoder(struct drm_connector * connector)1001 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1002 {
1003 	/* For atomic drivers only state objects are synchronously updated and
1004 	 * protected by modeset locks, so check those first. */
1005 	if (connector->state)
1006 		return connector->state->best_encoder;
1007 	return connector->encoder;
1008 }
1009 
drm_mode_expose_to_userspace(const struct drm_display_mode * mode,const struct drm_file * file_priv)1010 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1011 					 const struct drm_file *file_priv)
1012 {
1013 	/*
1014 	 * If user-space hasn't configured the driver to expose the stereo 3D
1015 	 * modes, don't expose them.
1016 	 */
1017 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1018 		return false;
1019 
1020 	return true;
1021 }
1022 
drm_mode_getconnector(struct drm_device * dev,void * data,struct drm_file * file_priv)1023 int drm_mode_getconnector(struct drm_device *dev, void *data,
1024 			  struct drm_file *file_priv)
1025 {
1026 	struct drm_mode_get_connector *out_resp = data;
1027 	struct drm_connector *connector;
1028 	struct drm_encoder *encoder;
1029 	struct drm_display_mode *mode;
1030 	int mode_count = 0;
1031 	int encoders_count = 0;
1032 	int ret = 0;
1033 	int copied = 0;
1034 	int i;
1035 	struct drm_mode_modeinfo u_mode;
1036 	struct drm_mode_modeinfo __user *mode_ptr;
1037 	uint32_t __user *encoder_ptr;
1038 
1039 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1040 		return -EINVAL;
1041 
1042 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1043 
1044 	mutex_lock(&dev->mode_config.mutex);
1045 
1046 	connector = drm_connector_lookup(dev, out_resp->connector_id);
1047 	if (!connector) {
1048 		ret = -ENOENT;
1049 		goto out_unlock;
1050 	}
1051 
1052 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1053 		if (connector->encoder_ids[i] != 0)
1054 			encoders_count++;
1055 
1056 	if (out_resp->count_modes == 0) {
1057 		connector->funcs->fill_modes(connector,
1058 					     dev->mode_config.max_width,
1059 					     dev->mode_config.max_height);
1060 	}
1061 
1062 	/* delayed so we get modes regardless of pre-fill_modes state */
1063 	list_for_each_entry(mode, &connector->modes, head)
1064 		if (drm_mode_expose_to_userspace(mode, file_priv))
1065 			mode_count++;
1066 
1067 	out_resp->connector_id = connector->base.id;
1068 	out_resp->connector_type = connector->connector_type;
1069 	out_resp->connector_type_id = connector->connector_type_id;
1070 	out_resp->mm_width = connector->display_info.width_mm;
1071 	out_resp->mm_height = connector->display_info.height_mm;
1072 	out_resp->subpixel = connector->display_info.subpixel_order;
1073 	out_resp->connection = connector->status;
1074 
1075 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1076 	encoder = drm_connector_get_encoder(connector);
1077 	if (encoder)
1078 		out_resp->encoder_id = encoder->base.id;
1079 	else
1080 		out_resp->encoder_id = 0;
1081 
1082 	/*
1083 	 * This ioctl is called twice, once to determine how much space is
1084 	 * needed, and the 2nd time to fill it.
1085 	 */
1086 	if ((out_resp->count_modes >= mode_count) && mode_count) {
1087 		copied = 0;
1088 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1089 		list_for_each_entry(mode, &connector->modes, head) {
1090 			if (!drm_mode_expose_to_userspace(mode, file_priv))
1091 				continue;
1092 
1093 			drm_mode_convert_to_umode(&u_mode, mode);
1094 			if (copy_to_user(mode_ptr + copied,
1095 					 &u_mode, sizeof(u_mode))) {
1096 				ret = -EFAULT;
1097 				goto out;
1098 			}
1099 			copied++;
1100 		}
1101 	}
1102 	out_resp->count_modes = mode_count;
1103 
1104 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1105 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1106 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1107 			&out_resp->count_props);
1108 	if (ret)
1109 		goto out;
1110 
1111 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1112 		copied = 0;
1113 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1114 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1115 			if (connector->encoder_ids[i] != 0) {
1116 				if (put_user(connector->encoder_ids[i],
1117 					     encoder_ptr + copied)) {
1118 					ret = -EFAULT;
1119 					goto out;
1120 				}
1121 				copied++;
1122 			}
1123 		}
1124 	}
1125 	out_resp->count_encoders = encoders_count;
1126 
1127 out:
1128 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1129 
1130 	drm_connector_unreference(connector);
1131 out_unlock:
1132 	mutex_unlock(&dev->mode_config.mutex);
1133 
1134 	return ret;
1135 }
1136 
1137