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

📄 pci_lib.c

📁 利用giveio实现在windows下直接访问pci卡的程序.安装giveio后,将源代码中的pci的venderid和deviceid改为自己pci卡的id号,就能够在widows下得到自己pci卡
💻 C
📖 第 1 页 / 共 2 页
字号:
                            0xFFFFFFFF, 4               \
                        );
                        v2 =                            \
                            pci_read                    \
                            (                           \
                                bus, dev, func,         \
                                (PCI_UINT8)(i*4+0x10),  \
                                4                       \
                            );
                        pci_write                       \
                        (                               \
                            bus, dev, func,             \
                            (PCI_UINT8)(i*4 + 0x10),    \
                            v, 4                        \
                        );

                        if (v & 1)
                        {
                            v2 = v2 & 0xFFFFFFFE;
                            v2 = ~v2 + 1;
                            cfg->base[i] = (PCI_UINT32)(v  & 0xFFFF);
                            cfg->size[i] = (PCI_UINT32)(v2 & 0xFFFF);
                            if (print)
                            {
                                base_start = \
                                    (PCI_UINT32)(cfg->base[i] & 0xFFFC);
                                base_end   = (PCI_UINT32)(base_start + \
                                    cfg->size[i] - 1);
                                PCIPRT(" * Base Register %d IO: ", i);
                                PCIPRT("%lx-%lx (%lx)\n", base_start, \
                                    base_end, cfg->size[i]);
                            } /* if (print) */
                        }
                        else
                        {
                            v2 = v2 & 0xFFFFFFF0;
                            v2 = ~v2 + 1;
                            cfg->base[i] = v;
                            cfg->size[i] = v2;
                            if (print)
                            {
                                base_start = (PCI_UINT32)(cfg->base[i] & \
                                    0xFFFFFFF0);
                                base_end   = (PCI_UINT32)(base_start + \
                                    cfg->size[i] - 1);
                                PCIPRT(" * Base Register %d MM: ", i);
                                PCIPRT("%lx-%lx (%lx)\n", base_start, \
                                    base_end, cfg->size[i]);
                            } /* if (print) */
                        } /* if (v & 1) */
                    }
                    else /* if (sizing) */
                    {
                        if (v & 1)
                        {
                            cfg->base[i] = (PCI_UINT32)(v  & 0xFFFF);
                            if (print)
                            {
                                base_start = \
                                    (PCI_UINT32)(cfg->base[i] & 0xFFFC);
                                PCIPRT(" * Base Register %d IO: %lx\n", \
                                    i, base_start);
                            } /* if (print) */
                        }
                        else
                        {
                            cfg->base[i] = v;
                            if (print)
                            {
                                base_start = \
                                    (PCI_UINT32)(cfg->base[i] & 0xFFFFFFF0);
                                PCIPRT(" * Base Register %d MM: %lx\n", \
                                    i, base_start);
                            } /* if (print) */
                        } /* if (v & 1) */
                    } /* if (sizing) */
                }
                else
                {
                    cfg->base[i] = 0;
                    cfg->size[i] = 0;
                } /* if (v) */
            } /* for (i=0; i<6; i++) */

            v = pci_read(bus, dev, func, 0x3D, 1);
            cfg->irq_pin = (PCI_UINT8)(v == 0xFF ? 0 : v);
            if (print)
            {
                PCIPRT(" * Interrupt Pin     : ");
                switch(cfg->irq_pin)
                {
                    case(0):
                        PCIPRT("NA\n");
                        break;
                    case(1):
                        PCIPRT("INTA#\n");
                        break;
                    case(2):
                        PCIPRT("INTB#\n");
                        break;
                    case(3):
                        PCIPRT("INTC#\n");
                        break;
                    case(4):
                        PCIPRT("INTD#\n");
                        break;
                    default:
                        PCIPRT("unknown (%X)\n", cfg->irq_pin);
                } /* switch(cfg->irq_pin) */
            } /* if (print) */

            v = pci_read(bus, dev, func, 0x3C, 1);
            cfg->irq_line = (PCI_UINT8)(v == 0xFF ? 0 : v);
            if (print)
            {
                PCIPRT(" * Interrupt Line    : 0x%02X (%d)\n", \
                    cfg->irq_line, cfg->irq_line);
            } /* if (print) */

            break;

        case(1):
            if (print)
            {
                PCIPRT(" * PCI <-> PCI Bridge\n");
            } /* if (print) */

            break;

        default:
            if (print)
            {
                PCIPRT(" * Unknown Header Type\n");
            } /* if (print) */

    } /* switch(cfg->header_type & 0x7F) */

    if (print)
    {
        PCIPRT("----------------------------------------------------------");
        PCIPRT("-------------------\n");
    } /* if (print) */

    return(TRUE);

} /* pci_probe */


