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
|
|
|
#define __NO_VERSION__
|
|
|
|
#include "drmP.h"
|
|
|
|
|
2001-02-15 01:12:14 -07:00
|
|
|
#define DRM_STUB_MAXCARDS 16 /* Enough for one machine */
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/** Stub list. One for each minor. */
|
2001-02-15 01:12:14 -07:00
|
|
|
static struct drm_stub_list {
|
|
|
|
const char *name;
|
2003-05-26 18:37:33 -06:00
|
|
|
struct file_operations *fops; /**< file operations */
|
|
|
|
struct proc_dir_entry *dev_root; /**< proc directory entry */
|
2001-02-15 01:12:14 -07:00
|
|
|
} *DRM(stub_list);
|
|
|
|
|
|
|
|
static struct proc_dir_entry *DRM(stub_root);
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-02-15 01:12:14 -07:00
|
|
|
static int DRM(stub_open)(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2004-04-08 06:25:31 -06:00
|
|
|
int minor = iminor(inode);
|
2001-02-15 01:12:14 -07:00
|
|
|
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);
|
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,
|
|
|
|
.open = DRM(stub_open)
|
2001-02-15 01:12:14 -07:00
|
|
|
};
|
|
|
|
|
2004-07-31 09:45:00 -06:00
|
|
|
static int drm_hotplug (struct class_device *dev, char **envp, int num_envp,
|
|
|
|
char *buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
drm_device_t *ddev;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
envp[i] = 0;
|
|
|
|
DRM_DEBUG(" - ok\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* 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().
|
|
|
|
*/
|
2001-02-15 01:12:14 -07:00
|
|
|
static int DRM(stub_getminor)(const char *name, struct file_operations *fops,
|
|
|
|
drm_device_t *dev)
|
|
|
|
{
|
|
|
|
int i;
|
2001-02-15 22:24:06 -07:00
|
|
|
|
2001-02-15 01:12:14 -07:00
|
|
|
if (!DRM(stub_list)) {
|
|
|
|
DRM(stub_list) = DRM(alloc)(sizeof(*DRM(stub_list))
|
|
|
|
* DRM_STUB_MAXCARDS, DRM_MEM_STUB);
|
2001-08-07 12:15:10 -06:00
|
|
|
if(!DRM(stub_list)) return -1;
|
2001-02-15 01:12:14 -07:00
|
|
|
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(stub_root) = DRM(proc_init)(dev, i, DRM(stub_root),
|
|
|
|
&DRM(stub_list)[i]
|
|
|
|
.dev_root);
|
2004-07-21 03:30:43 -06:00
|
|
|
(*DRM(stub_info).info_count)++;
|
|
|
|
DRM_DEBUG("info count increased %d\n", *DRM(stub_info).info_count);
|
2001-02-15 01:12:14 -07:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-02-15 01:12:14 -07:00
|
|
|
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_root),
|
|
|
|
DRM(stub_list)[minor].dev_root);
|
2004-07-14 06:34:55 -06:00
|
|
|
|
2004-07-21 03:30:43 -06:00
|
|
|
(*DRM(stub_info).info_count)--;
|
2004-07-14 06:34:55 -06:00
|
|
|
|
|
|
|
if ((*DRM(stub_info).info_count)!=0) {
|
2004-07-24 23:41:44 -06:00
|
|
|
if (DRM(numdevs)==0) {
|
|
|
|
DRM_DEBUG("inter_module_put called %d\n", *DRM(stub_info).info_count);
|
|
|
|
inter_module_put("drm");
|
|
|
|
}
|
2001-02-15 01:12:14 -07:00
|
|
|
} else {
|
2004-07-24 23:41:44 -06:00
|
|
|
DRM_DEBUG("unregistering inter_module \n");
|
2001-02-15 01:12:14 -07:00
|
|
|
inter_module_unregister("drm");
|
|
|
|
DRM(free)(DRM(stub_list),
|
|
|
|
sizeof(*DRM(stub_list)) * DRM_STUB_MAXCARDS,
|
|
|
|
DRM_MEM_STUB);
|
2004-05-02 06:27:17 -06:00
|
|
|
class_simple_destroy(DRM(stub_info).drm_class);
|
2001-02-15 01:12:14 -07:00
|
|
|
unregister_chrdev(DRM_MAJOR, "drm");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* Register.
|
|
|
|
*
|
|
|
|
* \param name driver name.
|
|
|
|
* \param fops file operations
|
|
|
|
* \param dev DRM device.
|
|
|
|
* \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
|
|
|
*
|
|
|
|
* Finally calls stub_info::info_register.
|
|
|
|
*/
|
2001-02-15 01:12:14 -07:00
|
|
|
int DRM(stub_register)(const char *name, struct file_operations *fops,
|
|
|
|
drm_device_t *dev)
|
|
|
|
{
|
2001-02-21 09:06:10 -07:00
|
|
|
struct drm_stub_info *i = NULL;
|
2004-04-08 07:11:04 -06:00
|
|
|
int ret1;
|
|
|
|
int ret2;
|
2001-07-30 13:59:39 -06:00
|
|
|
|
|
|
|
DRM_DEBUG("\n");
|
2004-07-24 23:41:44 -06:00
|
|
|
|
2004-07-25 02:47:38 -06:00
|
|
|
/* if we are registering a second device we don't need to worry
|
2004-07-24 23:41:44 -06:00
|
|
|
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");
|
2004-07-31 09:45:00 -06:00
|
|
|
class_simple_set_hotplug(DRM(stub_info).drm_class, drm_hotplug);
|
2004-07-24 23:41:44 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
DRM_DEBUG("calling inter_module_register\n");
|
|
|
|
inter_module_register("drm", THIS_MODULE, &DRM(stub_info));
|
2004-05-02 06:27:17 -06:00
|
|
|
}
|
2001-02-15 01:12:14 -07:00
|
|
|
}
|
2004-07-25 02:47:38 -06:00
|
|
|
else
|
2004-07-24 23:41:44 -06:00
|
|
|
DRM_DEBUG("already retrieved inter_module information\n");
|
2004-07-20 04:59:02 -06:00
|
|
|
|
2004-04-08 07:11:04 -06:00
|
|
|
if (DRM(stub_info).info_register) {
|
|
|
|
ret2 = DRM(stub_info).info_register(name, fops, dev);
|
2004-07-22 06:07:13 -06:00
|
|
|
if (ret2 < 0) {
|
2004-07-24 23:41:44 -06:00
|
|
|
if (DRM(numdevs)==0 && !i) {
|
2004-07-20 04:59:02 -06:00
|
|
|
inter_module_unregister("drm");
|
2004-04-08 07:11:04 -06:00
|
|
|
unregister_chrdev(DRM_MAJOR, "drm");
|
2004-05-02 06:27:17 -06:00
|
|
|
class_simple_destroy(DRM(stub_info).drm_class);
|
2004-07-21 03:30:43 -06:00
|
|
|
DRM_DEBUG("info_register failed deregistered everything\n");
|
2004-04-08 07:11:04 -06:00
|
|
|
}
|
2004-07-21 03:30:43 -06:00
|
|
|
DRM_DEBUG("info_register failed\n");
|
2004-04-08 07:11:04 -06:00
|
|
|
}
|
|
|
|
return ret2;
|
|
|
|
}
|
2001-02-15 01:12:14 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2004-07-25 02:47:38 -06:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* Unregister.
|
|
|
|
*
|
|
|
|
* \param minor
|
|
|
|
*
|
|
|
|
* Calls drm_stub_info::unregister.
|
|
|
|
*/
|
2001-02-15 01:12:14 -07:00
|
|
|
int DRM(stub_unregister)(int minor)
|
|
|
|
{
|
|
|
|
DRM_DEBUG("%d\n", minor);
|
|
|
|
if (DRM(stub_info).info_unregister)
|
|
|
|
return DRM(stub_info).info_unregister(minor);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-02 06:27:17 -06:00
|
|
|
|
2004-07-14 06:34:55 -06:00
|
|
|
int DRM(stub_count);
|
|
|
|
|
2004-05-02 06:27:17 -06:00
|
|
|
/** Stub information */
|
|
|
|
struct drm_stub_info DRM(stub_info) = {
|
|
|
|
.info_register = DRM(stub_getminor),
|
|
|
|
.info_unregister = DRM(stub_putminor),
|
|
|
|
.drm_class = NULL,
|
2004-07-14 06:34:55 -06:00
|
|
|
.info_count = &DRM(stub_count),
|
2004-05-02 06:27:17 -06:00
|
|
|
};
|