blob: 48ca2457df8c964977f3f7edae0980bc227b97cf [file] [log] [blame]
Daniel Vetter52217192016-08-12 22:48:50 +02001/*
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>
Laurent Pinchart93382032016-11-28 20:51:09 +020026#include <drm/drm_encoder.h>
Daniel Vetter52217192016-08-12 22:48:50 +020027
28#include "drm_crtc_internal.h"
29#include "drm_internal.h"
30
Daniel Vetterae2a6da2016-08-12 22:48:53 +020031/**
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.
Thierry Redingad093602017-02-28 15:46:39 +010038 * Hence they are reference-counted using drm_connector_get() and
39 * drm_connector_put().
Daniel Vetterae2a6da2016-08-12 22:48:53 +020040 *
Daniel Vetterd5745282017-01-25 07:26:45 +010041 * 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
Daniel Vetteraec97462017-01-25 07:26:48 +010043 * objects and initialized by setting the following fields. The connector is
44 * initialized with a call to drm_connector_init() with a pointer to the
45 * &struct drm_connector_funcs and a connector type, and then exposed to
46 * userspace with a call to drm_connector_register().
Daniel Vetterae2a6da2016-08-12 22:48:53 +020047 *
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
Daniel Vetterd5745282017-01-25 07:26:45 +010051 * driver must also set the &drm_connector.encoder field to point to the
Daniel Vetterae2a6da2016-08-12 22:48:53 +020052 * 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
Daniel Vetter52217192016-08-12 22:48:50 +020061struct 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 */
70static 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
91void 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
99void 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 *
Daniel Vetterae2a6da2016-08-12 22:48:53 +0200111 * The kernel supports per-connector configuration of its consoles through
Daniel Vetter52217192016-08-12 22:48:50 +0200112 * 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 */
117static 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) {
Jani Nikula6140cf22017-02-20 10:51:48 +0200131 DRM_INFO("forcing %s connector %s\n", connector->name,
132 drm_get_connector_force_name(mode->force));
Daniel Vetter52217192016-08-12 22:48:50 +0200133 connector->force = mode->force;
134 }
135
136 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
137 connector->name,
138 mode->xres, mode->yres,
139 mode->refresh_specified ? mode->refresh : 60,
140 mode->rb ? " reduced blanking" : "",
141 mode->margins ? " with margins" : "",
142 mode->interlace ? " interlaced" : "");
143}
144
145static void drm_connector_free(struct kref *kref)
146{
147 struct drm_connector *connector =
148 container_of(kref, struct drm_connector, base.refcount);
149 struct drm_device *dev = connector->dev;
150
151 drm_mode_object_unregister(dev, &connector->base);
152 connector->funcs->destroy(connector);
153}
154
155/**
156 * drm_connector_init - Init a preallocated connector
157 * @dev: DRM device
158 * @connector: the connector to init
159 * @funcs: callbacks for this connector
160 * @connector_type: user visible type of the connector
161 *
162 * Initialises a preallocated connector. Connectors should be
163 * subclassed as part of driver connector objects.
164 *
165 * Returns:
166 * Zero on success, error code on failure.
167 */
168int drm_connector_init(struct drm_device *dev,
169 struct drm_connector *connector,
170 const struct drm_connector_funcs *funcs,
171 int connector_type)
172{
173 struct drm_mode_config *config = &dev->mode_config;
174 int ret;
175 struct ida *connector_ida =
176 &drm_connector_enum_list[connector_type].ida;
177
Thierry Reding2135ea72017-02-28 15:46:37 +0100178 ret = __drm_mode_object_add(dev, &connector->base,
179 DRM_MODE_OBJECT_CONNECTOR,
180 false, drm_connector_free);
Daniel Vetter52217192016-08-12 22:48:50 +0200181 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100182 return ret;
Daniel Vetter52217192016-08-12 22:48:50 +0200183
184 connector->base.properties = &connector->properties;
185 connector->dev = dev;
186 connector->funcs = funcs;
187
188 ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
189 if (ret < 0)
190 goto out_put;
191 connector->index = ret;
192 ret = 0;
193
194 connector->connector_type = connector_type;
195 connector->connector_type_id =
196 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
197 if (connector->connector_type_id < 0) {
198 ret = connector->connector_type_id;
199 goto out_put_id;
200 }
201 connector->name =
202 kasprintf(GFP_KERNEL, "%s-%d",
203 drm_connector_enum_list[connector_type].name,
204 connector->connector_type_id);
205 if (!connector->name) {
206 ret = -ENOMEM;
207 goto out_put_type_id;
208 }
209
210 INIT_LIST_HEAD(&connector->probed_modes);
211 INIT_LIST_HEAD(&connector->modes);
Daniel Vettere73ab002016-12-18 14:35:45 +0100212 mutex_init(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200213 connector->edid_blob_ptr = NULL;
214 connector->status = connector_status_unknown;
215
216 drm_connector_get_cmdline_mode(connector);
217
218 /* We should add connectors at the end to avoid upsetting the connector
219 * index too much. */
Daniel Vetter613051d2016-12-14 00:08:06 +0100220 spin_lock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200221 list_add_tail(&connector->head, &config->connector_list);
222 config->num_connector++;
Daniel Vetter613051d2016-12-14 00:08:06 +0100223 spin_unlock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200224
225 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
226 drm_object_attach_property(&connector->base,
227 config->edid_property,
228 0);
229
230 drm_object_attach_property(&connector->base,
231 config->dpms_property, 0);
232
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200233 drm_object_attach_property(&connector->base,
234 config->link_status_property,
235 0);
236
Daniel Vetter52217192016-08-12 22:48:50 +0200237 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
238 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
239 }
240
241 connector->debugfs_entry = NULL;
242out_put_type_id:
243 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200244 ida_simple_remove(connector_ida, connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200245out_put_id:
246 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200247 ida_simple_remove(&config->connector_ida, connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200248out_put:
249 if (ret)
250 drm_mode_object_unregister(dev, &connector->base);
251
Daniel Vetter52217192016-08-12 22:48:50 +0200252 return ret;
253}
254EXPORT_SYMBOL(drm_connector_init);
255
256/**
257 * drm_mode_connector_attach_encoder - attach a connector to an encoder
258 * @connector: connector to attach
259 * @encoder: encoder to attach @connector to
260 *
261 * This function links up a connector to an encoder. Note that the routing
262 * restrictions between encoders and crtcs are exposed to userspace through the
263 * possible_clones and possible_crtcs bitmasks.
264 *
265 * Returns:
266 * Zero on success, negative errno on failure.
267 */
268int drm_mode_connector_attach_encoder(struct drm_connector *connector,
269 struct drm_encoder *encoder)
270{
271 int i;
272
273 /*
274 * In the past, drivers have attempted to model the static association
275 * of connector to encoder in simple connector/encoder devices using a
276 * direct assignment of connector->encoder = encoder. This connection
277 * is a logical one and the responsibility of the core, so drivers are
278 * expected not to mess with this.
279 *
280 * Note that the error return should've been enough here, but a large
281 * majority of drivers ignores the return value, so add in a big WARN
282 * to get people's attention.
283 */
284 if (WARN_ON(connector->encoder))
285 return -EINVAL;
286
287 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
288 if (connector->encoder_ids[i] == 0) {
289 connector->encoder_ids[i] = encoder->base.id;
290 return 0;
291 }
292 }
293 return -ENOMEM;
294}
295EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
296
297static void drm_mode_remove(struct drm_connector *connector,
298 struct drm_display_mode *mode)
299{
300 list_del(&mode->head);
301 drm_mode_destroy(connector->dev, mode);
302}
303
304/**
305 * drm_connector_cleanup - cleans up an initialised connector
306 * @connector: connector to cleanup
307 *
308 * Cleans up the connector but doesn't free the object.
309 */
310void drm_connector_cleanup(struct drm_connector *connector)
311{
312 struct drm_device *dev = connector->dev;
313 struct drm_display_mode *mode, *t;
314
315 /* The connector should have been removed from userspace long before
316 * it is finally destroyed.
317 */
318 if (WARN_ON(connector->registered))
319 drm_connector_unregister(connector);
320
321 if (connector->tile_group) {
322 drm_mode_put_tile_group(dev, connector->tile_group);
323 connector->tile_group = NULL;
324 }
325
326 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
327 drm_mode_remove(connector, mode);
328
329 list_for_each_entry_safe(mode, t, &connector->modes, head)
330 drm_mode_remove(connector, mode);
331
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200332 ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
333 connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200334
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200335 ida_simple_remove(&dev->mode_config.connector_ida,
336 connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200337
338 kfree(connector->display_info.bus_formats);
339 drm_mode_object_unregister(dev, &connector->base);
340 kfree(connector->name);
341 connector->name = NULL;
Daniel Vetter613051d2016-12-14 00:08:06 +0100342 spin_lock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200343 list_del(&connector->head);
344 dev->mode_config.num_connector--;
Daniel Vetter613051d2016-12-14 00:08:06 +0100345 spin_unlock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200346
347 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
348 if (connector->state && connector->funcs->atomic_destroy_state)
349 connector->funcs->atomic_destroy_state(connector,
350 connector->state);
351
Daniel Vettere73ab002016-12-18 14:35:45 +0100352 mutex_destroy(&connector->mutex);
353
Daniel Vetter52217192016-08-12 22:48:50 +0200354 memset(connector, 0, sizeof(*connector));
355}
356EXPORT_SYMBOL(drm_connector_cleanup);
357
358/**
359 * drm_connector_register - register a connector
360 * @connector: the connector to register
361 *
362 * Register userspace interfaces for a connector
363 *
364 * Returns:
365 * Zero on success, error code on failure.
366 */
367int drm_connector_register(struct drm_connector *connector)
368{
Daniel Vettere73ab002016-12-18 14:35:45 +0100369 int ret = 0;
Daniel Vetter52217192016-08-12 22:48:50 +0200370
Daniel Vettere6e7b482017-01-12 17:15:56 +0100371 if (!connector->dev->registered)
372 return 0;
373
Daniel Vettere73ab002016-12-18 14:35:45 +0100374 mutex_lock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200375 if (connector->registered)
Daniel Vettere73ab002016-12-18 14:35:45 +0100376 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200377
378 ret = drm_sysfs_connector_add(connector);
379 if (ret)
Daniel Vettere73ab002016-12-18 14:35:45 +0100380 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200381
382 ret = drm_debugfs_connector_add(connector);
383 if (ret) {
384 goto err_sysfs;
385 }
386
387 if (connector->funcs->late_register) {
388 ret = connector->funcs->late_register(connector);
389 if (ret)
390 goto err_debugfs;
391 }
392
393 drm_mode_object_register(connector->dev, &connector->base);
394
395 connector->registered = true;
Daniel Vettere73ab002016-12-18 14:35:45 +0100396 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200397
398err_debugfs:
399 drm_debugfs_connector_remove(connector);
400err_sysfs:
401 drm_sysfs_connector_remove(connector);
Daniel Vettere73ab002016-12-18 14:35:45 +0100402unlock:
403 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200404 return ret;
405}
406EXPORT_SYMBOL(drm_connector_register);
407
408/**
409 * drm_connector_unregister - unregister a connector
410 * @connector: the connector to unregister
411 *
412 * Unregister userspace interfaces for a connector
413 */
414void drm_connector_unregister(struct drm_connector *connector)
415{
Daniel Vettere73ab002016-12-18 14:35:45 +0100416 mutex_lock(&connector->mutex);
417 if (!connector->registered) {
418 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200419 return;
Daniel Vettere73ab002016-12-18 14:35:45 +0100420 }
Daniel Vetter52217192016-08-12 22:48:50 +0200421
422 if (connector->funcs->early_unregister)
423 connector->funcs->early_unregister(connector);
424
425 drm_sysfs_connector_remove(connector);
426 drm_debugfs_connector_remove(connector);
427
428 connector->registered = false;
Daniel Vettere73ab002016-12-18 14:35:45 +0100429 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200430}
431EXPORT_SYMBOL(drm_connector_unregister);
432
433void drm_connector_unregister_all(struct drm_device *dev)
434{
435 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100436 struct drm_connector_list_iter conn_iter;
Daniel Vetter52217192016-08-12 22:48:50 +0200437
Thierry Redingb982dab2017-02-28 15:46:43 +0100438 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100439 drm_for_each_connector_iter(connector, &conn_iter)
Daniel Vetter52217192016-08-12 22:48:50 +0200440 drm_connector_unregister(connector);
Thierry Redingb982dab2017-02-28 15:46:43 +0100441 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200442}
443
444int drm_connector_register_all(struct drm_device *dev)
445{
446 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100447 struct drm_connector_list_iter conn_iter;
448 int ret = 0;
Daniel Vetter52217192016-08-12 22:48:50 +0200449
Thierry Redingb982dab2017-02-28 15:46:43 +0100450 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100451 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter52217192016-08-12 22:48:50 +0200452 ret = drm_connector_register(connector);
453 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100454 break;
Daniel Vetter52217192016-08-12 22:48:50 +0200455 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100456 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200457
Daniel Vetter613051d2016-12-14 00:08:06 +0100458 if (ret)
459 drm_connector_unregister_all(dev);
Daniel Vetter52217192016-08-12 22:48:50 +0200460 return ret;
461}
462
463/**
464 * drm_get_connector_status_name - return a string for connector status
465 * @status: connector status to compute name of
466 *
467 * In contrast to the other drm_get_*_name functions this one here returns a
468 * const pointer and hence is threadsafe.
469 */
470const char *drm_get_connector_status_name(enum drm_connector_status status)
471{
472 if (status == connector_status_connected)
473 return "connected";
474 else if (status == connector_status_disconnected)
475 return "disconnected";
476 else
477 return "unknown";
478}
479EXPORT_SYMBOL(drm_get_connector_status_name);
480
Jani Nikula6140cf22017-02-20 10:51:48 +0200481/**
482 * drm_get_connector_force_name - return a string for connector force
483 * @force: connector force to get name of
484 *
485 * Returns: const pointer to name.
486 */
487const char *drm_get_connector_force_name(enum drm_connector_force force)
488{
489 switch (force) {
490 case DRM_FORCE_UNSPECIFIED:
491 return "unspecified";
492 case DRM_FORCE_OFF:
493 return "off";
494 case DRM_FORCE_ON:
495 return "on";
496 case DRM_FORCE_ON_DIGITAL:
497 return "digital";
498 default:
499 return "unknown";
500 }
501}
502
Daniel Vetter613051d2016-12-14 00:08:06 +0100503#ifdef CONFIG_LOCKDEP
504static struct lockdep_map connector_list_iter_dep_map = {
505 .name = "drm_connector_list_iter"
506};
507#endif
508
509/**
Thierry Redingb982dab2017-02-28 15:46:43 +0100510 * drm_connector_list_iter_begin - initialize a connector_list iterator
Daniel Vetter613051d2016-12-14 00:08:06 +0100511 * @dev: DRM device
512 * @iter: connector_list iterator
513 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100514 * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
Thierry Redingb982dab2017-02-28 15:46:43 +0100515 * must always be cleaned up again by calling drm_connector_list_iter_end().
Daniel Vetter613051d2016-12-14 00:08:06 +0100516 * Iteration itself happens using drm_connector_list_iter_next() or
517 * drm_for_each_connector_iter().
518 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100519void drm_connector_list_iter_begin(struct drm_device *dev,
520 struct drm_connector_list_iter *iter)
Daniel Vetter613051d2016-12-14 00:08:06 +0100521{
522 iter->dev = dev;
523 iter->conn = NULL;
524 lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
525}
Thierry Redingb982dab2017-02-28 15:46:43 +0100526EXPORT_SYMBOL(drm_connector_list_iter_begin);
Daniel Vetter613051d2016-12-14 00:08:06 +0100527
528/**
529 * drm_connector_list_iter_next - return next connector
530 * @iter: connectr_list iterator
531 *
532 * Returns the next connector for @iter, or NULL when the list walk has
533 * completed.
534 */
535struct drm_connector *
536drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
537{
538 struct drm_connector *old_conn = iter->conn;
539 struct drm_mode_config *config = &iter->dev->mode_config;
540 struct list_head *lhead;
541 unsigned long flags;
542
543 spin_lock_irqsave(&config->connector_list_lock, flags);
544 lhead = old_conn ? &old_conn->head : &config->connector_list;
545
546 do {
547 if (lhead->next == &config->connector_list) {
548 iter->conn = NULL;
549 break;
550 }
551
552 lhead = lhead->next;
553 iter->conn = list_entry(lhead, struct drm_connector, head);
554
555 /* loop until it's not a zombie connector */
556 } while (!kref_get_unless_zero(&iter->conn->base.refcount));
557 spin_unlock_irqrestore(&config->connector_list_lock, flags);
558
559 if (old_conn)
Thierry Redingad093602017-02-28 15:46:39 +0100560 drm_connector_put(old_conn);
Daniel Vetter613051d2016-12-14 00:08:06 +0100561
562 return iter->conn;
563}
564EXPORT_SYMBOL(drm_connector_list_iter_next);
565
566/**
Thierry Redingb982dab2017-02-28 15:46:43 +0100567 * drm_connector_list_iter_end - tear down a connector_list iterator
Daniel Vetter613051d2016-12-14 00:08:06 +0100568 * @iter: connector_list iterator
569 *
570 * Tears down @iter and releases any resources (like &drm_connector references)
571 * acquired while walking the list. This must always be called, both when the
572 * iteration completes fully or when it was aborted without walking the entire
573 * list.
574 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100575void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
Daniel Vetter613051d2016-12-14 00:08:06 +0100576{
577 iter->dev = NULL;
578 if (iter->conn)
Thierry Redingad093602017-02-28 15:46:39 +0100579 drm_connector_put(iter->conn);
Daniel Vetter613051d2016-12-14 00:08:06 +0100580 lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
581}
Thierry Redingb982dab2017-02-28 15:46:43 +0100582EXPORT_SYMBOL(drm_connector_list_iter_end);
Daniel Vetter613051d2016-12-14 00:08:06 +0100583
Daniel Vetter52217192016-08-12 22:48:50 +0200584static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
585 { SubPixelUnknown, "Unknown" },
586 { SubPixelHorizontalRGB, "Horizontal RGB" },
587 { SubPixelHorizontalBGR, "Horizontal BGR" },
588 { SubPixelVerticalRGB, "Vertical RGB" },
589 { SubPixelVerticalBGR, "Vertical BGR" },
590 { SubPixelNone, "None" },
591};
592
593/**
594 * drm_get_subpixel_order_name - return a string for a given subpixel enum
595 * @order: enum of subpixel_order
596 *
597 * Note you could abuse this and return something out of bounds, but that
598 * would be a caller error. No unscrubbed user data should make it here.
599 */
600const char *drm_get_subpixel_order_name(enum subpixel_order order)
601{
602 return drm_subpixel_enum_list[order].name;
603}
604EXPORT_SYMBOL(drm_get_subpixel_order_name);
605
606static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
607 { DRM_MODE_DPMS_ON, "On" },
608 { DRM_MODE_DPMS_STANDBY, "Standby" },
609 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
610 { DRM_MODE_DPMS_OFF, "Off" }
611};
612DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
613
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200614static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
615 { DRM_MODE_LINK_STATUS_GOOD, "Good" },
616 { DRM_MODE_LINK_STATUS_BAD, "Bad" },
617};
618DRM_ENUM_NAME_FN(drm_get_link_status_name, drm_link_status_enum_list)
619
Daniel Vetterb3c6c8b2016-08-12 22:48:55 +0200620/**
621 * drm_display_info_set_bus_formats - set the supported bus formats
622 * @info: display info to store bus formats in
623 * @formats: array containing the supported bus formats
624 * @num_formats: the number of entries in the fmts array
625 *
626 * Store the supported bus formats in display info structure.
627 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
628 * a full list of available formats.
629 */
630int drm_display_info_set_bus_formats(struct drm_display_info *info,
631 const u32 *formats,
632 unsigned int num_formats)
633{
634 u32 *fmts = NULL;
635
636 if (!formats && num_formats)
637 return -EINVAL;
638
639 if (formats && num_formats) {
640 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
641 GFP_KERNEL);
642 if (!fmts)
643 return -ENOMEM;
644 }
645
646 kfree(info->bus_formats);
647 info->bus_formats = fmts;
648 info->num_bus_formats = num_formats;
649
650 return 0;
651}
652EXPORT_SYMBOL(drm_display_info_set_bus_formats);
653
Daniel Vetter52217192016-08-12 22:48:50 +0200654/* Optional connector properties. */
655static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
656 { DRM_MODE_SCALE_NONE, "None" },
657 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
658 { DRM_MODE_SCALE_CENTER, "Center" },
659 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
660};
661
662static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
663 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
664 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
665 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
666};
667
668static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
669 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
670 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
671 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
672};
673DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
674
675static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
676 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
677 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
678 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
679};
680DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
681 drm_dvi_i_subconnector_enum_list)
682
683static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
684 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
685 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
686 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
687 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
688 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
689};
690DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
691
692static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
693 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
694 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
695 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
696 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
697 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
698};
699DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
700 drm_tv_subconnector_enum_list)
701
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100702/**
703 * DOC: standard connector properties
704 *
705 * DRM connectors have a few standardized properties:
706 *
707 * EDID:
708 * Blob property which contains the current EDID read from the sink. This
709 * is useful to parse sink identification information like vendor, model
710 * and serial. Drivers should update this property by calling
711 * drm_mode_connector_update_edid_property(), usually after having parsed
712 * the EDID using drm_add_edid_modes(). Userspace cannot change this
713 * property.
714 * DPMS:
715 * Legacy property for setting the power state of the connector. For atomic
716 * drivers this is only provided for backwards compatibility with existing
717 * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
718 * connector is linked to. Drivers should never set this property directly,
Daniel Vetterd5745282017-01-25 07:26:45 +0100719 * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
720 * callback. Atomic drivers should implement this hook using
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100721 * drm_atomic_helper_connector_dpms(). This is the only property standard
722 * connector property that userspace can change.
723 * PATH:
724 * Connector path property to identify how this sink is physically
725 * connected. Used by DP MST. This should be set by calling
726 * drm_mode_connector_set_path_property(), in the case of DP MST with the
727 * path property the MST manager created. Userspace cannot change this
728 * property.
729 * TILE:
730 * Connector tile group property to indicate how a set of DRM connector
731 * compose together into one logical screen. This is used by both high-res
732 * external screens (often only using a single cable, but exposing multiple
733 * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
734 * are not gen-locked. Note that for tiled panels which are genlocked, like
735 * dual-link LVDS or dual-link DSI, the driver should try to not expose the
736 * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
737 * should update this value using drm_mode_connector_set_tile_property().
738 * Userspace cannot change this property.
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200739 * link-status:
740 * Connector link-status property to indicate the status of link. The default
741 * value of link-status is "GOOD". If something fails during or after modeset,
742 * the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
743 * should update this value using drm_mode_connector_set_link_status_property().
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100744 *
745 * Connectors also have one standardized atomic property:
746 *
747 * CRTC_ID:
748 * Mode object ID of the &drm_crtc this connector should be connected to.
749 */
750
Daniel Vetter52217192016-08-12 22:48:50 +0200751int drm_connector_create_standard_properties(struct drm_device *dev)
752{
753 struct drm_property *prop;
754
755 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
756 DRM_MODE_PROP_IMMUTABLE,
757 "EDID", 0);
758 if (!prop)
759 return -ENOMEM;
760 dev->mode_config.edid_property = prop;
761
762 prop = drm_property_create_enum(dev, 0,
763 "DPMS", drm_dpms_enum_list,
764 ARRAY_SIZE(drm_dpms_enum_list));
765 if (!prop)
766 return -ENOMEM;
767 dev->mode_config.dpms_property = prop;
768
769 prop = drm_property_create(dev,
770 DRM_MODE_PROP_BLOB |
771 DRM_MODE_PROP_IMMUTABLE,
772 "PATH", 0);
773 if (!prop)
774 return -ENOMEM;
775 dev->mode_config.path_property = prop;
776
777 prop = drm_property_create(dev,
778 DRM_MODE_PROP_BLOB |
779 DRM_MODE_PROP_IMMUTABLE,
780 "TILE", 0);
781 if (!prop)
782 return -ENOMEM;
783 dev->mode_config.tile_property = prop;
784
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200785 prop = drm_property_create_enum(dev, 0, "link-status",
786 drm_link_status_enum_list,
787 ARRAY_SIZE(drm_link_status_enum_list));
788 if (!prop)
789 return -ENOMEM;
790 dev->mode_config.link_status_property = prop;
791
Daniel Vetter52217192016-08-12 22:48:50 +0200792 return 0;
793}
794
795/**
796 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
797 * @dev: DRM device
798 *
799 * Called by a driver the first time a DVI-I connector is made.
800 */
801int drm_mode_create_dvi_i_properties(struct drm_device *dev)
802{
803 struct drm_property *dvi_i_selector;
804 struct drm_property *dvi_i_subconnector;
805
806 if (dev->mode_config.dvi_i_select_subconnector_property)
807 return 0;
808
809 dvi_i_selector =
810 drm_property_create_enum(dev, 0,
811 "select subconnector",
812 drm_dvi_i_select_enum_list,
813 ARRAY_SIZE(drm_dvi_i_select_enum_list));
814 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
815
816 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
817 "subconnector",
818 drm_dvi_i_subconnector_enum_list,
819 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
820 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
821
822 return 0;
823}
824EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
825
826/**
827 * drm_create_tv_properties - create TV specific connector properties
828 * @dev: DRM device
829 * @num_modes: number of different TV formats (modes) supported
830 * @modes: array of pointers to strings containing name of each format
831 *
832 * Called by a driver's TV initialization routine, this function creates
833 * the TV specific connector properties for a given device. Caller is
834 * responsible for allocating a list of format names and passing them to
835 * this routine.
836 */
837int drm_mode_create_tv_properties(struct drm_device *dev,
838 unsigned int num_modes,
839 const char * const modes[])
840{
841 struct drm_property *tv_selector;
842 struct drm_property *tv_subconnector;
843 unsigned int i;
844
845 if (dev->mode_config.tv_select_subconnector_property)
846 return 0;
847
848 /*
849 * Basic connector properties
850 */
851 tv_selector = drm_property_create_enum(dev, 0,
852 "select subconnector",
853 drm_tv_select_enum_list,
854 ARRAY_SIZE(drm_tv_select_enum_list));
855 if (!tv_selector)
856 goto nomem;
857
858 dev->mode_config.tv_select_subconnector_property = tv_selector;
859
860 tv_subconnector =
861 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
862 "subconnector",
863 drm_tv_subconnector_enum_list,
864 ARRAY_SIZE(drm_tv_subconnector_enum_list));
865 if (!tv_subconnector)
866 goto nomem;
867 dev->mode_config.tv_subconnector_property = tv_subconnector;
868
869 /*
870 * Other, TV specific properties: margins & TV modes.
871 */
872 dev->mode_config.tv_left_margin_property =
873 drm_property_create_range(dev, 0, "left margin", 0, 100);
874 if (!dev->mode_config.tv_left_margin_property)
875 goto nomem;
876
877 dev->mode_config.tv_right_margin_property =
878 drm_property_create_range(dev, 0, "right margin", 0, 100);
879 if (!dev->mode_config.tv_right_margin_property)
880 goto nomem;
881
882 dev->mode_config.tv_top_margin_property =
883 drm_property_create_range(dev, 0, "top margin", 0, 100);
884 if (!dev->mode_config.tv_top_margin_property)
885 goto nomem;
886
887 dev->mode_config.tv_bottom_margin_property =
888 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
889 if (!dev->mode_config.tv_bottom_margin_property)
890 goto nomem;
891
892 dev->mode_config.tv_mode_property =
893 drm_property_create(dev, DRM_MODE_PROP_ENUM,
894 "mode", num_modes);
895 if (!dev->mode_config.tv_mode_property)
896 goto nomem;
897
898 for (i = 0; i < num_modes; i++)
899 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
900 i, modes[i]);
901
902 dev->mode_config.tv_brightness_property =
903 drm_property_create_range(dev, 0, "brightness", 0, 100);
904 if (!dev->mode_config.tv_brightness_property)
905 goto nomem;
906
907 dev->mode_config.tv_contrast_property =
908 drm_property_create_range(dev, 0, "contrast", 0, 100);
909 if (!dev->mode_config.tv_contrast_property)
910 goto nomem;
911
912 dev->mode_config.tv_flicker_reduction_property =
913 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
914 if (!dev->mode_config.tv_flicker_reduction_property)
915 goto nomem;
916
917 dev->mode_config.tv_overscan_property =
918 drm_property_create_range(dev, 0, "overscan", 0, 100);
919 if (!dev->mode_config.tv_overscan_property)
920 goto nomem;
921
922 dev->mode_config.tv_saturation_property =
923 drm_property_create_range(dev, 0, "saturation", 0, 100);
924 if (!dev->mode_config.tv_saturation_property)
925 goto nomem;
926
927 dev->mode_config.tv_hue_property =
928 drm_property_create_range(dev, 0, "hue", 0, 100);
929 if (!dev->mode_config.tv_hue_property)
930 goto nomem;
931
932 return 0;
933nomem:
934 return -ENOMEM;
935}
936EXPORT_SYMBOL(drm_mode_create_tv_properties);
937
938/**
939 * drm_mode_create_scaling_mode_property - create scaling mode property
940 * @dev: DRM device
941 *
942 * Called by a driver the first time it's needed, must be attached to desired
943 * connectors.
944 */
945int drm_mode_create_scaling_mode_property(struct drm_device *dev)
946{
947 struct drm_property *scaling_mode;
948
949 if (dev->mode_config.scaling_mode_property)
950 return 0;
951
952 scaling_mode =
953 drm_property_create_enum(dev, 0, "scaling mode",
954 drm_scaling_mode_enum_list,
955 ARRAY_SIZE(drm_scaling_mode_enum_list));
956
957 dev->mode_config.scaling_mode_property = scaling_mode;
958
959 return 0;
960}
961EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
962
963/**
964 * drm_mode_create_aspect_ratio_property - create aspect ratio property
965 * @dev: DRM device
966 *
967 * Called by a driver the first time it's needed, must be attached to desired
968 * connectors.
969 *
970 * Returns:
971 * Zero on success, negative errno on failure.
972 */
973int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
974{
975 if (dev->mode_config.aspect_ratio_property)
976 return 0;
977
978 dev->mode_config.aspect_ratio_property =
979 drm_property_create_enum(dev, 0, "aspect ratio",
980 drm_aspect_ratio_enum_list,
981 ARRAY_SIZE(drm_aspect_ratio_enum_list));
982
983 if (dev->mode_config.aspect_ratio_property == NULL)
984 return -ENOMEM;
985
986 return 0;
987}
988EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
989
990/**
991 * drm_mode_create_suggested_offset_properties - create suggests offset properties
992 * @dev: DRM device
993 *
994 * Create the the suggested x/y offset property for connectors.
995 */
996int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
997{
998 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
999 return 0;
1000
1001 dev->mode_config.suggested_x_property =
1002 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1003
1004 dev->mode_config.suggested_y_property =
1005 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1006
1007 if (dev->mode_config.suggested_x_property == NULL ||
1008 dev->mode_config.suggested_y_property == NULL)
1009 return -ENOMEM;
1010 return 0;
1011}
1012EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1013
1014/**
1015 * drm_mode_connector_set_path_property - set tile property on connector
1016 * @connector: connector to set property on.
1017 * @path: path to use for property; must not be NULL.
1018 *
1019 * This creates a property to expose to userspace to specify a
1020 * connector path. This is mainly used for DisplayPort MST where
1021 * connectors have a topology and we want to allow userspace to give
1022 * them more meaningful names.
1023 *
1024 * Returns:
1025 * Zero on success, negative errno on failure.
1026 */
1027int drm_mode_connector_set_path_property(struct drm_connector *connector,
1028 const char *path)
1029{
1030 struct drm_device *dev = connector->dev;
1031 int ret;
1032
1033 ret = drm_property_replace_global_blob(dev,
1034 &connector->path_blob_ptr,
1035 strlen(path) + 1,
1036 path,
1037 &connector->base,
1038 dev->mode_config.path_property);
1039 return ret;
1040}
1041EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1042
1043/**
1044 * drm_mode_connector_set_tile_property - set tile property on connector
1045 * @connector: connector to set property on.
1046 *
1047 * This looks up the tile information for a connector, and creates a
1048 * property for userspace to parse if it exists. The property is of
1049 * the form of 8 integers using ':' as a separator.
1050 *
1051 * Returns:
1052 * Zero on success, errno on failure.
1053 */
1054int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1055{
1056 struct drm_device *dev = connector->dev;
1057 char tile[256];
1058 int ret;
1059
1060 if (!connector->has_tile) {
1061 ret = drm_property_replace_global_blob(dev,
1062 &connector->tile_blob_ptr,
1063 0,
1064 NULL,
1065 &connector->base,
1066 dev->mode_config.tile_property);
1067 return ret;
1068 }
1069
1070 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1071 connector->tile_group->id, connector->tile_is_single_monitor,
1072 connector->num_h_tile, connector->num_v_tile,
1073 connector->tile_h_loc, connector->tile_v_loc,
1074 connector->tile_h_size, connector->tile_v_size);
1075
1076 ret = drm_property_replace_global_blob(dev,
1077 &connector->tile_blob_ptr,
1078 strlen(tile) + 1,
1079 tile,
1080 &connector->base,
1081 dev->mode_config.tile_property);
1082 return ret;
1083}
1084EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1085
1086/**
1087 * drm_mode_connector_update_edid_property - update the edid property of a connector
1088 * @connector: drm connector
1089 * @edid: new value of the edid property
1090 *
1091 * This function creates a new blob modeset object and assigns its id to the
1092 * connector's edid property.
1093 *
1094 * Returns:
1095 * Zero on success, negative errno on failure.
1096 */
1097int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1098 const struct edid *edid)
1099{
1100 struct drm_device *dev = connector->dev;
1101 size_t size = 0;
1102 int ret;
1103
1104 /* ignore requests to set edid when overridden */
1105 if (connector->override_edid)
1106 return 0;
1107
1108 if (edid)
1109 size = EDID_LENGTH * (1 + edid->extensions);
1110
1111 ret = drm_property_replace_global_blob(dev,
1112 &connector->edid_blob_ptr,
1113 size,
1114 edid,
1115 &connector->base,
1116 dev->mode_config.edid_property);
1117 return ret;
1118}
1119EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1120
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001121/**
1122 * drm_mode_connector_set_link_status_property - Set link status property of a connector
1123 * @connector: drm connector
1124 * @link_status: new value of link status property (0: Good, 1: Bad)
1125 *
1126 * In usual working scenario, this link status property will always be set to
1127 * "GOOD". If something fails during or after a mode set, the kernel driver
1128 * may set this link status property to "BAD". The caller then needs to send a
1129 * hotplug uevent for userspace to re-check the valid modes through
1130 * GET_CONNECTOR_IOCTL and retry modeset.
1131 *
1132 * Note: Drivers cannot rely on userspace to support this property and
1133 * issue a modeset. As such, they may choose to handle issues (like
1134 * re-training a link) without userspace's intervention.
1135 *
1136 * The reason for adding this property is to handle link training failures, but
1137 * it is not limited to DP or link training. For example, if we implement
1138 * asynchronous setcrtc, this property can be used to report any failures in that.
1139 */
1140void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1141 uint64_t link_status)
1142{
1143 struct drm_device *dev = connector->dev;
1144
1145 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1146 connector->state->link_status = link_status;
1147 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1148}
1149EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1150
Daniel Vetter52217192016-08-12 22:48:50 +02001151int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1152 struct drm_property *property,
1153 uint64_t value)
1154{
1155 int ret = -EINVAL;
1156 struct drm_connector *connector = obj_to_connector(obj);
1157
1158 /* Do DPMS ourselves */
1159 if (property == connector->dev->mode_config.dpms_property) {
1160 ret = (*connector->funcs->dpms)(connector, (int)value);
1161 } else if (connector->funcs->set_property)
1162 ret = connector->funcs->set_property(connector, property, value);
1163
1164 /* store the property value if successful */
1165 if (!ret)
1166 drm_object_property_set_value(&connector->base, property, value);
1167 return ret;
1168}
1169
1170int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1171 void *data, struct drm_file *file_priv)
1172{
1173 struct drm_mode_connector_set_property *conn_set_prop = data;
1174 struct drm_mode_obj_set_property obj_set_prop = {
1175 .value = conn_set_prop->value,
1176 .prop_id = conn_set_prop->prop_id,
1177 .obj_id = conn_set_prop->connector_id,
1178 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1179 };
1180
1181 /* It does all the locking and checking we need */
1182 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1183}
1184
1185static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1186{
1187 /* For atomic drivers only state objects are synchronously updated and
1188 * protected by modeset locks, so check those first. */
1189 if (connector->state)
1190 return connector->state->best_encoder;
1191 return connector->encoder;
1192}
1193
1194static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1195 const struct drm_file *file_priv)
1196{
1197 /*
1198 * If user-space hasn't configured the driver to expose the stereo 3D
1199 * modes, don't expose them.
1200 */
1201 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1202 return false;
1203
1204 return true;
1205}
1206
1207int drm_mode_getconnector(struct drm_device *dev, void *data,
1208 struct drm_file *file_priv)
1209{
1210 struct drm_mode_get_connector *out_resp = data;
1211 struct drm_connector *connector;
1212 struct drm_encoder *encoder;
1213 struct drm_display_mode *mode;
1214 int mode_count = 0;
1215 int encoders_count = 0;
1216 int ret = 0;
1217 int copied = 0;
1218 int i;
1219 struct drm_mode_modeinfo u_mode;
1220 struct drm_mode_modeinfo __user *mode_ptr;
1221 uint32_t __user *encoder_ptr;
1222
1223 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1224 return -EINVAL;
1225
1226 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1227
Daniel Vetter52217192016-08-12 22:48:50 +02001228 connector = drm_connector_lookup(dev, out_resp->connector_id);
Daniel Vetter91eefc02016-12-14 00:08:10 +01001229 if (!connector)
1230 return -ENOENT;
Daniel Vetter52217192016-08-12 22:48:50 +02001231
Daniel Vetter91eefc02016-12-14 00:08:10 +01001232 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1233 if (connector->encoder_ids[i] != 0)
1234 encoders_count++;
1235
1236 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1237 copied = 0;
1238 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1239 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1240 if (connector->encoder_ids[i] != 0) {
1241 if (put_user(connector->encoder_ids[i],
1242 encoder_ptr + copied)) {
1243 ret = -EFAULT;
Daniel Vettere94ac352017-06-20 22:28:37 +02001244 goto out;
Daniel Vetter91eefc02016-12-14 00:08:10 +01001245 }
1246 copied++;
1247 }
1248 }
1249 }
1250 out_resp->count_encoders = encoders_count;
1251
1252 out_resp->connector_id = connector->base.id;
1253 out_resp->connector_type = connector->connector_type;
1254 out_resp->connector_type_id = connector->connector_type_id;
1255
1256 mutex_lock(&dev->mode_config.mutex);
1257 if (out_resp->count_modes == 0) {
1258 connector->funcs->fill_modes(connector,
1259 dev->mode_config.max_width,
1260 dev->mode_config.max_height);
1261 }
1262
1263 out_resp->mm_width = connector->display_info.width_mm;
1264 out_resp->mm_height = connector->display_info.height_mm;
1265 out_resp->subpixel = connector->display_info.subpixel_order;
1266 out_resp->connection = connector->status;
1267
1268 /* delayed so we get modes regardless of pre-fill_modes state */
1269 list_for_each_entry(mode, &connector->modes, head)
1270 if (drm_mode_expose_to_userspace(mode, file_priv))
1271 mode_count++;
1272
Daniel Vetter52217192016-08-12 22:48:50 +02001273 /*
1274 * This ioctl is called twice, once to determine how much space is
1275 * needed, and the 2nd time to fill it.
1276 */
1277 if ((out_resp->count_modes >= mode_count) && mode_count) {
1278 copied = 0;
1279 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1280 list_for_each_entry(mode, &connector->modes, head) {
1281 if (!drm_mode_expose_to_userspace(mode, file_priv))
1282 continue;
1283
1284 drm_mode_convert_to_umode(&u_mode, mode);
1285 if (copy_to_user(mode_ptr + copied,
1286 &u_mode, sizeof(u_mode))) {
1287 ret = -EFAULT;
Daniel Vettere94ac352017-06-20 22:28:37 +02001288 mutex_unlock(&dev->mode_config.mutex);
1289
Daniel Vetter52217192016-08-12 22:48:50 +02001290 goto out;
1291 }
1292 copied++;
1293 }
1294 }
1295 out_resp->count_modes = mode_count;
Daniel Vetter52217192016-08-12 22:48:50 +02001296 mutex_unlock(&dev->mode_config.mutex);
Daniel Vettere94ac352017-06-20 22:28:37 +02001297
1298 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1299 encoder = drm_connector_get_encoder(connector);
1300 if (encoder)
1301 out_resp->encoder_id = encoder->base.id;
1302 else
1303 out_resp->encoder_id = 0;
1304
1305 /* Only grab properties after probing, to make sure EDID and other
1306 * properties reflect the latest status. */
1307 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1308 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1309 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1310 &out_resp->count_props);
1311 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1312
1313out:
Thierry Redingad093602017-02-28 15:46:39 +01001314 drm_connector_put(connector);
Daniel Vetter52217192016-08-12 22:48:50 +02001315
1316 return ret;
1317}
1318
Daniel Vetter9498c192016-11-14 12:58:24 +01001319
1320/**
1321 * DOC: Tile group
1322 *
1323 * Tile groups are used to represent tiled monitors with a unique integer
1324 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1325 * we store this in a tile group, so we have a common identifier for all tiles
1326 * in a monitor group. The property is called "TILE". Drivers can manage tile
1327 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1328 * drm_mode_get_tile_group(). But this is only needed for internal panels where
1329 * the tile group information is exposed through a non-standard way.
1330 */
1331
1332static void drm_tile_group_free(struct kref *kref)
1333{
1334 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1335 struct drm_device *dev = tg->dev;
1336 mutex_lock(&dev->mode_config.idr_mutex);
1337 idr_remove(&dev->mode_config.tile_idr, tg->id);
1338 mutex_unlock(&dev->mode_config.idr_mutex);
1339 kfree(tg);
1340}
1341
1342/**
1343 * drm_mode_put_tile_group - drop a reference to a tile group.
1344 * @dev: DRM device
1345 * @tg: tile group to drop reference to.
1346 *
1347 * drop reference to tile group and free if 0.
1348 */
1349void drm_mode_put_tile_group(struct drm_device *dev,
1350 struct drm_tile_group *tg)
1351{
1352 kref_put(&tg->refcount, drm_tile_group_free);
1353}
1354EXPORT_SYMBOL(drm_mode_put_tile_group);
1355
1356/**
1357 * drm_mode_get_tile_group - get a reference to an existing tile group
1358 * @dev: DRM device
1359 * @topology: 8-bytes unique per monitor.
1360 *
1361 * Use the unique bytes to get a reference to an existing tile group.
1362 *
1363 * RETURNS:
1364 * tile group or NULL if not found.
1365 */
1366struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1367 char topology[8])
1368{
1369 struct drm_tile_group *tg;
1370 int id;
1371 mutex_lock(&dev->mode_config.idr_mutex);
1372 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1373 if (!memcmp(tg->group_data, topology, 8)) {
1374 if (!kref_get_unless_zero(&tg->refcount))
1375 tg = NULL;
1376 mutex_unlock(&dev->mode_config.idr_mutex);
1377 return tg;
1378 }
1379 }
1380 mutex_unlock(&dev->mode_config.idr_mutex);
1381 return NULL;
1382}
1383EXPORT_SYMBOL(drm_mode_get_tile_group);
1384
1385/**
1386 * drm_mode_create_tile_group - create a tile group from a displayid description
1387 * @dev: DRM device
1388 * @topology: 8-bytes unique per monitor.
1389 *
1390 * Create a tile group for the unique monitor, and get a unique
1391 * identifier for the tile group.
1392 *
1393 * RETURNS:
1394 * new tile group or error.
1395 */
1396struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1397 char topology[8])
1398{
1399 struct drm_tile_group *tg;
1400 int ret;
1401
1402 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1403 if (!tg)
1404 return ERR_PTR(-ENOMEM);
1405
1406 kref_init(&tg->refcount);
1407 memcpy(tg->group_data, topology, 8);
1408 tg->dev = dev;
1409
1410 mutex_lock(&dev->mode_config.idr_mutex);
1411 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1412 if (ret >= 0) {
1413 tg->id = ret;
1414 } else {
1415 kfree(tg);
1416 tg = ERR_PTR(ret);
1417 }
1418
1419 mutex_unlock(&dev->mode_config.idr_mutex);
1420 return tg;
1421}
1422EXPORT_SYMBOL(drm_mode_create_tile_group);