summaryrefslogtreecommitdiff
path: root/linux-core/nv04_timer.c
AgeCommit message (Expand)Author
2007-03-26nouveau: move card initialisation into the drmBen Skeggs
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
/**
 * \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.
 */

#define __NO_VERSION__
#include "drmP.h"

#define DRM_STUB_MAXCARDS 16	/* Enough for one machine */

/** Stub list. One for each minor. */
static struct drm_stub_list {
	const char             *name;
	struct file_operations *fops;	/**< file operations */
	struct proc_dir_entry  *dev_root;	/**< proc directory entry */
} *DRM(stub_list);

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

	if (!DRM(stub_list) || !DRM(stub_list)[minor].fops) return -ENODEV;
	old_fops   = filp->f_op;
	filp->f_op = fops_get(DRM(stub_list)[minor].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  = DRM(stub_open)
};

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
static int drm_hotplug (struct class_device *dev, char **envp, int num_envp,
				char *buffer, int buffer_size)
{
	struct pci_dev *pdev;
	char *scratch;
	int i = 0;
	int length = 0;
	
	DRM_DEBUG("\n");
	if (!dev)
		return -ENODEV;

	pdev = to_pci_dev(dev->dev);
	if (!pdev)
		return -ENODEV;

	scratch = buffer;

	/* stuff we want to pass to /sbin/hotplug */
	envp[i++] = scratch;
	length += snprintf (scratch, buffer_size - length, "PCI_CLASS=%04X",
							pdev->class);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;

	envp[i++] = scratch;
	length += snprintf (scratch, buffer_size - length, "PCI_ID=%04X:%04X",
							pdev->vendor, pdev->device);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;
	
	envp[i++] = scratch;
	length += snprintf (scratch, buffer_size - length,
							"PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
							pdev->subsystem_device);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;

	envp[i++] = scratch;
	length += snprintf (scratch, buffer_size - length, "PCI_SLOT_NAME=%s",
							pci_name(pdev));
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;
#if 0	
	drm_device_t *ddev;
	ddev = pci_get_drvdata(pdev);
	if (ddev) {
		envp[i++] = scratch;
		length += snprintf (scratch, buffer_size - length, 
							"RESET=%s", (ddev->need_reset ? "true" : "false"));
		if ((buffer_size - length <= 0) || (i >= num_envp))
			return -ENOMEM;
	}
#endif	
	envp[i] = 0;
	
	return 0;
}
#endif

/**
 * Get a device minor number.
 *
 * \param name driver name.
 * \param fops file operations.
 * \param dev DRM device.
 * \return minor number on success, or a negative number on failure.
 *
 * Allocate and initialize ::stub_list if one doesn't exist already.  Search an
 * empty entry and initialize it to the given parameters, and create the proc
 * init entry via proc_init().
 */
static int DRM(stub_getminor)(const char *name, struct file_operations *fops,
			      drm_device_t *dev)
{
	int i;

	if (!DRM(stub_list)) {
		DRM(stub_list) = DRM(alloc)(sizeof(*DRM(stub_list))
					    * DRM_STUB_MAXCARDS, DRM_MEM_STUB);
		if(!DRM(stub_list)) return -1;
		for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
			DRM(stub_list)[i].name = NULL;
			DRM(stub_list)[i].fops = NULL;
		}
	}
	for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
		if (!DRM(stub_list)[i].fops) {
			DRM(stub_list)[i].name = name;
			DRM(stub_list)[i].fops = fops;
			DRM(proc_init)(dev, i, DRM(stub_info).proc_root,
							&DRM(stub_list)[i].dev_root);
			(*DRM(stub_info).info_count)++;
			DRM_DEBUG("info count increased %d\n", *DRM(stub_info).info_count);
			
			return i;
		}
	}
	return -1;
}

/**
 * Put a device minor number.
 *
 * \param minor minor number.
 * \return always zero.
 *
 * Cleans up the proc resources. If a minor is zero then release the foreign
 * "drm" data, otherwise unregisters the "drm" data, frees the stub list and
 * unregisters the character device. 
 */
