|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation; either version 2 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/module.h> |
| 9 | +#include <drm/drmP.h> |
| 10 | +#include <drm/drm_gem.h> |
| 11 | +#include <drm/drm_crtc_helper.h> |
| 12 | +#include "vkms_drv.h" |
| 13 | + |
| 14 | +#define DRIVER_NAME "vkms" |
| 15 | +#define DRIVER_DESC "Virtual Kernel Mode Setting" |
| 16 | +#define DRIVER_DATE "20180514" |
| 17 | +#define DRIVER_MAJOR 1 |
| 18 | +#define DRIVER_MINOR 0 |
| 19 | + |
| 20 | +static struct vkms_device *vkms_device; |
| 21 | + |
| 22 | +static const struct file_operations vkms_driver_fops = { |
| 23 | + .owner = THIS_MODULE, |
| 24 | + .open = drm_open, |
| 25 | + .mmap = drm_gem_mmap, |
| 26 | + .unlocked_ioctl = drm_ioctl, |
| 27 | + .compat_ioctl = drm_compat_ioctl, |
| 28 | + .poll = drm_poll, |
| 29 | + .read = drm_read, |
| 30 | + .llseek = no_llseek, |
| 31 | + .release = drm_release, |
| 32 | +}; |
| 33 | + |
| 34 | +static void vkms_release(struct drm_device *dev) |
| 35 | +{ |
| 36 | + struct vkms_device *vkms = container_of(dev, struct vkms_device, drm); |
| 37 | + |
| 38 | + platform_device_unregister(vkms->platform); |
| 39 | + drm_mode_config_cleanup(&vkms->drm); |
| 40 | + drm_dev_fini(&vkms->drm); |
| 41 | +} |
| 42 | + |
| 43 | +struct drm_driver vkms_driver = { |
| 44 | + .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM, |
| 45 | + .release = vkms_release, |
| 46 | + .fops = &vkms_driver_fops, |
| 47 | + |
| 48 | + .name = DRIVER_NAME, |
| 49 | + .desc = DRIVER_DESC, |
| 50 | + .date = DRIVER_DATE, |
| 51 | + .major = DRIVER_MAJOR, |
| 52 | + .minor = DRIVER_MINOR, |
| 53 | +}; |
| 54 | + |
| 55 | +static const u32 vkms_formats[] = { |
| 56 | + DRM_FORMAT_XRGB8888, |
| 57 | +}; |
| 58 | + |
| 59 | +static void vkms_connector_destroy(struct drm_connector *connector) |
| 60 | +{ |
| 61 | + drm_connector_unregister(connector); |
| 62 | + drm_connector_cleanup(connector); |
| 63 | +} |
| 64 | + |
| 65 | +static const struct drm_connector_funcs vkms_connector_funcs = { |
| 66 | + .fill_modes = drm_helper_probe_single_connector_modes, |
| 67 | + .destroy = vkms_connector_destroy, |
| 68 | +}; |
| 69 | + |
| 70 | +static int __init vkms_init(void) |
| 71 | +{ |
| 72 | + int ret; |
| 73 | + |
| 74 | + vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL); |
| 75 | + if (!vkms_device) |
| 76 | + return -ENOMEM; |
| 77 | + |
| 78 | + ret = drm_dev_init(&vkms_device->drm, &vkms_driver, NULL); |
| 79 | + if (ret) |
| 80 | + goto out_free; |
| 81 | + |
| 82 | + vkms_device->platform = |
| 83 | + platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
| 84 | + if (IS_ERR(vkms_device->platform)) { |
| 85 | + ret = PTR_ERR(vkms_device->platform); |
| 86 | + goto out_fini; |
| 87 | + } |
| 88 | + |
| 89 | + drm_mode_config_init(&vkms_device->drm); |
| 90 | + |
| 91 | + ret = drm_connector_init(&vkms_device->drm, &vkms_device->connector, |
| 92 | + &vkms_connector_funcs, |
| 93 | + DRM_MODE_CONNECTOR_VIRTUAL); |
| 94 | + if (ret < 0) { |
| 95 | + DRM_ERROR("Failed to init connector\n"); |
| 96 | + goto out_unregister; |
| 97 | + } |
| 98 | + |
| 99 | + ret = drm_simple_display_pipe_init(&vkms_device->drm, |
| 100 | + &vkms_device->pipe, |
| 101 | + NULL, |
| 102 | + vkms_formats, |
| 103 | + ARRAY_SIZE(vkms_formats), |
| 104 | + NULL, |
| 105 | + &vkms_device->connector); |
| 106 | + if (ret < 0) { |
| 107 | + DRM_ERROR("Cannot setup simple display pipe\n"); |
| 108 | + goto out_unregister; |
| 109 | + } |
| 110 | + |
| 111 | + ret = drm_dev_register(&vkms_device->drm, 0); |
| 112 | + if (ret) |
| 113 | + goto out_unregister; |
| 114 | + |
| 115 | + drm_connector_register(&vkms_device->connector); |
| 116 | + |
| 117 | + return 0; |
| 118 | + |
| 119 | +out_unregister: |
| 120 | + platform_device_unregister(vkms_device->platform); |
| 121 | +out_fini: |
| 122 | + drm_dev_fini(&vkms_device->drm); |
| 123 | +out_free: |
| 124 | + kfree(vkms_device); |
| 125 | + |
| 126 | + return ret; |
| 127 | +} |
| 128 | + |
| 129 | +static void __exit vkms_exit(void) |
| 130 | +{ |
| 131 | + if (!vkms_device) { |
| 132 | + DRM_INFO("vkms_device is NULL.\n"); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + drm_dev_unregister(&vkms_device->drm); |
| 137 | + drm_dev_put(&vkms_device->drm); |
| 138 | + |
| 139 | + kfree(vkms_device); |
| 140 | +} |
| 141 | + |
| 142 | +module_init(vkms_init); |
| 143 | +module_exit(vkms_exit); |
| 144 | + |
| 145 | +MODULE_DESCRIPTION(DRIVER_DESC); |
| 146 | +MODULE_LICENSE("GPL"); |
0 commit comments