Loading...
Note: File does not exist in v4.6.
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
61struct 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 *
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 */
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 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;
252out_put_type_id:
253 if (ret)
254 ida_simple_remove(connector_ida, connector->connector_type_id);
255out_put_id:
256 if (ret)
257 ida_simple_remove(&config->connector_ida, connector->index);
258out_put:
259 if (ret)
260 drm_mode_object_unregister(dev, &connector->base);
261
262out_unlock:
263 drm_modeset_unlock_all(dev);
264
265 return ret;
266}
267EXPORT_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 */
281int 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}
308EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
309
310static 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 */
323void 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}
367EXPORT_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 */
378int 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
409err_debugfs:
410 drm_debugfs_connector_remove(connector);
411err_sysfs:
412 drm_sysfs_connector_remove(connector);
413unlock:
414 mutex_unlock(&connector->mutex);
415 return ret;
416}
417EXPORT_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 */
425void 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}
442EXPORT_SYMBOL(drm_connector_unregister);
443
444void 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
453int 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
468err:
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 */
481const 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}
490EXPORT_SYMBOL(drm_get_connector_status_name);
491
492static 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 */
508const char *drm_get_subpixel_order_name(enum subpixel_order order)
509{
510 return drm_subpixel_enum_list[order].name;
511}
512EXPORT_SYMBOL(drm_get_subpixel_order_name);
513
514static 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};
520DRM_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 */
532int 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}
554EXPORT_SYMBOL(drm_display_info_set_bus_formats);
555
556/* Optional connector properties. */
557static 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
564static 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
570static 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};
575DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
576
577static 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};
582DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
583 drm_dvi_i_subconnector_enum_list)
584
585static 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};
592DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
593
594static 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};
601DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
602 drm_tv_subconnector_enum_list)
603
604/**
605 * DOC: standard connector properties
606 *
607 * DRM connectors have a few standardized properties:
608 *
609 * EDID:
610 * Blob property which contains the current EDID read from the sink. This
611 * is useful to parse sink identification information like vendor, model
612 * and serial. Drivers should update this property by calling
613 * drm_mode_connector_update_edid_property(), usually after having parsed
614 * the EDID using drm_add_edid_modes(). Userspace cannot change this
615 * property.
616 * DPMS:
617 * Legacy property for setting the power state of the connector. For atomic
618 * drivers this is only provided for backwards compatibility with existing
619 * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
620 * connector is linked to. Drivers should never set this property directly,
621 * it is handled by the DRM core by calling the ->dpms() callback in
622 * &drm_connector_funcs. Atomic drivers should implement this hook using
623 * drm_atomic_helper_connector_dpms(). This is the only property standard
624 * connector property that userspace can change.
625 * PATH:
626 * Connector path property to identify how this sink is physically
627 * connected. Used by DP MST. This should be set by calling
628 * drm_mode_connector_set_path_property(), in the case of DP MST with the
629 * path property the MST manager created. Userspace cannot change this
630 * property.
631 * TILE:
632 * Connector tile group property to indicate how a set of DRM connector
633 * compose together into one logical screen. This is used by both high-res
634 * external screens (often only using a single cable, but exposing multiple
635 * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
636 * are not gen-locked. Note that for tiled panels which are genlocked, like
637 * dual-link LVDS or dual-link DSI, the driver should try to not expose the
638 * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
639 * should update this value using drm_mode_connector_set_tile_property().
640 * Userspace cannot change this property.
641 *
642 * Connectors also have one standardized atomic property:
643 *
644 * CRTC_ID:
645 * Mode object ID of the &drm_crtc this connector should be connected to.
646 */
647
648int drm_connector_create_standard_properties(struct drm_device *dev)
649{
650 struct drm_property *prop;
651
652 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
653 DRM_MODE_PROP_IMMUTABLE,
654 "EDID", 0);
655 if (!prop)
656 return -ENOMEM;
657 dev->mode_config.edid_property = prop;
658
659 prop = drm_property_create_enum(dev, 0,
660 "DPMS", drm_dpms_enum_list,
661 ARRAY_SIZE(drm_dpms_enum_list));
662 if (!prop)
663 return -ENOMEM;
664 dev->mode_config.dpms_property = prop;
665
666 prop = drm_property_create(dev,
667 DRM_MODE_PROP_BLOB |
668 DRM_MODE_PROP_IMMUTABLE,
669 "PATH", 0);
670 if (!prop)
671 return -ENOMEM;
672 dev->mode_config.path_property = prop;
673
674 prop = drm_property_create(dev,
675 DRM_MODE_PROP_BLOB |
676 DRM_MODE_PROP_IMMUTABLE,
677 "TILE", 0);
678 if (!prop)
679 return -ENOMEM;
680 dev->mode_config.tile_property = prop;
681
682 return 0;
683}
684
685/**
686 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
687 * @dev: DRM device
688 *
689 * Called by a driver the first time a DVI-I connector is made.
690 */
691int drm_mode_create_dvi_i_properties(struct drm_device *dev)
692{
693 struct drm_property *dvi_i_selector;
694 struct drm_property *dvi_i_subconnector;
695
696 if (dev->mode_config.dvi_i_select_subconnector_property)
697 return 0;
698
699 dvi_i_selector =
700 drm_property_create_enum(dev, 0,
701 "select subconnector",
702 drm_dvi_i_select_enum_list,
703 ARRAY_SIZE(drm_dvi_i_select_enum_list));
704 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
705
706 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
707 "subconnector",
708 drm_dvi_i_subconnector_enum_list,
709 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
710 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
711
712 return 0;
713}
714EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
715
716/**
717 * drm_create_tv_properties - create TV specific connector properties
718 * @dev: DRM device
719 * @num_modes: number of different TV formats (modes) supported
720 * @modes: array of pointers to strings containing name of each format
721 *
722 * Called by a driver's TV initialization routine, this function creates
723 * the TV specific connector properties for a given device. Caller is
724 * responsible for allocating a list of format names and passing them to
725 * this routine.
726 */
727int drm_mode_create_tv_properties(struct drm_device *dev,
728 unsigned int num_modes,
729 const char * const modes[])
730{
731 struct drm_property *tv_selector;
732 struct drm_property *tv_subconnector;
733 unsigned int i;
734
735 if (dev->mode_config.tv_select_subconnector_property)
736 return 0;
737
738 /*
739 * Basic connector properties
740 */
741 tv_selector = drm_property_create_enum(dev, 0,
742 "select subconnector",
743 drm_tv_select_enum_list,
744 ARRAY_SIZE(drm_tv_select_enum_list));
745 if (!tv_selector)
746 goto nomem;
747
748 dev->mode_config.tv_select_subconnector_property = tv_selector;
749
750 tv_subconnector =
751 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
752 "subconnector",
753 drm_tv_subconnector_enum_list,
754 ARRAY_SIZE(drm_tv_subconnector_enum_list));
755 if (!tv_subconnector)
756 goto nomem;
757 dev->mode_config.tv_subconnector_property = tv_subconnector;
758
759 /*
760 * Other, TV specific properties: margins & TV modes.
761 */
762 dev->mode_config.tv_left_margin_property =
763 drm_property_create_range(dev, 0, "left margin", 0, 100);
764 if (!dev->mode_config.tv_left_margin_property)
765 goto nomem;
766
767 dev->mode_config.tv_right_margin_property =
768 drm_property_create_range(dev, 0, "right margin", 0, 100);
769 if (!dev->mode_config.tv_right_margin_property)
770 goto nomem;
771
772 dev->mode_config.tv_top_margin_property =
773 drm_property_create_range(dev, 0, "top margin", 0, 100);
774 if (!dev->mode_config.tv_top_margin_property)
775 goto nomem;
776
777 dev->mode_config.tv_bottom_margin_property =
778 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
779 if (!dev->mode_config.tv_bottom_margin_property)
780 goto nomem;
781
782 dev->mode_config.tv_mode_property =
783 drm_property_create(dev, DRM_MODE_PROP_ENUM,
784 "mode", num_modes);
785 if (!dev->mode_config.tv_mode_property)
786 goto nomem;
787
788 for (i = 0; i < num_modes; i++)
789 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
790 i, modes[i]);
791
792 dev->mode_config.tv_brightness_property =
793 drm_property_create_range(dev, 0, "brightness", 0, 100);
794 if (!dev->mode_config.tv_brightness_property)
795 goto nomem;
796
797 dev->mode_config.tv_contrast_property =
798 drm_property_create_range(dev, 0, "contrast", 0, 100);
799 if (!dev->mode_config.tv_contrast_property)
800 goto nomem;
801
802 dev->mode_config.tv_flicker_reduction_property =
803 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
804 if (!dev->mode_config.tv_flicker_reduction_property)
805 goto nomem;
806
807 dev->mode_config.tv_overscan_property =
808 drm_property_create_range(dev, 0, "overscan", 0, 100);
809 if (!dev->mode_config.tv_overscan_property)
810 goto nomem;
811
812 dev->mode_config.tv_saturation_property =
813 drm_property_create_range(dev, 0, "saturation", 0, 100);
814 if (!dev->mode_config.tv_saturation_property)
815 goto nomem;
816
817 dev->mode_config.tv_hue_property =
818 drm_property_create_range(dev, 0, "hue", 0, 100);
819 if (!dev->mode_config.tv_hue_property)
820 goto nomem;
821
822 return 0;
823nomem:
824 return -ENOMEM;
825}
826EXPORT_SYMBOL(drm_mode_create_tv_properties);
827
828/**
829 * drm_mode_create_scaling_mode_property - create scaling mode property
830 * @dev: DRM device
831 *
832 * Called by a driver the first time it's needed, must be attached to desired
833 * connectors.
834 */
835int drm_mode_create_scaling_mode_property(struct drm_device *dev)
836{
837 struct drm_property *scaling_mode;
838
839 if (dev->mode_config.scaling_mode_property)
840 return 0;
841
842 scaling_mode =
843 drm_property_create_enum(dev, 0, "scaling mode",
844 drm_scaling_mode_enum_list,
845 ARRAY_SIZE(drm_scaling_mode_enum_list));
846
847 dev->mode_config.scaling_mode_property = scaling_mode;
848
849 return 0;
850}
851EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
852
853/**
854 * drm_mode_create_aspect_ratio_property - create aspect ratio property
855 * @dev: DRM device
856 *
857 * Called by a driver the first time it's needed, must be attached to desired
858 * connectors.
859 *
860 * Returns:
861 * Zero on success, negative errno on failure.
862 */
863int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
864{
865 if (dev->mode_config.aspect_ratio_property)
866 return 0;
867
868 dev->mode_config.aspect_ratio_property =
869 drm_property_create_enum(dev, 0, "aspect ratio",
870 drm_aspect_ratio_enum_list,
871 ARRAY_SIZE(drm_aspect_ratio_enum_list));
872
873 if (dev->mode_config.aspect_ratio_property == NULL)
874 return -ENOMEM;
875
876 return 0;
877}
878EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
879
880/**
881 * drm_mode_create_suggested_offset_properties - create suggests offset properties
882 * @dev: DRM device
883 *
884 * Create the the suggested x/y offset property for connectors.
885 */
886int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
887{
888 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
889 return 0;
890
891 dev->mode_config.suggested_x_property =
892 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
893
894 dev->mode_config.suggested_y_property =
895 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
896
897 if (dev->mode_config.suggested_x_property == NULL ||
898 dev->mode_config.suggested_y_property == NULL)
899 return -ENOMEM;
900 return 0;
901}
902EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
903
904/**
905 * drm_mode_connector_set_path_property - set tile property on connector
906 * @connector: connector to set property on.
907 * @path: path to use for property; must not be NULL.
908 *
909 * This creates a property to expose to userspace to specify a
910 * connector path. This is mainly used for DisplayPort MST where
911 * connectors have a topology and we want to allow userspace to give
912 * them more meaningful names.
913 *
914 * Returns:
915 * Zero on success, negative errno on failure.
916 */
917int drm_mode_connector_set_path_property(struct drm_connector *connector,
918 const char *path)
919{
920 struct drm_device *dev = connector->dev;
921 int ret;
922
923 ret = drm_property_replace_global_blob(dev,
924 &connector->path_blob_ptr,
925 strlen(path) + 1,
926 path,
927 &connector->base,
928 dev->mode_config.path_property);
929 return ret;
930}
931EXPORT_SYMBOL(drm_mode_connector_set_path_property);
932
933/**
934 * drm_mode_connector_set_tile_property - set tile property on connector
935 * @connector: connector to set property on.
936 *
937 * This looks up the tile information for a connector, and creates a
938 * property for userspace to parse if it exists. The property is of
939 * the form of 8 integers using ':' as a separator.
940 *
941 * Returns:
942 * Zero on success, errno on failure.
943 */
944int drm_mode_connector_set_tile_property(struct drm_connector *connector)
945{
946 struct drm_device *dev = connector->dev;
947 char tile[256];
948 int ret;
949
950 if (!connector->has_tile) {
951 ret = drm_property_replace_global_blob(dev,
952 &connector->tile_blob_ptr,
953 0,
954 NULL,
955 &connector->base,
956 dev->mode_config.tile_property);
957 return ret;
958 }
959
960 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
961 connector->tile_group->id, connector->tile_is_single_monitor,
962 connector->num_h_tile, connector->num_v_tile,
963 connector->tile_h_loc, connector->tile_v_loc,
964 connector->tile_h_size, connector->tile_v_size);
965
966 ret = drm_property_replace_global_blob(dev,
967 &connector->tile_blob_ptr,
968 strlen(tile) + 1,
969 tile,
970 &connector->base,
971 dev->mode_config.tile_property);
972 return ret;
973}
974EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
975
976/**
977 * drm_mode_connector_update_edid_property - update the edid property of a connector
978 * @connector: drm connector
979 * @edid: new value of the edid property
980 *
981 * This function creates a new blob modeset object and assigns its id to the
982 * connector's edid property.
983 *
984 * Returns:
985 * Zero on success, negative errno on failure.
986 */
987int drm_mode_connector_update_edid_property(struct drm_connector *connector,
988 const struct edid *edid)
989{
990 struct drm_device *dev = connector->dev;
991 size_t size = 0;
992 int ret;
993
994 /* ignore requests to set edid when overridden */
995 if (connector->override_edid)
996 return 0;
997
998 if (edid)
999 size = EDID_LENGTH * (1 + edid->extensions);
1000
1001 ret = drm_property_replace_global_blob(dev,
1002 &connector->edid_blob_ptr,
1003 size,
1004 edid,
1005 &connector->base,
1006 dev->mode_config.edid_property);
1007 return ret;
1008}
1009EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1010
1011int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1012 struct drm_property *property,
1013 uint64_t value)
1014{
1015 int ret = -EINVAL;
1016 struct drm_connector *connector = obj_to_connector(obj);
1017
1018 /* Do DPMS ourselves */
1019 if (property == connector->dev->mode_config.dpms_property) {
1020 ret = (*connector->funcs->dpms)(connector, (int)value);
1021 } else if (connector->funcs->set_property)
1022 ret = connector->funcs->set_property(connector, property, value);
1023
1024 /* store the property value if successful */
1025 if (!ret)
1026 drm_object_property_set_value(&connector->base, property, value);
1027 return ret;
1028}
1029
1030int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1031 void *data, struct drm_file *file_priv)
1032{
1033 struct drm_mode_connector_set_property *conn_set_prop = data;
1034 struct drm_mode_obj_set_property obj_set_prop = {
1035 .value = conn_set_prop->value,
1036 .prop_id = conn_set_prop->prop_id,
1037 .obj_id = conn_set_prop->connector_id,
1038 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1039 };
1040
1041 /* It does all the locking and checking we need */
1042 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1043}
1044
1045static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1046{
1047 /* For atomic drivers only state objects are synchronously updated and
1048 * protected by modeset locks, so check those first. */
1049 if (connector->state)
1050 return connector->state->best_encoder;
1051 return connector->encoder;
1052}
1053
1054static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1055 const struct drm_file *file_priv)
1056{
1057 /*
1058 * If user-space hasn't configured the driver to expose the stereo 3D
1059 * modes, don't expose them.
1060 */
1061 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1062 return false;
1063
1064 return true;
1065}
1066
1067int drm_mode_getconnector(struct drm_device *dev, void *data,
1068 struct drm_file *file_priv)
1069{
1070 struct drm_mode_get_connector *out_resp = data;
1071 struct drm_connector *connector;
1072 struct drm_encoder *encoder;
1073 struct drm_display_mode *mode;
1074 int mode_count = 0;
1075 int encoders_count = 0;
1076 int ret = 0;
1077 int copied = 0;
1078 int i;
1079 struct drm_mode_modeinfo u_mode;
1080 struct drm_mode_modeinfo __user *mode_ptr;
1081 uint32_t __user *encoder_ptr;
1082
1083 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1084 return -EINVAL;
1085
1086 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1087
1088 mutex_lock(&dev->mode_config.mutex);
1089
1090 connector = drm_connector_lookup(dev, out_resp->connector_id);
1091 if (!connector) {
1092 ret = -ENOENT;
1093 goto out_unlock;
1094 }
1095
1096 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1097 if (connector->encoder_ids[i] != 0)
1098 encoders_count++;
1099
1100 if (out_resp->count_modes == 0) {
1101 connector->funcs->fill_modes(connector,
1102 dev->mode_config.max_width,
1103 dev->mode_config.max_height);
1104 }
1105
1106 /* delayed so we get modes regardless of pre-fill_modes state */
1107 list_for_each_entry(mode, &connector->modes, head)
1108 if (drm_mode_expose_to_userspace(mode, file_priv))
1109 mode_count++;
1110
1111 out_resp->connector_id = connector->base.id;
1112 out_resp->connector_type = connector->connector_type;
1113 out_resp->connector_type_id = connector->connector_type_id;
1114 out_resp->mm_width = connector->display_info.width_mm;
1115 out_resp->mm_height = connector->display_info.height_mm;
1116 out_resp->subpixel = connector->display_info.subpixel_order;
1117 out_resp->connection = connector->status;
1118
1119 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1120 encoder = drm_connector_get_encoder(connector);
1121 if (encoder)
1122 out_resp->encoder_id = encoder->base.id;
1123 else
1124 out_resp->encoder_id = 0;
1125
1126 /*
1127 * This ioctl is called twice, once to determine how much space is
1128 * needed, and the 2nd time to fill it.
1129 */
1130 if ((out_resp->count_modes >= mode_count) && mode_count) {
1131 copied = 0;
1132 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1133 list_for_each_entry(mode, &connector->modes, head) {
1134 if (!drm_mode_expose_to_userspace(mode, file_priv))
1135 continue;
1136
1137 drm_mode_convert_to_umode(&u_mode, mode);
1138 if (copy_to_user(mode_ptr + copied,
1139 &u_mode, sizeof(u_mode))) {
1140 ret = -EFAULT;
1141 goto out;
1142 }
1143 copied++;
1144 }
1145 }
1146 out_resp->count_modes = mode_count;
1147
1148 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1149 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1150 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1151 &out_resp->count_props);
1152 if (ret)
1153 goto out;
1154
1155 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1156 copied = 0;
1157 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1158 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1159 if (connector->encoder_ids[i] != 0) {
1160 if (put_user(connector->encoder_ids[i],
1161 encoder_ptr + copied)) {
1162 ret = -EFAULT;
1163 goto out;
1164 }
1165 copied++;
1166 }
1167 }
1168 }
1169 out_resp->count_encoders = encoders_count;
1170
1171out:
1172 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1173
1174 drm_connector_unreference(connector);
1175out_unlock:
1176 mutex_unlock(&dev->mode_config.mutex);
1177
1178 return ret;
1179}
1180
1181
1182/**
1183 * DOC: Tile group
1184 *
1185 * Tile groups are used to represent tiled monitors with a unique integer
1186 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1187 * we store this in a tile group, so we have a common identifier for all tiles
1188 * in a monitor group. The property is called "TILE". Drivers can manage tile
1189 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1190 * drm_mode_get_tile_group(). But this is only needed for internal panels where
1191 * the tile group information is exposed through a non-standard way.
1192 */
1193
1194static void drm_tile_group_free(struct kref *kref)
1195{
1196 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1197 struct drm_device *dev = tg->dev;
1198 mutex_lock(&dev->mode_config.idr_mutex);
1199 idr_remove(&dev->mode_config.tile_idr, tg->id);
1200 mutex_unlock(&dev->mode_config.idr_mutex);
1201 kfree(tg);
1202}
1203
1204/**
1205 * drm_mode_put_tile_group - drop a reference to a tile group.
1206 * @dev: DRM device
1207 * @tg: tile group to drop reference to.
1208 *
1209 * drop reference to tile group and free if 0.
1210 */
1211void drm_mode_put_tile_group(struct drm_device *dev,
1212 struct drm_tile_group *tg)
1213{
1214 kref_put(&tg->refcount, drm_tile_group_free);
1215}
1216EXPORT_SYMBOL(drm_mode_put_tile_group);
1217
1218/**
1219 * drm_mode_get_tile_group - get a reference to an existing tile group
1220 * @dev: DRM device
1221 * @topology: 8-bytes unique per monitor.
1222 *
1223 * Use the unique bytes to get a reference to an existing tile group.
1224 *
1225 * RETURNS:
1226 * tile group or NULL if not found.
1227 */
1228struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1229 char topology[8])
1230{
1231 struct drm_tile_group *tg;
1232 int id;
1233 mutex_lock(&dev->mode_config.idr_mutex);
1234 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1235 if (!memcmp(tg->group_data, topology, 8)) {
1236 if (!kref_get_unless_zero(&tg->refcount))
1237 tg = NULL;
1238 mutex_unlock(&dev->mode_config.idr_mutex);
1239 return tg;
1240 }
1241 }
1242 mutex_unlock(&dev->mode_config.idr_mutex);
1243 return NULL;
1244}
1245EXPORT_SYMBOL(drm_mode_get_tile_group);
1246
1247/**
1248 * drm_mode_create_tile_group - create a tile group from a displayid description
1249 * @dev: DRM device
1250 * @topology: 8-bytes unique per monitor.
1251 *
1252 * Create a tile group for the unique monitor, and get a unique
1253 * identifier for the tile group.
1254 *
1255 * RETURNS:
1256 * new tile group or error.
1257 */
1258struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1259 char topology[8])
1260{
1261 struct drm_tile_group *tg;
1262 int ret;
1263
1264 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1265 if (!tg)
1266 return ERR_PTR(-ENOMEM);
1267
1268 kref_init(&tg->refcount);
1269 memcpy(tg->group_data, topology, 8);
1270 tg->dev = dev;
1271
1272 mutex_lock(&dev->mode_config.idr_mutex);
1273 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1274 if (ret >= 0) {
1275 tg->id = ret;
1276 } else {
1277 kfree(tg);
1278 tg = ERR_PTR(ret);
1279 }
1280
1281 mutex_unlock(&dev->mode_config.idr_mutex);
1282 return tg;
1283}
1284EXPORT_SYMBOL(drm_mode_create_tile_group);