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

📄 mavcmd.c

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 C
📖 第 1 页 / 共 3 页
字号:
//****************************************************************************
//
// MAVCMD.C - Functions to communicate with the Maverick(tm) USB driver.
//
// Copyright (c) 2000 Cirrus Logic, Inc.
//
//****************************************************************************
#include <windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include "../usbdrvr/mavguid.h"
#include "../usbdrvr/mavusb.h"
#include "mavcmd.h"

//****************************************************************************
//
// The handle to the file object used to communicate with the Maverick(tm) USB
// driver.
//
//****************************************************************************
static HANDLE hFile = INVALID_HANDLE_VALUE;
static HANDLE hStatusFile = INVALID_HANDLE_VALUE;

//****************************************************************************
//
// Maverick_OpenDevice opens the Maverick Digital Audio Player.
//
//****************************************************************************
unsigned long
Maverick_OpenDevice(void)
{
    HDEVINFO hardwareDeviceInfo;
    SP_INTERFACE_DEVICE_DATA deviceInfoData;
    PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
    ULONG predictedLength = 0;
    ULONG requiredLength = 0;
    ULONG i;

    //
    // Open a handle to the plug and play dev node.
    // SetupDiGetClassDevs() returns a device information set that contains
    // info on all installed devices of a specified class.
    //
    hardwareDeviceInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_MAVERICK,
                                             NULL, NULL,
                                             (DIGCF_PRESENT |
                                              DIGCF_INTERFACEDEVICE));

    //
    // Set the size of the device info data structure.
    //
    deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

    //
    // Loop through all available USB devices until we find ours or we run out
    // of devices.
    //
    for(i = 0; ; i++)
    {
        //
        // SetupDiEnumDeviceInterfaces() returns information about device
        // interfaces exposed by one or more devices.  Each call returns
        // information about one interface; the routine can be called
        // repeatedly to get information about several interfaces exposed
        // by one or more devices.
        //
        if(SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0,
                                       (LPGUID)&GUID_CLASS_MAVERICK, i,
                                       &deviceInfoData))
        {
            //
            // Allocate a function class device data structure to receive the
            // goods about this particular device.
            //
            SetupDiGetInterfaceDeviceDetail(hardwareDeviceInfo,
                                            &deviceInfoData, NULL, 0,
                                            &requiredLength, NULL);

            //
            // Set the predicted structure length to the required length.
            //
            predictedLength = requiredLength;

            //
            // Allocate memory for the function class device data structure.
            //
            functionClassDeviceData = malloc(predictedLength);
            functionClassDeviceData->cbSize =
                sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

            //
            // Retrieve the information from Plug and Play.
            //
            if(SetupDiGetInterfaceDeviceDetail(hardwareDeviceInfo,
                                               &deviceInfoData,
                                               functionClassDeviceData,
                                               predictedLength,
                                               &requiredLength, NULL))
            {
                //
                // We were able to get the information from Plug and Play, so
                // attempt to open the device.
                //
                hFile = CreateFile(functionClassDeviceData->DevicePath,
                                   GENERIC_READ | GENERIC_WRITE,
                                   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                                   OPEN_EXISTING, 0, NULL);

                //
                // Open another handle to the device for performing status
                // checks.
                //
                hStatusFile = CreateFile(functionClassDeviceData->DevicePath,
                                         GENERIC_READ | GENERIC_WRITE,
                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
                                         NULL, OPEN_EXISTING, 0, NULL);
            }

            //
            // Free the memory for the function class device data structure.
            //
            free(functionClassDeviceData);

            //
            // If we were able to open the device, then stop looking.
            //
            if(hFile != INVALID_HANDLE_VALUE)
            {
                break;
            }
        }
        else
        {
            //
            // Stop looking if there are no more items to check.
            //
            if(GetLastError() == ERROR_NO_MORE_ITEMS)
            {
                break;
            }
        }
    }

    //
    // SetupDiDestroyDeviceInfoList() destroys a device information set
    // and frees all associated memory.
    //
    SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);

    //
    // If we were able to open the device, then return success.
    //
    if(hFile != INVALID_HANDLE_VALUE)
    {
        return(1);
    }

    //
    // Return failure.
    //
    return(0);
}

