⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pci_lib.c

📁 利用giveio实现在windows下直接访问pci卡的程序.安装giveio后,将源代码中的pci的venderid和deviceid改为自己pci卡的id号,就能够在widows下得到自己pci卡
💻 C
📖 第 1 页 / 共 2 页
字号:

#include "mvb_def.h"
 
#include <pci_lib.h>

#ifdef _MSC_VER
#   include <conio.h> /* _inp, _outp */
#   include <stdio.h> /* printf      */
#endif

#ifdef _WIN32
#   include <windows.h> /* GetVersionEx, CreateFile, CloseHandle */
#endif

#ifdef __ECOS__
#   include <cyg/hal/hal_io.h> /* HAL_READ_UINTx, HAL_WRITE_UINTx */
#   include <cyg/infra/diag.h> /* diag_printf                     */
#endif

#include <string.h> /* memset */


/* ==========================================================================
 *
 *  Macros
 *
 * ==========================================================================
 */
 
#ifdef __ECOS__
#   define PCIPRT diag_printf
#else
#   define PCIPRT printf
#endif


/* ==========================================================================
 *
 *  Types
 *
 * ==========================================================================
 */

/* --------------------------------------------------------------------------
 *  Structured Type   : PCI_CONFADD
 * --------------------------------------------------------------------------
 */
typedef struct
{
    unsigned int reg    : 8;
    unsigned int func   : 3;
    unsigned int dev    : 5;
    unsigned int bus    : 8;
    unsigned int rsvd   : 7;
    unsigned int enable : 1;

}   PCI_CONFADD;


/* ==========================================================================
 *
 *  Procedures
 *
 * ==========================================================================
 */

#ifdef _MSC_VER

/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read8
 * --------------------------------------------------------------------------
 */
static
PCI_UINT8
pci_io_read8
(
    PCI_UINT16  base
)
{

    return((PCI_UINT8)_inp((unsigned short)base));

} /* pci_io_read8 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read16
 * --------------------------------------------------------------------------
 */
static
PCI_UINT16
pci_io_read16
(
    PCI_UINT16  base
)
{

    return((PCI_UINT16)_inpw((unsigned short)base));

} /* pci_io_read16 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read32
 * --------------------------------------------------------------------------
 */
static
PCI_UINT32
pci_io_read32
(
    PCI_UINT16  base
)
{

    return((PCI_UINT32)_inpd((unsigned short)base));

} /* pci_io_read32 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write8
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write8
(
    PCI_UINT16  base,
    PCI_UINT8   val
)
{

    _outp((unsigned short)base, (int)val);

} /* pci_io_write8 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write16
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write16
(
    PCI_UINT16  base,
    PCI_UINT16  val
)
{

    _outpw((unsigned short)base, (unsigned short)val);

} /* pci_io_write16 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write32
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write32
(
    PCI_UINT16  base,
    PCI_UINT32  val
)
{

    _outpd((unsigned short)base, (unsigned long)val);

} /* pci_io_write32 */

#endif /* _MSC_VER */


#ifdef __ECOS__

/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read8
 * --------------------------------------------------------------------------
 */
static
PCI_UINT8
pci_io_read8
(
    PCI_UINT16  base
)
{
    cyg_uint8   val8;

    HAL_READ_UINT8(base, val8);

    return((PCI_UINT8)val8);

} /* pci_io_read8 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read16
 * --------------------------------------------------------------------------
 */
static
PCI_UINT16
pci_io_read16
(
    PCI_UINT16  base
)
{
    cyg_uint16  val16;

    HAL_READ_UINT16(base, val16);

    return((PCI_UINT16)val16);

} /* pci_io_read16 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_read32
 * --------------------------------------------------------------------------
 */
static
PCI_UINT32
pci_io_read32
(
    PCI_UINT16  base
)
{
    cyg_uint32  val32;

    HAL_READ_UINT32(base, val32);

    return((PCI_UINT32)val32);

} /* pci_io_read32 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write8
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write8
(
    PCI_UINT16  base,
    PCI_UINT8   val
)
{

    HAL_WRITE_UINT8(base, val);

} /* pci_io_write8 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write16
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write16
(
    PCI_UINT16  base,
    PCI_UINT16  val
)
{

    HAL_WRITE_UINT16(base, val);

} /* pci_io_write16 */


/* --------------------------------------------------------------------------
 *  Procedure : pci_io_write32
 * --------------------------------------------------------------------------
 */
static
void
pci_io_write32
(
    PCI_UINT16  base,
    PCI_UINT32  val
)
{

    HAL_WRITE_UINT32(base, val);

} /* pci_io_write32 */

#endif /* __ECOS__ */


/* --------------------------------------------------------------------------
 *  Procedure : pci_read
 * --------------------------------------------------------------------------
 */
