vkms_output.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. */
  8. #include "vkms_drv.h"
  9. #include <drm/drm_crtc_helper.h>
  10. #include <drm/drm_atomic_helper.h>
  11. static void vkms_connector_destroy(struct drm_connector *connector)
  12. {
  13. drm_connector_unregister(connector);
  14. drm_connector_cleanup(connector);
  15. }
  16. static const struct drm_connector_funcs vkms_connector_funcs = {
  17. .fill_modes = drm_helper_probe_single_connector_modes,
  18. .destroy = vkms_connector_destroy,
  19. .reset = drm_atomic_helper_connector_reset,
  20. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  21. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  22. };
  23. static const struct drm_encoder_funcs vkms_encoder_funcs = {
  24. .destroy = drm_encoder_cleanup,
  25. };
  26. static int vkms_conn_get_modes(struct drm_connector *connector)
  27. {
  28. int count;
  29. count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
  30. drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
  31. return count;
  32. }
  33. static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
  34. .get_modes = vkms_conn_get_modes,
  35. };
  36. int vkms_output_init(struct vkms_device *vkmsdev)
  37. {
  38. struct vkms_output *output = &vkmsdev->output;
  39. struct drm_device *dev = &vkmsdev->drm;
  40. struct drm_connector *connector = &output->connector;
  41. struct drm_encoder *encoder = &output->encoder;
  42. struct drm_crtc *crtc = &output->crtc;
  43. struct drm_plane *primary, *cursor = NULL;
  44. int ret;
  45. primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY);
  46. if (IS_ERR(primary))
  47. return PTR_ERR(primary);
  48. if (enable_cursor) {
  49. cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR);
  50. if (IS_ERR(cursor)) {
  51. ret = PTR_ERR(cursor);
  52. goto err_cursor;
  53. }
  54. }
  55. ret = vkms_crtc_init(dev, crtc, primary, cursor);
  56. if (ret)
  57. goto err_crtc;
  58. ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
  59. DRM_MODE_CONNECTOR_VIRTUAL);
  60. if (ret) {
  61. DRM_ERROR("Failed to init connector\n");
  62. goto err_connector;
  63. }
  64. drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
  65. ret = drm_connector_register(connector);
  66. if (ret) {
  67. DRM_ERROR("Failed to register connector\n");
  68. goto err_connector_register;
  69. }
  70. ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
  71. DRM_MODE_ENCODER_VIRTUAL, NULL);
  72. if (ret) {
  73. DRM_ERROR("Failed to init encoder\n");
  74. goto err_encoder;
  75. }
  76. encoder->possible_crtcs = 1;
  77. ret = drm_connector_attach_encoder(connector, encoder);
  78. if (ret) {
  79. DRM_ERROR("Failed to attach connector to encoder\n");
  80. goto err_attach;
  81. }
  82. drm_mode_config_reset(dev);
  83. return 0;
  84. err_attach:
  85. drm_encoder_cleanup(encoder);
  86. err_encoder:
  87. drm_connector_unregister(connector);
  88. err_connector_register:
  89. drm_connector_cleanup(connector);
  90. err_connector:
  91. drm_crtc_cleanup(crtc);
  92. err_crtc:
  93. if (enable_cursor)
  94. drm_plane_cleanup(cursor);
  95. err_cursor:
  96. drm_plane_cleanup(primary);
  97. return ret;
  98. }