]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/vkms/vkms_drv.c
bd9d4b2389bd9ee085b08888dc084aac37867e6e
[linux.git] / drivers / gpu / drm / vkms / vkms_drv.c
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/drm_gem.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/drm_fb_helper.h>
14 #include "vkms_drv.h"
15
16 #define DRIVER_NAME     "vkms"
17 #define DRIVER_DESC     "Virtual Kernel Mode Setting"
18 #define DRIVER_DATE     "20180514"
19 #define DRIVER_MAJOR    1
20 #define DRIVER_MINOR    0
21
22 static struct vkms_device *vkms_device;
23
24 static const struct file_operations vkms_driver_fops = {
25         .owner          = THIS_MODULE,
26         .open           = drm_open,
27         .mmap           = drm_gem_mmap,
28         .unlocked_ioctl = drm_ioctl,
29         .compat_ioctl   = drm_compat_ioctl,
30         .poll           = drm_poll,
31         .read           = drm_read,
32         .llseek         = no_llseek,
33         .release        = drm_release,
34 };
35
36 static const struct vm_operations_struct vkms_gem_vm_ops = {
37         .fault = vkms_gem_fault,
38         .open = drm_gem_vm_open,
39         .close = drm_gem_vm_close,
40 };
41
42 static void vkms_release(struct drm_device *dev)
43 {
44         struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
45
46         platform_device_unregister(vkms->platform);
47         drm_atomic_helper_shutdown(&vkms->drm);
48         drm_mode_config_cleanup(&vkms->drm);
49         drm_dev_fini(&vkms->drm);
50         destroy_workqueue(vkms->output.crc_workq);
51 }
52
53 static struct drm_driver vkms_driver = {
54         .driver_features        = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
55         .release                = vkms_release,
56         .fops                   = &vkms_driver_fops,
57         .dumb_create            = vkms_dumb_create,
58         .dumb_map_offset        = vkms_dumb_map,
59         .gem_vm_ops             = &vkms_gem_vm_ops,
60         .gem_free_object_unlocked = vkms_gem_free_object,
61         .get_vblank_timestamp   = vkms_get_vblank_timestamp,
62
63         .name                   = DRIVER_NAME,
64         .desc                   = DRIVER_DESC,
65         .date                   = DRIVER_DATE,
66         .major                  = DRIVER_MAJOR,
67         .minor                  = DRIVER_MINOR,
68 };
69
70 static const struct drm_mode_config_funcs vkms_mode_funcs = {
71         .fb_create = drm_gem_fb_create,
72         .atomic_check = drm_atomic_helper_check,
73         .atomic_commit = drm_atomic_helper_commit,
74 };
75
76 static int vkms_modeset_init(struct vkms_device *vkmsdev)
77 {
78         struct drm_device *dev = &vkmsdev->drm;
79
80         drm_mode_config_init(dev);
81         dev->mode_config.funcs = &vkms_mode_funcs;
82         dev->mode_config.min_width = XRES_MIN;
83         dev->mode_config.min_height = YRES_MIN;
84         dev->mode_config.max_width = XRES_MAX;
85         dev->mode_config.max_height = YRES_MAX;
86
87         return vkms_output_init(vkmsdev);
88 }
89
90 static int __init vkms_init(void)
91 {
92         int ret;
93
94         vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL);
95         if (!vkms_device)
96                 return -ENOMEM;
97
98         ret = drm_dev_init(&vkms_device->drm, &vkms_driver, NULL);
99         if (ret)
100                 goto out_free;
101
102         vkms_device->platform =
103                 platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
104         if (IS_ERR(vkms_device->platform)) {
105                 ret = PTR_ERR(vkms_device->platform);
106                 goto out_fini;
107         }
108
109         vkms_device->drm.irq_enabled = true;
110
111         ret = drm_vblank_init(&vkms_device->drm, 1);
112         if (ret) {
113                 DRM_ERROR("Failed to vblank\n");
114                 goto out_fini;
115         }
116
117         ret = vkms_modeset_init(vkms_device);
118         if (ret)
119                 goto out_unregister;
120
121         ret = drm_dev_register(&vkms_device->drm, 0);
122         if (ret)
123                 goto out_unregister;
124
125         return 0;
126
127 out_unregister:
128         platform_device_unregister(vkms_device->platform);
129
130 out_fini:
131         drm_dev_fini(&vkms_device->drm);
132
133 out_free:
134         kfree(vkms_device);
135         return ret;
136 }
137
138 static void __exit vkms_exit(void)
139 {
140         if (!vkms_device) {
141                 DRM_INFO("vkms_device is NULL.\n");
142                 return;
143         }
144
145         drm_dev_unregister(&vkms_device->drm);
146         drm_dev_put(&vkms_device->drm);
147
148         kfree(vkms_device);
149 }
150
151 module_init(vkms_init);
152 module_exit(vkms_exit);
153
154 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
155 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
156 MODULE_DESCRIPTION(DRIVER_DESC);
157 MODULE_LICENSE("GPL");