static
PCI_UINT32
pci_read
(
    PCI_UINT8   bus,
    PCI_UINT8   dev,
    PCI_UINT8   func,
    PCI_UINT8   reg,
    PCI_UINT8   bytes
)
{
    PCI_UINT16  base;

    union {
        PCI_CONFADD c;
        PCI_UINT32  n;
    } u;

    u.n        = 0;
    u.c.enable = 1;
    u.c.rsvd   = 0;
    u.c.bus    = bus;
    u.c.dev    = dev;
    u.c.func   = func;
    u.c.reg    = reg & 0xFC;

    pci_io_write32((PCI_UINT16)0xCF8, (PCI_UINT32)u.n);

    base = (PCI_UINT16)(0xCFC + (reg & 0x03));

    switch(bytes)
    {
        case(1):
            return((PCI_UINT32)pci_io_read8(base));
        case(2):
            return((PCI_UINT32)pci_io_read16(base));
        case(4):
            return((PCI_UINT32)pci_io_read32(base));
        default:
            return(0);
    } /* switch(bytes) */

} /* pci_read */


/* --------------------------------------------------------------------------
 *  Procedure : pci_write
 * --------------------------------------------------------------------------
 */
static
void
pci_write
(
    PCI_UINT8   bus,
    PCI_UINT8   dev,
    PCI_UINT8   func,
    PCI_UINT8   reg,
    PCI_UINT32  v,
    PCI_UINT8   bytes
)
{
    PCI_UINT16  base;

    union {
        PCI_CONFADD c;
        PCI_UINT32  n;
    } u;

    u.n        = 0;
    u.c.enable = 1;
    u.c.rsvd   = 0;
    u.c.bus    = bus;
    u.c.dev    = dev;
    u.c.func   = func;
    u.c.reg    = reg & 0xFC;

    base = (PCI_UINT16)(0xCFC + (reg & 0x03));

    pci_io_write32((PCI_UINT16)0xCF8, (PCI_UINT32)u.n);

    switch(bytes)
    {
        case(1):
            pci_io_write8((PCI_UINT16)base, (PCI_UINT8)v);
            break;
        case(2):
            pci_io_write16((PCI_UINT16)base, (PCI_UINT16)v);
            break;
        case(4):
            pci_io_write32((PCI_UINT16)base, (PCI_UINT32)v);
            break;
    } /* switch(bytes) */

} /* pci_write */


/* --------------------------------------------------------------------------
 *  Procedure : pci_probe
 * --------------------------------------------------------------------------
 */
extern
PCI_UINT16
pci_probe
(
    PCI_UINT8   bus,
    PCI_UINT8   dev,
    PCI_UINT8   func,
    PCI_UINT8   print,
    PCI_UINT8   sizing,
    PCI_CONFIG  *cfg
)
{
    PCI_UINT32  *p_cfg = (PCI_UINT32*)cfg;
    PCI_UINT32  v, v2;
    PCI_UINT16  i;
    PCI_UINT32  base_start, base_end;

    /* get common header */
    for (i=0; i<4; i++)
    {
        p_cfg[i] = pci_read(bus, dev, func, (PCI_UINT8)(4*i), 4);
    } /* for (i=0; i<4; i++) */

    if (cfg->vendor_id == 0xFFFF)
    {
        return(FALSE);
    } /* if (cfg->vendor_id == 0xFFFF) */

    cfg->subsystem_vendor_id = (PCI_UINT16)pci_read(bus, dev, func, 0x2C, 2);
    cfg->subsystem_id        = (PCI_UINT16)pci_read(bus, dev, func, 0x2E, 2);

    cfg->bus                 = bus;
    cfg->dev                 = dev;
    cfg->func                = func;

    if (print)
    {
        PCIPRT("\nDevice Info (bus=%d, dev=%d, func=%d)\n", bus, dev, func);
        PCIPRT("----------------------------------------------------------");
        PCIPRT("-------------------\n");
        PCIPRT(" * Vendor    : %04X   Device    : %04X\n", \
            cfg->vendor_id, cfg->device_id);
        PCIPRT(" * Sub-Vendor: %04X   Sub-Device: %04X\n", \
            cfg->subsystem_vendor_id, cfg->subsystem_id);
        PCIPRT(" * Status    : %04X   Command   : %04X\n", \
            cfg->status, cfg->command);
        PCIPRT("----------------------------------------------------------");
        PCIPRT("-------------------\n");
        PCIPRT(" * Class/SubClass/Interface: %02X/%02X/%02X\n", \
            cfg->base_class, cfg->sub_class, cfg->programming_interface);
        PCIPRT(" * BIST/Type/Lat/CLS       : %02X/%02X/%02X/%02X\n", \
            cfg->bist, cfg->header_type, cfg->latency_timer, \
            cfg->cache_line_size);
        PCIPRT("----------------------------------------------------------");
        PCIPRT("-------------------\n");
    } /* if (print) */

    switch(cfg->header_type & 0x7F)
    {
        case(0): /* normal device */
            for (i=0; i<6; i++)
            {
                v = pci_read(bus, dev, func, (PCI_UINT8)(i*4 + 0x10), 4);
                if (v)
                {
                    if (sizing)
                    {
                        pci_write                       \
                        (                               \
                            bus, dev, func,             \
                            (PCI_UINT8)(i*4 + 0x10),    \

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -