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