Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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
 13#include "bochs.h"
 14
 15static bool enable_fbdev = true;
 16module_param_named(fbdev, enable_fbdev, bool, 0444);
 17MODULE_PARM_DESC(fbdev, "register fbdev device");
 18
 19/* ---------------------------------------------------------------------- */
 20/* drm interface                                                          */
 21
 22static int bochs_unload(struct drm_device *dev)
 23{
 24	struct bochs_device *bochs = dev->dev_private;
 25
 26	bochs_fbdev_fini(bochs);
 27	bochs_kms_fini(bochs);
 28	bochs_mm_fini(bochs);
 29	bochs_hw_fini(dev);
 30	kfree(bochs);
 31	dev->dev_private = NULL;
 32	return 0;
 33}
 34
 35static int bochs_load(struct drm_device *dev, unsigned long flags)
 36{
 37	struct bochs_device *bochs;
 38	int ret;
 39
 40	bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
 41	if (bochs == NULL)
 42		return -ENOMEM;
 43	dev->dev_private = bochs;
 44	bochs->dev = dev;
 45
 46	ret = bochs_hw_init(dev, flags);
 47	if (ret)
 48		goto err;
 49
 50	ret = bochs_mm_init(bochs);
 51	if (ret)
 52		goto err;
 53
 54	ret = bochs_kms_init(bochs);
 55	if (ret)
 56		goto err;
 57
 58	if (enable_fbdev)
 59		bochs_fbdev_init(bochs);
 60
 61	return 0;
 62
 63err:
 64	bochs_unload(dev);
 65	return ret;
 66}
 67
 68static const struct file_operations bochs_fops = {
 69	.owner		= THIS_MODULE,
 70	.open		= drm_open,
 71	.release	= drm_release,
 72	.unlocked_ioctl	= drm_ioctl,
 73	.compat_ioctl	= drm_compat_ioctl,
 74	.poll		= drm_poll,
 75	.read		= drm_read,
 76	.llseek		= no_llseek,
 77	.mmap           = bochs_mmap,
 78};
 79
 80static struct drm_driver bochs_driver = {
 81	.driver_features	= DRIVER_GEM | DRIVER_MODESET,
 82	.load			= bochs_load,
 83	.unload			= bochs_unload,
 84	.set_busid		= drm_pci_set_busid,
 85	.fops			= &bochs_fops,
 86	.name			= "bochs-drm",
 87	.desc			= "bochs dispi vga interface (qemu stdvga)",
 88	.date			= "20130925",
 89	.major			= 1,
 90	.minor			= 0,
 91	.gem_free_object_unlocked = bochs_gem_free_object,
 92	.dumb_create            = bochs_dumb_create,
 93	.dumb_map_offset        = bochs_dumb_mmap_offset,
 94	.dumb_destroy           = drm_gem_dumb_destroy,
 95};
 96
 97/* ---------------------------------------------------------------------- */
 98/* pm interface                                                           */
 99
100#ifdef CONFIG_PM_SLEEP
101static int bochs_pm_suspend(struct device *dev)
102{
103	struct pci_dev *pdev = to_pci_dev(dev);
104	struct drm_device *drm_dev = pci_get_drvdata(pdev);
105	struct bochs_device *bochs = drm_dev->dev_private;
106
107	drm_kms_helper_poll_disable(drm_dev);
108
109	if (bochs->fb.initialized) {
110		console_lock();
111		drm_fb_helper_set_suspend(&bochs->fb.helper, 1);
112		console_unlock();
113	}
114
115	return 0;
116}
117
118static int bochs_pm_resume(struct device *dev)
119{
120	struct pci_dev *pdev = to_pci_dev(dev);
121	struct drm_device *drm_dev = pci_get_drvdata(pdev);
122	struct bochs_device *bochs = drm_dev->dev_private;
123
124	drm_helper_resume_force_mode(drm_dev);
125
126	if (bochs->fb.initialized) {
127		console_lock();
128		drm_fb_helper_set_suspend(&bochs->fb.helper, 0);
129		console_unlock();
130	}
131
132	drm_kms_helper_poll_enable(drm_dev);
133	return 0;
134}
135#endif
136
137static const struct dev_pm_ops bochs_pm_ops = {
138	SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
139				bochs_pm_resume)
140};
141
142/* ---------------------------------------------------------------------- */
143/* pci interface                                                          */
144
145static int bochs_kick_out_firmware_fb(struct pci_dev *pdev)
146{
147	struct apertures_struct *ap;
148
149	ap = alloc_apertures(1);
150	if (!ap)
151		return -ENOMEM;
152
153	ap->ranges[0].base = pci_resource_start(pdev, 0);
154	ap->ranges[0].size = pci_resource_len(pdev, 0);
155	drm_fb_helper_remove_conflicting_framebuffers(ap, "bochsdrmfb", false);
156	kfree(ap);
157
158	return 0;
159}
160
161static int bochs_pci_probe(struct pci_dev *pdev,
162			   const struct pci_device_id *ent)
163{
164	unsigned long fbsize;
165	int ret;
166
167	fbsize = pci_resource_len(pdev, 0);
168	if (fbsize < 4 * 1024 * 1024) {
169		DRM_ERROR("less than 4 MB video memory, ignoring device\n");
170		return -ENOMEM;
171	}
172
173	ret = bochs_kick_out_firmware_fb(pdev);
174	if (ret)
175		return ret;
176
177	return drm_get_pci_dev(pdev, ent, &bochs_driver);
178}
179
180static void bochs_pci_remove(struct pci_dev *pdev)
181{
182	struct drm_device *dev = pci_get_drvdata(pdev);
183
184	drm_put_dev(dev);
185}
186
187static const struct pci_device_id bochs_pci_tbl[] = {
188	{
189		.vendor      = 0x1234,
190		.device      = 0x1111,
191		.subvendor   = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
192		.subdevice   = PCI_SUBDEVICE_ID_QEMU,
193		.driver_data = BOCHS_QEMU_STDVGA,
194	},
195	{
196		.vendor      = 0x1234,
197		.device      = 0x1111,
198		.subvendor   = PCI_ANY_ID,
199		.subdevice   = PCI_ANY_ID,
200		.driver_data = BOCHS_UNKNOWN,
201	},
202	{ /* end of list */ }
203};
204
205static struct pci_driver bochs_pci_driver = {
206	.name =		"bochs-drm",
207	.id_table =	bochs_pci_tbl,
208	.probe =	bochs_pci_probe,
209	.remove =	bochs_pci_remove,
210	.driver.pm =    &bochs_pm_ops,
211};
212
213/* ---------------------------------------------------------------------- */
214/* module init/exit                                                       */
215
216static int __init bochs_init(void)
217{
218	return drm_pci_init(&bochs_driver, &bochs_pci_driver);
219}
220
221static void __exit bochs_exit(void)
222{
223	drm_pci_exit(&bochs_driver, &bochs_pci_driver);
224}
225
226module_init(bochs_init);
227module_exit(bochs_exit);
228
229MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
230MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
231MODULE_LICENSE("GPL");