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

📄 system_original.c

📁 三星2410的BSP开发包
💻 C
📖 第 1 页 / 共 4 页
字号:

    //
    // 2. Choose which configuration to use
    //
    nCfg = sizeof(CfTable)/sizeof(PARSED_CFTABLE);
    status = v_pfnCardGetParsedTuple(
                pDisk->d_hSock,
                CISTPL_CFTABLE_ENTRY,
                CfTable,
                &nCfg);
    if (status) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: CardGetParsedTuple(CISTPL_CFTABLE_ENTRY) failed %d\r\n"),
            status));
        return status;
    }

    // if info is unavailable, try for performance rather than power-savings
    fPowerAC = TRUE;
    if (v_pfnGetSystemPowerStatusEx && (hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, (TEXT("SYSTEM/GweApiSetReady")))) != NULL) {
        SYSTEM_POWER_STATUS_EX PowerStatus;
        WaitForSingleObject(hEvent, INFINITE);
        if (v_pfnGetSystemPowerStatusEx(&PowerStatus, TRUE))
            fPowerAC = (PowerStatus.ACLineStatus == AC_LINE_ONLINE);
    }
    req_vcc = fPowerAC ? 50 : 33;
    reg_base = ATA_IO_REG_BASE;

ac_try_again:
    pDisk->d_f16Bit = FALSE;
    for (i = 0; i < nCfg; i++) {
        DEBUGMSG(ZONE_PCMCIA, (TEXT("ATADISK Cfg[%d] = x%02x: def:%c %d dV  %d ranges\n"),
                               i, CfTable[i].ConfigIndex, CfTable[i].ContainsDefaults?'Y':'N',
                               CfTable[i].VccDescr.NominalV, CfTable[i].NumIOEntries));
        if (!isVoltageNear(req_vcc, CfTable[i].VccDescr.NominalV))
            continue;
        for (j = 0; j < CfTable[i].NumIOEntries; j++) {
            DEBUGMSG(ZONE_PCMCIA, (TEXT("ATADISK Cfg[%d]: r %d: base 0x%x\n"), i, j, CfTable[i].IOBase[j]));
            if (CfTable[i].IOBase[j] == reg_base) {
                //
                // 3. Request and map an I/O window to the ATA registers
                //
                k = CfTable[i].NumIOAddrLines;
                if (k < 12) k = 12;
                switch (GetATAWindows(pDisk, CfTable[i].IOAccess, reg_base, (k >= 16) ? 0 : (1 << k))) {
                case GAW_SUCCESS:
                    goto ac_address_ok;

                case GAW_PRIMARY_FAIL:
                    reg_base = ATA_IO_REG_BASE_SECONDARY;
                    goto ac_try_again;

                case GAW_SECONDARY_FAIL:
                    goto try_memory_registers;
                }
            }
        }
    }
    // No I/O configurations matched our requested voltage so fall back and try again
    if ((fPowerAC ? 50 : 33) == req_vcc) {
        req_vcc = fPowerAC ? 33 : 50;
        // we assume that any I/O window available at one voltage is supported at both,
        // so the only way to get here is if no windows are supported at the preferred voltage;
        // hence there is no need to reset reg_base.
        goto ac_try_again;
    }

    // No valid I/O configurations.  Should not happen, but if it does, flag an error
    goto config_error;
    
try_memory_registers: 
    //
    // Couldn't map a valid I/O address range (perhaps device doesn't support I/O access for this
    // slot). Try a common memory window.
    //
    for (i = 0; i < nCfg; i++) {
        if (CfTable[i].IFacePresent && (CfTable[i].IFaceType == 0)) {  /* Constant defined somewhere? */
            DEBUGMSG(ZONE_PCMCIA, (TEXT("ATADISK: Config 0x%x (Mem) supports "), CfTable[i].ConfigIndex));
            //
            // For now, just use 8 bit access
            //
            DEBUGMSG(ZONE_PCMCIA, (TEXT("8-bit only access\r\n")));
            if (GetATAWindows(pDisk, ACCESS_MEMORY_ONLY, 0, 0) == GAW_SUCCESS) {
                pDisk->d_Flags |= ATADISK_FLAG_MEMORY_MAPPED;
                goto ac_address_ok;
            }
        }
    }

config_error:
    DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
             (TEXT("ATADISK: Unable to find adequate configuration!\r\n")));
    return CERR_BAD_BASE;

ac_address_ok:
    //
    // 4. Request the PCMCIA interrupt
    //
    status = v_pfnCardRequestIRQ(
                pDisk->d_hPcmcia,
                pDisk->d_hSock,
                (CARD_ISR)PcmciaIntr,
                (DWORD)pDisk);
    if (status != CERR_SUCCESS) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: CardRequestIRQ failed %d\r\n"), status));
        return status;
    }

    //
    // 5. Request the configuration (this will enable the interrupt)
    //
