blob: 6a0551744d135c5b9efd81041c3a4d5a0f823e17 [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>
26
27#include "drm_crtc_internal.h"
28#include "drm_internal.h"
29
Daniel Vetterae2a6da2016-08-12 22:48:53 +020030/**
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
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) {
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
159static 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 */
182int 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 connector->edid_blob_ptr = NULL;
229 connector->status = connector_status_unknown;
230
231 drm_connector_get_cmdline_mode(connector);
232
233 /* We should add connectors at the end to avoid upsetting the connector
234 * index too much. */
235 list_add_tail(&connector->head, &config->connector_list);
236 config->num_connector++;
237
238 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
239 drm_object_attach_property(&connector->base,
240 config->edid_property,
241 0);
242
243 drm_object_attach_property(&connector->base,
244 config->dpms_property, 0);
245
246 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
247 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
248 }
249
250 connector->debugfs_entry = NULL;
251out_put_type_id:
252 if (ret)
253 ida_remove(connector_ida, connector->connector_type_id);
254out_put_id:
255 if (ret)
256 ida_remove(&config->connector_ida, connector->index);
257out_put:
258 if (ret)
259 drm_mode_object_unregister(dev, &connector->base);
260
261out_unlock:
262 drm_modeset_unlock_all(dev);
263
264 return ret;
265}
266EXPORT_SYMBOL(drm_connector_init);
267
268/**
269 * drm_mode_connector_attach_encoder - attach a connector to an encoder
270 * @connector: connector to attach
271 * @encoder: encoder to attach @connector to
272 *
273 * This function links up a connector to an encoder. Note that the routing
274 * restrictions between encoders and crtcs are exposed to userspace through the
275 * possible_clones and possible_crtcs bitmasks.
276 *
277 * Returns:
278 * Zero on success, negative errno on failure.
279 */
280int drm_mode_connector_attach_encoder(struct drm_connector *connector,
281 struct drm_encoder *encoder)
282{
283 int i;
284
285 /*
286 * In the past, drivers have attempted to model the static association
287 * of connector to encoder in simple connector/encoder devices using a
288 * direct assignment of connector->encoder = encoder. This connection
289 * is a logical one and the responsibility of the core, so drivers are
290 * expected not to mess with this.
291 *
292 * Note that the error return should've been enough here, but a large
293 * majority of drivers ignores the return value, so add in a big WARN
294 * to get people's attention.
295 */
296 if (WARN_ON(connector->encoder))
297 return -EINVAL;
298
299 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
300 if (connector->encoder_ids[i] == 0) {
301 connector->encoder_ids[i] = encoder->base.id;
302 return 0;
303 }
304 }
305 return -ENOMEM;
306}
307EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
308
309static void drm_mode_remove(struct drm_connector *connector,
310 struct drm_display_mode *mode)
311{
312 list_del(&mode->head);
313 drm_mode_destroy(connector->dev, mode);
314}
315
316/**
317 * drm_connector_cleanup - cleans up an initialised connector
318 * @connector: connector to cleanup
319 *
320 * Cleans up the connector but doesn't free the object.
321 */
322void drm_connector_cleanup(struct drm_connector *connector)
323{
324 struct drm_device *dev = connector->dev;
325 struct drm_display_mode *mode, *t;
326
327 /* The connector should have been removed from userspace long before
328 * it is finally destroyed.
329 */
330 if (WARN_ON(connector->registered))
331 drm_connector_unregister(connector);
332
333 if (connector->tile_group) {
334 drm_mode_put_tile_group(dev, connector->tile_group);
335 connector->tile_group = NULL;
336 }
337
338 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
339 drm_mode_remove(connector, mode);
340
341 list_for_each_entry_safe(mode, t, &connector->modes, head)
342 drm_mode_remove(connector, mode);
343
344 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
345 connector->connector_type_id);
346
347 ida_remove(&dev->mode_config.connector_ida,
348 connector->index);
349
350 kfree(connector->display_info.bus_formats);
351 drm_mode_object_unregister(dev, &connector->base);
352 kfree(connector->name);
353 connector->name = NULL;
354 list_del(&connector->head);
355 dev->mode_config.num_connector--;
356
357 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
358 if (connector->state && connector->funcs->atomic_destroy_state)
359 connector->funcs->atomic_destroy_state(connector,
360 connector->state);
361
362 memset(connector, 0, sizeof(*connector));
363}
364EXPORT_SYMBOL(drm_connector_cleanup);
365
366/**
367 * drm_connector_register - register a connector
368 * @connector: the connector to register
369 *
370 * Register userspace interfaces for a connector
371 *
372 * Returns:
373 * Zero on success, error code on failure.
374 */
375int drm_connector_register(struct drm_connector *connector)
376{
377 int ret;
378
379 if (connector->registered)
380 return 0;
381
382 ret = drm_sysfs_connector_add(connector);
383 if (ret)
384 return ret;
385
386 ret = drm_debugfs_connector_add(connector);
387 if (ret) {
388 goto err_sysfs;
389 }
390
391 if (connector->funcs->late_register) {
392 ret = connector->funcs->late_register(connector);
393 if (ret)
394 goto err_debugfs;
395 }
396
397 drm_mode_object_register(connector->dev, &connector->base);
398
399 connector->registered = true;
400 return 0;
401
402err_debugfs:
403 drm_debugfs_connector_remove(connector);
404err_sysfs:
405 drm_sysfs_connector_remove(connector);
406 return ret;
407}
408EXPORT_SYMBOL(drm_connector_register);
409
410/**
411 * drm_connector_unregister - unregister a connector
412 * @connector: the connector to unregister
413 *
414 * Unregister userspace interfaces for a connector
415 */
416void drm_connector_unregister(struct drm_connector *connector)
417{
418 if (!connector->registered)
419 return;
420
421 if (connector->funcs->early_unregister)
422 connector->funcs->early_unregister(connector);
423
424 drm_sysfs_connector_remove(connector);
425 drm_debugfs_connector_remove(connector);
426
427 connector->registered = false;
428}
429EXPORT_SYMBOL(drm_connector_unregister);
430
431void drm_connector_unregister_all(struct drm_device *dev)
432{
433 struct drm_connector *connector;
434
435 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
436 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
437 drm_connector_unregister(connector);
438}
439
440int drm_connector_register_all(struct drm_device *dev)
441{
442 struct drm_connector *connector;
443 int ret;
444
445 /* FIXME: taking the mode config mutex ends up in a clash with
446 * fbcon/backlight registration */
447 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
448 ret = drm_connector_register(connector);
449 if (ret)
450 goto err;
451 }
452
453 return 0;
454
455err:
456 mutex_unlock(&dev->mode_config.mutex);
457 drm_connector_unregister_all(dev);
458 return ret;
459}
460
461/**
462 * drm_get_connector_status_name - return a string for connector status
463 * @status: connector status to compute name of
464 *
465 * In contrast to the other drm_get_*_name functions this one here returns a
466 * const pointer and hence is threadsafe.
467 */
468const char *drm_get_connector_status_name(enum drm_connector_status status)
469{
470 if (status == connector_status_connected)
471 return "connected";
472 else if (status == connector_status_disconnected)
473 return "disconnected";
474 else
475 return "unknown";
476}
477EXPORT_SYMBOL(drm_get_connector_status_name);
478
479static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
480 { SubPixelUnknown, "Unknown" },
481 { SubPixelHorizontalRGB, "Horizontal RGB" },
482 { SubPixelHorizontalBGR, "Horizontal BGR" },
483 { SubPixelVerticalRGB, "Vertical RGB" },
484 { SubPixelVerticalBGR, "Vertical BGR" },
485 { SubPixelNone, "None" },
486};
487
488/**
489 * drm_get_subpixel_order_name - return a string for a given subpixel enum
490 * @order: enum of subpixel_order
491 *
492 * Note you could abuse this and return something out of bounds, but that
493 * would be a caller error. No unscrubbed user data should make it here.
494 */
495const char *drm_get_subpixel_order_name(enum subpixel_order order)
496{
497 return drm_subpixel_enum_list[order].name;
498}
499EXPORT_SYMBOL(drm_get_subpixel_order_name);
500
501static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
502 { DRM_MODE_DPMS_ON, "On" },
503 { DRM_MODE_DPMS_STANDBY, "Standby" },
504 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
505 { DRM_MODE_DPMS_OFF, "Off" }
506};
507DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
508
509/* Optional connector properties. */
510static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
511 { DRM_MODE_SCALE_NONE, "None" },
512 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
513 { DRM_MODE_SCALE_CENTER, "Center" },
514 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
515};
516
517static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
518 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
519 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
520 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
521};
522
523static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
524 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
525 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
526 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
527};
528DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
529
530static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
531 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
532 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
533 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
534};
535DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
536 drm_dvi_i_subconnector_enum_list)
537
538static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
539 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
540 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
541 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
542 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
543 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
544};
545DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
546
547static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
548 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
549 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
550 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
551 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
552 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
553};
554DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
555 drm_tv_subconnector_enum_list)
556
557int drm_connector_create_standard_properties(struct drm_device *dev)
558{
559 struct drm_property *prop;
560
561 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
562 DRM_MODE_PROP_IMMUTABLE,
563 "EDID", 0);
564 if (!prop)
565 return -ENOMEM;
566 dev->mode_config.edid_property = prop;
567
568 prop = drm_property_create_enum(dev, 0,
569 "DPMS", drm_dpms_enum_list,
570 ARRAY_SIZE(drm_dpms_enum_list));
571 if (!prop)
572 return -ENOMEM;
573 dev->mode_config.dpms_property = prop;
574
575 prop = drm_property_create(dev,
576 DRM_MODE_PROP_BLOB |
577 DRM_MODE_PROP_IMMUTABLE,
578 "PATH", 0);
579 if (!prop)
580 return -ENOMEM;
581 dev->mode_config.path_property = prop;
582
583 prop = drm_property_create(dev,
584 DRM_MODE_PROP_BLOB |
585 DRM_MODE_PROP_IMMUTABLE,
586 "TILE", 0);
587 if (!prop)
588 return -ENOMEM;
589 dev->mode_config.tile_property = prop;
590
591 return 0;
592}
593
594/**
595 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
596 * @dev: DRM device
597 *
598 * Called by a driver the first time a DVI-I connector is made.
599 */
600int drm_mode_create_dvi_i_properties(struct drm_device *dev)
601{
602 struct drm_property *dvi_i_selector;
603 struct drm_property *dvi_i_subconnector;
604
605 if (dev->mode_config.dvi_i_select_subconnector_property)
606 return 0;
607
608 dvi_i_selector =
609 drm_property_create_enum(dev, 0,
610 "select subconnector",
611 drm_dvi_i_select_enum_list,
612 ARRAY_SIZE(drm_dvi_i_select_enum_list));
613 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
614
615 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
616 "subconnector",
617 drm_dvi_i_subconnector_enum_list,
618 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
619 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
620
621 return 0;
622}
623EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
624
625/**
626 * drm_create_tv_properties - create TV specific connector properties
627 * @dev: DRM device
628 * @num_modes: number of different TV formats (modes) supported
629 * @modes: array of pointers to strings containing name of each format
630 *
631 * Called by a driver's TV initialization routine, this function creates
632 * the TV specific connector properties for a given device. Caller is
633 * responsible for allocating a list of format names and passing them to
634 * this routine.
635 */
636int drm_mode_create_tv_properties(struct drm_device *dev,
637 unsigned int num_modes,
638 const char * const modes[])
639{
640 struct drm_property *tv_selector;
641 struct drm_property *tv_subconnector;
642 unsigned int i;
643
644 if (dev->mode_config.tv_select_subconnector_property)
645 return 0;
646
647 /*
648 * Basic connector properties
649 */
650 tv_selector = drm_property_create_enum(dev, 0,
651 "select subconnector",
652 drm_tv_select_enum_list,
653 ARRAY_SIZE(drm_tv_select_enum_list));
654 if (!tv_selector)
655 goto nomem;
656
657 dev->mode_config.tv_select_subconnector_property = tv_selector;
658
659 tv_subconnector =
660 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
661 "subconnector",
662 drm_tv_subconnector_enum_list,
663 ARRAY_SIZE(drm_tv_subconnector_enum_list));
664 if (!tv_subconnector)
665 goto nomem;
666 dev->mode_config.tv_subconnector_property = tv_subconnector;
667
668 /*
669 * Other, TV specific properties: margins & TV modes.
670 */
671 dev->mode_config.tv_left_margin_property =
672 drm_property_create_range(dev, 0, "left margin", 0, 100);
673 if (!dev->mode_config.tv_left_margin_property)
674 goto nomem;
675
676 dev->mode_config.tv_right_margin_property =
677 drm_property_create_range(dev, 0, "right margin", 0, 100);
678 if (!dev->mode_config.tv_right_margin_property)
679 goto nomem;
680
681 dev->mode_config.tv_top_margin_property =
682 drm_property_create_range(dev, 0, "top margin", 0, 100);
683 if (!dev->mode_config.tv_top_margin_property)
684 goto nomem;
685
686 dev->mode_config.tv_bottom_margin_property =
687 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
688 if (!dev->mode_config.tv_bottom_margin_property)
689 goto nomem;
690
691 dev->mode_config.tv_mode_property =
692 drm_property_create(dev, DRM_MODE_PROP_ENUM,
693 "mode", num_modes);
694 if (!dev->mode_config.tv_mode_property)
695 goto nomem;
696
697 for (i = 0; i < num_modes; i++)
698 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
699 i, modes[i]);
700
701 dev->mode_config.tv_brightness_property =
702 drm_property_create_range(dev, 0, "brightness", 0, 100);
703 if (!dev->mode_config.tv_brightness_property)
704 goto nomem;
705
706 dev->mode_config.tv_contrast_property =
707 drm_property_create_range(dev, 0, "contrast", 0, 100);
708 if (!dev->mode_config.tv_contrast_property)
709 goto nomem;
710
711 dev->mode_config.tv_flicker_reduction_property =
712 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
713 if (!dev->mode_config.tv_flicker_reduction_property)
714 goto nomem;
715
716 dev->mode_config.tv_overscan_property =
717 drm_property_create_range(dev, 0, "overscan", 0, 100);
718 if (!dev->mode_config.tv_overscan_property)
719 goto nomem;
720
721 dev->mode_config.tv_saturation_property =
722 drm_property_create_range(dev, 0, "saturation", 0, 100);
723 if (!dev->mode_config.tv_saturation_property)
724 goto nomem;
725
726 dev->mode_config.tv_hue_property =
727 drm_property_create_range(dev, 0, "hue", 0, 100);
728 if (!dev->mode_config.tv_hue_property)
729 goto nomem;
730
731 return 0;
732nomem:
733 return -ENOMEM;
734}
735EXPORT_SYMBOL(drm_mode_create_tv_properties);
736
737/**
738 * drm_mode_create_scaling_mode_property - create scaling mode property
739 * @dev: DRM device
740 *
741 * Called by a driver the first time it's needed, must be attached to desired
742 * connectors.
743 */
744int drm_mode_create_scaling_mode_property(struct drm_device *dev)
745{
746 struct drm_property *scaling_mode;
747
748 if (dev->mode_config.scaling_mode_property)
749 return 0;
750
751 scaling_mode =
752 drm_property_create_enum(dev, 0, "scaling mode",
753 drm_scaling_mode_enum_list,
754 ARRAY_SIZE(drm_scaling_mode_enum_list));
755
756 dev->mode_config.scaling_mode_property = scaling_mode;
757
758 return 0;
759}
760EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
761
762/**
763 * drm_mode_create_aspect_ratio_property - create aspect ratio property
764 * @dev: DRM device
765 *
766 * Called by a driver the first time it's needed, must be attached to desired
767 * connectors.
768 *
769 * Returns:
770 * Zero on success, negative errno on failure.
771 */
772int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
773{
774 if (dev->mode_config.aspect_ratio_property)
775 return 0;
776
777 dev->mode_config.aspect_ratio_property =
778 drm_property_create_enum(dev, 0, "aspect ratio",
779 drm_aspect_ratio_enum_list,
780 ARRAY_SIZE(drm_aspect_ratio_enum_list));
781
782 if (dev->mode_config.aspect_ratio_property == NULL)
783 return -ENOMEM;
784
785 return 0;
786}
787EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
788
789/**
790 * drm_mode_create_suggested_offset_properties - create suggests offset properties
791 * @dev: DRM device
792 *
793 * Create the the suggested x/y offset property for connectors.
794 */
795int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
796{
797 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
798 return 0;
799
800 dev->mode_config.suggested_x_property =
801 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
802
803 dev->mode_config.suggested_y_property =
804 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
805
806 if (dev->mode_config.suggested_x_property == NULL ||
807 dev->mode_config.suggested_y_property == NULL)
808 return -ENOMEM;
809 return 0;
810}
811EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
812
813/**
814 * drm_mode_connector_set_path_property - set tile property on connector
815 * @connector: connector to set property on.
816 * @path: path to use for property; must not be NULL.
817 *
818 * This creates a property to expose to userspace to specify a
819 * connector path. This is mainly used for DisplayPort MST where
820 * connectors have a topology and we want to allow userspace to give
821 * them more meaningful names.
822 *
823 * Returns:
824 * Zero on success, negative errno on failure.
825 */
826int drm_mode_connector_set_path_property(struct drm_connector *connector,
827 const char *path)
828{
829 struct drm_device *dev = connector->dev;
830 int ret;
831
832 ret = drm_property_replace_global_blob(dev,
833 &connector->path_blob_ptr,
834 strlen(path) + 1,
835 path,
836 &connector->base,
837 dev->mode_config.path_property);
838 return ret;
839}
840EXPORT_SYMBOL(drm_mode_connector_set_path_property);
841
842/**
843 * drm_mode_connector_set_tile_property - set tile property on connector
844 * @connector: connector to set property on.
845 *
846 * This looks up the tile information for a connector, and creates a
847 * property for userspace to parse if it exists. The property is of
848 * the form of 8 integers using ':' as a separator.
849 *
850 * Returns:
851 * Zero on success, errno on failure.
852 */
853int drm_mode_connector_set_tile_property(struct drm_connector *connector)
854{
855 struct drm_device *dev = connector->dev;
856 char tile[256];
857 int ret;
858
859 if (!connector->has_tile) {
860 ret = drm_property_replace_global_blob(dev,
861 &connector->tile_blob_ptr,
862 0,
863 NULL,
864 &connector->base,
865 dev->mode_config.tile_property);
866 return ret;
867 }
868
869 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
870 connector->tile_group->id, connector->tile_is_single_monitor,
871 connector->num_h_tile, connector->num_v_tile,
872 connector->tile_h_loc, connector->tile_v_loc,
873 connector->tile_h_size, connector->tile_v_size);
874
875 ret = drm_property_replace_global_blob(dev,
876 &connector->tile_blob_ptr,
877 strlen(tile) + 1,
878 tile,
879 &connector->base,
880 dev->mode_config.tile_property);
881 return ret;
882}
883EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
884
885/**
886 * drm_mode_connector_update_edid_property - update the edid property of a connector
887 * @connector: drm connector
888 * @edid: new value of the edid property
889 *
890 * This function creates a new blob modeset object and assigns its id to the
891 * connector's edid property.
892 *
893 * Returns:
894 * Zero on success, negative errno on failure.
895 */
896int drm_mode_connector_update_edid_property(struct drm_connector *connector,
897 const struct edid *edid)
898{
899 struct drm_device *dev = connector->dev;
900 size_t size = 0;
901 int ret;
902
903 /* ignore requests to set edid when overridden */
904 if (connector->override_edid)
905 return 0;
906
907 if (edid)
908 size = EDID_LENGTH * (1 + edid->extensions);
909
910 ret = drm_property_replace_global_blob(dev,
911 &connector->edid_blob_ptr,
912 size,
913 edid,
914 &connector->base,
915 dev->mode_config.edid_property);
916 return ret;
917}
918EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
919
920int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
921 struct drm_property *property,
922 uint64_t value)
923{
924 int ret = -EINVAL;
925 struct drm_connector *connector = obj_to_connector(obj);
926
927 /* Do DPMS ourselves */
928 if (property == connector->dev->mode_config.dpms_property) {
929 ret = (*connector->funcs->dpms)(connector, (int)value);
930 } else if (connector->funcs->set_property)
931 ret = connector->funcs->set_property(connector, property, value);
932
933 /* store the property value if successful */
934 if (!ret)
935 drm_object_property_set_value(&connector->base, property, value);
936 return ret;
937}
938
939int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
940 void *data, struct drm_file *file_priv)
941{
942 struct drm_mode_connector_set_property *conn_set_prop = data;
943 struct drm_mode_obj_set_property obj_set_prop = {
944 .value = conn_set_prop->value,
945 .prop_id = conn_set_prop->prop_id,
946 .obj_id = conn_set_prop->connector_id,
947 .obj_type = DRM_MODE_OBJECT_CONNECTOR
948 };
949
950 /* It does all the locking and checking we need */
951 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
952}
953
954static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
955{
956 /* For atomic drivers only state objects are synchronously updated and
957 * protected by modeset locks, so check those first. */
958 if (connector->state)
959 return connector->state->best_encoder;
960 return connector->encoder;
961}
962
963static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
964 const struct drm_file *file_priv)
965{
966 /*
967 * If user-space hasn't configured the driver to expose the stereo 3D
968 * modes, don't expose them.
969 */
970 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
971 return false;
972
973 return true;
974}
975
976int drm_mode_getconnector(struct drm_device *dev, void *data,
977 struct drm_file *file_priv)
978{
979 struct drm_mode_get_connector *out_resp = data;
980 struct drm_connector *connector;
981 struct drm_encoder *encoder;
982 struct drm_display_mode *mode;
983 int mode_count = 0;
984 int encoders_count = 0;
985 int ret = 0;
986 int copied = 0;
987 int i;
988 struct drm_mode_modeinfo u_mode;
989 struct drm_mode_modeinfo __user *mode_ptr;
990 uint32_t __user *encoder_ptr;
991
992 if (!drm_core_check_feature(dev, DRIVER_MODESET))
993 return -EINVAL;
994
995 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
996
997 mutex_lock(&dev->mode_config.mutex);
998
999 connector = drm_connector_lookup(dev, out_resp->connector_id);
1000 if (!connector) {
1001 ret = -ENOENT;
1002 goto out_unlock;
1003 }
1004
1005 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1006 if (connector->encoder_ids[i] != 0)
1007 encoders_count++;
1008
1009 if (out_resp->count_modes == 0) {
1010 connector->funcs->fill_modes(connector,
1011 dev->mode_config.max_width,
1012 dev->mode_config.max_height);
1013 }
1014
1015 /* delayed so we get modes regardless of pre-fill_modes state */
1016 list_for_each_entry(mode, &connector->modes, head)
1017 if (drm_mode_expose_to_userspace(mode, file_priv))
1018 mode_count++;
1019
1020 out_resp->connector_id = connector->base.id;
1021 out_resp->connector_type = connector->connector_type;
1022 out_resp->connector_type_id = connector->connector_type_id;
1023 out_resp->mm_width = connector->display_info.width_mm;
1024 out_resp->mm_height = connector->display_info.height_mm;
1025 out_resp->subpixel = connector->display_info.subpixel_order;
1026 out_resp->connection = connector->status;
1027
1028 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1029 encoder = drm_connector_get_encoder(connector);
1030 if (encoder)
1031 out_resp->encoder_id = encoder->base.id;
1032 else
1033 out_resp->encoder_id = 0;
1034
1035 /*
1036 * This ioctl is called twice, once to determine how much space is
1037 * needed, and the 2nd time to fill it.
1038 */
1039 if ((out_resp->count_modes >= mode_count) && mode_count) {
1040 copied = 0;
1041 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1042 list_for_each_entry(mode, &connector->modes, head) {
1043 if (!drm_mode_expose_to_userspace(mode, file_priv))
1044 continue;
1045
1046 drm_mode_convert_to_umode(&u_mode, mode);
1047 if (copy_to_user(mode_ptr + copied,
1048 &u_mode, sizeof(u_mode))) {
1049 ret = -EFAULT;
1050 goto out;
1051 }
1052 copied++;
1053 }
1054 }
1055 out_resp->count_modes = mode_count;
1056
1057 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1058 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1059 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1060 &out_resp->count_props);
1061 if (ret)
1062 goto out;
1063
1064 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1065 copied = 0;
1066 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1067 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1068 if (connector->encoder_ids[i] != 0) {
1069 if (put_user(connector->encoder_ids[i],
1070 encoder_ptr + copied)) {
1071 ret = -EFAULT;
1072 goto out;
1073 }
1074 copied++;
1075 }
1076 }
1077 }
1078 out_resp->count_encoders = encoders_count;
1079
1080out:
1081 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1082
1083 drm_connector_unreference(connector);
1084out_unlock:
1085 mutex_unlock(&dev->mode_config.mutex);
1086
1087 return ret;
1088}
1089