/* mach64_drv.c -- ATI Rage 128 driver -*- linux-c -*- * Created: Mon Dec 13 09:47:27 1999 by faith@precisioninsight.com */ /*- * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Authors: * Rickard E. (Rik) Faith * Gareth Hughes */ #include #include "drmP.h" #include "drm.h" #include "mach64_drm.h" #include "mach64_drv.h" #include "drm_pciids.h" /* drv_PCI_IDs comes from drm_pciids.h, generated from drm_pciids.txt. */ static drm_pci_id_list_t mach64_pciidlist[] = { mach64_PCI_IDS }; static void mach64_configure(drm_device_t *dev) { dev->driver.buf_priv_size = 1; /* No dev_priv */ dev->driver.lastclose = mach64_driver_lastclose; dev->driver.vblank_wait = mach64_driver_vblank_wait; dev->driver.irq_preinstall = mach64_driver_irq_preinstall; dev->driver.irq_postinstall = mach64_driver_irq_postinstall; dev->driver.irq_uninstall = mach64_driver_irq_uninstall; dev->driver.irq_handler = mach64_driver_irq_handler; dev->driver.dma_ioctl = mach64_dma_buffers; dev->driver.ioctls = mach64_ioctls; dev->driver.max_ioctl = mach64_max_ioctl; dev->driver.name = DRIVER_NAME; dev->driver.desc = DRIVER_DESC; dev->driver.date = DRIVER_DATE; dev->driver.major = DRIVER_MAJOR; dev->driver.minor = DRIVER_MINOR; dev->driver.patchlevel = DRIVER_PATCHLEVEL; dev->driver.use_agp = 1; dev->driver.use_mtrr = 1; dev->driver.use_pci_dma = 1; dev->driver.use_dma = 1; dev->driver.use_irq = 1; dev->driver.use_vbl_irq = 1; } #ifdef __FreeBSD__ static int mach64_probe(device_t dev) { return drm_probe(dev, mach64_pciidlist); } static int mach64_attach(device_t nbdev) { drm_device_t *dev = device_get_softc(nbdev); bzero(dev, sizeof(drm_device_t)); mach64_configure(dev); return drm_attach(nbdev, mach64_pciidlist); } static device_method_t mach64_methods[] = { /* Device interface */ DEVMETHOD(device_probe, mach64_probe), DEVMETHOD(device_attach, mach64_attach), DEVMETHOD(device_detach, drm_detach), { 0, 0 } }; static driver_t mach64_driver = { "drm", mach64_methods, sizeof(drm_device_t) }; extern devclass_t drm_devclass; #if __FreeBSD_version >= 700010 DRIVER_MODULE(mach64, vgapci, mach64_driver, drm_devclass, 0, 0); #else DRIVER_MODULE(mach64, pci, mach64_driver, drm_devclass, 0, 0); #endif MODULE_DEPEND(mach64, drm, 1, 1, 1); #elif defined(__NetBSD__) || defined(__OpenBSD__) CFDRIVER_DECL(mach64, DV_TTY, NULL); #endif ='#n30'>30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/**
 * \file drm_stub.h
 * Stub support
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 */

/*
 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
 *
 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "drmP.h"

static unsigned int cards_limit = 16;	/* Enough for one machine */
static unsigned int debug = 0;		/* 1 to enable debug output */

MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL and additional rights");
MODULE_PARM_DESC(cards_limit, "Maximum number of graphics cards");
MODULE_PARM_DESC(debug, "Enable debug output");

module_param(cards_limit, int, 0444);
module_param(debug, int, 0666);

drm_global_t *DRM(global);

/**
 * File \c open operation.
 *
 * \param inode device inode.
 * \param filp file pointer.
 *
 * Puts the dev->fops corresponding to the device minor number into
 * \p filp, call the \c open method, and restore the file operations.
 */
