2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* \file drm_stub.h
|
|
|
|
* Stub support
|
|
|
|
*
|
|
|
|
* \author Rickard E. (Rik) Faith <faith@valinux.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2001-02-15 01:12:14 -07:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-02-15 22:24:06 -07:00
|
|
|
#include "drmP.h"
|
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
static unsigned int cards_limit = 16; /* Enough for one machine */
|
|
|
|
static unsigned int debug = 0; /* 1 to enable debug output */
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
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);
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* File \c open operation.
|
|
|
|
*
|
|
|
|
* \param inode device inode.
|
|
|
|
* \param filp file pointer.
|
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* Puts the dev->fops corresponding to the device minor number into
|
2003-05-26 18:37:33 -06:00
|
|
|
* \p filp, call the \c open method, and restore the file operations.
|
|
|
|
*/
|
2004-09-16 12:42:03 -06:00
|
|
|
static int stub_open(struct inode *inode, struct file *filp)
|
2001-02-15 01:12:14 -07:00
|
|
|
{
|
2004-09-16 12:42:03 -06:00
|
|
|
drm_device_t *dev = NULL;
|
|
|
|
int minor = iminor(inode);
|
|
|
|
int err = -ENODEV;
|
2001-02-15 01:12:14 -07:00
|
|
|
struct file_operations *old_fops;
|
2004-09-16 12:42:03 -06:00
|
|
|
|
|
|
|
DRM_DEBUG("\n");
|
2004-09-14 18:20:21 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
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);
|
2001-02-15 01:12:14 -07:00
|
|
|
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);
|
2001-02-15 22:24:06 -07:00
|
|
|
|
2001-02-15 01:12:14 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/** File operations structure */
|
2001-02-15 01:12:14 -07:00
|
|
|
static struct file_operations DRM(stub_fops) = {
|
2002-08-06 12:00:57 -06:00
|
|
|
.owner = THIS_MODULE,
|
2004-09-16 12:42:03 -06:00
|
|
|
.open = stub_open
|
2001-02-15 01:12:14 -07:00
|
|
|
};
|
|
|
|
|
2004-07-31 09:45:00 -06:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* Get a device minor number.
|
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* \param pdev PCI device structure
|
|
|
|
* \param ent entry from the PCI ID table with device type flags
|
|
|
|
* \return negative number on failure.
|
2003-05-26 18:37:33 -06:00
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* Search an empty entry and initialize it to the given parameters, and
|
|
|
|
* create the proc init entry via proc_init().
|
2003-05-26 18:37:33 -06:00
|
|
|
*/
|
2004-09-16 12:42:03 -06:00
|
|
|
static int get_minor(struct pci_dev *pdev, const struct pci_device_id *ent)
|
2001-02-15 01:12:14 -07:00
|
|
|
{
|
2004-09-16 12:42:03 -06:00
|
|
|
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;
|
|
|
|
}
|
2004-09-22 13:13:02 -06:00
|
|
|
if (!DRM(fb_loaded)) {
|
2004-09-16 12:42:03 -06:00
|
|
|
pci_set_drvdata(pdev, dev);
|
|
|
|
pci_request_regions(pdev, DRIVER_NAME);
|
|
|
|
pci_enable_device(pdev);
|
|
|
|
}
|
2004-09-22 23:39:15 -06:00
|
|
|
dev_class = DRM(sysfs_device_add)(DRM(global)->drm_class,
|
|
|
|
MKDEV(DRM_MAJOR, minor), DRM_PCI_DEV(pdev), "card%d", minor);
|
2004-09-16 12:42:03 -06:00
|
|
|
if (IS_ERR(dev_class)) {
|
2004-09-22 23:39:15 -06:00
|
|
|
printk (KERN_ERR "DRM: Error sysfs_device_add.\n");
|
2004-09-16 12:42:03 -06:00
|
|
|
ret = PTR_ERR(dev_class);
|
|
|
|
goto err_g2;
|
|
|
|
}
|
2004-09-14 18:20:21 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
DRM_DEBUG("new primary minor assigned %d\n", minor);
|
|
|
|
return 0;
|
2001-02-15 01:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-22 23:39:15 -06:00
|
|
|
dev_class = DRM(sysfs_device_add)(DRM(global)->drm_class,
|
|
|
|
MKDEV(DRM_MAJOR, minor), DRM_PCI_DEV(dev->pdev), "card%d", minor);
|
2004-09-16 12:42:03 -06:00
|
|
|
if (IS_ERR(dev_class)) {
|
2004-09-22 23:39:15 -06:00
|
|
|
printk (KERN_ERR "DRM: Error sysfs_device_add.\n");
|
2004-09-16 12:42:03 -06:00
|
|
|
ret = PTR_ERR(dev_class);
|
|
|
|
goto err_g2;
|
|
|
|
}
|
|
|
|
*sec_minor = minors;
|
|
|
|
|
|
|
|
DRM_DEBUG("new secondary minor assigned %d\n", minor);
|
|
|
|
return 0;
|
2001-02-15 01:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
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;
|
2001-02-15 01:12:14 -07:00
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* Put a device minor number.
|
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* \param dev device data structure
|
|
|
|
* \return always zero
|
2003-05-26 18:37:33 -06:00
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* 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
|
2003-05-26 18:37:33 -06:00
|
|
|
* unregisters the character device.
|
|
|
|
*/
|
2004-09-16 12:42:03 -06:00
|
|
|
int DRM(put_minor)(drm_device_t *dev)
|
2001-02-15 01:12:14 -07:00
|
|
|
{
|
2004-09-16 12:42:03 -06:00
|
|
|
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);
|
2004-09-22 23:39:15 -06:00
|
|
|
DRM(sysfs_device_remove)(MKDEV(DRM_MAJOR, dev->minor));
|
2004-07-14 06:34:55 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
*minors = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};
|
|
|
|
DRM(free)(dev, sizeof(*dev), DRM_MEM_STUB);
|
2004-09-14 18:20:21 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
/* 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");
|
2004-07-24 23:41:44 -06:00
|
|
|
inter_module_put("drm");
|
2004-09-16 12:42:03 -06:00
|
|
|
return 0;
|
2004-07-24 23:41:44 -06:00
|
|
|
}
|
2001-02-15 01:12:14 -07:00
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
DRM_DEBUG("unregistering inter_module \n");
|
|
|
|
inter_module_unregister("drm");
|
|
|
|
remove_proc_entry("dri", NULL);
|
2004-09-22 23:39:15 -06:00
|
|
|
DRM(sysfs_destroy)(DRM(global)->drm_class);
|
|
|
|
|
2004-09-23 11:22:27 -06:00
|
|
|
unregister_chrdev(DRM_MAJOR, "drm");
|
2004-09-16 12:42:03 -06:00
|
|
|
|
|
|
|
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);
|
2004-09-22 23:39:15 -06:00
|
|
|
DRM(sysfs_device_remove)(MKDEV(DRM_MAJOR, minor));
|
2004-09-16 12:42:03 -06:00
|
|
|
|
|
|
|
*sec_minor = (drm_minor_t){.dev = NULL, .class = DRM_MINOR_FREE};
|
|
|
|
|
2001-02-15 01:12:14 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* Register.
|
|
|
|
*
|
2004-09-16 12:42:03 -06:00
|
|
|
* \param pdev - PCI device structure
|
|
|
|
* \param ent entry from the PCI ID table with device type flags
|
2003-05-26 18:37:33 -06:00
|
|
|
* \return zero on success or a negative number on failure.
|
|
|
|
*
|
2004-07-20 04:59:02 -06:00
|
|
|
* 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.
|
2003-05-26 18:37:33 -06:00
|
|
|
*/
|
2004-09-16 12:42:03 -06:00
|
|
|
int DRM(probe)(struct pci_dev *pdev, const struct pci_device_id *ent)
|
2001-02-15 01:12:14 -07:00
|
|
|
{
|
2004-09-16 12:42:03 -06:00
|
|
|
drm_global_t *global;
|
|
|
|
int ret = -ENOMEM;
|
2001-07-30 13:59:39 -06:00
|
|
|
|
|
|
|
DRM_DEBUG("\n");
|
2004-07-24 23:41:44 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
/* 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");
|
2004-08-03 03:21:11 -06:00
|
|
|
|
2004-09-16 12:42:03 -06:00
|
|
|
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);
|
|
|
|
global->minors = DRM(calloc)(global->cards_limit,
|
|
|
|
sizeof(*global->minors), DRM_MEM_STUB);
|
|
|
|
if(!global->minors)
|
|
|
|
goto err_p1;
|
2004-09-14 18:20:21 -06:00
|
|
|
|
2004-09-23 11:22:27 -06:00
|
|
|
if (register_chrdev(DRM_MAJOR, "drm", &DRM(stub_fops)))
|
2004-09-16 12:42:03 -06:00
|
|
|
goto err_p1;
|
|
|
|
|
2004-09-22 23:39:15 -06:00
|
|
|
global->drm_class = DRM(sysfs_create)(THIS_MODULE, "drm");
|
2004-09-16 12:42:03 -06:00
|
|
|
if (IS_ERR(global->drm_class)) {
|
|
|
|
printk (KERN_ERR "DRM: Error creating drm class.\n");
|
|
|
|
ret = PTR_ERR(global->drm_class);
|
2004-09-23 11:22:27 -06:00
|
|
|
goto err_p2;
|
2004-05-02 06:27:17 -06:00
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
|
|
|
|
global->proc_root = create_proc_entry("dri", S_IFDIR, NULL);
|
|
|
|
if (!global->proc_root) {
|
|
|
|
DRM_ERROR("Cannot create /proc/dri\n");
|
|
|
|
ret = -1;
|
2004-09-23 11:22:27 -06:00
|
|
|
goto err_p3;
|
2004-04-08 07:11:04 -06:00
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
DRM_DEBUG("calling inter_module_register\n");
|
|
|
|
inter_module_register("drm", THIS_MODULE, global);
|
|
|
|
|
|
|
|
DRM(global) = global;
|
2004-09-14 18:20:21 -06:00
|
|
|
}
|
2004-09-16 12:42:03 -06:00
|
|
|
if ((ret = get_minor(pdev, ent))) {
|
|
|
|
if (global)
|
2004-09-23 11:22:27 -06:00
|
|
|
goto err_p3;
|
2004-09-16 12:42:03 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_p3:
|
2004-09-23 11:22:27 -06:00
|
|
|
DRM(sysfs_destroy)(global->drm_class);
|
2004-09-16 12:42:03 -06:00
|
|
|
err_p2:
|
2004-09-23 11:22:27 -06:00
|
|
|
unregister_chrdev(DRM_MAJOR, "drm");
|
2004-09-16 12:42:03 -06:00
|
|
|
DRM(free)(global->minors, sizeof(*global->minors) * global->cards_limit, DRM_MEM_STUB);
|
|
|
|
err_p1:
|
|
|
|
DRM(free)(global, sizeof(*global), DRM_MEM_STUB);
|
|
|
|
DRM(global) = NULL;
|
|
|
|
return ret;
|
2004-09-15 11:44:30 -06:00
|
|
|
}
|