/* --------------------------------------------------------------------------
 *  Procedure : pci_scan
 * --------------------------------------------------------------------------
 */
extern
PCI_UINT16
pci_scan (void)
{
    PCI_UINT16  bus;    /* 0 - 255 */
    PCI_UINT16  dev;    /* 0 -  31 */
    PCI_UINT16  func;   /* 0 -   7 */
    PCI_CONFIG  pci_cfg;

    for (bus=0; bus<=255; bus++)
    {
        for (dev=0; dev<=31; dev++)
        {
            for (func=0; func<=7; func++)
            {
                memset((void*)&pci_cfg, 0, sizeof(PCI_CONFIG));

                if (pci_probe((PCI_UINT8)bus, (PCI_UINT8)dev, \
                    (PCI_UINT8)func, 1, 0, &pci_cfg))
                {
                    if (func == 0)
                    {
                        if ((pci_cfg.header_type & 0x80) == 0x00)
                        {
                            break;
                        } /* if ((pci_cfg.header_type & 0x80) == 0x00) */
                    } /* if (func == 0) */
                }
                else
                {
                    break;
                } /* if (pci_probe(...)) */
            } /* for (func=0; func<=7; func++) */
        } /* for (dev=0; dev<=31; dev++) */
    } /* for (bus=0; bus<=255; bus++) */

    return(TRUE);

} /* pci_scan */


/* --------------------------------------------------------------------------
 *  Procedure : pci_search
 * --------------------------------------------------------------------------
 */
extern
PCI_UINT16
pci_search
(
    PCI_CONFIG  *pci_cfg,
    PCI_UINT16  *found
)
{
    PCI_UINT16  vendor_id;
    PCI_UINT16  device_id;
    PCI_UINT16  bus;  /* 0 - 255 */
    PCI_UINT16  dev;  /* 0 -  31 */
    PCI_UINT16  func; /* 0 -   7 */
    PCI_UINT16  bus_c;
    PCI_UINT16  dev_c;

    if ((pci_cfg->dev > 31) || (pci_cfg->func > 7))
    {
        *found = FALSE;
        return(PCI_ERROR);
    } /* if ((pci_cfg->dev > 31) || (pci_cfg->func > 7)) */

    vendor_id = pci_cfg->vendor_id;
    device_id = pci_cfg->device_id;
    bus       = (PCI_UINT16)pci_cfg->bus;
    dev       = (PCI_UINT16)pci_cfg->dev;
    func      = (PCI_UINT16)pci_cfg->func;

    for (bus_c=bus; bus_c<=255; bus_c++)
    {
        for (dev_c=dev; dev_c<=31; dev_c++)
        {
            memset((void*)pci_cfg, 0, sizeof(PCI_CONFIG));

            if (pci_probe((PCI_UINT8)bus_c, (PCI_UINT8)dev_c, \
                (PCI_UINT8)func, 0, 0, pci_cfg))
            {
                if ((pci_cfg->vendor_id == vendor_id) && \
                    (pci_cfg->device_id == device_id))
                {
                    *found = TRUE;
                    return(PCI_OK);
                } /* if ((...)) */
            } /* if (pci_probe(...)) */
        } /* for (dev_c=dev; dev_c<=31; dev_c++) */
    } /* for (bus_c=bus; bus_c<=255; bus_c++) */

    memset((void*)pci_cfg, 0xFF, sizeof(PCI_CONFIG));

    *found = FALSE;

    return(PCI_OK);

} /* pci_search */


/* --------------------------------------------------------------------------
 *  Procedure : pci_init
 * --------------------------------------------------------------------------
 */
#ifdef _WIN32

extern
PCI_RESULT
pci_init (void)
{
    PCI_RESULT      result = PCI_OK;
    OSVERSIONINFO   osvi;
    HANDLE          h;

    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    GetVersionEx(&osvi);

    if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        h = CreateFile                  \
            (                           \
                "\\\\.\\giveio",        \
                GENERIC_READ,           \
                0,                      \
                NULL,                   \
                OPEN_EXISTING,          \
                FILE_ATTRIBUTE_NORMAL,  \
                NULL                    \
            );

        if (h == INVALID_HANDLE_VALUE)
        {
            result = PCI_ERROR;
        } /* if (h == INVALID_HANDLE_VALUE) */

        CloseHandle(h);

    } /* if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) */

    return(result);

} /* pci_init */

#else /* !_WIN32 */

extern
PCI_RESULT
pci_init (void)
{
    PCI_RESULT  result = PCI_OK;

    return(result);

} /* pci_init */

#endif /* _WIN32 */