static int stub_open(struct inode *inode, struct file *filp)
{
	drm_device_t *dev = NULL;
	int minor = iminor(inode);
	int err = -ENODEV;
	struct file_operations *old_fops;
	
	DRM_DEBUG("\n");

	if (!((minor >= 0) && (minor < DRM(global)->cards_limit)))
		return -ENODEV;

	dev = DRM(global)->minors[minor].dev;
	if (!dev)
		return -ENODEV;

	old_fops = filp->f_op;
	filp->f_op = fops_get(dev->fops);
	if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
		fops_put(filp->f_op);
		filp->f_op = fops_get(old_fops);
	}
	fops_put(old_fops);

	return err;
}

/** File operations structure */
static struct file_operations DRM(stub_fops) = {
	.owner = THIS_MODULE,
	.open  = stub_open
};


/**
 * Get a device minor number.
 *
 * \param pdev PCI device structure
 * \param ent entry from the PCI ID table with device type flags
 * \return negative number on failure.
 *
 * Search an empty entry and initialize it to the given parameters, and 
 * create the proc init entry via proc_init().
 */
static int get_minor(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct class_device *dev_class;
	drm_device_t *dev;
	int ret;
	int minor;
	drm_minor_t *minors = &DRM(global)->minors[0];

	DRM_DEBUG("\n");

	for (minor = 0; minor < DRM(global)->cards_limit; minor++, minors++) {
		if (minors->class == DRM_MINOR_FREE) {

			DRM_DEBUG("assigning minor %d\n", minor);
			dev = DRM(calloc)(1, sizeof(*dev), DRM_MEM_STUB);
			if(!dev) 
				return -ENOMEM;

			*minors = (drm_minor_t){.dev = dev, .class = DRM_MINOR_PRIMARY};
			dev->minor = minor;
			if ((ret = DRM(fill_in_dev)(dev, pdev, ent))) {
				printk (KERN_ERR "DRM: Fill_in_dev failed.\n");
				goto err_g1;
			}
			if ((ret = DRM(proc_init)(dev, minor, DRM(global)->proc_root, &minors->dev_root))) {
				printk (KERN_ERR "DRM: Failed to initialize /proc/dri.\n");
				goto err_g1;
			}
			if (!DRM(fb_loaded)) {
				pci_set_drvdata(pdev, dev);
				pci_request_regions(pdev, DRIVER_NAME);
				pci_enable_device(pdev);
			}
			dev_class = DRM(sysfs_device_add)(DRM(global)->drm_class, 
					MKDEV(DRM_MAJOR, minor), DRM_PCI_DEV(pdev), "card%d", minor);
			if (IS_ERR(dev_class)) {
				printk (KERN_ERR "DRM: Error sysfs_device_add.\n");
				ret = PTR_ERR(dev_class);
				goto err_g2;
			}

			DRM_DEBUG("new primary minor assigned %d\n", minor);
			return 0;
		}
	}
	DRM_ERROR("out of minors\n");
	return -ENOMEM;
err_g2:
	if (!DRM(fb_loaded)) {
		pci_set_drvdata(pdev, NULL);
		pci_release_regions(pdev);
		pci_disable_device(pdev);
	}
	DRM(proc_cleanup)(minor, DRM(global)->proc_root, minors->dev_root);
err_g1:
	*minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};
	DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB);
	return ret;
}

/**
 * Get a secondary minor number.
 *
 * \param dev device data structure
 * \param sec-minor structure to hold the assigned minor
 * \return negative number on failure.
 *
 * Search an empty entry and initialize it to the given parameters, and 
 * create the proc init entry via proc_init(). This routines assigns
 * minor numbers to secondary heads of multi-headed cards
 */
