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

📄 ncdevice.c

📁 CE下 NET2778 NDIS Drivers, 在每个平台上都可以使用
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************

Copyright (C) 2004, NetChip Technology, Inc. (http://www.netchip.com)

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

NCDEVICE.C

NetChip device level interface. The lower edge of this module interfaces
to the NetChip Hardware Abstraction Layer (NcHAL); the upper edge connects
to the NetChip API using a private interface. (Applications should not
require access to this module's functions! Applications should use the
NetChip API interface module)

Primarily, this device level module handles: 
 - Chip initialization
 - USB Chapter Nine compliance, including enumeration and control transfers
 - Device events (PnP, suspend, etc.)
 - Support for API 

NetChip support: If you have technical problems, comments or feedback about this product, 
please contact us: support@netchip.com. Please write "Firmware API" in the subject.
In the body, please provide the software release code (e.g. RE010203), the application
filename, the NetChip chip you are using, plus any helpful details.

******************************************************************************/

///////////////////////////////////////////////////////////////////////////////
#if _NC_RDK_AND_WINDOWS
// Compiling firmware for NetChip PCI-RDK in a Windows environment:
//  - When running under Windows, NetChip's special RDK software uses Windows-OS 
//    specific functions to communicate with the NET2272 and the RDK driver. These
//    are not applicable when porting to non-Windows and non-RDK platforms
#include <NcPci.h>          // Interface to NetChip PCI RDK driver
#include <Nc2272Db.h>       // NET2272 RDK daughterboard register and bit definitions
#endif  // _NC_RDK_AND_WINDOWS

///////////////////////////////////////////////////////////////////////////////
#include <NcCommon.h>       // Collection of common NetChip header files
#include <NcFwApiPriv.h>    // Private interface (Apps do not directly reference content)
#include <NcDevice.h>       // Useful NET2272 functions, types and macros (for USB transfers, etc.)

///////////////////////////////////////////////////////////////////////////////
#pragma message (_NC_MESSAGE"Recommendation: Port this file directly, without functional changes")
// Few, if any functional changes should be required when porting this file to other
// platforms. NetChip has taken every effort to make this file applicable for any usage 
// on any platform:
//  - Optimization: After successful porting and all applicable tests pass reliably,
//    unused sections may be discarded. Other sections may be tuned for performance.
//  - Please contact NetChip support if this file fails to meet your requirements

///////////////////////////////////////////////////////////////////////////////
#ifndef NET2272_16BIT
#pragma message ("Nc2272.C: NET2272_16BIT not defined!!\n")
#endif

///////////////////////////////////////////////////////////////////////////////
// Helper macro for API's private Endpoint Zero transfers:
#define NC_EP0_TRANSFER(Buffer, Size) \
    PrivateTransferZero.TransferBuffer = (PBYTE)(Buffer); \
    PrivateTransferZero.TransferSize = (Size); 

///////////////////////////////////////////////////////////////////////////////
// Private variables
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Transfer object used for Standard Requests on Endpoint Zero
NC_TRANSFER_OBJECT PrivateTransferZero;

// Store client's Transfer Object here while handling Standard Requests
PNC_TRANSFER_OBJECT ClientTransferZero;

///////////////////////////////////////////////////////////////////////////////
// Semi-private variables
//  - These variables are shared between the Device and the API
//  - Clients should NOT need these variables
extern PNC_DEVICE_OBJECT PrivDeviceObject;
extern PUSB_STRING_DESCRIPTOR UsbStrings[];
extern PNC_ENDPOINT_OBJECT PhysicalEndpoints[];
extern NC_ENDPOINT_OBJECT LogicalEndpoints[];
extern PUSB_CONFIGURATION_DESCRIPTOR UsbConfiguration;
extern WORDBYTE Sizeof_UsbConfiguration;
extern PUSB_CONFIGURATION_DESCRIPTOR UsbConfiguration_OtherSpeed;
extern WORDBYTE Sizeof_UsbConfiguration_OtherSpeed;

///////////////////////////////////////////////////////////////////////////////
// Public variables
///////////////////////////////////////////////////////////////////////////////

// Base address of NetChip chip on PCI-RDK
PNETCHIP_DATA_TYPE NetchipBaseAddress;         // Base address of NetChip chip registers

///////////////////////////////////////////////////////////////////////////////
// Semi-private prototypes
//  - These functions are shared privately between the NetChip device and API modules

///////////////////////////////////////////////////////////////////////////////
// Reset all data endpoints
//  - Does NOT include endpoint zero
void
NcResetDataEp(
    PNC_DEVICE_OBJECT DeviceObject
    );


///////////////////////////////////////////////////////////////////////////////
// Helper functions:
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
NCSTATUS 
NcDev_AreYouThere(
    void
    )
{   // Quick test to see if CPU can communicate properly with the NET2272
    //  - Verifies connection using writes and reads to write/read and read-only registers

#pragma message(_NC_MESSAGE"NcDev_AreYouThere(): NET2272 chip test may not be needed")
    // This routine is strongly recommended especially during early bring-up of new
    // hardware, however for designs that do not apply Power On System Tests (POST) 
    // it may discarded (or perhaps just reduced).

    UINT ii;
    BYTE Val, RefVal;

    // Verify NET2272 write/read SCRATCH register can write and read
    RefVal = (BYTE)NETCHIP_READ(SCRATCH);
    for (ii = 0; ii < 0x100; ii += 7)
    {
        NETCHIP_WRITE(SCRATCH, ii);
        if ((Val = NETCHIP_READ(SCRATCH)) != ii)
        {
            NCPRINTF(VOLUME_LOW, ("AreYouThere(): write/read SCRATCH register test failed: wrote:0x%2.2x, read:0x%2.2x\n",
                ii, Val
                ));
            return NCSTATUS_UNSUCCESSFUL;
        }
    }
    // To be nice, we write the original SCRATCH value back:
    NETCHIP_WRITE(SCRATCH, RefVal);

    // Verify NET2272 CHIPREV register is read-only:
    RefVal = NETCHIP_READ(CHIPREV_2272);
    for (ii = 0; ii < 0x100; ii += 7)
    {
        NETCHIP_WRITE(CHIPREV_2272, ii);
        if ((Val = NETCHIP_READ(CHIPREV_2272)) != RefVal)
        {
            NCPRINTF(VOLUME_LOW, ("AreYouThere(): write/read CHIPREV register test failed: wrote 0x%2.2x, read:0x%2.2x expected:0x%2.2x\n",
                ii, Val, RefVal));
            return NCSTATUS_UNSUCCESSFUL;
        }
    }

    // Verify NET2272's "NET2270 legacy revision" register
    //  - NET2272 has two revision registers. The NET2270 legacy revision register should
    //    read the same value, regardless of the NET2272 silicon revision. (The legacy
    //    register applies to NET2270 firmware being applied to the NET2272)
    Val = NETCHIP_READ(CHIPREV_LEGACY);
    if (Val != NET2270_LEGACY_REV)
    {   // Unexpected legacy revision value
        //  - Perhaps the chip is a NET2270?
        NCPRINTF(VOLUME_LOW, (
            "NcDev_AreYouThere(): Incorrect legacy register value:\n"
            " - CHIPREV_LEGACY: expected 0x%2.2x, got:0x%2.2x. (Not NET2272?)\n",
            NET2270_LEGACY_REV, 
            Val
            ));
        return NCSTATUS_UNSUCCESSFUL;
    }

    // Verify NET2272 silicon revision
    //  - This revision register is appropriate for the silicon version of the NET2272
    Val = NETCHIP_READ(CHIPREV_2272);

    switch (Val)
    {   // This NET2272 firmware is designed for these versions of NET2272 silicon:
    case CHIPREV_NET2272_R1:
        break;
    case CHIPREV_NET2272_R1A:
        break;
    default:
        // NET2272 silicon version *may* not work with this firmware
        //  - Show warning:
        NCPRINTF(VOLUME_LOW, (
            "NcDev_AreYouThere(): Unexpected NET2272 silicon revision register value:\n"
            " - CHIPREV_2272: 0x%2.2x\n",
            Val
            ));
        // Return Success, even though the chip rev is not an expected value
        //  - Older, pre-built firmware can attempt to operate on newer silicon
        //  - Often, new silicon is perfectly compatible
    }

    // Success: NET2272 checks out OK
    PrivDeviceObject->ChipState = NC_CHIP_STATE_RUNNING;
    return NCSTATUS_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////
void
NcDev_TestMode(
    UINT TestMode
    )
{   // Place NET2272 into a test mode (USBTEST)
    //  - USB 2.0 devices are required to support Test Modes
    //  - We are usually called from setup request handler (host-initiated test mode) 
    //    but for special testing we can also be called from main(), (i.e. with no host connection)
    static BYTE UsbTestPacket[] = 
    {   // Packet data required for Test_Packet test mode
        //  - USB test packet (See TEST_MODE, TEST_PACKET, USB 2.0: 7.1.20)
        //  - See NET2272 spec "USB Test Modes" to verify correct content and length
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,                   // JKJKJKJK * 9
        0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,                         // JJKKJJKK * 8
        0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,                         // JJJJKKKK * 8
        0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // JJJJJJJKKKKKKK * 8
        0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,                               // JJJJJJJK * 8
        0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E                    // {JKKKKKKK * 10}, JK
    };
    UINT ii;

    ASSERT(!(NETCHIP_READ(DMAREQ) & (1<<DMA_REQUEST_ENABLE)))   // DMA must not be active!

    // Disable all NET2272 interrupts
    //  - USB 2.0: 7.1.20: "... the exit action is to power cycle the device." In other
    //    words, nothing (even a cable unplug) should stop a test mode test.
    NETCHIP_WRITE(IRQENB0, 0x00);
    NETCHIP_WRITE(IRQENB1, 0x00);

    // Force High-Speed USB
    NETCHIP_WRITE(XCVRDIAG, 1<<FORCE_HIGH_SPEED);

    NETCHIP_WRITE(EP_STAT0, (1<<DATA_PACKET_TRANSMITTED_INTERRUPT));
    NETCHIP_WRITE(EP_RSPCLR, 
        (1<<CONTROL_STATUS_PHASE_HANDSHAKE) |
        (1<<HIDE_STATUS_PHASE) |
        0);
    NETCHIP_WRITE(EP_CFG, (1<<ENDPOINT_DIRECTION)); // Force direction to IN 
    NETCHIP_WRITE(EP_STAT1, (1<<BUFFER_FLUSH)); 

    // Wait a reasonable interval for status phase to complete
    for (ii = 5; ii; ii--)
    {
        if (NETCHIP_READ(EP_STAT0) & (1<<DATA_PACKET_TRANSMITTED_INTERRUPT))
        {   // Status phase complete
            break;
        }
        SLEEP(100);
    }

    // Start USB test mode
    NETCHIP_WRITE(USBTEST, TestMode);

    if (TestMode == USB_TEST_PACKET)
    {   // Load test packet
#if NET2272_16BIT
        // Switch to 8-bit EP_DATA access
        NETCHIP_WRITE(LOCCTL, NETCHIP_READ(LOCCTL) & ~(1<<DATA_WIDTH));
#endif
        for (ii = 0; ii < sizeof(UsbTestPacket); ii++)
        {
            NETCHIP_WRITE(EP_DATA, UsbTestPacket[ii]);
        }

⌨️ 快捷键说明

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