//****************************************************************************
//
// Maverick_CloseDevice closes the Maverick Digital Audio Player.
//
//****************************************************************************
void
Maverick_CloseDevice(void)
{
    //
    // Make sure that the device was opened.
    //
    if(hFile == INVALID_HANDLE_VALUE)
    {
        return;
    }

    //
    // Close the handle to the driver.
    //
    CloseHandle(hFile);
    hFile = INVALID_HANDLE_VALUE;
    CloseHandle(hStatusFile);
    hStatusFile = INVALID_HANDLE_VALUE;
}

//****************************************************************************
//
// Maverick_GetDescriptor returns the value of the specified descriptor.
//
//****************************************************************************
unsigned long
Maverick_GetDescriptor(unsigned long ulDescriptor, unsigned long ulIndex,
                       void *pvValue, unsigned long *pulLength)
{
    MavUsb_IoctlParams sParam;
    MavUsb_IoctlResults sResult;
    unsigned long ulCount, ulResult;

    //
    // Set up the "get_descriptor" request.
    //
    sParam.ulRequestCode = REQUEST_GET_DESCRIPTOR;

    //
    // Fill in the parameters for the request.
    //
    sParam.uParams.sGetDescriptor.ulDescriptor = ulDescriptor;
    sParam.uParams.sGetDescriptor.ulIndex = ulIndex;

    //
    // Send the request to the Maverick(tm) USB driver.
    //
    ulResult = DeviceIoControl(hFile, IOCTL_MAVUSB_DEVICE_REQUEST, &sParam,
                               sizeof(MavUsb_IoctlParams), &sResult,
                               sizeof(MavUsb_IoctlResults), &ulCount, NULL);

    //
    // If the IOCTL was successfuly, the correct number of bytes were returned,
    // and the request was handled by the device, then return the value of the
    // descriptor.
    //
    if(ulResult &&
       (ulCount == sizeof(MavUsb_IoctlResults)) &&
       sResult.ulReturnCode)
    {
        //
        // Return the value of the descriptor.
        //
        memcpy(pvValue, sResult.uResults.sGetDescriptor.pcValue,
               min(*pulLength, sResult.uResults.sGetDescriptor.ulLength));

        //
        // Return the length of the descriptor.
        //
        *pulLength = sResult.uResults.sGetDescriptor.ulLength;

        //
        // Success.
        //
        return(1);
    }

    //
    // Return an error.
    //
    return(0);
}

//****************************************************************************
//
// Maverick_SetDescriptor sets the value of the specified descriptor.
//
//****************************************************************************
unsigned long
Maverick_SetDescriptor(unsigned long ulDescriptor, unsigned long ulIndex,
                       void *pvValue, unsigned long ulLength)
{
    MavUsb_IoctlParams sParam;
    MavUsb_IoctlResults sResult;
    unsigned long ulCount, ulResult;

    //
    // Make sure that the length is valid.
    //
    if(ulLength > sizeof(sParam.uParams.sSetDescriptor.pcValue))
    {
        return(0);
    }

    //
    // Set up the "set_descriptor" request.
    //
    sParam.ulRequestCode = REQUEST_SET_DESCRIPTOR;

    //
    // Fill in the parameters for the request.
    //
    sParam.uParams.sSetDescriptor.ulDescriptor = ulDescriptor;
    sParam.uParams.sSetDescriptor.ulIndex = ulIndex;
    memcpy(sParam.uParams.sSetDescriptor.pcValue, pvValue, ulLength);
    sParam.uParams.sSetDescriptor.ulLength = ulLength;

    //
    // Send the request to the Maverick(tm) USB driver.
    //
    ulResult = DeviceIoControl(hFile, IOCTL_MAVUSB_DEVICE_REQUEST, &sParam,
                               sizeof(MavUsb_IoctlParams), &sResult,
                               sizeof(MavUsb_IoctlResults), &ulCount, NULL);

    //
    // If the IOCTL was successfuly, the correct number of bytes were returned,
    // and the request was handled by the device, then return success.
    //
    if(ulResult &&
       (ulCount == sizeof(MavUsb_IoctlResults)) &&
       sResult.ulReturnCode)
    {
        //
        // Success.
        //
        return(1);
    }

    //
    // Return an error.
    //
    return(0);
}

