📄 pci-scan.c
字号:
/* pci-scan.c: Linux PCI network adapter support code. */
/*
Originally written 1999-2001 by Donald Becker.
This software may be used and distributed according to the terms
of the GNU General Public License (GPL), incorporated herein by
reference. Drivers interacting with these functions are derivative
works and thus are covered the GPL. They must include an explicit
GPL notice.
This code provides common scan and activate functions for PCI network
interfaces.
The author may be reached as becker@scyld.com, or
Donald Becker
Scyld Computing Corporation
410 Severn Ave., Suite 210
Annapolis MD 21403
Other contributers:
*/
static const char version[] =
"pci-scan.c:v1.06 5/18/2001 Donald Becker <becker@scyld.com>"
"pci-scan.c:v1.07 8/10/2001 Modified"
"pci-scan.c:v1.08 8/15/2001 Modified"
"pci-scan.c:v1.09 8/17/2001 Modified"
" http://www.scyld.com/linux/drivers.html\n";
/* A few user-configurable values that may be modified when a module. */
static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
static int min_pci_latency = 0xff;
#if ! defined(__KERNEL__)
#define __KERNEL__ 1
#endif
#if !defined(__OPTIMIZE__)
#warning You must compile this file with the correct options!
#warning See the last lines of the source file.
#error You must compile this driver with the proper options, including "-O".
#endif
#include <linux/config.h>
#if defined(CONFIG_SMP) && ! defined(__SMP__)
#define __SMP__
#endif
#include <linux/version.h>
#include <linux/module.h>
/* Older kernels do not include this automatically. */
#if LINUX_VERSION_CODE < 0x20300 && defined(MODVERSIONS)
#include <linux/modversions.h>
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
#if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
#define MODVERSIONS
#endif
#endif
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/ioport.h>
#include <linux/malloc.h>
#include <asm/io.h>
#include "pci-scan.h"
#include "kern_compat.h"
#if (LINUX_VERSION_CODE >= 0x20100) && defined(MODULE)
char kernel_version[] = UTS_RELEASE;
#endif
#if (LINUX_VERSION_CODE < 0x20100)
#define PCI_CAPABILITY_LIST 0x34 /* Offset of first capability list entry */
#define PCI_STATUS_CAP_LIST 0x10 /* Support Capability List */
#define PCI_CAP_ID_PM 0x01 /* Power Management */
#endif
int (*register_cb_hook)(struct drv_id_info *did);
void (*unregister_cb_hook)(struct drv_id_info *did);
#if LINUX_VERSION_CODE > 0x20118 && defined(MODULE)
MODULE_PARM(debug, "i");
MODULE_PARM(min_pci_latency, "i");
#if defined(EXPORT_SYMTAB)
EXPORT_SYMBOL_NOVERS(pci_drv_register);
EXPORT_SYMBOL_NOVERS(pci_drv_unregister);
EXPORT_SYMBOL_NOVERS(acpi_wake);
EXPORT_SYMBOL_NOVERS(acpi_set_pwr_state);
EXPORT_SYMBOL_NOVERS(register_cb_hook);
EXPORT_SYMBOL_NOVERS(unregister_cb_hook);
#endif
#endif
/* List of registered drivers. */
static struct drv_id_info *drv_list;
/* List of detected PCI devices, for APM events. */
static struct dev_info {
struct dev_info *next;
void *dev;
struct drv_id_info *drv_id;
int flags;
} *dev_list;
/*
This code is not intended to support every configuration.
It is intended to minimize duplicated code by providing the functions
needed in almost every PCI driver.
The "no kitchen sink" policy:
Additional features and code will be added to this module only if more
than half of the drivers for common hardware would benefit from the feature.
*/
/*
Ideally we would detect and number all cards of a type (e.g. network) in
PCI slot order.
But that does not work with hot-swap card, CardBus cards and added drivers.
So instead we detect just the each chip table in slot order.
This routine takes a PCI ID table, scans the PCI bus, and calls the
associated attach/probe1 routine with the hardware already activated and
single I/O or memory address already mapped.
This routine will later be supplemented with CardBus and hot-swap PCI
support using the same table. Thus the pci_chip_tbl[] should not be
marked as __initdata.
*/
#if LINUX_VERSION_CODE >= 0x20200
/* Grrrr.. complex abstaction layers with negative benefit. */
int pci_drv_register(struct drv_id_info *drv_id, void *initial_device)
{
int chip_idx, cards_found;
struct pci_dev *pdev = NULL;
struct pci_id_info *pci_tbl = drv_id->pci_dev_tbl;
struct drv_id_info *drv;
void *newdev;
/* Ignore a double-register attempt. */
for (drv = drv_list; drv; drv = drv->next)
if (drv == drv_id)
return -EBUSY;
for (cards_found = 0;
(pdev = pci_find_class(drv_id->pci_class, pdev)) != 0;
cards_found++) {
u32 pci_id, pci_subsys_id, pci_class_rev;
u16 pci_command, new_command;
int pci_flags;
long pciaddr; /* Bus address. */
long ioaddr; /* Mapped address for this processor. */
pci_read_config_dword(pdev, PCI_VENDOR_ID, &pci_id);
/* Offset 0x2c is PCI_SUBSYSTEM_ID aka PCI_SUBSYSTEM_VENDOR_ID. */
pci_read_config_dword(pdev, 0x2c, &pci_subsys_id);
pci_read_config_dword(pdev, PCI_REVISION_ID, &pci_class_rev);
if (debug > 3)
printk(KERN_DEBUG "PCI ID %8.8x subsystem ID is %8.8x.\n",
pci_id, pci_subsys_id);
for (chip_idx = 0; pci_tbl[chip_idx].name; chip_idx++) {
struct pci_id_info *chip = &pci_tbl[chip_idx];
if ((pci_id & chip->id.pci_mask) == chip->id.pci
&& (pci_subsys_id&chip->id.subsystem_mask) == chip->id.subsystem
&& (pci_class_rev&chip->id.revision_mask) == chip->id.revision)
break;
}
if (pci_tbl[chip_idx].name == 0) /* Compiled out! */
continue;
pci_flags = pci_tbl[chip_idx].pci_flags;
#if LINUX_VERSION_CODE >= 0x2030C
/* Wow. A oversized, hard-to-use abstraction. Bogus. */
pciaddr = pdev->resource[(pci_flags >> 4) & 7].start;
#else
pciaddr = pdev->base_address[(pci_flags >> 4) & 7];
#if defined(__alpha__) /* Really any machine with 64 bit addressing. */
if (pci_flags & PCI_ADDR_64BITS)
pciaddr |= ((long)pdev->base_address[((pci_flags>>4)&7)+ 1]) << 32;
#endif
#endif
if (debug > 2)
printk(KERN_INFO "Found %s at PCI address %#lx, mapped IRQ %d.\n",
pci_tbl[chip_idx].name, pciaddr, pdev->irq);
if ( ! (pci_flags & PCI_UNUSED_IRQ) &&
(pdev->irq == 0 || pdev->irq == 255)) {
if (pdev->bus->number == 32) /* Broken CardBus activation. */
printk(KERN_WARNING "Resources for CardBus device '%s' have"
" not been allocated.\n"
KERN_WARNING "Activation has been delayed.\n",
pci_tbl[chip_idx].name);
else
printk(KERN_WARNING "PCI device '%s' was not assigned an "
"IRQ.\n"
KERN_WARNING "It will not be activated.\n",
pci_tbl[chip_idx].name);
continue;
}
if ((pciaddr & PCI_BASE_ADDRESS_SPACE_IO)) {
ioaddr = pciaddr & PCI_BASE_ADDRESS_IO_MASK;
if (check_region(ioaddr, pci_tbl[chip_idx].io_size))
continue;
} else if ((ioaddr = (long)ioremap(pciaddr & PCI_BASE_ADDRESS_MEM_MASK,
pci_tbl[chip_idx].io_size)) == 0) {
printk(KERN_INFO "Failed to map PCI address %#lx for device "
"'%s'.\n", pciaddr, pci_tbl[chip_idx].name);
continue;
}
if ( ! (pci_flags & PCI_NO_ACPI_WAKE))
acpi_wake(pdev);
pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
new_command = pci_command | (pci_flags & 7);
if (pci_command != new_command) {
printk(KERN_INFO " The PCI BIOS has not enabled the"
" device at %d/%d! Updating PCI command %4.4x->%4.4x.\n",
pdev->bus->number, pdev->devfn, pci_command, new_command);
pci_write_config_word(pdev, PCI_COMMAND, new_command);
}
newdev = drv_id->probe1(pdev, initial_device,
ioaddr, pdev->irq, chip_idx, cards_found);
if (newdev == NULL)
continue;
initial_device = 0;
if (pci_flags & PCI_COMMAND_MASTER) {
pci_set_master(pdev);
if ( ! (pci_flags & PCI_NO_MIN_LATENCY)) {
u8 pci_latency;
pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
if (pci_latency < min_pci_latency) {
printk(KERN_INFO " PCI latency timer (CFLT) is "
"unreasonably low at %d. Setting to %d clocks.\n",
pci_latency, min_pci_latency);
pci_write_config_byte(pdev, PCI_LATENCY_TIMER,
min_pci_latency);
}
}
}
{
struct dev_info *devp =
kmalloc(sizeof(struct dev_info), GFP_KERNEL);
if (devp == 0)
continue;
devp->next = dev_list;
devp->dev = newdev;
devp->drv_id = drv_id;
dev_list = devp;
}
}
if (((drv_id->flags & PCI_HOTSWAP)
&& register_cb_hook && (*register_cb_hook)(drv_id) == 0)
|| cards_found) {
MOD_INC_USE_COUNT;
drv_id->next = drv_list;
drv_list = drv_id;
return 0;
} else
return -ENODEV;
}
#else
int pci_drv_register(struct drv_id_info *drv_id, void *initial_device)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -