drm: Split out drm_probe_helper.h
[linux-block.git] / drivers / gpu / drm / bochs / bochs_drv.c
... / ...
CommitLineData
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/mm.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <drm/drm_fb_helper.h>
12#include <drm/drm_probe_helper.h>
13
14#include "bochs.h"
15
16static int bochs_modeset = -1;
17module_param_named(modeset, bochs_modeset, int, 0444);
18MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
19
20/* ---------------------------------------------------------------------- */
21/* drm interface */
22
23static void bochs_unload(struct drm_device *dev)
24{
25 struct bochs_device *bochs = dev->dev_private;
26
27 bochs_kms_fini(bochs);
28 bochs_mm_fini(bochs);
29 bochs_hw_fini(dev);
30 kfree(bochs);
31 dev->dev_private = NULL;
32}
33
34static int bochs_load(struct drm_device *dev)
35{
36 struct bochs_device *bochs;
37 int ret;
38
39 bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
40 if (bochs == NULL)
41 return -ENOMEM;
42 dev->dev_private = bochs;
43 bochs->dev = dev;
44
45 ret = bochs_hw_init(dev);
46 if (ret)
47 goto err;
48
49 ret = bochs_mm_init(bochs);
50 if (ret)
51 goto err;
52
53 ret = bochs_kms_init(bochs);
54 if (ret)
55 goto err;
56
57 return 0;
58
59err:
60 bochs_unload(dev);
61 return ret;
62}
63
64static const struct file_operations bochs_fops = {
65 .owner = THIS_MODULE,
66 .open = drm_open,
67 .release = drm_release,
68 .unlocked_ioctl = drm_ioctl,
69 .compat_ioctl = drm_compat_ioctl,
70 .poll = drm_poll,
71 .read = drm_read,
72 .llseek = no_llseek,
73 .mmap = bochs_mmap,
74};
75
76static struct drm_driver bochs_driver = {
77 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
78 DRIVER_PRIME,
79 .fops = &bochs_fops,
80 .name = "bochs-drm",
81 .desc = "bochs dispi vga interface (qemu stdvga)",
82 .date = "20130925",
83 .major = 1,
84 .minor = 0,
85 .gem_free_object_unlocked = bochs_gem_free_object,
86 .dumb_create = bochs_dumb_create,
87 .dumb_map_offset = bochs_dumb_mmap_offset,
88
89 .gem_prime_export = drm_gem_prime_export,
90 .gem_prime_import = drm_gem_prime_import,
91 .gem_prime_pin = bochs_gem_prime_pin,
92 .gem_prime_unpin = bochs_gem_prime_unpin,
93 .gem_prime_vmap = bochs_gem_prime_vmap,
94 .gem_prime_vunmap = bochs_gem_prime_vunmap,
95 .gem_prime_mmap = bochs_gem_prime_mmap,
96};
97
98/* ---------------------------------------------------------------------- */
99/* pm interface */
100
101#ifdef CONFIG_PM_SLEEP
102static int bochs_pm_suspend(struct device *dev)
103{
104 struct pci_dev *pdev = to_pci_dev(dev);
105 struct drm_device *drm_dev = pci_get_drvdata(pdev);
106
107 return drm_mode_config_helper_suspend(drm_dev);
108}
109
110static int bochs_pm_resume(struct device *dev)
111{
112 struct pci_dev *pdev = to_pci_dev(dev);
113 struct drm_device *drm_dev = pci_get_drvdata(pdev);
114
115 return drm_mode_config_helper_resume(drm_dev);
116}
117#endif
118
119static const struct dev_pm_ops bochs_pm_ops = {
120 SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
121 bochs_pm_resume)
122};
123
124/* ---------------------------------------------------------------------- */
125/* pci interface */
126
127static int bochs_pci_probe(struct pci_dev *pdev,
128 const struct pci_device_id *ent)
129{
130 struct drm_device *dev;
131 unsigned long fbsize;
132 int ret;
133
134 fbsize = pci_resource_len(pdev, 0);
135 if (fbsize < 4 * 1024 * 1024) {
136 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
137 return -ENOMEM;
138 }
139
140 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "bochsdrmfb");
141 if (ret)
142 return ret;
143
144 dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
145 if (IS_ERR(dev))
146 return PTR_ERR(dev);
147
148 dev->pdev = pdev;
149 pci_set_drvdata(pdev, dev);
150
151 ret = bochs_load(dev);
152 if (ret)
153 goto err_free_dev;
154
155 ret = drm_dev_register(dev, 0);
156 if (ret)
157 goto err_unload;
158
159 drm_fbdev_generic_setup(dev, 32);
160 return ret;
161
162err_unload:
163 bochs_unload(dev);
164err_free_dev:
165 drm_dev_put(dev);
166 return ret;
167}
168
169static void bochs_pci_remove(struct pci_dev *pdev)
170{
171 struct drm_device *dev = pci_get_drvdata(pdev);
172
173 drm_dev_unregister(dev);
174 bochs_unload(dev);
175 drm_dev_put(dev);
176}
177
178static const struct pci_device_id bochs_pci_tbl[] = {
179 {
180 .vendor = 0x1234,
181 .device = 0x1111,
182 .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
183 .subdevice = PCI_SUBDEVICE_ID_QEMU,
184 .driver_data = BOCHS_QEMU_STDVGA,
185 },
186 {
187 .vendor = 0x1234,
188 .device = 0x1111,
189 .subvendor = PCI_ANY_ID,
190 .subdevice = PCI_ANY_ID,
191 .driver_data = BOCHS_UNKNOWN,
192 },
193 { /* end of list */ }
194};
195
196static struct pci_driver bochs_pci_driver = {
197 .name = "bochs-drm",
198 .id_table = bochs_pci_tbl,
199 .probe = bochs_pci_probe,
200 .remove = bochs_pci_remove,
201 .driver.pm = &bochs_pm_ops,
202};
203
204/* ---------------------------------------------------------------------- */
205/* module init/exit */
206
207static int __init bochs_init(void)
208{
209 if (vgacon_text_force() && bochs_modeset == -1)
210 return -EINVAL;
211
212 if (bochs_modeset == 0)
213 return -EINVAL;
214
215 return pci_register_driver(&bochs_pci_driver);
216}
217
218static void __exit bochs_exit(void)
219{
220 pci_unregister_driver(&bochs_pci_driver);
221}
222
223module_init(bochs_init);
224module_exit(bochs_exit);
225
226MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
227MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
228MODULE_LICENSE("GPL");