int DRM(get_secondary_minor)(drm_device_t *dev, drm_minor_t **sec_minor)
{
	drm_minor_t *minors = &DRM(global)->minors[0];
	struct class_device *dev_class;
	int ret;
	int minor;

	DRM_DEBUG("\n");

	for (minor = 0; minor < DRM(global)->cards_limit; minor++, minors++) {
		if (minors->class == DRM_MINOR_FREE) {

			*minors = (drm_minor_t){.dev = dev, .class = DRM_MINOR_SECONDARY};
			if ((ret = DRM(proc_init)(dev, minor, DRM(global)->proc_root, &minors->dev_root))) {
				printk (KERN_ERR "DRM: Failed to initialize /proc/dri.\n");
				goto err_g1;
			}

			dev_class = DRM(sysfs_device_add)(DRM(global)->drm_class, 
					MKDEV(DRM_MAJOR, minor), DRM_PCI_DEV(dev->pdev), "card%d", minor);
			if (IS_ERR(dev_class)) {
				printk (KERN_ERR "DRM: Error sysfs_device_add.\n");
				ret = PTR_ERR(dev_class);
				goto err_g2;
			}
			*sec_minor = minors;

			DRM_DEBUG("new secondary minor assigned %d\n", minor);
			return 0;
		}
	}
	DRM_ERROR("out of minors\n");
	return -ENOMEM;
err_g2:
	DRM(proc_cleanup)(minor, DRM(global)->proc_root, minors->dev_root);
err_g1:
	*minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};
	DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB);
	return ret;
}

/**
 * Put a device minor number.
 *
 * \param dev device data structure
 * \return always zero
 *
 * Cleans up the proc resources. If it is the last minor then release the foreign
 * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
 * unregisters the character device. 
 */
int DRM(put_minor)(drm_device_t *dev)
{
	drm_minor_t *minors = &DRM(global)->minors[dev->minor];
	int i;
	
	DRM_DEBUG("release primary minor %d\n", dev->minor);

	DRM(proc_cleanup)(dev->minor, DRM(global)->proc_root, minors->dev_root);
	DRM(sysfs_device_remove)(MKDEV(DRM_MAJOR, dev->minor));

	*minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};
	DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB);

	/* if any device pointers are non-NULL we are not the last module */
	for (i = 0; i < DRM(global)->cards_limit; i++) {
		if (DRM(global)->minors[i].class != DRM_MINOR_FREE) {
			DRM_DEBUG("inter_module_put called\n");
			inter_module_put("drm");
			return 0;
		}
	}
	DRM_DEBUG("unregistering inter_module \n");
	inter_module_unregister("drm");
	remove_proc_entry("dri", NULL);
	DRM(sysfs_destroy)(DRM(global)->drm_class);

	unregister_chrdev(DRM_MAJOR, "drm");

	DRM(free)(DRM(global)->minors, sizeof(*DRM(global)->minors) *
				DRM(global)->cards_limit, DRM_MEM_STUB);
	DRM(free)(DRM(global), sizeof(*DRM(global)), DRM_MEM_STUB);
	DRM(global) = NULL;

	return 0;
}

/**
 * Put a secondary minor number.
 *
 * \param sec_minor - structure to be released
 * \return always zero
 *
 * Cleans up the proc resources. Not legal for this to be the
 * last minor released.
 * 
 */
int DRM(put_secondary_minor)(drm_minor_t *sec_minor)
{
	int minor = sec_minor - &DRM(global)->minors[0];

	DRM_DEBUG("release secondary minor %d\n", minor);

	DRM(proc_cleanup)(minor, DRM(global)->proc_root, sec_minor->dev_root);
	DRM(sysfs_device_remove)(MKDEV(DRM_MAJOR, minor));

	*sec_minor = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};

	return 0;
}

/**
 * Register.
 *
 * \param pdev - PCI device structure
 * \param ent entry from the PCI ID table with device type flags
 * \return zero on success or a negative number on failure.
 *
 * Attempt to gets inter module "drm" information. If we are first
 * then register the character device and inter module information.
 * Try and register, if we fail to register, backout previous work.
 */
int DRM(probe)(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	drm_global_t *global;
	int ret = -ENOMEM;

	DRM_DEBUG("\n");

	/* use the inter_module_get to check - as if the same module
		registers chrdev twice it succeeds */
	global = (drm_global_t *)inter_module_get("drm");
	if (global) {
		DRM(global) = global;
		global = NULL;
	} else {
		DRM_DEBUG("first probe\n");

		global = DRM(calloc)(1, sizeof(*global), DRM_MEM_STUB);
		if(!global) 
			return -ENOMEM;

		global->cards_limit = (cards_limit < DRM_MAX_MINOR + 1 ? cards_limit : DRM_MAX_MINOR + 1);