Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2016 Intel Corporation
0003  *
0004  * Permission to use, copy, modify, distribute, and sell this software and its
0005  * documentation for any purpose is hereby granted without fee, provided that
0006  * the above copyright notice appear in all copies and that both that copyright
0007  * notice and this permission notice appear in supporting documentation, and
0008  * that the name of the copyright holders not be used in advertising or
0009  * publicity pertaining to distribution of the software without specific,
0010  * written prior permission.  The copyright holders make no representations
0011  * about the suitability of this software for any purpose.  It is provided "as
0012  * is" without express or implied warranty.
0013  *
0014  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
0015  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
0016  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
0017  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
0018  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
0019  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
0020  * OF THIS SOFTWARE.
0021  */
0022 
0023 #include <drm/drm_auth.h>
0024 #include <drm/drm_connector.h>
0025 #include <drm/drm_edid.h>
0026 #include <drm/drm_encoder.h>
0027 #include <drm/drm_panel.h>
0028 #include <drm/drm_utils.h>
0029 #include <drm/drm_print.h>
0030 #include <drm/drm_drv.h>
0031 #include <drm/drm_file.h>
0032 #include <drm/drm_privacy_screen_consumer.h>
0033 #include <drm/drm_sysfs.h>
0034 
0035 #include <linux/fb.h>
0036 #include <linux/uaccess.h>
0037 
0038 #include "drm_crtc_internal.h"
0039 #include "drm_internal.h"
0040 
0041 /**
0042  * DOC: overview
0043  *
0044  * In DRM connectors are the general abstraction for display sinks, and include
0045  * also fixed panels or anything else that can display pixels in some form. As
0046  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
0047  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
0048  * Hence they are reference-counted using drm_connector_get() and
0049  * drm_connector_put().
0050  *
0051  * KMS driver must create, initialize, register and attach at a &struct
0052  * drm_connector for each such sink. The instance is created as other KMS
0053  * objects and initialized by setting the following fields. The connector is
0054  * initialized with a call to drm_connector_init() with a pointer to the
0055  * &struct drm_connector_funcs and a connector type, and then exposed to
0056  * userspace with a call to drm_connector_register().
0057  *
0058  * Connectors must be attached to an encoder to be used. For devices that map
0059  * connectors to encoders 1:1, the connector should be attached at
0060  * initialization time with a call to drm_connector_attach_encoder(). The
0061  * driver must also set the &drm_connector.encoder field to point to the
0062  * attached encoder.
0063  *
0064  * For connectors which are not fixed (like built-in panels) the driver needs to
0065  * support hotplug notifications. The simplest way to do that is by using the
0066  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
0067  * hardware support for hotplug interrupts. Connectors with hardware hotplug
0068  * support can instead use e.g. drm_helper_hpd_irq_event().
0069  */
0070 
0071 /*
0072  * Global connector list for drm_connector_find_by_fwnode().
0073  * Note drm_connector_[un]register() first take connector->lock and then
0074  * take the connector_list_lock.
0075  */
0076 static DEFINE_MUTEX(connector_list_lock);
0077 static LIST_HEAD(connector_list);
0078 
0079 struct drm_conn_prop_enum_list {
0080     int type;
0081     const char *name;
0082     struct ida ida;
0083 };
0084 
0085 /*
0086  * Connector and encoder types.
0087  */
0088 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
0089     { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
0090     { DRM_MODE_CONNECTOR_VGA, "VGA" },
0091     { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
0092     { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
0093     { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
0094     { DRM_MODE_CONNECTOR_Composite, "Composite" },
0095     { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
0096     { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
0097     { DRM_MODE_CONNECTOR_Component, "Component" },
0098     { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
0099     { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
0100     { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
0101     { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
0102     { DRM_MODE_CONNECTOR_TV, "TV" },
0103     { DRM_MODE_CONNECTOR_eDP, "eDP" },
0104     { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
0105     { DRM_MODE_CONNECTOR_DSI, "DSI" },
0106     { DRM_MODE_CONNECTOR_DPI, "DPI" },
0107     { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
0108     { DRM_MODE_CONNECTOR_SPI, "SPI" },
0109     { DRM_MODE_CONNECTOR_USB, "USB" },
0110 };
0111 
0112 void drm_connector_ida_init(void)
0113 {
0114     int i;
0115 
0116     for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
0117         ida_init(&drm_connector_enum_list[i].ida);
0118 }
0119 
0120 void drm_connector_ida_destroy(void)
0121 {
0122     int i;
0123 
0124     for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
0125         ida_destroy(&drm_connector_enum_list[i].ida);
0126 }
0127 
0128 /**
0129  * drm_get_connector_type_name - return a string for connector type
0130  * @type: The connector type (DRM_MODE_CONNECTOR_*)
0131  *
0132  * Returns: the name of the connector type, or NULL if the type is not valid.
0133  */
0134 const char *drm_get_connector_type_name(unsigned int type)
0135 {
0136     if (type < ARRAY_SIZE(drm_connector_enum_list))
0137         return drm_connector_enum_list[type].name;
0138 
0139     return NULL;
0140 }
0141 EXPORT_SYMBOL(drm_get_connector_type_name);
0142 
0143 /**
0144  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
0145  * @connector: connector to query
0146  *
0147  * The kernel supports per-connector configuration of its consoles through
0148  * use of the video= parameter. This function parses that option and
0149  * extracts the user's specified mode (or enable/disable status) for a
0150  * particular connector. This is typically only used during the early fbdev
0151  * setup.
0152  */
0153 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
0154 {
0155     struct drm_cmdline_mode *mode = &connector->cmdline_mode;
0156     char *option = NULL;
0157 
0158     if (fb_get_options(connector->name, &option))
0159         return;
0160 
0161     if (!drm_mode_parse_command_line_for_connector(option,
0162                                connector,
0163                                mode))
0164         return;
0165 
0166     if (mode->force) {
0167         DRM_INFO("forcing %s connector %s\n", connector->name,
0168              drm_get_connector_force_name(mode->force));
0169         connector->force = mode->force;
0170     }
0171 
0172     if (mode->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN) {
0173         DRM_INFO("cmdline forces connector %s panel_orientation to %d\n",
0174              connector->name, mode->panel_orientation);
0175         drm_connector_set_panel_orientation(connector,
0176                             mode->panel_orientation);
0177     }
0178 
0179     DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
0180               connector->name, mode->name,
0181               mode->xres, mode->yres,
0182               mode->refresh_specified ? mode->refresh : 60,
0183               mode->rb ? " reduced blanking" : "",
0184               mode->margins ? " with margins" : "",
0185               mode->interlace ?  " interlaced" : "");
0186 }
0187 
0188 static void drm_connector_free(struct kref *kref)
0189 {
0190     struct drm_connector *connector =
0191         container_of(kref, struct drm_connector, base.refcount);
0192     struct drm_device *dev = connector->dev;
0193 
0194     drm_mode_object_unregister(dev, &connector->base);
0195     connector->funcs->destroy(connector);
0196 }
0197 
0198 void drm_connector_free_work_fn(struct work_struct *work)
0199 {
0200     struct drm_connector *connector, *n;
0201     struct drm_device *dev =
0202         container_of(work, struct drm_device, mode_config.connector_free_work);
0203     struct drm_mode_config *config = &dev->mode_config;
0204     unsigned long flags;
0205     struct llist_node *freed;
0206 
0207     spin_lock_irqsave(&config->connector_list_lock, flags);
0208     freed = llist_del_all(&config->connector_free_list);
0209     spin_unlock_irqrestore(&config->connector_list_lock, flags);
0210 
0211     llist_for_each_entry_safe(connector, n, freed, free_node) {
0212         drm_mode_object_unregister(dev, &connector->base);
0213         connector->funcs->destroy(connector);
0214     }
0215 }
0216 
0217 /**
0218  * drm_connector_init - Init a preallocated connector
0219  * @dev: DRM device
0220  * @connector: the connector to init
0221  * @funcs: callbacks for this connector
0222  * @connector_type: user visible type of the connector
0223  *
0224  * Initialises a preallocated connector. Connectors should be
0225  * subclassed as part of driver connector objects.
0226  *
0227  * Returns:
0228  * Zero on success, error code on failure.
0229  */
0230 int drm_connector_init(struct drm_device *dev,
0231                struct drm_connector *connector,
0232                const struct drm_connector_funcs *funcs,
0233                int connector_type)
0234 {
0235     struct drm_mode_config *config = &dev->mode_config;
0236     int ret;
0237     struct ida *connector_ida =
0238         &drm_connector_enum_list[connector_type].ida;
0239 
0240     WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
0241         (!funcs->atomic_destroy_state ||
0242          !funcs->atomic_duplicate_state));
0243 
0244     ret = __drm_mode_object_add(dev, &connector->base,
0245                     DRM_MODE_OBJECT_CONNECTOR,
0246                     false, drm_connector_free);
0247     if (ret)
0248         return ret;
0249 
0250     connector->base.properties = &connector->properties;
0251     connector->dev = dev;
0252     connector->funcs = funcs;
0253 
0254     /* connector index is used with 32bit bitmasks */
0255     ret = ida_alloc_max(&config->connector_ida, 31, GFP_KERNEL);
0256     if (ret < 0) {
0257         DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
0258                   drm_connector_enum_list[connector_type].name,
0259                   ret);
0260         goto out_put;
0261     }
0262     connector->index = ret;
0263     ret = 0;
0264 
0265     connector->connector_type = connector_type;
0266     connector->connector_type_id =
0267         ida_alloc_min(connector_ida, 1, GFP_KERNEL);
0268     if (connector->connector_type_id < 0) {
0269         ret = connector->connector_type_id;
0270         goto out_put_id;
0271     }
0272     connector->name =
0273         kasprintf(GFP_KERNEL, "%s-%d",
0274               drm_connector_enum_list[connector_type].name,
0275               connector->connector_type_id);
0276     if (!connector->name) {
0277         ret = -ENOMEM;
0278         goto out_put_type_id;
0279     }
0280 
0281     INIT_LIST_HEAD(&connector->global_connector_list_entry);
0282     INIT_LIST_HEAD(&connector->probed_modes);
0283     INIT_LIST_HEAD(&connector->modes);
0284     mutex_init(&connector->mutex);
0285     connector->edid_blob_ptr = NULL;
0286     connector->epoch_counter = 0;
0287     connector->tile_blob_ptr = NULL;
0288     connector->status = connector_status_unknown;
0289     connector->display_info.panel_orientation =
0290         DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
0291 
0292     drm_connector_get_cmdline_mode(connector);
0293 
0294     /* We should add connectors at the end to avoid upsetting the connector
0295      * index too much.
0296      */
0297     spin_lock_irq(&config->connector_list_lock);
0298     list_add_tail(&connector->head, &config->connector_list);
0299     config->num_connector++;
0300     spin_unlock_irq(&config->connector_list_lock);
0301 
0302     if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
0303         connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
0304         drm_connector_attach_edid_property(connector);
0305 
0306     drm_object_attach_property(&connector->base,
0307                       config->dpms_property, 0);
0308 
0309     drm_object_attach_property(&connector->base,
0310                    config->link_status_property,
0311                    0);
0312 
0313     drm_object_attach_property(&connector->base,
0314                    config->non_desktop_property,
0315                    0);
0316     drm_object_attach_property(&connector->base,
0317                    config->tile_property,
0318                    0);
0319 
0320     if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
0321         drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
0322     }
0323 
0324     connector->debugfs_entry = NULL;
0325 out_put_type_id:
0326     if (ret)
0327         ida_free(connector_ida, connector->connector_type_id);
0328 out_put_id:
0329     if (ret)
0330         ida_free(&config->connector_ida, connector->index);
0331 out_put:
0332     if (ret)
0333         drm_mode_object_unregister(dev, &connector->base);
0334 
0335     return ret;
0336 }
0337 EXPORT_SYMBOL(drm_connector_init);
0338 
0339 /**
0340  * drm_connector_init_with_ddc - Init a preallocated connector
0341  * @dev: DRM device
0342  * @connector: the connector to init
0343  * @funcs: callbacks for this connector
0344  * @connector_type: user visible type of the connector
0345  * @ddc: pointer to the associated ddc adapter
0346  *
0347  * Initialises a preallocated connector. Connectors should be
0348  * subclassed as part of driver connector objects.
0349  *
0350  * Ensures that the ddc field of the connector is correctly set.
0351  *
0352  * Returns:
0353  * Zero on success, error code on failure.
0354  */
0355 int drm_connector_init_with_ddc(struct drm_device *dev,
0356                 struct drm_connector *connector,
0357                 const struct drm_connector_funcs *funcs,
0358                 int connector_type,
0359                 struct i2c_adapter *ddc)
0360 {
0361     int ret;
0362 
0363     ret = drm_connector_init(dev, connector, funcs, connector_type);
0364     if (ret)
0365         return ret;
0366 
0367     /* provide ddc symlink in sysfs */
0368     connector->ddc = ddc;
0369 
0370     return ret;
0371 }
0372 EXPORT_SYMBOL(drm_connector_init_with_ddc);
0373 
0374 /**
0375  * drm_connector_attach_edid_property - attach edid property.
0376  * @connector: the connector
0377  *
0378  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
0379  * edid property attached by default.  This function can be used to
0380  * explicitly enable the edid property in these cases.
0381  */
0382 void drm_connector_attach_edid_property(struct drm_connector *connector)
0383 {
0384     struct drm_mode_config *config = &connector->dev->mode_config;
0385 
0386     drm_object_attach_property(&connector->base,
0387                    config->edid_property,
0388                    0);
0389 }
0390 EXPORT_SYMBOL(drm_connector_attach_edid_property);
0391 
0392 /**
0393  * drm_connector_attach_encoder - attach a connector to an encoder
0394  * @connector: connector to attach
0395  * @encoder: encoder to attach @connector to
0396  *
0397  * This function links up a connector to an encoder. Note that the routing
0398  * restrictions between encoders and crtcs are exposed to userspace through the
0399  * possible_clones and possible_crtcs bitmasks.
0400  *
0401  * Returns:
0402  * Zero on success, negative errno on failure.
0403  */
0404 int drm_connector_attach_encoder(struct drm_connector *connector,
0405                  struct drm_encoder *encoder)
0406 {
0407     /*
0408      * In the past, drivers have attempted to model the static association
0409      * of connector to encoder in simple connector/encoder devices using a
0410      * direct assignment of connector->encoder = encoder. This connection
0411      * is a logical one and the responsibility of the core, so drivers are
0412      * expected not to mess with this.
0413      *
0414      * Note that the error return should've been enough here, but a large
0415      * majority of drivers ignores the return value, so add in a big WARN
0416      * to get people's attention.
0417      */
0418     if (WARN_ON(connector->encoder))
0419         return -EINVAL;
0420 
0421     connector->possible_encoders |= drm_encoder_mask(encoder);
0422 
0423     return 0;
0424 }
0425 EXPORT_SYMBOL(drm_connector_attach_encoder);
0426 
0427 /**
0428  * drm_connector_has_possible_encoder - check if the connector and encoder are
0429  * associated with each other
0430  * @connector: the connector
0431  * @encoder: the encoder
0432  *
0433  * Returns:
0434  * True if @encoder is one of the possible encoders for @connector.
0435  */
0436 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
0437                     struct drm_encoder *encoder)
0438 {
0439     return connector->possible_encoders & drm_encoder_mask(encoder);
0440 }
0441 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
0442 
0443 static void drm_mode_remove(struct drm_connector *connector,
0444                 struct drm_display_mode *mode)
0445 {
0446     list_del(&mode->head);
0447     drm_mode_destroy(connector->dev, mode);
0448 }
0449 
0450 /**
0451  * drm_connector_cleanup - cleans up an initialised connector
0452  * @connector: connector to cleanup
0453  *
0454  * Cleans up the connector but doesn't free the object.
0455  */
0456 void drm_connector_cleanup(struct drm_connector *connector)
0457 {
0458     struct drm_device *dev = connector->dev;
0459     struct drm_display_mode *mode, *t;
0460 
0461     /* The connector should have been removed from userspace long before
0462      * it is finally destroyed.
0463      */
0464     if (WARN_ON(connector->registration_state ==
0465             DRM_CONNECTOR_REGISTERED))
0466         drm_connector_unregister(connector);
0467 
0468     if (connector->privacy_screen) {
0469         drm_privacy_screen_put(connector->privacy_screen);
0470         connector->privacy_screen = NULL;
0471     }
0472 
0473     if (connector->tile_group) {
0474         drm_mode_put_tile_group(dev, connector->tile_group);
0475         connector->tile_group = NULL;
0476     }
0477 
0478     list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
0479         drm_mode_remove(connector, mode);
0480 
0481     list_for_each_entry_safe(mode, t, &connector->modes, head)
0482         drm_mode_remove(connector, mode);
0483 
0484     ida_free(&drm_connector_enum_list[connector->connector_type].ida,
0485               connector->connector_type_id);
0486 
0487     ida_free(&dev->mode_config.connector_ida, connector->index);
0488 
0489     kfree(connector->display_info.bus_formats);
0490     drm_mode_object_unregister(dev, &connector->base);
0491     kfree(connector->name);
0492     connector->name = NULL;
0493     fwnode_handle_put(connector->fwnode);
0494     connector->fwnode = NULL;
0495     spin_lock_irq(&dev->mode_config.connector_list_lock);
0496     list_del(&connector->head);
0497     dev->mode_config.num_connector--;
0498     spin_unlock_irq(&dev->mode_config.connector_list_lock);
0499 
0500     WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
0501     if (connector->state && connector->funcs->atomic_destroy_state)
0502         connector->funcs->atomic_destroy_state(connector,
0503                                connector->state);
0504 
0505     mutex_destroy(&connector->mutex);
0506 
0507     memset(connector, 0, sizeof(*connector));
0508 }
0509 EXPORT_SYMBOL(drm_connector_cleanup);
0510 
0511 /**
0512  * drm_connector_register - register a connector
0513  * @connector: the connector to register
0514  *
0515  * Register userspace interfaces for a connector. Only call this for connectors
0516  * which can be hotplugged after drm_dev_register() has been called already,
0517  * e.g. DP MST connectors. All other connectors will be registered automatically
0518  * when calling drm_dev_register().
0519  *
0520  * Returns:
0521  * Zero on success, error code on failure.
0522  */
0523 int drm_connector_register(struct drm_connector *connector)
0524 {
0525     int ret = 0;
0526 
0527     if (!connector->dev->registered)
0528         return 0;
0529 
0530     mutex_lock(&connector->mutex);
0531     if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
0532         goto unlock;
0533 
0534     ret = drm_sysfs_connector_add(connector);
0535     if (ret)
0536         goto unlock;
0537 
0538     drm_debugfs_connector_add(connector);
0539 
0540     if (connector->funcs->late_register) {
0541         ret = connector->funcs->late_register(connector);
0542         if (ret)
0543             goto err_debugfs;
0544     }
0545 
0546     drm_mode_object_register(connector->dev, &connector->base);
0547 
0548     connector->registration_state = DRM_CONNECTOR_REGISTERED;
0549 
0550     /* Let userspace know we have a new connector */
0551     drm_sysfs_connector_hotplug_event(connector);
0552 
0553     if (connector->privacy_screen)
0554         drm_privacy_screen_register_notifier(connector->privacy_screen,
0555                        &connector->privacy_screen_notifier);
0556 
0557     mutex_lock(&connector_list_lock);
0558     list_add_tail(&connector->global_connector_list_entry, &connector_list);
0559     mutex_unlock(&connector_list_lock);
0560     goto unlock;
0561 
0562 err_debugfs:
0563     drm_debugfs_connector_remove(connector);
0564     drm_sysfs_connector_remove(connector);
0565 unlock:
0566     mutex_unlock(&connector->mutex);
0567     return ret;
0568 }
0569 EXPORT_SYMBOL(drm_connector_register);
0570 
0571 /**
0572  * drm_connector_unregister - unregister a connector
0573  * @connector: the connector to unregister
0574  *
0575  * Unregister userspace interfaces for a connector. Only call this for
0576  * connectors which have registered explicitly by calling drm_dev_register(),
0577  * since connectors are unregistered automatically when drm_dev_unregister() is
0578  * called.
0579  */
0580 void drm_connector_unregister(struct drm_connector *connector)
0581 {
0582     mutex_lock(&connector->mutex);
0583     if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
0584         mutex_unlock(&connector->mutex);
0585         return;
0586     }
0587 
0588     mutex_lock(&connector_list_lock);
0589     list_del_init(&connector->global_connector_list_entry);
0590     mutex_unlock(&connector_list_lock);
0591 
0592     if (connector->privacy_screen)
0593         drm_privacy_screen_unregister_notifier(
0594                     connector->privacy_screen,
0595                     &connector->privacy_screen_notifier);
0596 
0597     if (connector->funcs->early_unregister)
0598         connector->funcs->early_unregister(connector);
0599 
0600     drm_sysfs_connector_remove(connector);
0601     drm_debugfs_connector_remove(connector);
0602 
0603     connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
0604     mutex_unlock(&connector->mutex);
0605 }
0606 EXPORT_SYMBOL(drm_connector_unregister);
0607 
0608 void drm_connector_unregister_all(struct drm_device *dev)
0609 {
0610     struct drm_connector *connector;
0611     struct drm_connector_list_iter conn_iter;
0612 
0613     drm_connector_list_iter_begin(dev, &conn_iter);
0614     drm_for_each_connector_iter(connector, &conn_iter)
0615         drm_connector_unregister(connector);
0616     drm_connector_list_iter_end(&conn_iter);
0617 }
0618 
0619 int drm_connector_register_all(struct drm_device *dev)
0620 {
0621     struct drm_connector *connector;
0622     struct drm_connector_list_iter conn_iter;
0623     int ret = 0;
0624 
0625     drm_connector_list_iter_begin(dev, &conn_iter);
0626     drm_for_each_connector_iter(connector, &conn_iter) {
0627         ret = drm_connector_register(connector);
0628         if (ret)
0629             break;
0630     }
0631     drm_connector_list_iter_end(&conn_iter);
0632 
0633     if (ret)
0634         drm_connector_unregister_all(dev);
0635     return ret;
0636 }
0637 
0638 /**
0639  * drm_get_connector_status_name - return a string for connector status
0640  * @status: connector status to compute name of
0641  *
0642  * In contrast to the other drm_get_*_name functions this one here returns a
0643  * const pointer and hence is threadsafe.
0644  *
0645  * Returns: connector status string
0646  */
0647 const char *drm_get_connector_status_name(enum drm_connector_status status)
0648 {
0649     if (status == connector_status_connected)
0650         return "connected";
0651     else if (status == connector_status_disconnected)
0652         return "disconnected";
0653     else
0654         return "unknown";
0655 }
0656 EXPORT_SYMBOL(drm_get_connector_status_name);
0657 
0658 /**
0659  * drm_get_connector_force_name - return a string for connector force
0660  * @force: connector force to get name of
0661  *
0662  * Returns: const pointer to name.
0663  */
0664 const char *drm_get_connector_force_name(enum drm_connector_force force)
0665 {
0666     switch (force) {
0667     case DRM_FORCE_UNSPECIFIED:
0668         return "unspecified";
0669     case DRM_FORCE_OFF:
0670         return "off";
0671     case DRM_FORCE_ON:
0672         return "on";
0673     case DRM_FORCE_ON_DIGITAL:
0674         return "digital";
0675     default:
0676         return "unknown";
0677     }
0678 }
0679 
0680 #ifdef CONFIG_LOCKDEP
0681 static struct lockdep_map connector_list_iter_dep_map = {
0682     .name = "drm_connector_list_iter"
0683 };
0684 #endif
0685 
0686 /**
0687  * drm_connector_list_iter_begin - initialize a connector_list iterator
0688  * @dev: DRM device
0689  * @iter: connector_list iterator
0690  *
0691  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
0692  * must always be cleaned up again by calling drm_connector_list_iter_end().
0693  * Iteration itself happens using drm_connector_list_iter_next() or
0694  * drm_for_each_connector_iter().
0695  */
0696 void drm_connector_list_iter_begin(struct drm_device *dev,
0697                    struct drm_connector_list_iter *iter)
0698 {
0699     iter->dev = dev;
0700     iter->conn = NULL;
0701     lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
0702 }
0703 EXPORT_SYMBOL(drm_connector_list_iter_begin);
0704 
0705 /*
0706  * Extra-safe connector put function that works in any context. Should only be
0707  * used from the connector_iter functions, where we never really expect to
0708  * actually release the connector when dropping our final reference.
0709  */
0710 static void
0711 __drm_connector_put_safe(struct drm_connector *conn)
0712 {
0713     struct drm_mode_config *config = &conn->dev->mode_config;
0714 
0715     lockdep_assert_held(&config->connector_list_lock);
0716 
0717     if (!refcount_dec_and_test(&conn->base.refcount.refcount))
0718         return;
0719 
0720     llist_add(&conn->free_node, &config->connector_free_list);
0721     schedule_work(&config->connector_free_work);
0722 }
0723 
0724 /**
0725  * drm_connector_list_iter_next - return next connector
0726  * @iter: connector_list iterator
0727  *
0728  * Returns: the next connector for @iter, or NULL when the list walk has
0729  * completed.
0730  */
0731 struct drm_connector *
0732 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
0733 {
0734     struct drm_connector *old_conn = iter->conn;
0735     struct drm_mode_config *config = &iter->dev->mode_config;
0736     struct list_head *lhead;
0737     unsigned long flags;
0738 
0739     spin_lock_irqsave(&config->connector_list_lock, flags);
0740     lhead = old_conn ? &old_conn->head : &config->connector_list;
0741 
0742     do {
0743         if (lhead->next == &config->connector_list) {
0744             iter->conn = NULL;
0745             break;
0746         }
0747 
0748         lhead = lhead->next;
0749         iter->conn = list_entry(lhead, struct drm_connector, head);
0750 
0751         /* loop until it's not a zombie connector */
0752     } while (!kref_get_unless_zero(&iter->conn->base.refcount));
0753 
0754     if (old_conn)
0755         __drm_connector_put_safe(old_conn);
0756     spin_unlock_irqrestore(&config->connector_list_lock, flags);
0757 
0758     return iter->conn;
0759 }
0760 EXPORT_SYMBOL(drm_connector_list_iter_next);
0761 
0762 /**
0763  * drm_connector_list_iter_end - tear down a connector_list iterator
0764  * @iter: connector_list iterator
0765  *
0766  * Tears down @iter and releases any resources (like &drm_connector references)
0767  * acquired while walking the list. This must always be called, both when the
0768  * iteration completes fully or when it was aborted without walking the entire
0769  * list.
0770  */
0771 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
0772 {
0773     struct drm_mode_config *config = &iter->dev->mode_config;
0774     unsigned long flags;
0775 
0776     iter->dev = NULL;
0777     if (iter->conn) {
0778         spin_lock_irqsave(&config->connector_list_lock, flags);
0779         __drm_connector_put_safe(iter->conn);
0780         spin_unlock_irqrestore(&config->connector_list_lock, flags);
0781     }
0782     lock_release(&connector_list_iter_dep_map, _RET_IP_);
0783 }
0784 EXPORT_SYMBOL(drm_connector_list_iter_end);
0785 
0786 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
0787     { SubPixelUnknown, "Unknown" },
0788     { SubPixelHorizontalRGB, "Horizontal RGB" },
0789     { SubPixelHorizontalBGR, "Horizontal BGR" },
0790     { SubPixelVerticalRGB, "Vertical RGB" },
0791     { SubPixelVerticalBGR, "Vertical BGR" },
0792     { SubPixelNone, "None" },
0793 };
0794 
0795 /**
0796  * drm_get_subpixel_order_name - return a string for a given subpixel enum
0797  * @order: enum of subpixel_order
0798  *
0799  * Note you could abuse this and return something out of bounds, but that
0800  * would be a caller error.  No unscrubbed user data should make it here.
0801  *
0802  * Returns: string describing an enumerated subpixel property
0803  */
0804 const char *drm_get_subpixel_order_name(enum subpixel_order order)
0805 {
0806     return drm_subpixel_enum_list[order].name;
0807 }
0808 EXPORT_SYMBOL(drm_get_subpixel_order_name);
0809 
0810 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
0811     { DRM_MODE_DPMS_ON, "On" },
0812     { DRM_MODE_DPMS_STANDBY, "Standby" },
0813     { DRM_MODE_DPMS_SUSPEND, "Suspend" },
0814     { DRM_MODE_DPMS_OFF, "Off" }
0815 };
0816 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
0817 
0818 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
0819     { DRM_MODE_LINK_STATUS_GOOD, "Good" },
0820     { DRM_MODE_LINK_STATUS_BAD, "Bad" },
0821 };
0822 
0823 /**
0824  * drm_display_info_set_bus_formats - set the supported bus formats
0825  * @info: display info to store bus formats in
0826  * @formats: array containing the supported bus formats
0827  * @num_formats: the number of entries in the fmts array
0828  *
0829  * Store the supported bus formats in display info structure.
0830  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
0831  * a full list of available formats.
0832  *
0833  * Returns:
0834  * 0 on success or a negative error code on failure.
0835  */
0836 int drm_display_info_set_bus_formats(struct drm_display_info *info,
0837                      const u32 *formats,
0838                      unsigned int num_formats)
0839 {
0840     u32 *fmts = NULL;
0841 
0842     if (!formats && num_formats)
0843         return -EINVAL;
0844 
0845     if (formats && num_formats) {
0846         fmts = kmemdup(formats, sizeof(*formats) * num_formats,
0847                    GFP_KERNEL);
0848         if (!fmts)
0849             return -ENOMEM;
0850     }
0851 
0852     kfree(info->bus_formats);
0853     info->bus_formats = fmts;
0854     info->num_bus_formats = num_formats;
0855 
0856     return 0;
0857 }
0858 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
0859 
0860 /* Optional connector properties. */
0861 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
0862     { DRM_MODE_SCALE_NONE, "None" },
0863     { DRM_MODE_SCALE_FULLSCREEN, "Full" },
0864     { DRM_MODE_SCALE_CENTER, "Center" },
0865     { DRM_MODE_SCALE_ASPECT, "Full aspect" },
0866 };
0867 
0868 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
0869     { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
0870     { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
0871     { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
0872 };
0873 
0874 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
0875     { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
0876     { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
0877     { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
0878     { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
0879     { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
0880 };
0881 
0882 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
0883     { DRM_MODE_PANEL_ORIENTATION_NORMAL,    "Normal"    },
0884     { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down"   },
0885     { DRM_MODE_PANEL_ORIENTATION_LEFT_UP,   "Left Side Up"  },
0886     { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,  "Right Side Up" },
0887 };
0888 
0889 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
0890     { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
0891     { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
0892     { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
0893 };
0894 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
0895 
0896 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
0897     { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
0898     { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
0899     { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
0900 };
0901 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
0902          drm_dvi_i_subconnector_enum_list)
0903 
0904 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
0905     { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
0906     { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
0907     { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
0908     { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
0909     { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
0910 };
0911 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
0912 
0913 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
0914     { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
0915     { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
0916     { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
0917     { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
0918     { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
0919 };
0920 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
0921          drm_tv_subconnector_enum_list)
0922 
0923 static const struct drm_prop_enum_list drm_dp_subconnector_enum_list[] = {
0924     { DRM_MODE_SUBCONNECTOR_Unknown,     "Unknown"   }, /* DVI-I, TV-out and DP */
0925     { DRM_MODE_SUBCONNECTOR_VGA,         "VGA"       }, /* DP */
0926     { DRM_MODE_SUBCONNECTOR_DVID,        "DVI-D"     }, /* DP */
0927     { DRM_MODE_SUBCONNECTOR_HDMIA,       "HDMI"      }, /* DP */
0928     { DRM_MODE_SUBCONNECTOR_DisplayPort, "DP"        }, /* DP */
0929     { DRM_MODE_SUBCONNECTOR_Wireless,    "Wireless"  }, /* DP */
0930     { DRM_MODE_SUBCONNECTOR_Native,      "Native"    }, /* DP */
0931 };
0932 
0933 DRM_ENUM_NAME_FN(drm_get_dp_subconnector_name,
0934          drm_dp_subconnector_enum_list)
0935 
0936 static const struct drm_prop_enum_list hdmi_colorspaces[] = {
0937     /* For Default case, driver will set the colorspace */
0938     { DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
0939     /* Standard Definition Colorimetry based on CEA 861 */
0940     { DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
0941     { DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
0942     /* Standard Definition Colorimetry based on IEC 61966-2-4 */
0943     { DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
0944     /* High Definition Colorimetry based on IEC 61966-2-4 */
0945     { DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
0946     /* Colorimetry based on IEC 61966-2-1/Amendment 1 */
0947     { DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
0948     /* Colorimetry based on IEC 61966-2-5 [33] */
0949     { DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
0950     /* Colorimetry based on IEC 61966-2-5 */
0951     { DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
0952     /* Colorimetry based on ITU-R BT.2020 */
0953     { DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
0954     /* Colorimetry based on ITU-R BT.2020 */
0955     { DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
0956     /* Colorimetry based on ITU-R BT.2020 */
0957     { DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
0958     /* Added as part of Additional Colorimetry Extension in 861.G */
0959     { DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
0960     { DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
0961 };
0962 
0963 /*
0964  * As per DP 1.4a spec, 2.2.5.7.5 VSC SDP Payload for Pixel Encoding/Colorimetry
0965  * Format Table 2-120
0966  */
0967 static const struct drm_prop_enum_list dp_colorspaces[] = {
0968     /* For Default case, driver will set the colorspace */
0969     { DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
0970     { DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED, "RGB_Wide_Gamut_Fixed_Point" },
0971     /* Colorimetry based on scRGB (IEC 61966-2-2) */
0972     { DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT, "RGB_Wide_Gamut_Floating_Point" },
0973     /* Colorimetry based on IEC 61966-2-5 */
0974     { DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
0975     /* Colorimetry based on SMPTE RP 431-2 */
0976     { DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
0977     /* Colorimetry based on ITU-R BT.2020 */
0978     { DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
0979     { DRM_MODE_COLORIMETRY_BT601_YCC, "BT601_YCC" },
0980     { DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
0981     /* Standard Definition Colorimetry based on IEC 61966-2-4 */
0982     { DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
0983     /* High Definition Colorimetry based on IEC 61966-2-4 */
0984     { DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
0985     /* Colorimetry based on IEC 61966-2-1/Amendment 1 */
0986     { DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
0987     /* Colorimetry based on IEC 61966-2-5 [33] */
0988     { DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
0989     /* Colorimetry based on ITU-R BT.2020 */
0990     { DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
0991     /* Colorimetry based on ITU-R BT.2020 */
0992     { DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
0993 };
0994 
0995 /**
0996  * DOC: standard connector properties
0997  *
0998  * DRM connectors have a few standardized properties:
0999  *
1000  * EDID:
1001  *  Blob property which contains the current EDID read from the sink. This
1002  *  is useful to parse sink identification information like vendor, model
1003  *  and serial. Drivers should update this property by calling
1004  *  drm_connector_update_edid_property(), usually after having parsed
1005  *  the EDID using drm_add_edid_modes(). Userspace cannot change this
1006  *  property.
1007  *
1008  *  User-space should not parse the EDID to obtain information exposed via
1009  *  other KMS properties (because the kernel might apply limits, quirks or
1010  *  fixups to the EDID). For instance, user-space should not try to parse
1011  *  mode lists from the EDID.
1012  * DPMS:
1013  *  Legacy property for setting the power state of the connector. For atomic
1014  *  drivers this is only provided for backwards compatibility with existing
1015  *  drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
1016  *  connector is linked to. Drivers should never set this property directly,
1017  *  it is handled by the DRM core by calling the &drm_connector_funcs.dpms
1018  *  callback. For atomic drivers the remapping to the "ACTIVE" property is
1019  *  implemented in the DRM core.
1020  *
1021  *  Note that this property cannot be set through the MODE_ATOMIC ioctl,
1022  *  userspace must use "ACTIVE" on the CRTC instead.
1023  *
1024  *  WARNING:
1025  *
1026  *  For userspace also running on legacy drivers the "DPMS" semantics are a
1027  *  lot more complicated. First, userspace cannot rely on the "DPMS" value
1028  *  returned by the GETCONNECTOR actually reflecting reality, because many
1029  *  drivers fail to update it. For atomic drivers this is taken care of in
1030  *  drm_atomic_helper_update_legacy_modeset_state().
1031  *
1032  *  The second issue is that the DPMS state is only well-defined when the
1033  *  connector is connected to a CRTC. In atomic the DRM core enforces that
1034  *  "ACTIVE" is off in such a case, no such checks exists for "DPMS".
1035  *
1036  *  Finally, when enabling an output using the legacy SETCONFIG ioctl then
1037  *  "DPMS" is forced to ON. But see above, that might not be reflected in
1038  *  the software value on legacy drivers.
1039  *
1040  *  Summarizing: Only set "DPMS" when the connector is known to be enabled,
1041  *  assume that a successful SETCONFIG call also sets "DPMS" to on, and
1042  *  never read back the value of "DPMS" because it can be incorrect.
1043  * PATH:
1044  *  Connector path property to identify how this sink is physically
1045  *  connected. Used by DP MST. This should be set by calling
1046  *  drm_connector_set_path_property(), in the case of DP MST with the
1047  *  path property the MST manager created. Userspace cannot change this
1048  *  property.
1049  * TILE:
1050  *  Connector tile group property to indicate how a set of DRM connector
1051  *  compose together into one logical screen. This is used by both high-res
1052  *  external screens (often only using a single cable, but exposing multiple
1053  *  DP MST sinks), or high-res integrated panels (like dual-link DSI) which
1054  *  are not gen-locked. Note that for tiled panels which are genlocked, like
1055  *  dual-link LVDS or dual-link DSI, the driver should try to not expose the
1056  *  tiling and virtualise both &drm_crtc and &drm_plane if needed. Drivers
1057  *  should update this value using drm_connector_set_tile_property().
1058  *  Userspace cannot change this property.
1059  * link-status:
1060  *      Connector link-status property to indicate the status of link. The
1061  *      default value of link-status is "GOOD". If something fails during or
1062  *      after modeset, the kernel driver may set this to "BAD" and issue a
1063  *      hotplug uevent. Drivers should update this value using
1064  *      drm_connector_set_link_status_property().
1065  *
1066  *      When user-space receives the hotplug uevent and detects a "BAD"
1067  *      link-status, the sink doesn't receive pixels anymore (e.g. the screen
1068  *      becomes completely black). The list of available modes may have
1069  *      changed. User-space is expected to pick a new mode if the current one
1070  *      has disappeared and perform a new modeset with link-status set to
1071  *      "GOOD" to re-enable the connector.
1072  *
1073  *      If multiple connectors share the same CRTC and one of them gets a "BAD"
1074  *      link-status, the other are unaffected (ie. the sinks still continue to
1075  *      receive pixels).
1076  *
1077  *      When user-space performs an atomic commit on a connector with a "BAD"
1078  *      link-status without resetting the property to "GOOD", the sink may
1079  *      still not receive pixels. When user-space performs an atomic commit
1080  *      which resets the link-status property to "GOOD" without the
1081  *      ALLOW_MODESET flag set, it might fail because a modeset is required.
1082  *
1083  *      User-space can only change link-status to "GOOD", changing it to "BAD"
1084  *      is a no-op.
1085  *
1086  *      For backwards compatibility with non-atomic userspace the kernel
1087  *      tries to automatically set the link-status back to "GOOD" in the
1088  *      SETCRTC IOCTL. This might fail if the mode is no longer valid, similar
1089  *      to how it might fail if a different screen has been connected in the
1090  *      interim.
1091  * non_desktop:
1092  *  Indicates the output should be ignored for purposes of displaying a
1093  *  standard desktop environment or console. This is most likely because
1094  *  the output device is not rectilinear.
1095  * Content Protection:
1096  *  This property is used by userspace to request the kernel protect future
1097  *  content communicated over the link. When requested, kernel will apply
1098  *  the appropriate means of protection (most often HDCP), and use the
1099  *  property to tell userspace the protection is active.
1100  *
1101  *  Drivers can set this up by calling
1102  *  drm_connector_attach_content_protection_property() on initialization.
1103  *
1104  *  The value of this property can be one of the following:
1105  *
1106  *  DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
1107  *      The link is not protected, content is transmitted in the clear.
1108  *  DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
1109  *      Userspace has requested content protection, but the link is not
1110  *      currently protected. When in this state, kernel should enable
1111  *      Content Protection as soon as possible.
1112  *  DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
1113  *      Userspace has requested content protection, and the link is
1114  *      protected. Only the driver can set the property to this value.
1115  *      If userspace attempts to set to ENABLED, kernel will return
1116  *      -EINVAL.
1117  *
1118  *  A few guidelines:
1119  *
1120  *  - DESIRED state should be preserved until userspace de-asserts it by
1121  *    setting the property to UNDESIRED. This means ENABLED should only
1122  *    transition to UNDESIRED when the user explicitly requests it.
1123  *  - If the state is DESIRED, kernel should attempt to re-authenticate the
1124  *    link whenever possible. This includes across disable/enable, dpms,
1125  *    hotplug, downstream device changes, link status failures, etc..
1126  *  - Kernel sends uevent with the connector id and property id through
1127  *    @drm_hdcp_update_content_protection, upon below kernel triggered
1128  *    scenarios:
1129  *
1130  *      - DESIRED -> ENABLED (authentication success)
1131  *      - ENABLED -> DESIRED (termination of authentication)
1132  *  - Please note no uevents for userspace triggered property state changes,
1133  *    which can't fail such as
1134  *
1135  *      - DESIRED/ENABLED -> UNDESIRED
1136  *      - UNDESIRED -> DESIRED
1137  *  - Userspace is responsible for polling the property or listen to uevents
1138  *    to determine when the value transitions from ENABLED to DESIRED.
1139  *    This signifies the link is no longer protected and userspace should
1140  *    take appropriate action (whatever that might be).
1141  *
1142  * HDCP Content Type:
1143  *  This Enum property is used by the userspace to declare the content type
1144  *  of the display stream, to kernel. Here display stream stands for any
1145  *  display content that userspace intended to display through HDCP
1146  *  encryption.
1147  *
1148  *  Content Type of a stream is decided by the owner of the stream, as
1149  *  "HDCP Type0" or "HDCP Type1".
1150  *
1151  *  The value of the property can be one of the below:
1152  *    - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
1153  *    - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
1154  *
1155  *  When kernel starts the HDCP authentication (see "Content Protection"
1156  *  for details), it uses the content type in "HDCP Content Type"
1157  *  for performing the HDCP authentication with the display sink.
1158  *
1159  *  Please note in HDCP spec versions, a link can be authenticated with
1160  *  HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
1161  *  authenticated with HDCP1.4 only for Content Type 0(though it is implicit
1162  *  in nature. As there is no reference for Content Type in HDCP1.4).
1163  *
1164  *  HDCP2.2 authentication protocol itself takes the "Content Type" as a
1165  *  parameter, which is a input for the DP HDCP2.2 encryption algo.
1166  *
1167  *  In case of Type 0 content protection request, kernel driver can choose
1168  *  either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
1169  *  "HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
1170  *  that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
1171  *  But if the content is classified as "HDCP Type 1", above mentioned
1172  *  HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
1173  *  authenticate the HDCP1.4 capable sink for "HDCP Type 1".
1174  *
1175  *  Please note userspace can be ignorant of the HDCP versions used by the
1176  *  kernel driver to achieve the "HDCP Content Type".
1177  *
1178  *  At current scenario, classifying a content as Type 1 ensures that the
1179  *  content will be displayed only through the HDCP2.2 encrypted link.
1180  *
1181  *  Note that the HDCP Content Type property is introduced at HDCP 2.2, and
1182  *  defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
1183  *  (hence supporting Type 0 and Type 1). Based on how next versions of
1184  *  HDCP specs are defined content Type could be used for higher versions
1185  *  too.
1186  *
1187  *  If content type is changed when "Content Protection" is not UNDESIRED,
1188  *  then kernel will disable the HDCP and re-enable with new type in the
1189  *  same atomic commit. And when "Content Protection" is ENABLED, it means
1190  *  that link is HDCP authenticated and encrypted, for the transmission of
1191  *  the Type of stream mentioned at "HDCP Content Type".
1192  *
1193  * HDR_OUTPUT_METADATA:
1194  *  Connector property to enable userspace to send HDR Metadata to
1195  *  driver. This metadata is based on the composition and blending
1196  *  policies decided by user, taking into account the hardware and
1197  *  sink capabilities. The driver gets this metadata and creates a
1198  *  Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1199  *  SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1200  *  sent to sink. This notifies the sink of the upcoming frame's Color
1201  *  Encoding and Luminance parameters.
1202  *
1203  *  Userspace first need to detect the HDR capabilities of sink by
1204  *  reading and parsing the EDID. Details of HDR metadata for HDMI
1205  *  are added in CTA 861.G spec. For DP , its defined in VESA DP
1206  *  Standard v1.4. It needs to then get the metadata information
1207  *  of the video/game/app content which are encoded in HDR (basically
1208  *  using HDR transfer functions). With this information it needs to
1209  *  decide on a blending policy and compose the relevant
1210  *  layers/overlays into a common format. Once this blending is done,
1211  *  userspace will be aware of the metadata of the composed frame to
1212  *  be send to sink. It then uses this property to communicate this
1213  *  metadata to driver which then make a Infoframe packet and sends
1214  *  to sink based on the type of encoder connected.
1215  *
1216  *  Userspace will be responsible to do Tone mapping operation in case:
1217  *      - Some layers are HDR and others are SDR
1218  *      - HDR layers luminance is not same as sink
1219  *
1220  *  It will even need to do colorspace conversion and get all layers
1221  *  to one common colorspace for blending. It can use either GL, Media
1222  *  or display engine to get this done based on the capabilities of the
1223  *  associated hardware.
1224  *
1225  *  Driver expects metadata to be put in &struct hdr_output_metadata
1226  *  structure from userspace. This is received as blob and stored in
1227  *  &drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1228  *  sink metadata in &struct hdr_sink_metadata, as
1229  *  &drm_connector.hdr_sink_metadata.  Driver uses
1230  *  drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1231  *  hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1232  *  HDMI encoder.
1233  *
1234  * max bpc:
1235  *  This range property is used by userspace to limit the bit depth. When
1236  *  used the driver would limit the bpc in accordance with the valid range
1237  *  supported by the hardware and sink. Drivers to use the function
1238  *  drm_connector_attach_max_bpc_property() to create and attach the
1239  *  property to the connector during initialization.
1240  *
1241  * Connectors also have one standardized atomic property:
1242  *
1243  * CRTC_ID:
1244  *  Mode object ID of the &drm_crtc this connector should be connected to.
1245  *
1246  * Connectors for LCD panels may also have one standardized property:
1247  *
1248  * panel orientation:
1249  *  On some devices the LCD panel is mounted in the casing in such a way
1250  *  that the up/top side of the panel does not match with the top side of
1251  *  the device. Userspace can use this property to check for this.
1252  *  Note that input coordinates from touchscreens (input devices with
1253  *  INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1254  *  coordinates, so if userspace rotates the picture to adjust for
1255  *  the orientation it must also apply the same transformation to the
1256  *  touchscreen input coordinates. This property is initialized by calling
1257  *  drm_connector_set_panel_orientation() or
1258  *  drm_connector_set_panel_orientation_with_quirk()
1259  *
1260  * scaling mode:
1261  *  This property defines how a non-native mode is upscaled to the native
1262  *  mode of an LCD panel:
1263  *
1264  *  None:
1265  *      No upscaling happens, scaling is left to the panel. Not all
1266  *      drivers expose this mode.
1267  *  Full:
1268  *      The output is upscaled to the full resolution of the panel,
1269  *      ignoring the aspect ratio.
1270  *  Center:
1271  *      No upscaling happens, the output is centered within the native
1272  *      resolution the panel.
1273  *  Full aspect:
1274  *      The output is upscaled to maximize either the width or height
1275  *      while retaining the aspect ratio.
1276  *
1277  *  This property should be set up by calling
1278  *  drm_connector_attach_scaling_mode_property(). Note that drivers
1279  *  can also expose this property to external outputs, in which case they
1280  *  must support "None", which should be the default (since external screens
1281  *  have a built-in scaler).
1282  *
1283  * subconnector:
1284  *  This property is used by DVI-I, TVout and DisplayPort to indicate different
1285  *  connector subtypes. Enum values more or less match with those from main
1286  *  connector types.
1287  *  For DVI-I and TVout there is also a matching property "select subconnector"
1288  *  allowing to switch between signal types.
1289  *  DP subconnector corresponds to a downstream port.
1290  *
1291  * privacy-screen sw-state, privacy-screen hw-state:
1292  *  These 2 optional properties can be used to query the state of the
1293  *  electronic privacy screen that is available on some displays; and in
1294  *  some cases also control the state. If a driver implements these
1295  *  properties then both properties must be present.
1296  *
1297  *  "privacy-screen hw-state" is read-only and reflects the actual state
1298  *  of the privacy-screen, possible values: "Enabled", "Disabled,
1299  *  "Enabled-locked", "Disabled-locked". The locked states indicate
1300  *  that the state cannot be changed through the DRM API. E.g. there
1301  *  might be devices where the firmware-setup options, or a hardware
1302  *  slider-switch, offer always on / off modes.
1303  *
1304  *  "privacy-screen sw-state" can be set to change the privacy-screen state
1305  *  when not locked. In this case the driver must update the hw-state
1306  *  property to reflect the new state on completion of the commit of the
1307  *  sw-state property. Setting the sw-state property when the hw-state is
1308  *  locked must be interpreted by the driver as a request to change the
1309  *  state to the set state when the hw-state becomes unlocked. E.g. if
1310  *  "privacy-screen hw-state" is "Enabled-locked" and the sw-state
1311  *  gets set to "Disabled" followed by the user unlocking the state by
1312  *  changing the slider-switch position, then the driver must set the
1313  *  state to "Disabled" upon receiving the unlock event.
1314  *
1315  *  In some cases the privacy-screen's actual state might change outside of
1316  *  control of the DRM code. E.g. there might be a firmware handled hotkey
1317  *  which toggles the actual state, or the actual state might be changed
1318  *  through another userspace API such as writing /proc/acpi/ibm/lcdshadow.
1319  *  In this case the driver must update both the hw-state and the sw-state
1320  *  to reflect the new value, overwriting any pending state requests in the
1321  *  sw-state. Any pending sw-state requests are thus discarded.
1322  *
1323  *  Note that the ability for the state to change outside of control of
1324  *  the DRM master process means that userspace must not cache the value
1325  *  of the sw-state. Caching the sw-state value and including it in later
1326  *  atomic commits may lead to overriding a state change done through e.g.
1327  *  a firmware handled hotkey. Therefor userspace must not include the
1328  *  privacy-screen sw-state in an atomic commit unless it wants to change
1329  *  its value.
1330  */
1331 
1332 int drm_connector_create_standard_properties(struct drm_device *dev)
1333 {
1334     struct drm_property *prop;
1335 
1336     prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1337                    DRM_MODE_PROP_IMMUTABLE,
1338                    "EDID", 0);
1339     if (!prop)
1340         return -ENOMEM;
1341     dev->mode_config.edid_property = prop;
1342 
1343     prop = drm_property_create_enum(dev, 0,
1344                    "DPMS", drm_dpms_enum_list,
1345                    ARRAY_SIZE(drm_dpms_enum_list));
1346     if (!prop)
1347         return -ENOMEM;
1348     dev->mode_config.dpms_property = prop;
1349 
1350     prop = drm_property_create(dev,
1351                    DRM_MODE_PROP_BLOB |
1352                    DRM_MODE_PROP_IMMUTABLE,
1353                    "PATH", 0);
1354     if (!prop)
1355         return -ENOMEM;
1356     dev->mode_config.path_property = prop;
1357 
1358     prop = drm_property_create(dev,
1359                    DRM_MODE_PROP_BLOB |
1360                    DRM_MODE_PROP_IMMUTABLE,
1361                    "TILE", 0);
1362     if (!prop)
1363         return -ENOMEM;
1364     dev->mode_config.tile_property = prop;
1365 
1366     prop = drm_property_create_enum(dev, 0, "link-status",
1367                     drm_link_status_enum_list,
1368                     ARRAY_SIZE(drm_link_status_enum_list));
1369     if (!prop)
1370         return -ENOMEM;
1371     dev->mode_config.link_status_property = prop;
1372 
1373     prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1374     if (!prop)
1375         return -ENOMEM;
1376     dev->mode_config.non_desktop_property = prop;
1377 
1378     prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1379                    "HDR_OUTPUT_METADATA", 0);
1380     if (!prop)
1381         return -ENOMEM;
1382     dev->mode_config.hdr_output_metadata_property = prop;
1383 
1384     return 0;
1385 }
1386 
1387 /**
1388  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1389  * @dev: DRM device
1390  *
1391  * Called by a driver the first time a DVI-I connector is made.
1392  *
1393  * Returns: %0
1394  */
1395 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1396 {
1397     struct drm_property *dvi_i_selector;
1398     struct drm_property *dvi_i_subconnector;
1399 
1400     if (dev->mode_config.dvi_i_select_subconnector_property)
1401         return 0;
1402 
1403     dvi_i_selector =
1404         drm_property_create_enum(dev, 0,
1405                     "select subconnector",
1406                     drm_dvi_i_select_enum_list,
1407                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
1408     dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1409 
1410     dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1411                     "subconnector",
1412                     drm_dvi_i_subconnector_enum_list,
1413                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1414     dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1415 
1416     return 0;
1417 }
1418 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1419 
1420 /**
1421  * drm_connector_attach_dp_subconnector_property - create subconnector property for DP
1422  * @connector: drm_connector to attach property
1423  *
1424  * Called by a driver when DP connector is created.
1425  */
1426 void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector)
1427 {
1428     struct drm_mode_config *mode_config = &connector->dev->mode_config;
1429 
1430     if (!mode_config->dp_subconnector_property)
1431         mode_config->dp_subconnector_property =
1432             drm_property_create_enum(connector->dev,
1433                 DRM_MODE_PROP_IMMUTABLE,
1434                 "subconnector",
1435                 drm_dp_subconnector_enum_list,
1436                 ARRAY_SIZE(drm_dp_subconnector_enum_list));
1437 
1438     drm_object_attach_property(&connector->base,
1439                    mode_config->dp_subconnector_property,
1440                    DRM_MODE_SUBCONNECTOR_Unknown);
1441 }
1442 EXPORT_SYMBOL(drm_connector_attach_dp_subconnector_property);
1443 
1444 /**
1445  * DOC: HDMI connector properties
1446  *
1447  * content type (HDMI specific):
1448  *  Indicates content type setting to be used in HDMI infoframes to indicate
1449  *  content type for the external device, so that it adjusts its display
1450  *  settings accordingly.
1451  *
1452  *  The value of this property can be one of the following:
1453  *
1454  *  No Data:
1455  *      Content type is unknown
1456  *  Graphics:
1457  *      Content type is graphics
1458  *  Photo:
1459  *      Content type is photo
1460  *  Cinema:
1461  *      Content type is cinema
1462  *  Game:
1463  *      Content type is game
1464  *
1465  *  The meaning of each content type is defined in CTA-861-G table 15.
1466  *
1467  *  Drivers can set up this property by calling
1468  *  drm_connector_attach_content_type_property(). Decoding to
1469  *  infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1470  */
1471 
1472 /**
1473  * drm_connector_attach_content_type_property - attach content-type property
1474  * @connector: connector to attach content type property on.
1475  *
1476  * Called by a driver the first time a HDMI connector is made.
1477  *
1478  * Returns: %0
1479  */
1480 int drm_connector_attach_content_type_property(struct drm_connector *connector)
1481 {
1482     if (!drm_mode_create_content_type_property(connector->dev))
1483         drm_object_attach_property(&connector->base,
1484                        connector->dev->mode_config.content_type_property,
1485                        DRM_MODE_CONTENT_TYPE_NO_DATA);
1486     return 0;
1487 }
1488 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
1489 
1490 /**
1491  * drm_connector_attach_tv_margin_properties - attach TV connector margin
1492  *  properties
1493  * @connector: DRM connector
1494  *
1495  * Called by a driver when it needs to attach TV margin props to a connector.
1496  * Typically used on SDTV and HDMI connectors.
1497  */
1498 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
1499 {
1500     struct drm_device *dev = connector->dev;
1501 
1502     drm_object_attach_property(&connector->base,
1503                    dev->mode_config.tv_left_margin_property,
1504                    0);
1505     drm_object_attach_property(&connector->base,
1506                    dev->mode_config.tv_right_margin_property,
1507                    0);
1508     drm_object_attach_property(&connector->base,
1509                    dev->mode_config.tv_top_margin_property,
1510                    0);
1511     drm_object_attach_property(&connector->base,
1512                    dev->mode_config.tv_bottom_margin_property,
1513                    0);
1514 }
1515 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
1516 
1517 /**
1518  * drm_mode_create_tv_margin_properties - create TV connector margin properties
1519  * @dev: DRM device
1520  *
1521  * Called by a driver's HDMI connector initialization routine, this function
1522  * creates the TV margin properties for a given device. No need to call this
1523  * function for an SDTV connector, it's already called from
1524  * drm_mode_create_tv_properties().
1525  *
1526  * Returns:
1527  * 0 on success or a negative error code on failure.
1528  */
1529 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
1530 {
1531     if (dev->mode_config.tv_left_margin_property)
1532         return 0;
1533 
1534     dev->mode_config.tv_left_margin_property =
1535         drm_property_create_range(dev, 0, "left margin", 0, 100);
1536     if (!dev->mode_config.tv_left_margin_property)
1537         return -ENOMEM;
1538 
1539     dev->mode_config.tv_right_margin_property =
1540         drm_property_create_range(dev, 0, "right margin", 0, 100);
1541     if (!dev->mode_config.tv_right_margin_property)
1542         return -ENOMEM;
1543 
1544     dev->mode_config.tv_top_margin_property =
1545         drm_property_create_range(dev, 0, "top margin", 0, 100);
1546     if (!dev->mode_config.tv_top_margin_property)
1547         return -ENOMEM;
1548 
1549     dev->mode_config.tv_bottom_margin_property =
1550         drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1551     if (!dev->mode_config.tv_bottom_margin_property)
1552         return -ENOMEM;
1553 
1554     return 0;
1555 }
1556 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
1557 
1558 /**
1559  * drm_mode_create_tv_properties - create TV specific connector properties
1560  * @dev: DRM device
1561  * @num_modes: number of different TV formats (modes) supported
1562  * @modes: array of pointers to strings containing name of each format
1563  *
1564  * Called by a driver's TV initialization routine, this function creates
1565  * the TV specific connector properties for a given device.  Caller is
1566  * responsible for allocating a list of format names and passing them to
1567  * this routine.
1568  *
1569  * Returns:
1570  * 0 on success or a negative error code on failure.
1571  */
1572 int drm_mode_create_tv_properties(struct drm_device *dev,
1573                   unsigned int num_modes,
1574                   const char * const modes[])
1575 {
1576     struct drm_property *tv_selector;
1577     struct drm_property *tv_subconnector;
1578     unsigned int i;
1579 
1580     if (dev->mode_config.tv_select_subconnector_property)
1581         return 0;
1582 
1583     /*
1584      * Basic connector properties
1585      */
1586     tv_selector = drm_property_create_enum(dev, 0,
1587                       "select subconnector",
1588                       drm_tv_select_enum_list,
1589                       ARRAY_SIZE(drm_tv_select_enum_list));
1590     if (!tv_selector)
1591         goto nomem;
1592 
1593     dev->mode_config.tv_select_subconnector_property = tv_selector;
1594 
1595     tv_subconnector =
1596         drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1597                     "subconnector",
1598                     drm_tv_subconnector_enum_list,
1599                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1600     if (!tv_subconnector)
1601         goto nomem;
1602     dev->mode_config.tv_subconnector_property = tv_subconnector;
1603 
1604     /*
1605      * Other, TV specific properties: margins & TV modes.
1606      */
1607     if (drm_mode_create_tv_margin_properties(dev))
1608         goto nomem;
1609 
1610     dev->mode_config.tv_mode_property =
1611         drm_property_create(dev, DRM_MODE_PROP_ENUM,
1612                     "mode", num_modes);
1613     if (!dev->mode_config.tv_mode_property)
1614         goto nomem;
1615 
1616     for (i = 0; i < num_modes; i++)
1617         drm_property_add_enum(dev->mode_config.tv_mode_property,
1618                       i, modes[i]);
1619 
1620     dev->mode_config.tv_brightness_property =
1621         drm_property_create_range(dev, 0, "brightness", 0, 100);
1622     if (!dev->mode_config.tv_brightness_property)
1623         goto nomem;
1624 
1625     dev->mode_config.tv_contrast_property =
1626         drm_property_create_range(dev, 0, "contrast", 0, 100);
1627     if (!dev->mode_config.tv_contrast_property)
1628         goto nomem;
1629 
1630     dev->mode_config.tv_flicker_reduction_property =
1631         drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1632     if (!dev->mode_config.tv_flicker_reduction_property)
1633         goto nomem;
1634 
1635     dev->mode_config.tv_overscan_property =
1636         drm_property_create_range(dev, 0, "overscan", 0, 100);
1637     if (!dev->mode_config.tv_overscan_property)
1638         goto nomem;
1639 
1640     dev->mode_config.tv_saturation_property =
1641         drm_property_create_range(dev, 0, "saturation", 0, 100);
1642     if (!dev->mode_config.tv_saturation_property)
1643         goto nomem;
1644 
1645     dev->mode_config.tv_hue_property =
1646         drm_property_create_range(dev, 0, "hue", 0, 100);
1647     if (!dev->mode_config.tv_hue_property)
1648         goto nomem;
1649 
1650     return 0;
1651 nomem:
1652     return -ENOMEM;
1653 }
1654 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1655 
1656 /**
1657  * drm_mode_create_scaling_mode_property - create scaling mode property
1658  * @dev: DRM device
1659  *
1660  * Called by a driver the first time it's needed, must be attached to desired
1661  * connectors.
1662  *
1663  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1664  * instead to correctly assign &drm_connector_state.scaling_mode
1665  * in the atomic state.
1666  *
1667  * Returns: %0
1668  */
1669 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1670 {
1671     struct drm_property *scaling_mode;
1672 
1673     if (dev->mode_config.scaling_mode_property)
1674         return 0;
1675 
1676     scaling_mode =
1677         drm_property_create_enum(dev, 0, "scaling mode",
1678                 drm_scaling_mode_enum_list,
1679                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1680 
1681     dev->mode_config.scaling_mode_property = scaling_mode;
1682 
1683     return 0;
1684 }
1685 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1686 
1687 /**
1688  * DOC: Variable refresh properties
1689  *
1690  * Variable refresh rate capable displays can dynamically adjust their
1691  * refresh rate by extending the duration of their vertical front porch
1692  * until page flip or timeout occurs. This can reduce or remove stuttering
1693  * and latency in scenarios where the page flip does not align with the
1694  * vblank interval.
1695  *
1696  * An example scenario would be an application flipping at a constant rate
1697  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1698  * interval and the same contents will be displayed twice. This can be
1699  * observed as stuttering for content with motion.
1700  *
1701  * If variable refresh rate was active on a display that supported a
1702  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1703  * for the example scenario. The minimum supported variable refresh rate of
1704  * 35Hz is below the page flip frequency and the vertical front porch can
1705  * be extended until the page flip occurs. The vblank interval will be
1706  * directly aligned to the page flip rate.
1707  *
1708  * Not all userspace content is suitable for use with variable refresh rate.
1709  * Large and frequent changes in vertical front porch duration may worsen
1710  * perceived stuttering for input sensitive applications.
1711  *
1712  * Panel brightness will also vary with vertical front porch duration. Some
1713  * panels may have noticeable differences in brightness between the minimum
1714  * vertical front porch duration and the maximum vertical front porch duration.
1715  * Large and frequent changes in vertical front porch duration may produce
1716  * observable flickering for such panels.
1717  *
1718  * Userspace control for variable refresh rate is supported via properties
1719  * on the &drm_connector and &drm_crtc objects.
1720  *
1721  * "vrr_capable":
1722  *  Optional &drm_connector boolean property that drivers should attach
1723  *  with drm_connector_attach_vrr_capable_property() on connectors that
1724  *  could support variable refresh rates. Drivers should update the
1725  *  property value by calling drm_connector_set_vrr_capable_property().
1726  *
1727  *  Absence of the property should indicate absence of support.
1728  *
1729  * "VRR_ENABLED":
1730  *  Default &drm_crtc boolean property that notifies the driver that the
1731  *  content on the CRTC is suitable for variable refresh rate presentation.
1732  *  The driver will take this property as a hint to enable variable
1733  *  refresh rate support if the receiver supports it, ie. if the
1734  *  "vrr_capable" property is true on the &drm_connector object. The
1735  *  vertical front porch duration will be extended until page-flip or
1736  *  timeout when enabled.
1737  *
1738  *  The minimum vertical front porch duration is defined as the vertical
1739  *  front porch duration for the current mode.
1740  *
1741  *  The maximum vertical front porch duration is greater than or equal to
1742  *  the minimum vertical front porch duration. The duration is derived
1743  *  from the minimum supported variable refresh rate for the connector.
1744  *
1745  *  The driver may place further restrictions within these minimum
1746  *  and maximum bounds.
1747  */
1748 
1749 /**
1750  * drm_connector_attach_vrr_capable_property - creates the
1751  * vrr_capable property
1752  * @connector: connector to create the vrr_capable property on.
1753  *
1754  * This is used by atomic drivers to add support for querying
1755  * variable refresh rate capability for a connector.
1756  *
1757  * Returns:
1758  * Zero on success, negative errno on failure.
1759  */
1760 int drm_connector_attach_vrr_capable_property(
1761     struct drm_connector *connector)
1762 {
1763     struct drm_device *dev = connector->dev;
1764     struct drm_property *prop;
1765 
1766     if (!connector->vrr_capable_property) {
1767         prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1768             "vrr_capable");
1769         if (!prop)
1770             return -ENOMEM;
1771 
1772         connector->vrr_capable_property = prop;
1773         drm_object_attach_property(&connector->base, prop, 0);
1774     }
1775 
1776     return 0;
1777 }
1778 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1779 
1780 /**
1781  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1782  * @connector: connector to attach scaling mode property on.
1783  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1784  *
1785  * This is used to add support for scaling mode to atomic drivers.
1786  * The scaling mode will be set to &drm_connector_state.scaling_mode
1787  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1788  *
1789  * This is the atomic version of drm_mode_create_scaling_mode_property().
1790  *
1791  * Returns:
1792  * Zero on success, negative errno on failure.
1793  */
1794 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1795                            u32 scaling_mode_mask)
1796 {
1797     struct drm_device *dev = connector->dev;
1798     struct drm_property *scaling_mode_property;
1799     int i;
1800     const unsigned valid_scaling_mode_mask =
1801         (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1802 
1803     if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1804             scaling_mode_mask & ~valid_scaling_mode_mask))
1805         return -EINVAL;
1806 
1807     scaling_mode_property =
1808         drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1809                     hweight32(scaling_mode_mask));
1810 
1811     if (!scaling_mode_property)
1812         return -ENOMEM;
1813 
1814     for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1815         int ret;
1816 
1817         if (!(BIT(i) & scaling_mode_mask))
1818             continue;
1819 
1820         ret = drm_property_add_enum(scaling_mode_property,
1821                         drm_scaling_mode_enum_list[i].type,
1822                         drm_scaling_mode_enum_list[i].name);
1823 
1824         if (ret) {
1825             drm_property_destroy(dev, scaling_mode_property);
1826 
1827             return ret;
1828         }
1829     }
1830 
1831     drm_object_attach_property(&connector->base,
1832                    scaling_mode_property, 0);
1833 
1834     connector->scaling_mode_property = scaling_mode_property;
1835 
1836     return 0;
1837 }
1838 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1839 
1840 /**
1841  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1842  * @dev: DRM device
1843  *
1844  * Called by a driver the first time it's needed, must be attached to desired
1845  * connectors.
1846  *
1847  * Returns:
1848  * Zero on success, negative errno on failure.
1849  */
1850 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1851 {
1852     if (dev->mode_config.aspect_ratio_property)
1853         return 0;
1854 
1855     dev->mode_config.aspect_ratio_property =
1856         drm_property_create_enum(dev, 0, "aspect ratio",
1857                 drm_aspect_ratio_enum_list,
1858                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1859 
1860     if (dev->mode_config.aspect_ratio_property == NULL)
1861         return -ENOMEM;
1862 
1863     return 0;
1864 }
1865 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1866 
1867 /**
1868  * DOC: standard connector properties
1869  *
1870  * Colorspace:
1871  *     This property helps select a suitable colorspace based on the sink
1872  *     capability. Modern sink devices support wider gamut like BT2020.
1873  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
1874  *     is being played by the user, same for any other colorspace. Thereby
1875  *     giving a good visual experience to users.
1876  *
1877  *     The expectation from userspace is that it should parse the EDID
1878  *     and get supported colorspaces. Use this property and switch to the
1879  *     one supported. Sink supported colorspaces should be retrieved by
1880  *     userspace from EDID and driver will not explicitly expose them.
1881  *
1882  *     Basically the expectation from userspace is:
1883  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1884  *        colorspace
1885  *      - Set this new property to let the sink know what it
1886  *        converted the CRTC output to.
1887  *      - This property is just to inform sink what colorspace
1888  *        source is trying to drive.
1889  *
1890  * Because between HDMI and DP have different colorspaces,
1891  * drm_mode_create_hdmi_colorspace_property() is used for HDMI connector and
1892  * drm_mode_create_dp_colorspace_property() is used for DP connector.
1893  */
1894 
1895 /**
1896  * drm_mode_create_hdmi_colorspace_property - create hdmi colorspace property
1897  * @connector: connector to create the Colorspace property on.
1898  *
1899  * Called by a driver the first time it's needed, must be attached to desired
1900  * HDMI connectors.
1901  *
1902  * Returns:
1903  * Zero on success, negative errno on failure.
1904  */
1905 int drm_mode_create_hdmi_colorspace_property(struct drm_connector *connector)
1906 {
1907     struct drm_device *dev = connector->dev;
1908 
1909     if (connector->colorspace_property)
1910         return 0;
1911 
1912     connector->colorspace_property =
1913         drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
1914                      hdmi_colorspaces,
1915                      ARRAY_SIZE(hdmi_colorspaces));
1916 
1917     if (!connector->colorspace_property)
1918         return -ENOMEM;
1919 
1920     return 0;
1921 }
1922 EXPORT_SYMBOL(drm_mode_create_hdmi_colorspace_property);
1923 
1924 /**
1925  * drm_mode_create_dp_colorspace_property - create dp colorspace property
1926  * @connector: connector to create the Colorspace property on.
1927  *
1928  * Called by a driver the first time it's needed, must be attached to desired
1929  * DP connectors.
1930  *
1931  * Returns:
1932  * Zero on success, negative errno on failure.
1933  */
1934 int drm_mode_create_dp_colorspace_property(struct drm_connector *connector)
1935 {
1936     struct drm_device *dev = connector->dev;
1937 
1938     if (connector->colorspace_property)
1939         return 0;
1940 
1941     connector->colorspace_property =
1942         drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
1943                      dp_colorspaces,
1944                      ARRAY_SIZE(dp_colorspaces));
1945 
1946     if (!connector->colorspace_property)
1947         return -ENOMEM;
1948 
1949     return 0;
1950 }
1951 EXPORT_SYMBOL(drm_mode_create_dp_colorspace_property);
1952 
1953 /**
1954  * drm_mode_create_content_type_property - create content type property
1955  * @dev: DRM device
1956  *
1957  * Called by a driver the first time it's needed, must be attached to desired
1958  * connectors.
1959  *
1960  * Returns:
1961  * Zero on success, negative errno on failure.
1962  */
1963 int drm_mode_create_content_type_property(struct drm_device *dev)
1964 {
1965     if (dev->mode_config.content_type_property)
1966         return 0;
1967 
1968     dev->mode_config.content_type_property =
1969         drm_property_create_enum(dev, 0, "content type",
1970                      drm_content_type_enum_list,
1971                      ARRAY_SIZE(drm_content_type_enum_list));
1972 
1973     if (dev->mode_config.content_type_property == NULL)
1974         return -ENOMEM;
1975 
1976     return 0;
1977 }
1978 EXPORT_SYMBOL(drm_mode_create_content_type_property);
1979 
1980 /**
1981  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1982  * @dev: DRM device
1983  *
1984  * Create the suggested x/y offset property for connectors.
1985  *
1986  * Returns:
1987  * 0 on success or a negative error code on failure.
1988  */
1989 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1990 {
1991     if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1992         return 0;
1993 
1994     dev->mode_config.suggested_x_property =
1995         drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1996 
1997     dev->mode_config.suggested_y_property =
1998         drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1999 
2000     if (dev->mode_config.suggested_x_property == NULL ||
2001         dev->mode_config.suggested_y_property == NULL)
2002         return -ENOMEM;
2003     return 0;
2004 }
2005 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
2006 
2007 /**
2008  * drm_connector_set_path_property - set tile property on connector
2009  * @connector: connector to set property on.
2010  * @path: path to use for property; must not be NULL.
2011  *
2012  * This creates a property to expose to userspace to specify a
2013  * connector path. This is mainly used for DisplayPort MST where
2014  * connectors have a topology and we want to allow userspace to give
2015  * them more meaningful names.
2016  *
2017  * Returns:
2018  * Zero on success, negative errno on failure.
2019  */
2020 int drm_connector_set_path_property(struct drm_connector *connector,
2021                     const char *path)
2022 {
2023     struct drm_device *dev = connector->dev;
2024     int ret;
2025 
2026     ret = drm_property_replace_global_blob(dev,
2027                            &connector->path_blob_ptr,
2028                            strlen(path) + 1,
2029                            path,
2030                            &connector->base,
2031                            dev->mode_config.path_property);
2032     return ret;
2033 }
2034 EXPORT_SYMBOL(drm_connector_set_path_property);
2035 
2036 /**
2037  * drm_connector_set_tile_property - set tile property on connector
2038  * @connector: connector to set property on.
2039  *
2040  * This looks up the tile information for a connector, and creates a
2041  * property for userspace to parse if it exists. The property is of
2042  * the form of 8 integers using ':' as a separator.
2043  * This is used for dual port tiled displays with DisplayPort SST
2044  * or DisplayPort MST connectors.
2045  *
2046  * Returns:
2047  * Zero on success, errno on failure.
2048  */
2049 int drm_connector_set_tile_property(struct drm_connector *connector)
2050 {
2051     struct drm_device *dev = connector->dev;
2052     char tile[256];
2053     int ret;
2054 
2055     if (!connector->has_tile) {
2056         ret  = drm_property_replace_global_blob(dev,
2057                             &connector->tile_blob_ptr,
2058                             0,
2059                             NULL,
2060                             &connector->base,
2061                             dev->mode_config.tile_property);
2062         return ret;
2063     }
2064 
2065     snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
2066          connector->tile_group->id, connector->tile_is_single_monitor,
2067          connector->num_h_tile, connector->num_v_tile,
2068          connector->tile_h_loc, connector->tile_v_loc,
2069          connector->tile_h_size, connector->tile_v_size);
2070 
2071     ret = drm_property_replace_global_blob(dev,
2072                            &connector->tile_blob_ptr,
2073                            strlen(tile) + 1,
2074                            tile,
2075                            &connector->base,
2076                            dev->mode_config.tile_property);
2077     return ret;
2078 }
2079 EXPORT_SYMBOL(drm_connector_set_tile_property);
2080 
2081 /**
2082  * drm_connector_set_link_status_property - Set link status property of a connector
2083  * @connector: drm connector
2084  * @link_status: new value of link status property (0: Good, 1: Bad)
2085  *
2086  * In usual working scenario, this link status property will always be set to
2087  * "GOOD". If something fails during or after a mode set, the kernel driver
2088  * may set this link status property to "BAD". The caller then needs to send a
2089  * hotplug uevent for userspace to re-check the valid modes through
2090  * GET_CONNECTOR_IOCTL and retry modeset.
2091  *
2092  * Note: Drivers cannot rely on userspace to support this property and
2093  * issue a modeset. As such, they may choose to handle issues (like
2094  * re-training a link) without userspace's intervention.
2095  *
2096  * The reason for adding this property is to handle link training failures, but
2097  * it is not limited to DP or link training. For example, if we implement
2098  * asynchronous setcrtc, this property can be used to report any failures in that.
2099  */
2100 void drm_connector_set_link_status_property(struct drm_connector *connector,
2101                         uint64_t link_status)
2102 {
2103     struct drm_device *dev = connector->dev;
2104 
2105     drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2106     connector->state->link_status = link_status;
2107     drm_modeset_unlock(&dev->mode_config.connection_mutex);
2108 }
2109 EXPORT_SYMBOL(drm_connector_set_link_status_property);
2110 
2111 /**
2112  * drm_connector_attach_max_bpc_property - attach "max bpc" property
2113  * @connector: connector to attach max bpc property on.
2114  * @min: The minimum bit depth supported by the connector.
2115  * @max: The maximum bit depth supported by the connector.
2116  *
2117  * This is used to add support for limiting the bit depth on a connector.
2118  *
2119  * Returns:
2120  * Zero on success, negative errno on failure.
2121  */
2122 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
2123                       int min, int max)
2124 {
2125     struct drm_device *dev = connector->dev;
2126     struct drm_property *prop;
2127 
2128     prop = connector->max_bpc_property;
2129     if (!prop) {
2130         prop = drm_property_create_range(dev, 0, "max bpc", min, max);
2131         if (!prop)
2132             return -ENOMEM;
2133 
2134         connector->max_bpc_property = prop;
2135     }
2136 
2137     drm_object_attach_property(&connector->base, prop, max);
2138     connector->state->max_requested_bpc = max;
2139     connector->state->max_bpc = max;
2140 
2141     return 0;
2142 }
2143 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
2144 
2145 /**
2146  * drm_connector_attach_hdr_output_metadata_property - attach "HDR_OUTPUT_METADA" property
2147  * @connector: connector to attach the property on.
2148  *
2149  * This is used to allow the userspace to send HDR Metadata to the
2150  * driver.
2151  *
2152  * Returns:
2153  * Zero on success, negative errno on failure.
2154  */
2155 int drm_connector_attach_hdr_output_metadata_property(struct drm_connector *connector)
2156 {
2157     struct drm_device *dev = connector->dev;
2158     struct drm_property *prop = dev->mode_config.hdr_output_metadata_property;
2159 
2160     drm_object_attach_property(&connector->base, prop, 0);
2161 
2162     return 0;
2163 }
2164 EXPORT_SYMBOL(drm_connector_attach_hdr_output_metadata_property);
2165 
2166 /**
2167  * drm_connector_attach_colorspace_property - attach "Colorspace" property
2168  * @connector: connector to attach the property on.
2169  *
2170  * This is used to allow the userspace to signal the output colorspace
2171  * to the driver.
2172  *
2173  * Returns:
2174  * Zero on success, negative errno on failure.
2175  */
2176 int drm_connector_attach_colorspace_property(struct drm_connector *connector)
2177 {
2178     struct drm_property *prop = connector->colorspace_property;
2179 
2180     drm_object_attach_property(&connector->base, prop, DRM_MODE_COLORIMETRY_DEFAULT);
2181 
2182     return 0;
2183 }
2184 EXPORT_SYMBOL(drm_connector_attach_colorspace_property);
2185 
2186 /**
2187  * drm_connector_atomic_hdr_metadata_equal - checks if the hdr metadata changed
2188  * @old_state: old connector state to compare
2189  * @new_state: new connector state to compare
2190  *
2191  * This is used by HDR-enabled drivers to test whether the HDR metadata
2192  * have changed between two different connector state (and thus probably
2193  * requires a full blown mode change).
2194  *
2195  * Returns:
2196  * True if the metadata are equal, False otherwise
2197  */
2198 bool drm_connector_atomic_hdr_metadata_equal(struct drm_connector_state *old_state,
2199                          struct drm_connector_state *new_state)
2200 {
2201     struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2202     struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2203 
2204     if (!old_blob || !new_blob)
2205         return old_blob == new_blob;
2206 
2207     if (old_blob->length != new_blob->length)
2208         return false;
2209 
2210     return !memcmp(old_blob->data, new_blob->data, old_blob->length);
2211 }
2212 EXPORT_SYMBOL(drm_connector_atomic_hdr_metadata_equal);
2213 
2214 /**
2215  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
2216  * capable property for a connector
2217  * @connector: drm connector
2218  * @capable: True if the connector is variable refresh rate capable
2219  *
2220  * Should be used by atomic drivers to update the indicated support for
2221  * variable refresh rate over a connector.
2222  */
2223 void drm_connector_set_vrr_capable_property(
2224         struct drm_connector *connector, bool capable)
2225 {
2226     if (!connector->vrr_capable_property)
2227         return;
2228 
2229     drm_object_property_set_value(&connector->base,
2230                       connector->vrr_capable_property,
2231                       capable);
2232 }
2233 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
2234 
2235 /**
2236  * drm_connector_set_panel_orientation - sets the connector's panel_orientation
2237  * @connector: connector for which to set the panel-orientation property.
2238  * @panel_orientation: drm_panel_orientation value to set
2239  *
2240  * This function sets the connector's panel_orientation and attaches
2241  * a "panel orientation" property to the connector.
2242  *
2243  * Calling this function on a connector where the panel_orientation has
2244  * already been set is a no-op (e.g. the orientation has been overridden with
2245  * a kernel commandline option).
2246  *
2247  * It is allowed to call this function with a panel_orientation of
2248  * DRM_MODE_PANEL_ORIENTATION_UNKNOWN, in which case it is a no-op.
2249  *
2250  * The function shouldn't be called in panel after drm is registered (i.e.
2251  * drm_dev_register() is called in drm).
2252  *
2253  * Returns:
2254  * Zero on success, negative errno on failure.
2255  */
2256 int drm_connector_set_panel_orientation(
2257     struct drm_connector *connector,
2258     enum drm_panel_orientation panel_orientation)
2259 {
2260     struct drm_device *dev = connector->dev;
2261     struct drm_display_info *info = &connector->display_info;
2262     struct drm_property *prop;
2263 
2264     /* Already set? */
2265     if (info->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2266         return 0;
2267 
2268     /* Don't attach the property if the orientation is unknown */
2269     if (panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2270         return 0;
2271 
2272     info->panel_orientation = panel_orientation;
2273 
2274     prop = dev->mode_config.panel_orientation_property;
2275     if (!prop) {
2276         prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2277                 "panel orientation",
2278                 drm_panel_orientation_enum_list,
2279                 ARRAY_SIZE(drm_panel_orientation_enum_list));
2280         if (!prop)
2281             return -ENOMEM;
2282 
2283         dev->mode_config.panel_orientation_property = prop;
2284     }
2285 
2286     drm_object_attach_property(&connector->base, prop,
2287                    info->panel_orientation);
2288     return 0;
2289 }
2290 EXPORT_SYMBOL(drm_connector_set_panel_orientation);
2291 
2292 /**
2293  * drm_connector_set_panel_orientation_with_quirk - set the
2294  *  connector's panel_orientation after checking for quirks
2295  * @connector: connector for which to init the panel-orientation property.
2296  * @panel_orientation: drm_panel_orientation value to set
2297  * @width: width in pixels of the panel, used for panel quirk detection
2298  * @height: height in pixels of the panel, used for panel quirk detection
2299  *
2300  * Like drm_connector_set_panel_orientation(), but with a check for platform
2301  * specific (e.g. DMI based) quirks overriding the passed in panel_orientation.
2302  *
2303  * Returns:
2304  * Zero on success, negative errno on failure.
2305  */
2306 int drm_connector_set_panel_orientation_with_quirk(
2307     struct drm_connector *connector,
2308     enum drm_panel_orientation panel_orientation,
2309     int width, int height)
2310 {
2311     int orientation_quirk;
2312 
2313     orientation_quirk = drm_get_panel_orientation_quirk(width, height);
2314     if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2315         panel_orientation = orientation_quirk;
2316 
2317     return drm_connector_set_panel_orientation(connector,
2318                            panel_orientation);
2319 }
2320 EXPORT_SYMBOL(drm_connector_set_panel_orientation_with_quirk);
2321 
2322 /**
2323  * drm_connector_set_orientation_from_panel -
2324  *  set the connector's panel_orientation from panel's callback.
2325  * @connector: connector for which to init the panel-orientation property.
2326  * @panel: panel that can provide orientation information.
2327  *
2328  * Drm drivers should call this function before drm_dev_register().
2329  * Orientation is obtained from panel's .get_orientation() callback.
2330  *
2331  * Returns:
2332  * Zero on success, negative errno on failure.
2333  */
2334 int drm_connector_set_orientation_from_panel(
2335     struct drm_connector *connector,
2336     struct drm_panel *panel)
2337 {
2338     enum drm_panel_orientation orientation;
2339 
2340     if (panel && panel->funcs && panel->funcs->get_orientation)
2341         orientation = panel->funcs->get_orientation(panel);
2342     else
2343         orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
2344 
2345     return drm_connector_set_panel_orientation(connector, orientation);
2346 }
2347 EXPORT_SYMBOL(drm_connector_set_orientation_from_panel);
2348 
2349 static const struct drm_prop_enum_list privacy_screen_enum[] = {
2350     { PRIVACY_SCREEN_DISABLED,      "Disabled" },
2351     { PRIVACY_SCREEN_ENABLED,       "Enabled" },
2352     { PRIVACY_SCREEN_DISABLED_LOCKED,   "Disabled-locked" },
2353     { PRIVACY_SCREEN_ENABLED_LOCKED,    "Enabled-locked" },
2354 };
2355 
2356 /**
2357  * drm_connector_create_privacy_screen_properties - create the drm connecter's
2358  *    privacy-screen properties.
2359  * @connector: connector for which to create the privacy-screen properties
2360  *
2361  * This function creates the "privacy-screen sw-state" and "privacy-screen
2362  * hw-state" properties for the connector. They are not attached.
2363  */
2364 void
2365 drm_connector_create_privacy_screen_properties(struct drm_connector *connector)
2366 {
2367     if (connector->privacy_screen_sw_state_property)
2368         return;
2369 
2370     /* Note sw-state only supports the first 2 values of the enum */
2371     connector->privacy_screen_sw_state_property =
2372         drm_property_create_enum(connector->dev, DRM_MODE_PROP_ENUM,
2373                 "privacy-screen sw-state",
2374                 privacy_screen_enum, 2);
2375 
2376     connector->privacy_screen_hw_state_property =
2377         drm_property_create_enum(connector->dev,
2378                 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ENUM,
2379                 "privacy-screen hw-state",
2380                 privacy_screen_enum,
2381                 ARRAY_SIZE(privacy_screen_enum));
2382 }
2383 EXPORT_SYMBOL(drm_connector_create_privacy_screen_properties);
2384 
2385 /**
2386  * drm_connector_attach_privacy_screen_properties - attach the drm connecter's
2387  *    privacy-screen properties.
2388  * @connector: connector on which to attach the privacy-screen properties
2389  *
2390  * This function attaches the "privacy-screen sw-state" and "privacy-screen
2391  * hw-state" properties to the connector. The initial state of both is set
2392  * to "Disabled".
2393  */
2394 void
2395 drm_connector_attach_privacy_screen_properties(struct drm_connector *connector)
2396 {
2397     if (!connector->privacy_screen_sw_state_property)
2398         return;
2399 
2400     drm_object_attach_property(&connector->base,
2401                    connector->privacy_screen_sw_state_property,
2402                    PRIVACY_SCREEN_DISABLED);
2403 
2404     drm_object_attach_property(&connector->base,
2405                    connector->privacy_screen_hw_state_property,
2406                    PRIVACY_SCREEN_DISABLED);
2407 }
2408 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_properties);
2409 
2410 static void drm_connector_update_privacy_screen_properties(
2411     struct drm_connector *connector, bool set_sw_state)
2412 {
2413     enum drm_privacy_screen_status sw_state, hw_state;
2414 
2415     drm_privacy_screen_get_state(connector->privacy_screen,
2416                      &sw_state, &hw_state);
2417 
2418     if (set_sw_state)
2419         connector->state->privacy_screen_sw_state = sw_state;
2420     drm_object_property_set_value(&connector->base,
2421             connector->privacy_screen_hw_state_property, hw_state);
2422 }
2423 
2424 static int drm_connector_privacy_screen_notifier(
2425     struct notifier_block *nb, unsigned long action, void *data)
2426 {
2427     struct drm_connector *connector =
2428         container_of(nb, struct drm_connector, privacy_screen_notifier);
2429     struct drm_device *dev = connector->dev;
2430 
2431     drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2432     drm_connector_update_privacy_screen_properties(connector, true);
2433     drm_modeset_unlock(&dev->mode_config.connection_mutex);
2434 
2435     drm_sysfs_connector_status_event(connector,
2436                 connector->privacy_screen_sw_state_property);
2437     drm_sysfs_connector_status_event(connector,
2438                 connector->privacy_screen_hw_state_property);
2439 
2440     return NOTIFY_DONE;
2441 }
2442 
2443 /**
2444  * drm_connector_attach_privacy_screen_provider - attach a privacy-screen to
2445  *    the connector
2446  * @connector: connector to attach the privacy-screen to
2447  * @priv: drm_privacy_screen to attach
2448  *
2449  * Create and attach the standard privacy-screen properties and register
2450  * a generic notifier for generating sysfs-connector-status-events
2451  * on external changes to the privacy-screen status.
2452  * This function takes ownership of the passed in drm_privacy_screen and will
2453  * call drm_privacy_screen_put() on it when the connector is destroyed.
2454  */
2455 void drm_connector_attach_privacy_screen_provider(
2456     struct drm_connector *connector, struct drm_privacy_screen *priv)
2457 {
2458     connector->privacy_screen = priv;
2459     connector->privacy_screen_notifier.notifier_call =
2460         drm_connector_privacy_screen_notifier;
2461 
2462     drm_connector_create_privacy_screen_properties(connector);
2463     drm_connector_update_privacy_screen_properties(connector, true);
2464     drm_connector_attach_privacy_screen_properties(connector);
2465 }
2466 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_provider);
2467 
2468 /**
2469  * drm_connector_update_privacy_screen - update connector's privacy-screen sw-state
2470  * @connector_state: connector-state to update the privacy-screen for
2471  *
2472  * This function calls drm_privacy_screen_set_sw_state() on the connector's
2473  * privacy-screen.
2474  *
2475  * If the connector has no privacy-screen, then this is a no-op.
2476  */
2477 void drm_connector_update_privacy_screen(const struct drm_connector_state *connector_state)
2478 {
2479     struct drm_connector *connector = connector_state->connector;
2480     int ret;
2481 
2482     if (!connector->privacy_screen)
2483         return;
2484 
2485     ret = drm_privacy_screen_set_sw_state(connector->privacy_screen,
2486                           connector_state->privacy_screen_sw_state);
2487     if (ret) {
2488         drm_err(connector->dev, "Error updating privacy-screen sw_state\n");
2489         return;
2490     }
2491 
2492     /* The hw_state property value may have changed, update it. */
2493     drm_connector_update_privacy_screen_properties(connector, false);
2494 }
2495 EXPORT_SYMBOL(drm_connector_update_privacy_screen);
2496 
2497 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
2498                     struct drm_property *property,
2499                     uint64_t value)
2500 {
2501     int ret = -EINVAL;
2502     struct drm_connector *connector = obj_to_connector(obj);
2503 
2504     /* Do DPMS ourselves */
2505     if (property == connector->dev->mode_config.dpms_property) {
2506         ret = (*connector->funcs->dpms)(connector, (int)value);
2507     } else if (connector->funcs->set_property)
2508         ret = connector->funcs->set_property(connector, property, value);
2509 
2510     if (!ret)
2511         drm_object_property_set_value(&connector->base, property, value);
2512     return ret;
2513 }
2514 
2515 int drm_connector_property_set_ioctl(struct drm_device *dev,
2516                      void *data, struct drm_file *file_priv)
2517 {
2518     struct drm_mode_connector_set_property *conn_set_prop = data;
2519     struct drm_mode_obj_set_property obj_set_prop = {
2520         .value = conn_set_prop->value,
2521         .prop_id = conn_set_prop->prop_id,
2522         .obj_id = conn_set_prop->connector_id,
2523         .obj_type = DRM_MODE_OBJECT_CONNECTOR
2524     };
2525 
2526     /* It does all the locking and checking we need */
2527     return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
2528 }
2529 
2530 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2531 {
2532     /* For atomic drivers only state objects are synchronously updated and
2533      * protected by modeset locks, so check those first.
2534      */
2535     if (connector->state)
2536         return connector->state->best_encoder;
2537     return connector->encoder;
2538 }
2539 
2540 static bool
2541 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2542                  const struct list_head *modes,
2543                  const struct drm_file *file_priv)
2544 {
2545     /*
2546      * If user-space hasn't configured the driver to expose the stereo 3D
2547      * modes, don't expose them.
2548      */
2549     if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2550         return false;
2551     /*
2552      * If user-space hasn't configured the driver to expose the modes
2553      * with aspect-ratio, don't expose them. However if such a mode
2554      * is unique, let it be exposed, but reset the aspect-ratio flags
2555      * while preparing the list of user-modes.
2556      */
2557     if (!file_priv->aspect_ratio_allowed) {
2558         const struct drm_display_mode *mode_itr;
2559 
2560         list_for_each_entry(mode_itr, modes, head) {
2561             if (mode_itr->expose_to_userspace &&
2562                 drm_mode_match(mode_itr, mode,
2563                        DRM_MODE_MATCH_TIMINGS |
2564                        DRM_MODE_MATCH_CLOCK |
2565                        DRM_MODE_MATCH_FLAGS |
2566                        DRM_MODE_MATCH_3D_FLAGS))
2567                 return false;
2568         }
2569     }
2570 
2571     return true;
2572 }
2573 
2574 int drm_mode_getconnector(struct drm_device *dev, void *data,
2575               struct drm_file *file_priv)
2576 {
2577     struct drm_mode_get_connector *out_resp = data;
2578     struct drm_connector *connector;
2579     struct drm_encoder *encoder;
2580     struct drm_display_mode *mode;
2581     int mode_count = 0;
2582     int encoders_count = 0;
2583     int ret = 0;
2584     int copied = 0;
2585     struct drm_mode_modeinfo u_mode;
2586     struct drm_mode_modeinfo __user *mode_ptr;
2587     uint32_t __user *encoder_ptr;
2588     bool is_current_master;
2589 
2590     if (!drm_core_check_feature(dev, DRIVER_MODESET))
2591         return -EOPNOTSUPP;
2592 
2593     memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2594 
2595     connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
2596     if (!connector)
2597         return -ENOENT;
2598 
2599     encoders_count = hweight32(connector->possible_encoders);
2600 
2601     if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2602         copied = 0;
2603         encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2604 
2605         drm_connector_for_each_possible_encoder(connector, encoder) {
2606             if (put_user(encoder->base.id, encoder_ptr + copied)) {
2607                 ret = -EFAULT;
2608                 goto out;
2609             }
2610             copied++;
2611         }
2612     }
2613     out_resp->count_encoders = encoders_count;
2614 
2615     out_resp->connector_id = connector->base.id;
2616     out_resp->connector_type = connector->connector_type;
2617     out_resp->connector_type_id = connector->connector_type_id;
2618 
2619     is_current_master = drm_is_current_master(file_priv);
2620 
2621     mutex_lock(&dev->mode_config.mutex);
2622     if (out_resp->count_modes == 0) {
2623         if (is_current_master)
2624             connector->funcs->fill_modes(connector,
2625                              dev->mode_config.max_width,
2626                              dev->mode_config.max_height);
2627         else
2628             drm_dbg_kms(dev, "User-space requested a forced probe on [CONNECTOR:%d:%s] but is not the DRM master, demoting to read-only probe",
2629                     connector->base.id, connector->name);
2630     }
2631 
2632     out_resp->mm_width = connector->display_info.width_mm;
2633     out_resp->mm_height = connector->display_info.height_mm;
2634     out_resp->subpixel = connector->display_info.subpixel_order;
2635     out_resp->connection = connector->status;
2636 
2637     /* delayed so we get modes regardless of pre-fill_modes state */
2638     list_for_each_entry(mode, &connector->modes, head) {
2639         WARN_ON(mode->expose_to_userspace);
2640 
2641         if (drm_mode_expose_to_userspace(mode, &connector->modes,
2642                          file_priv)) {
2643             mode->expose_to_userspace = true;
2644             mode_count++;
2645         }
2646     }
2647 
2648     /*
2649      * This ioctl is called twice, once to determine how much space is
2650      * needed, and the 2nd time to fill it.
2651      */
2652     if ((out_resp->count_modes >= mode_count) && mode_count) {
2653         copied = 0;
2654         mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2655         list_for_each_entry(mode, &connector->modes, head) {
2656             if (!mode->expose_to_userspace)
2657                 continue;
2658 
2659             /* Clear the tag for the next time around */
2660             mode->expose_to_userspace = false;
2661 
2662             drm_mode_convert_to_umode(&u_mode, mode);
2663             /*
2664              * Reset aspect ratio flags of user-mode, if modes with
2665              * aspect-ratio are not supported.
2666              */
2667             if (!file_priv->aspect_ratio_allowed)
2668                 u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2669             if (copy_to_user(mode_ptr + copied,
2670                      &u_mode, sizeof(u_mode))) {
2671                 ret = -EFAULT;
2672 
2673                 /*
2674                  * Clear the tag for the rest of
2675                  * the modes for the next time around.
2676                  */
2677                 list_for_each_entry_continue(mode, &connector->modes, head)
2678                     mode->expose_to_userspace = false;
2679 
2680                 mutex_unlock(&dev->mode_config.mutex);
2681 
2682                 goto out;
2683             }
2684             copied++;
2685         }
2686     } else {
2687         /* Clear the tag for the next time around */
2688         list_for_each_entry(mode, &connector->modes, head)
2689             mode->expose_to_userspace = false;
2690     }
2691 
2692     out_resp->count_modes = mode_count;
2693     mutex_unlock(&dev->mode_config.mutex);
2694 
2695     drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2696     encoder = drm_connector_get_encoder(connector);
2697     if (encoder)
2698         out_resp->encoder_id = encoder->base.id;
2699     else
2700         out_resp->encoder_id = 0;
2701 
2702     /* Only grab properties after probing, to make sure EDID and other
2703      * properties reflect the latest status.
2704      */
2705     ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2706             (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2707             (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2708             &out_resp->count_props);
2709     drm_modeset_unlock(&dev->mode_config.connection_mutex);
2710 
2711 out:
2712     drm_connector_put(connector);
2713 
2714     return ret;
2715 }
2716 
2717 /**
2718  * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode
2719  * @fwnode: fwnode for which to find the matching drm_connector
2720  *
2721  * This functions looks up a drm_connector based on its associated fwnode. When
2722  * a connector is found a reference to the connector is returned. The caller must
2723  * call drm_connector_put() to release this reference when it is done with the
2724  * connector.
2725  *
2726  * Returns: A reference to the found connector or an ERR_PTR().
2727  */
2728 struct drm_connector *drm_connector_find_by_fwnode(struct fwnode_handle *fwnode)
2729 {
2730     struct drm_connector *connector, *found = ERR_PTR(-ENODEV);
2731 
2732     if (!fwnode)
2733         return ERR_PTR(-ENODEV);
2734 
2735     mutex_lock(&connector_list_lock);
2736 
2737     list_for_each_entry(connector, &connector_list, global_connector_list_entry) {
2738         if (connector->fwnode == fwnode ||
2739             (connector->fwnode && connector->fwnode->secondary == fwnode)) {
2740             drm_connector_get(connector);
2741             found = connector;
2742             break;
2743         }
2744     }
2745 
2746     mutex_unlock(&connector_list_lock);
2747 
2748     return found;
2749 }
2750 
2751 /**
2752  * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
2753  * @connector_fwnode: fwnode_handle to report the event on
2754  *
2755  * On some hardware a hotplug event notification may come from outside the display
2756  * driver / device. An example of this is some USB Type-C setups where the hardware
2757  * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
2758  * status bit to the GPU's DP HPD pin.
2759  *
2760  * This function can be used to report these out-of-band events after obtaining
2761  * a drm_connector reference through calling drm_connector_find_by_fwnode().
2762  */
2763 void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode)
2764 {
2765     struct drm_connector *connector;
2766 
2767     connector = drm_connector_find_by_fwnode(connector_fwnode);
2768     if (IS_ERR(connector))
2769         return;
2770 
2771     if (connector->funcs->oob_hotplug_event)
2772         connector->funcs->oob_hotplug_event(connector);
2773 
2774     drm_connector_put(connector);
2775 }
2776 EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
2777 
2778 
2779 /**
2780  * DOC: Tile group
2781  *
2782  * Tile groups are used to represent tiled monitors with a unique integer
2783  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
2784  * we store this in a tile group, so we have a common identifier for all tiles
2785  * in a monitor group. The property is called "TILE". Drivers can manage tile
2786  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
2787  * drm_mode_get_tile_group(). But this is only needed for internal panels where
2788  * the tile group information is exposed through a non-standard way.
2789  */
2790 
2791 static void drm_tile_group_free(struct kref *kref)
2792 {
2793     struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2794     struct drm_device *dev = tg->dev;
2795 
2796     mutex_lock(&dev->mode_config.idr_mutex);
2797     idr_remove(&dev->mode_config.tile_idr, tg->id);
2798     mutex_unlock(&dev->mode_config.idr_mutex);
2799     kfree(tg);
2800 }
2801 
2802 /**
2803  * drm_mode_put_tile_group - drop a reference to a tile group.
2804  * @dev: DRM device
2805  * @tg: tile group to drop reference to.
2806  *
2807  * drop reference to tile group and free if 0.
2808  */
2809 void drm_mode_put_tile_group(struct drm_device *dev,
2810                  struct drm_tile_group *tg)
2811 {
2812     kref_put(&tg->refcount, drm_tile_group_free);
2813 }
2814 EXPORT_SYMBOL(drm_mode_put_tile_group);
2815 
2816 /**
2817  * drm_mode_get_tile_group - get a reference to an existing tile group
2818  * @dev: DRM device
2819  * @topology: 8-bytes unique per monitor.
2820  *
2821  * Use the unique bytes to get a reference to an existing tile group.
2822  *
2823  * RETURNS:
2824  * tile group or NULL if not found.
2825  */
2826 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2827                            const char topology[8])
2828 {
2829     struct drm_tile_group *tg;
2830     int id;
2831 
2832     mutex_lock(&dev->mode_config.idr_mutex);
2833     idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2834         if (!memcmp(tg->group_data, topology, 8)) {
2835             if (!kref_get_unless_zero(&tg->refcount))
2836                 tg = NULL;
2837             mutex_unlock(&dev->mode_config.idr_mutex);
2838             return tg;
2839         }
2840     }
2841     mutex_unlock(&dev->mode_config.idr_mutex);
2842     return NULL;
2843 }
2844 EXPORT_SYMBOL(drm_mode_get_tile_group);
2845 
2846 /**
2847  * drm_mode_create_tile_group - create a tile group from a displayid description
2848  * @dev: DRM device
2849  * @topology: 8-bytes unique per monitor.
2850  *
2851  * Create a tile group for the unique monitor, and get a unique
2852  * identifier for the tile group.
2853  *
2854  * RETURNS:
2855  * new tile group or NULL.
2856  */
2857 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2858                           const char topology[8])
2859 {
2860     struct drm_tile_group *tg;
2861     int ret;
2862 
2863     tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2864     if (!tg)
2865         return NULL;
2866 
2867     kref_init(&tg->refcount);
2868     memcpy(tg->group_data, topology, 8);
2869     tg->dev = dev;
2870 
2871     mutex_lock(&dev->mode_config.idr_mutex);
2872     ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2873     if (ret >= 0) {
2874         tg->id = ret;
2875     } else {
2876         kfree(tg);
2877         tg = NULL;
2878     }
2879 
2880     mutex_unlock(&dev->mode_config.idr_mutex);
2881     return tg;
2882 }
2883 EXPORT_SYMBOL(drm_mode_create_tile_group);