static int DRM(stub_putminor)(int minor)
{
	if (minor < 0 || minor >= DRM_STUB_MAXCARDS) return -1;
	DRM(stub_list)[minor].name = NULL;
	DRM(stub_list)[minor].fops = NULL;
	DRM(proc_cleanup)(minor, DRM(stub_info).proc_root,
			  DRM(stub_list)[minor].dev_root);

	(*DRM(stub_info).info_count)--;

	if ((*DRM(stub_info).info_count) != 0) {
		if (DRM(numdevs) == 0) {
			DRM_DEBUG("inter_module_put called %d\n", *DRM(stub_info).info_count);
			inter_module_put("drm");
		}
	} else {
		DRM_DEBUG("unregistering inter_module \n");
		inter_module_unregister("drm");
		DRM(free)(DRM(stub_list),
			  sizeof(*DRM(stub_list)) * DRM_STUB_MAXCARDS,
			  DRM_MEM_STUB);
		remove_proc_entry("dri", NULL);
		class_simple_destroy(DRM(stub_info).drm_class);
		unregister_chrdev(DRM_MAJOR, "drm");
	}
	return 0;
}

/**
 * Register.
 *
 * \param name driver name.
 * \param fops file operations
 * \param dev DRM device.
 * \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.
 *
 * Finally calls stub_info::info_register.
 */
int DRM(stub_register)(const char *name, struct file_operations *fops,
		       drm_device_t *dev)
{
	struct drm_stub_info *i = NULL;
	int ret1;
	int ret2;

	DRM_DEBUG("\n");

	/* if we are registering a second device we don't need to worry
	   about inter module get/put and other things as they've been
	   done already */
	if (DRM(numdevs) == 0) {
		/* use the inter_module_get to check - as if the same module
		   registers chrdev twice it succeeds */
		i = (struct drm_stub_info *)inter_module_get("drm");
		if (i) {
			/* Already registered */
			DRM(stub_info).info_register   = i->info_register;
			DRM(stub_info).info_unregister = i->info_unregister;
			DRM(stub_info).drm_class = i->drm_class;
			DRM(stub_info).info_count = i->info_count;
			DRM_DEBUG("already registered %d\n", *i->info_count);
		} else if (*DRM(stub_info).info_count == 0) {

			ret1 = register_chrdev(DRM_MAJOR, "drm", &DRM(stub_fops));
			if (ret1 < 0) {
				printk (KERN_ERR "Error registering drm major number.\n");
				return ret1;
			}

			DRM(stub_info).drm_class = class_simple_create(THIS_MODULE, "drm");
			if (IS_ERR(DRM(stub_info).drm_class)) {
				printk (KERN_ERR "Error creating drm class.\n");
				unregister_chrdev(DRM_MAJOR, "drm");
				return PTR_ERR(DRM(stub_info).drm_class);
			}
			class_simple_set_hotplug(DRM(stub_info).drm_class, drm_hotplug);

			DRM_DEBUG("calling inter_module_register\n");
			inter_module_register("drm", THIS_MODULE, &DRM(stub_info));
			
			DRM(stub_info).proc_root = create_proc_entry("dri", S_IFDIR, NULL);
			if (!DRM(stub_info).proc_root) {
				DRM_ERROR("Cannot create /proc/dri\n");
				inter_module_unregister("drm");
				unregister_chrdev(DRM_MAJOR, "drm");
				class_simple_destroy(DRM(stub_info).drm_class);
				return -1;
			}
		
		}
	} else
		DRM_DEBUG("already retrieved inter_module information\n");

	if (DRM(stub_info).info_register) {
		ret2 = DRM(stub_info).info_register(name, fops, dev);
		if (ret2 < 0) {
			if (DRM(numdevs) == 0 && !i) {
				inter_module_unregister("drm");
				unregister_chrdev(DRM_MAJOR, "drm");
				class_simple_destroy(DRM(stub_info).drm_class);
				remove_proc_entry("dri", NULL);
				DRM_DEBUG("info_register failed, deregistered everything\n");
			}
			DRM_DEBUG("info_register failed\n");
			return ret2;
		}
		class_simple_device_add(DRM(stub_info).drm_class, 
				MKDEV(DRM_MAJOR, ret2), &dev->pdev->dev, "card%d", ret2);
		return ret2;
	}
	return -1;
}

/**
 * Unregister.
 *
 * \param minor
 *
 * Calls drm_stub_info::unregister.
 */
int DRM(stub_unregister)(int minor)
{
	DRM_DEBUG("%d\n", minor);
	class_simple_device_remove(MKDEV(DRM_MAJOR, minor));
	if (DRM(stub_info).info_unregister)
		return DRM(stub_info).info_unregister(minor);
	return -1;
}

int DRM(stub_count);

/** Stub information */
struct drm_stub_info DRM(stub_info) = {
	.info_register   = DRM(stub_getminor),
	.info_unregister = DRM(stub_putminor),
	.drm_class = NULL,
	.info_count = &DRM(stub_count),
	.proc_root = NULL,
};