GNU Linux-libre 4.9.337-gnu1
[releases.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         if (dev->registered)
368                 drm_sysfs_hotplug_event(dev);
369 }
370 EXPORT_SYMBOL(drm_connector_cleanup);
371
372 /**
373  * drm_connector_register - register a connector
374  * @connector: the connector to register
375  *
376  * Register userspace interfaces for a connector
377  *
378  * Returns:
379  * Zero on success, error code on failure.
380  */
381 int drm_connector_register(struct drm_connector *connector)
382 {
383         int ret = 0;
384
385         if (!connector->dev->registered)
386                 return 0;
387
388         mutex_lock(&connector->mutex);
389         if (connector->registered)
390                 goto unlock;
391
392         ret = drm_sysfs_connector_add(connector);
393         if (ret)
394                 goto unlock;
395
396         ret = drm_debugfs_connector_add(connector);
397         if (ret) {
398                 goto err_sysfs;
399         }
400
401         if (connector->funcs->late_register) {
402                 ret = connector->funcs->late_register(connector);
403                 if (ret)
404                         goto err_debugfs;
405         }
406
407         drm_mode_object_register(connector->dev, &connector->base);
408
409         connector->registered = true;
410         goto unlock;
411
412 err_debugfs:
413         drm_debugfs_connector_remove(connector);
414 err_sysfs:
415         drm_sysfs_connector_remove(connector);
416 unlock:
417         mutex_unlock(&connector->mutex);
418         return ret;
419 }
420 EXPORT_SYMBOL(drm_connector_register);
421
422 /**
423  * drm_connector_unregister - unregister a connector
424  * @connector: the connector to unregister
425  *
426  * Unregister userspace interfaces for a connector
427  */
428 void drm_connector_unregister(struct drm_connector *connector)
429 {
430         mutex_lock(&connector->mutex);
431         if (!connector->registered) {
432                 mutex_unlock(&connector->mutex);
433                 return;
434         }
435
436         if (connector->funcs->early_unregister)
437                 connector->funcs->early_unregister(connector);
438
439         drm_sysfs_connector_remove(connector);
440         drm_debugfs_connector_remove(connector);
441
442         connector->registered = false;
443         mutex_unlock(&connector->mutex);
444 }
445 EXPORT_SYMBOL(drm_connector_unregister);
446
447 void drm_connector_unregister_all(struct drm_device *dev)
448 {
449         struct drm_connector *connector;
450
451         /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
452         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
453                 drm_connector_unregister(connector);
454 }
455
456 int drm_connector_register_all(struct drm_device *dev)
457 {
458         struct drm_connector *connector;
459         int ret;
460
461         /* FIXME: taking the mode config mutex ends up in a clash with
462          * fbcon/backlight registration */
463         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
464                 ret = drm_connector_register(connector);
465                 if (ret)
466                         goto err;
467         }
468
469         return 0;
470
471 err:
472         mutex_unlock(&dev->mode_config.mutex);
473         drm_connector_unregister_all(dev);
474         return ret;
475 }
476
477 /**
478  * drm_get_connector_status_name - return a string for connector status
479  * @status: connector status to compute name of
480  *
481  * In contrast to the other drm_get_*_name functions this one here returns a
482  * const pointer and hence is threadsafe.
483  */
484 const char *drm_get_connector_status_name(enum drm_connector_status status)
485 {
486         if (status == connector_status_connected)
487                 return "connected";
488         else if (status == connector_status_disconnected)
489                 return "disconnected";
490         else
491                 return "unknown";
492 }
493 EXPORT_SYMBOL(drm_get_connector_status_name);
494
495 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
496         { SubPixelUnknown, "Unknown" },
497         { SubPixelHorizontalRGB, "Horizontal RGB" },
498         { SubPixelHorizontalBGR, "Horizontal BGR" },
499         { SubPixelVerticalRGB, "Vertical RGB" },
500         { SubPixelVerticalBGR, "Vertical BGR" },
501         { SubPixelNone, "None" },
502 };
503
504 /**
505  * drm_get_subpixel_order_name - return a string for a given subpixel enum
506  * @order: enum of subpixel_order
507  *
508  * Note you could abuse this and return something out of bounds, but that
509  * would be a caller error.  No unscrubbed user data should make it here.
510  */
511 const char *drm_get_subpixel_order_name(enum subpixel_order order)
512 {
513         return drm_subpixel_enum_list[order].name;
514 }
515 EXPORT_SYMBOL(drm_get_subpixel_order_name);
516
517 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
518         { DRM_MODE_DPMS_ON, "On" },
519         { DRM_MODE_DPMS_STANDBY, "Standby" },
520         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
521         { DRM_MODE_DPMS_OFF, "Off" }
522 };
523 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
524
525 /**
526  * drm_display_info_set_bus_formats - set the supported bus formats
527  * @info: display info to store bus formats in
528  * @formats: array containing the supported bus formats
529  * @num_formats: the number of entries in the fmts array
530  *
531  * Store the supported bus formats in display info structure.
532  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
533  * a full list of available formats.
534  */
535 int drm_display_info_set_bus_formats(struct drm_display_info *info,
536                                      const u32 *formats,
537                                      unsigned int num_formats)
538 {
539         u32 *fmts = NULL;
540
541         if (!formats && num_formats)
542                 return -EINVAL;
543
544         if (formats && num_formats) {
545                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
546                                GFP_KERNEL);
547                 if (!fmts)
548                         return -ENOMEM;
549         }
550
551         kfree(info->bus_formats);
552         info->bus_formats = fmts;
553         info->num_bus_formats = num_formats;
554
555         return 0;
556 }
557 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
558
559 /* Optional connector properties. */
560 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
561         { DRM_MODE_SCALE_NONE, "None" },
562         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
563         { DRM_MODE_SCALE_CENTER, "Center" },
564         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
565 };
566
567 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
568         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
569         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
570         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
571 };
572
573 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
574         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
575         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
576         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
577 };
578 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
579
580 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
581         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
582         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
583         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
584 };
585 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
586                  drm_dvi_i_subconnector_enum_list)
587
588 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
589         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
590         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
591         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
592         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
593         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
594 };
595 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
596
597 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
598         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
599         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
600         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
601         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
602         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
603 };
604 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
605                  drm_tv_subconnector_enum_list)
606
607 int drm_connector_create_standard_properties(struct drm_device *dev)
608 {
609         struct drm_property *prop;
610
611         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
612                                    DRM_MODE_PROP_IMMUTABLE,
613                                    "EDID", 0);
614         if (!prop)
615                 return -ENOMEM;
616         dev->mode_config.edid_property = prop;
617
618         prop = drm_property_create_enum(dev, 0,
619                                    "DPMS", drm_dpms_enum_list,
620                                    ARRAY_SIZE(drm_dpms_enum_list));
621         if (!prop)
622                 return -ENOMEM;
623         dev->mode_config.dpms_property = prop;
624
625         prop = drm_property_create(dev,
626                                    DRM_MODE_PROP_BLOB |
627                                    DRM_MODE_PROP_IMMUTABLE,
628                                    "PATH", 0);
629         if (!prop)
630                 return -ENOMEM;
631         dev->mode_config.path_property = prop;
632
633         prop = drm_property_create(dev,
634                                    DRM_MODE_PROP_BLOB |
635                                    DRM_MODE_PROP_IMMUTABLE,
636                                    "TILE", 0);
637         if (!prop)
638                 return -ENOMEM;
639         dev->mode_config.tile_property = prop;
640
641         return 0;
642 }
643
644 /**
645  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
646  * @dev: DRM device
647  *
648  * Called by a driver the first time a DVI-I connector is made.
649  */
650 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
651 {
652         struct drm_property *dvi_i_selector;
653         struct drm_property *dvi_i_subconnector;
654
655         if (dev->mode_config.dvi_i_select_subconnector_property)
656                 return 0;
657
658         dvi_i_selector =
659                 drm_property_create_enum(dev, 0,
660                                     "select subconnector",
661                                     drm_dvi_i_select_enum_list,
662                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
663         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
664
665         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
666                                     "subconnector",
667                                     drm_dvi_i_subconnector_enum_list,
668                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
669         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
670
671         return 0;
672 }
673 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
674
675 /**
676  * drm_create_tv_properties - create TV specific connector properties
677  * @dev: DRM device
678  * @num_modes: number of different TV formats (modes) supported
679  * @modes: array of pointers to strings containing name of each format
680  *
681  * Called by a driver's TV initialization routine, this function creates
682  * the TV specific connector properties for a given device.  Caller is
683  * responsible for allocating a list of format names and passing them to
684  * this routine.
685  */
686 int drm_mode_create_tv_properties(struct drm_device *dev,
687                                   unsigned int num_modes,
688                                   const char * const modes[])
689 {
690         struct drm_property *tv_selector;
691         struct drm_property *tv_subconnector;
692         unsigned int i;
693
694         if (dev->mode_config.tv_select_subconnector_property)
695                 return 0;
696
697         /*
698          * Basic connector properties
699          */
700         tv_selector = drm_property_create_enum(dev, 0,
701                                           "select subconnector",
702                                           drm_tv_select_enum_list,
703                                           ARRAY_SIZE(drm_tv_select_enum_list));
704         if (!tv_selector)
705                 goto nomem;
706
707         dev->mode_config.tv_select_subconnector_property = tv_selector;
708
709         tv_subconnector =
710                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
711                                     "subconnector",
712                                     drm_tv_subconnector_enum_list,
713                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
714         if (!tv_subconnector)
715                 goto nomem;
716         dev->mode_config.tv_subconnector_property = tv_subconnector;
717
718         /*
719          * Other, TV specific properties: margins & TV modes.
720          */
721         dev->mode_config.tv_left_margin_property =
722                 drm_property_create_range(dev, 0, "left margin", 0, 100);
723         if (!dev->mode_config.tv_left_margin_property)
724                 goto nomem;
725
726         dev->mode_config.tv_right_margin_property =
727                 drm_property_create_range(dev, 0, "right margin", 0, 100);
728         if (!dev->mode_config.tv_right_margin_property)
729                 goto nomem;
730
731         dev->mode_config.tv_top_margin_property =
732                 drm_property_create_range(dev, 0, "top margin", 0, 100);
733         if (!dev->mode_config.tv_top_margin_property)
734                 goto nomem;
735
736         dev->mode_config.tv_bottom_margin_property =
737                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
738         if (!dev->mode_config.tv_bottom_margin_property)
739                 goto nomem;
740
741         dev->mode_config.tv_mode_property =
742                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
743                                     "mode", num_modes);
744         if (!dev->mode_config.tv_mode_property)
745                 goto nomem;
746
747         for (i = 0; i < num_modes; i++)
748                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
749                                       i, modes[i]);
750
751         dev->mode_config.tv_brightness_property =
752                 drm_property_create_range(dev, 0, "brightness", 0, 100);
753         if (!dev->mode_config.tv_brightness_property)
754                 goto nomem;
755
756         dev->mode_config.tv_contrast_property =
757                 drm_property_create_range(dev, 0, "contrast", 0, 100);
758         if (!dev->mode_config.tv_contrast_property)
759                 goto nomem;
760
761         dev->mode_config.tv_flicker_reduction_property =
762                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
763         if (!dev->mode_config.tv_flicker_reduction_property)
764                 goto nomem;
765
766         dev->mode_config.tv_overscan_property =
767                 drm_property_create_range(dev, 0, "overscan", 0, 100);
768         if (!dev->mode_config.tv_overscan_property)
769                 goto nomem;
770
771         dev->mode_config.tv_saturation_property =
772                 drm_property_create_range(dev, 0, "saturation", 0, 100);
773         if (!dev->mode_config.tv_saturation_property)
774                 goto nomem;
775
776         dev->mode_config.tv_hue_property =
777                 drm_property_create_range(dev, 0, "hue", 0, 100);
778         if (!dev->mode_config.tv_hue_property)
779                 goto nomem;
780
781         return 0;
782 nomem:
783         return -ENOMEM;
784 }
785 EXPORT_SYMBOL(drm_mode_create_tv_properties);
786
787 /**
788  * drm_mode_create_scaling_mode_property - create scaling mode property
789  * @dev: DRM device
790  *
791  * Called by a driver the first time it's needed, must be attached to desired
792  * connectors.
793  */
794 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
795 {
796         struct drm_property *scaling_mode;
797
798         if (dev->mode_config.scaling_mode_property)
799                 return 0;
800
801         scaling_mode =
802                 drm_property_create_enum(dev, 0, "scaling mode",
803                                 drm_scaling_mode_enum_list,
804                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
805
806         dev->mode_config.scaling_mode_property = scaling_mode;
807
808         return 0;
809 }
810 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
811
812 /**
813  * drm_mode_create_aspect_ratio_property - create aspect ratio property
814  * @dev: DRM device
815  *
816  * Called by a driver the first time it's needed, must be attached to desired
817  * connectors.
818  *
819  * Returns:
820  * Zero on success, negative errno on failure.
821  */
822 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
823 {
824         if (dev->mode_config.aspect_ratio_property)
825                 return 0;
826
827         dev->mode_config.aspect_ratio_property =
828                 drm_property_create_enum(dev, 0, "aspect ratio",
829                                 drm_aspect_ratio_enum_list,
830                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
831
832         if (dev->mode_config.aspect_ratio_property == NULL)
833                 return -ENOMEM;
834
835         return 0;
836 }
837 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
838
839 /**
840  * drm_mode_create_suggested_offset_properties - create suggests offset properties
841  * @dev: DRM device
842  *
843  * Create the the suggested x/y offset property for connectors.
844  */
845 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
846 {
847         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
848                 return 0;
849
850         dev->mode_config.suggested_x_property =
851                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
852
853         dev->mode_config.suggested_y_property =
854                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
855
856         if (dev->mode_config.suggested_x_property == NULL ||
857             dev->mode_config.suggested_y_property == NULL)
858                 return -ENOMEM;
859         return 0;
860 }
861 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
862
863 /**
864  * drm_mode_connector_set_path_property - set tile property on connector
865  * @connector: connector to set property on.
866  * @path: path to use for property; must not be NULL.
867  *
868  * This creates a property to expose to userspace to specify a
869  * connector path. This is mainly used for DisplayPort MST where
870  * connectors have a topology and we want to allow userspace to give
871  * them more meaningful names.
872  *
873  * Returns:
874  * Zero on success, negative errno on failure.
875  */
876 int drm_mode_connector_set_path_property(struct drm_connector *connector,
877                                          const char *path)
878 {
879         struct drm_device *dev = connector->dev;
880         int ret;
881
882         ret = drm_property_replace_global_blob(dev,
883                                                &connector->path_blob_ptr,
884                                                strlen(path) + 1,
885                                                path,
886                                                &connector->base,
887                                                dev->mode_config.path_property);
888         return ret;
889 }
890 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
891
892 /**
893  * drm_mode_connector_set_tile_property - set tile property on connector
894  * @connector: connector to set property on.
895  *
896  * This looks up the tile information for a connector, and creates a
897  * property for userspace to parse if it exists. The property is of
898  * the form of 8 integers using ':' as a separator.
899  *
900  * Returns:
901  * Zero on success, errno on failure.
902  */
903 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
904 {
905         struct drm_device *dev = connector->dev;
906         char tile[256];
907         int ret;
908
909         if (!connector->has_tile) {
910                 ret  = drm_property_replace_global_blob(dev,
911                                                         &connector->tile_blob_ptr,
912                                                         0,
913                                                         NULL,
914                                                         &connector->base,
915                                                         dev->mode_config.tile_property);
916                 return ret;
917         }
918
919         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
920                  connector->tile_group->id, connector->tile_is_single_monitor,
921                  connector->num_h_tile, connector->num_v_tile,
922                  connector->tile_h_loc, connector->tile_v_loc,
923                  connector->tile_h_size, connector->tile_v_size);
924
925         ret = drm_property_replace_global_blob(dev,
926                                                &connector->tile_blob_ptr,
927                                                strlen(tile) + 1,
928                                                tile,
929                                                &connector->base,
930                                                dev->mode_config.tile_property);
931         return ret;
932 }
933 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
934
935 /**
936  * drm_mode_connector_update_edid_property - update the edid property of a connector
937  * @connector: drm connector
938  * @edid: new value of the edid property
939  *
940  * This function creates a new blob modeset object and assigns its id to the
941  * connector's edid property.
942  *
943  * Returns:
944  * Zero on success, negative errno on failure.
945  */
946 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
947                                             const struct edid *edid)
948 {
949         struct drm_device *dev = connector->dev;
950         size_t size = 0;
951         int ret;
952
953         /* ignore requests to set edid when overridden */
954         if (connector->override_edid)
955                 return 0;
956
957         if (edid)
958                 size = EDID_LENGTH * (1 + edid->extensions);
959
960         ret = drm_property_replace_global_blob(dev,
961                                                &connector->edid_blob_ptr,
962                                                size,
963                                                edid,
964                                                &connector->base,
965                                                dev->mode_config.edid_property);
966         return ret;
967 }
968 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
969
970 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
971                                     struct drm_property *property,
972                                     uint64_t value)
973 {
974         int ret = -EINVAL;
975         struct drm_connector *connector = obj_to_connector(obj);
976
977         /* Do DPMS ourselves */
978         if (property == connector->dev->mode_config.dpms_property) {
979                 ret = (*connector->funcs->dpms)(connector, (int)value);
980         } else if (connector->funcs->set_property)
981                 ret = connector->funcs->set_property(connector, property, value);
982
983         /* store the property value if successful */
984         if (!ret)
985                 drm_object_property_set_value(&connector->base, property, value);
986         return ret;
987 }
988
989 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
990                                        void *data, struct drm_file *file_priv)
991 {
992         struct drm_mode_connector_set_property *conn_set_prop = data;
993         struct drm_mode_obj_set_property obj_set_prop = {
994                 .value = conn_set_prop->value,
995                 .prop_id = conn_set_prop->prop_id,
996                 .obj_id = conn_set_prop->connector_id,
997                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
998         };
999
1000         /* It does all the locking and checking we need */
1001         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1002 }
1003
1004 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1005 {
1006         /* For atomic drivers only state objects are synchronously updated and
1007          * protected by modeset locks, so check those first. */
1008         if (connector->state)
1009                 return connector->state->best_encoder;
1010         return connector->encoder;
1011 }
1012
1013 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1014                                          const struct drm_file *file_priv)
1015 {
1016         /*
1017          * If user-space hasn't configured the driver to expose the stereo 3D
1018          * modes, don't expose them.
1019          */
1020         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1021                 return false;
1022
1023         return true;
1024 }
1025
1026 int drm_mode_getconnector(struct drm_device *dev, void *data,
1027                           struct drm_file *file_priv)
1028 {
1029         struct drm_mode_get_connector *out_resp = data;
1030         struct drm_connector *connector;
1031         struct drm_encoder *encoder;
1032         struct drm_display_mode *mode;
1033         int mode_count = 0;
1034         int encoders_count = 0;
1035         int ret = 0;
1036         int copied = 0;
1037         int i;
1038         struct drm_mode_modeinfo u_mode;
1039         struct drm_mode_modeinfo __user *mode_ptr;
1040         uint32_t __user *encoder_ptr;
1041
1042         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1043                 return -EINVAL;
1044
1045         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1046
1047         mutex_lock(&dev->mode_config.mutex);
1048
1049         connector = drm_connector_lookup(dev, out_resp->connector_id);
1050         if (!connector) {
1051                 ret = -ENOENT;
1052                 goto out_unlock;
1053         }
1054
1055         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1056                 if (connector->encoder_ids[i] != 0)
1057                         encoders_count++;
1058
1059         if (out_resp->count_modes == 0) {
1060                 connector->funcs->fill_modes(connector,
1061                                              dev->mode_config.max_width,
1062                                              dev->mode_config.max_height);
1063         }
1064
1065         /* delayed so we get modes regardless of pre-fill_modes state */
1066         list_for_each_entry(mode, &connector->modes, head)
1067                 if (drm_mode_expose_to_userspace(mode, file_priv))
1068                         mode_count++;
1069
1070         out_resp->connector_id = connector->base.id;
1071         out_resp->connector_type = connector->connector_type;
1072         out_resp->connector_type_id = connector->connector_type_id;
1073         out_resp->mm_width = connector->display_info.width_mm;
1074         out_resp->mm_height = connector->display_info.height_mm;
1075         out_resp->subpixel = connector->display_info.subpixel_order;
1076         out_resp->connection = connector->status;
1077
1078         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1079         encoder = drm_connector_get_encoder(connector);
1080         if (encoder)
1081                 out_resp->encoder_id = encoder->base.id;
1082         else
1083                 out_resp->encoder_id = 0;
1084
1085         /*
1086          * This ioctl is called twice, once to determine how much space is
1087          * needed, and the 2nd time to fill it.
1088          */
1089         if ((out_resp->count_modes >= mode_count) && mode_count) {
1090                 copied = 0;
1091                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1092                 list_for_each_entry(mode, &connector->modes, head) {
1093                         if (!drm_mode_expose_to_userspace(mode, file_priv))
1094                                 continue;
1095
1096                         drm_mode_convert_to_umode(&u_mode, mode);
1097                         if (copy_to_user(mode_ptr + copied,
1098                                          &u_mode, sizeof(u_mode))) {
1099                                 ret = -EFAULT;
1100                                 goto out;
1101                         }
1102                         copied++;
1103                 }
1104         }
1105         out_resp->count_modes = mode_count;
1106
1107         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1108                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1109                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1110                         &out_resp->count_props);
1111         if (ret)
1112                 goto out;
1113
1114         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1115                 copied = 0;
1116                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1117                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1118                         if (connector->encoder_ids[i] != 0) {
1119                                 if (put_user(connector->encoder_ids[i],
1120                                              encoder_ptr + copied)) {
1121                                         ret = -EFAULT;
1122                                         goto out;
1123                                 }
1124                                 copied++;
1125                         }
1126                 }
1127         }
1128         out_resp->count_encoders = encoders_count;
1129
1130 out:
1131         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1132
1133         drm_connector_unreference(connector);
1134 out_unlock:
1135         mutex_unlock(&dev->mode_config.mutex);
1136
1137         return ret;
1138 }
1139