//****************************************************************************
//
// Maverick_NumDrives returns the number of drives supported by the file
// system layer.
//
//****************************************************************************
unsigned long
Maverick_NumDrives(void)
{
    MavUsb_IoctlParams sParam;
    MavUsb_IoctlResults sResult;
    unsigned long ulCount, ulResult;

    //
    // Set up the "num_drives" request.
    //
    sParam.ulRequestCode = REQUEST_NUM_DRIVES;

    //
    // Send the request to the Maverick(tm) USB driver.
    //
    ulResult = DeviceIoControl(hFile, IOCTL_MAVUSB_DEVICE_REQUEST, &sParam,
                               sizeof(MavUsb_IoctlParams), &sResult,
                               sizeof(MavUsb_IoctlResults), &ulCount, NULL);

    //
    // If the IOCTL was successfuly, the correct number of bytes were returned,
    // and the request was handled by the device, then return the number of
    // drives.
    //
    if(ulResult &&
       (ulCount == sizeof(MavUsb_IoctlResults)) &&
       sResult.ulReturnCode)
    {
        //
        // Return the number of drives we support.
        //
        return(sResult.uResults.sNumDrives.ulNumDrives);
    }

    //
    // There was an error, so return zero.
    //
    return(0);
}

//****************************************************************************
//
// Maverick_Open opens or creates the specified file on the specified drive.
//
//****************************************************************************
unsigned long
Maverick_Open(unsigned long ulDrive, const unsigned short *pusFileName,
              unsigned long ulFlags)
{
    MavUsb_IoctlParams sParam;
    MavUsb_IoctlResults sResult;
    unsigned long ulCount, ulResult;

    //
    // Set up the "open" request.
    //
    sParam.ulRequestCode = REQUEST_OPEN;
    sParam.uParams.sOpen.ulDriveNum = ulDrive;
    memcpy(sParam.uParams.sOpen.pusFileName, pusFileName, 256);
    sParam.uParams.sOpen.ulFlags = ulFlags;

    //
    // Send the request to the Maverick(tm) USB driver.
    //
    ulResult = DeviceIoControl(hFile, IOCTL_MAVUSB_DEVICE_REQUEST, &sParam,
                               sizeof(MavUsb_IoctlParams), &sResult,
                               sizeof(MavUsb_IoctlResults), &ulCount, NULL);

    //
    // If the IOCTL was successfuly, the correct number of bytes were returned,
    // and the request was handled by the device, then return success.
    //
    if(ulResult &&
       (ulCount == sizeof(MavUsb_IoctlResults)) &&
       sResult.ulReturnCode)
    {
        //
        // Success.
        //
        return(1);
    }

    //
    // Return an error.
    //
    return(0);
}

//****************************************************************************
//
// Maverick_Create creates the specified file with the given size on the
// specified drive.
//
//****************************************************************************
unsigned long
Maverick_Create(unsigned long ulDrive, const unsigned short *pusFileName,
                unsigned long ulFileLength)
{
    MavUsb_IoctlParams sParam;
    MavUsb_IoctlResults sResult;
    unsigned long ulCount, ulResult;

    //
    // Set up the "create" request.
    //
    sParam.ulRequestCode = REQUEST_CREATE;
    sParam.uParams.sCreate.ulDriveNum = ulDrive;
    memcpy(sParam.uParams.sCreate.pusFileName, pusFileName, 256);
    sParam.uParams.sCreate.ulLength = ulFileLength;

    //
    // Send the request to the Maverick(tm) USB driver.
    //
    ulResult = DeviceIoControl(hFile, IOCTL_MAVUSB_DEVICE_REQUEST, &sParam,
                               sizeof(MavUsb_IoctlParams), &sResult,
                               sizeof(MavUsb_IoctlResults), &ulCount, NULL);

⌨️ 快捷键说明

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