// get PCI base addr
PCI_RESULT pci_base_addr (UNSIGNED16 *hw_base_addr)
{
    PCI_RESULT  result;
    PCI_CONFIG  pci_cfg;
    PCI_UINT16  found;

	UNSIGNED16 base_addr;
	
#ifdef O_PCI_HINT_PRINT
    PCIPRT("\n");
    PCIPRT("PCI Library\n");
    PCIPRT("===========\n");

    PCIPRT("\n");
    PCIPRT("pci_init()=");
#endif

    result = pci_init();
    if (result != PCI_OK)
    {
        return(PCI_ERROR);
    } /* if (result != PCI_OK) */

#ifdef O_PCI_HINT_PRINT
    PCIPRT("%u\n", result);
#endif

#ifdef PCI_LIB_SCAN
    PCIPRT("\n");
    PCIPRT("pci_scan()\n");
    pci_scan();
#endif /* PCI_LIB_SCAN */

#ifdef O_PCI_HINT_PRINT
    PCIPRT("\n");
    PCIPRT("pci_search()\n");
#endif

    memset((void*)&pci_cfg, 0, sizeof(PCI_CONFIG));
    /* search for... */
	// vendor_id & device_id are different with duagon
    pci_cfg.vendor_id = PCI_VENDOR_ID;
    pci_cfg.device_id = PCI_DEVICE_ID;
    /* start search at... */
    pci_cfg.bus  = 0; /* 0 - 255 */
    pci_cfg.dev  = 0; /* 0 -  31 */
    /* search for... */
    pci_cfg.func = 0; /* 0 -   7 */
    found = FALSE;
    result = pci_search(&pci_cfg, &found);
    if ((result == PCI_OK) && (found == TRUE))
    {
	#if defined (O_PCI_HINT_PRINT)
        result = pci_probe(pci_cfg.bus, pci_cfg.dev, pci_cfg.func, 1, 1, &pci_cfg);
	#else
		result = pci_probe(pci_cfg.bus, pci_cfg.dev, pci_cfg.func, 0, 1, &pci_cfg);
	#endif
        if (result == FALSE)
        {
		#ifdef O_PCI_HINT_PRINT
            PCIPRT("ERROR: pci_probe()=%d\n", result);
		#endif

		return(PCI_ERROR);
        } /* if (result == FALSE) */
    }
    else
    {
	#ifdef O_PCI_HINT_PRINT
       PCIPRT("ERROR: pci_search()=%d\n", result);
	#endif
        return(PCI_ERROR);
		
    } /* if ((result == PCI_OK) && (found == TRUE)) */

#ifdef O_PCI_HINT_PRINT
    PCIPRT("\n");
    PCIPRT("DONE!\n");
#endif

	// I/O: LSB = 1
	base_addr = (UNSIGNED16)(pci_cfg.base[3]&0xFFFFFFFE);
	*hw_base_addr = base_addr;
		
    return(PCI_OK);
}

#ifdef PCI_LIB_MAIN
/* --------------------------------------------------------------------------
 *  Procedure : main
 * --------------------------------------------------------------------------
 */
int
main (void)
{
    PCI_RESULT  result;
    PCI_CONFIG  pci_cfg;
    PCI_UINT16  found;

    PCIPRT("\n");
    PCIPRT("PCI Library\n");
    PCIPRT("===========\n");

    PCIPRT("\n");
    PCIPRT("pci_init()=");
    result = pci_init();
    if (result != PCI_OK)
    {
        return(-1);
    } /* if (result != PCI_OK) */
    PCIPRT("%u\n", result);

#ifdef PCI_LIB_SCAN
    PCIPRT("\n");
    PCIPRT("pci_scan()\n");
    pci_scan();
#endif /* PCI_LIB_SCAN */

    PCIPRT("\n");
    PCIPRT("pci_search()\n");
    memset((void*)&pci_cfg, 0, sizeof(PCI_CONFIG));
    /* search for... */
    pci_cfg.vendor_id = 0x9710;
    pci_cfg.device_id = 0x9845;
    /* start search at... */
    pci_cfg.bus  = 0; /* 0 - 255 */
    pci_cfg.dev  = 0; /* 0 -  31 */
    /* search for... */
    pci_cfg.func = 0; /* 0 -   7 */
    found = FALSE;
    result = pci_search(&pci_cfg, &found);
    if ((result == PCI_OK) && (found == TRUE))
    {
        result = pci_probe(pci_cfg.bus, pci_cfg.dev, pci_cfg.func, 1, 1, &pci_cfg);
        if (result == FALSE)
        {
            PCIPRT("ERROR: pci_probe()=%d\n", result);
        } /* if (result == FALSE) */
    }
    else
    {
        PCIPRT("ERROR: pci_search()=%d\n", result);
    } /* if ((result == PCI_OK) && (found == TRUE)) */

    PCIPRT("\n");
    PCIPRT("DONE!\n");

#ifdef _DEBUG
    for(;;) {;}
#endif

    return(0);

} /* main */
#endif /* PCI_LIB_MAIN */

⌨️ 快捷键说明

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