ac_req_config:
    CfgInfo.hSocket = pDisk->d_hSock;
    CfgInfo.fAttributes = CFG_ATTR_IRQ_STEERING;
    CfgInfo.fInterfaceType = CFG_IFACE_MEMORY_IO;
    CfgInfo.uVcc = req_vcc;
    CfgInfo.uVpp1 = 0;
    CfgInfo.uVpp2 = 0;
    CfgInfo.fRegisters = CFG_REGISTER_CONFIG|CFG_REGISTER_STATUS;
    CfgInfo.uConfigReg = CfTable[i].ConfigIndex;
    CfgInfo.uStatusReg = 0;
    
    DEBUGMSG(ZONE_PCMCIA|ZONE_INIT,
             (TEXT("ATADISK: CardRequestConfiguration 0x%x at %d dV\r\n"),
              CfgInfo.uConfigReg, CfgInfo.uVcc));

    status = v_pfnCardRequestConfiguration(
                pDisk->d_hPcmcia,
                &CfgInfo);
    if (status != CERR_SUCCESS) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: CardRequestConfiguration failed %d\r\n"), status));

        if (status == CERR_BAD_VCC && (fPowerAC ? 50 : 33) == req_vcc) {
            req_vcc = fPowerAC ? 33 : 50;  // try the other known Vcc
            goto ac_req_config;
        }
    }

    return status;
}   // ATAConfig
    

//
// ATAInit:
// 1. Find which socket the disk card is in by looking in the active device key
// 2. Register as a client driver with PCMCIA.DLL.
// 3. Find and set up the PCMCIA I/O card configuration to use.
// 4. Init the disk card
//
// ActivePath is the registry path to our device's active key under
// HKEY_LOCAL_MACHINE\Drivers\Active
//
// Return pointer to new disk structure or NULL.
//
PDISK
ATAInit(
    CARD_SOCKET_HANDLE hSock,
    LPTSTR ActiveKey
    )
{
    DWORD status;
    CARD_REGISTER_PARMS Parms;
    PDISK pDisk;
    DWORD ValLen;
    HKEY  hKey;

    pDisk = CreateDiskObject();
    if (pDisk == NULL) {
        DEBUGMSG(ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: LocalAlloc(PDISK) failed %d\r\n"), GetLastError()));
        return NULL;
    }

    pDisk->d_hSock = hSock;
    if (pDisk->d_ActivePath = LocalAlloc(LPTR, wcslen(ActiveKey)*sizeof(WCHAR)+sizeof(WCHAR))) {
        wcscpy(pDisk->d_ActivePath, ActiveKey);
    }

    //
    // Register callback function with PCMCIA driver
    //
    DEBUGMSG(ZONE_PCMCIA|ZONE_INIT,
        (TEXT("ATADISK: Attempting to register with PCMCIA.DLL\r\n")));

    Parms.fEventMask =  EVENT_MASK_CARD_DETECT;
    Parms.uClientData = 0;

    Parms.fAttributes = CLIENT_ATTR_IO_DRIVER | CLIENT_ATTR_NOTIFY_SHARED |
                        CLIENT_ATTR_NOTIFY_EXCLUSIVE;
    pDisk->d_hPcmcia = (CARD_CLIENT_HANDLE)v_pfnCardRegisterClient(PcmciaCallBack,&Parms);
    if (pDisk->d_hPcmcia == 0) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: CardRegisterClient failed %d\r\n"), GetLastError()));
        goto ai_fail;
    }

    ValLen = sizeof(DWORD);
    hKey = OpenDriverKey( ActiveKey);
    if (hKey) {
        if (ERROR_SUCCESS != RegQueryValueEx(hKey, L"Settings", NULL, NULL, (PUCHAR)&pDisk->d_Flags, &ValLen)) {
            pDisk->d_Flags = 0;
        } else {
            DEBUGMSG( ZONE_INIT, (L"ATADISK: Settings = %08X\r\n", pDisk->d_Flags));
        }
        RegCloseKey( hKey);
    }    


    //
    // ATAConfig figures out which configuration to use and maps an I/O window
    // and does all the other PCMCIA specific setup.
    //
    status = ATAConfig(pDisk);
    if (status != CERR_SUCCESS) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: ATAConfig failed %d\r\n"), status));
        goto ai_fail;
    }

    //
    // InitDisk 
    //
    
    status = InitDisk(pDisk, ActiveKey);
    if (status) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_INIT|ZONE_ERROR,
            (TEXT("ATADISK: InitDisk failed %d\r\n"), status));
        goto ai_fail;
    }

    if (pDisk->d_DiskCardState == STATE_INITING) {
        pDisk->d_DiskCardState = STATE_CLOSED;
        DEBUGMSG(ZONE_INIT,
            (TEXT("ATADISK: ATAInit returning 0x%x\r\n"), pDisk));
        return pDisk;
    }


ai_fail:
    CloseDisk(pDisk);
    return NULL;
}   // ATAInit


//
// Function to retrieve the pcmcia socket handle from the device's active key
//
DWORD
ATAGetSocket(
    LPTSTR ActivePath,
    CARD_SOCKET_HANDLE * pSock
    )
{
    DWORD ValType;
    DWORD ValLen;
    HKEY hDiskCardKey;
    DWORD status;

    //
    // Get CARD_SOCKET_HANDLE from active device registry key
    //
    status = RegOpenKeyEx(
                HKEY_LOCAL_MACHINE,
                ActivePath,
                0,
                0,
                &hDiskCardKey);
    if (status) {
        DEBUGMSG(ZONE_INIT|ZONE_ERROR,
                 (TEXT("ATADISK:ATAGetSocket RegOpenKeyEx(HLM\\%s) returned %d!!!\r\n"),
                  ActivePath, status));
        return status;
    }

    ValLen = sizeof(CARD_SOCKET_HANDLE);
    status = RegQueryValueEx(
                hDiskCardKey,
                DEVLOAD_SOCKET_VALNAME,
                NULL,
                &ValType,
                (PUCHAR)pSock,
                &ValLen);
    if (status != ERROR_SUCCESS) {
        DEBUGMSG(ZONE_INIT|ZONE_ERROR,
                 (TEXT("ATADISK:ATAGetSocket - RegQueryValueEx(%s) returned %d\r\n"),
                  DEVLOAD_SOCKET_VALNAME, status));
    }
    RegCloseKey(hDiskCardKey);
    return status;
}   // ATAGetSocket


//
// Function to detect an ATA device.  This detection relies on the fact that
// device.exe has already determined there is a disk device in this socket by
// looking at the CISTPL_FUNCID tuple.  This function will look at the CISTPL_FUNCE
// tuples to see if it is an ATA device.
//
// Return TRUE if there is an ATA device in the specified socket; FALSE if not.
BOOL
ATADetect(
    CARD_SOCKET_HANDLE hSock
    )
{
    DWORD status;
    UCHAR buf[sizeof(CARD_DATA_PARMS) + 256];
    PCARD_TUPLE_PARMS pParms;
    PCARD_DATA_PARMS  pTuple;
    PUCHAR pData;

    //
    // A type 1 CISTPL_FUNCE will tell if it is an ATA device.
    //
    pParms = (PCARD_TUPLE_PARMS)buf;
    pParms->hSocket = hSock;
    pParms->uDesiredTuple = CISTPL_FUNCE;
    pParms->fAttributes = 0;
    status = v_pfnCardGetFirstTuple(pParms);
    if (status != CERR_SUCCESS) {
        DEBUGMSG(ZONE_PCMCIA|ZONE_ERROR,
            (TEXT("ATADISK:CardGetFirstTuple returned %d\r\n"), status));
        return FALSE;
    }

    while (status == CERR_SUCCESS) {
        pTuple = (PCARD_DATA_PARMS)buf;
        pTuple->uBufLen = sizeof(buf) - sizeof(CARD_DATA_PARMS);
        pTuple->uTupleOffset = 0;
        status = v_pfnCardGetTupleData(pTuple);
        if (status != CERR_SUCCESS) {
            DEBUGMSG(ZONE_PCMCIA|ZONE_ERROR,
                (TEXT("DEVLOAD: CardGetTupleData returned %d\r\n"), status));
            return FALSE;
        }

        pData = buf + sizeof(CARD_DATA_PARMS);
        //
        // If the CISTPL_FUNCE is type 1 and its data is 1, then it is an ATA device
        //
        if ((pData[0] == 1) && (pData[1] == 1)) {
            return TRUE;
        }

        status = v_pfnCardGetNextTuple(pParms);
    }   // while

    return FALSE;
}   // ATADetect

//
// Detection modules entrypoint.  Device.exe will call here to give this
// driver a chance to detect a newly inserted card.
//
// Return NULL if undetected or device key name of driver to load.
//
LPTSTR
DetectATADisk(
    CARD_SOCKET_HANDLE hSock,
    UCHAR DevType,
    LPTSTR DevKey,
    DWORD  DevKeyLen
    )
{
    if (DevType == PCCARD_TYPE_FIXED_DISK) {
        if (ATADetect(hSock) == TRUE) {
            _tcscpy(DevKey, TEXT("ATADisk"));
            return DevKey;
        }
    }
    return NULL;
}

⌨️ 快捷键说明

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