/***************************************************************************//**
* \file CDC_cdc.c
* \version 3.20
*
* \brief
*  This file contains the USB MSC Class request handler and global API for MSC 
*  class.
*
* Related Document:
*  Universal Serial Bus Class Definitions for Communication Devices Version 1.1
*
********************************************************************************
* \copyright
* Copyright 2012-2016, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#include "CDC_msc.h"
#include "CDC_pvt.h"
#include "cyapicallbacks.h"

#if (CDC_HANDLE_MSC_REQUESTS)

/***************************************
*          Internal variables
***************************************/

static uint8 CDC_lunCount = CDC_MSC_LUN_NUMBER;


/*******************************************************************************
* Function Name: CDC_DispatchMSCClassRqst
****************************************************************************//**
*   
*  \internal 
*  This routine dispatches MSC class requests.
*
* \return
*  Status of request processing: handled or not handled.
*
* \globalvars
*  CDC_lunCount - stores number of LUN (logical units).
*
* \reentrant
*  No.
*
*******************************************************************************/
uint8 CDC_DispatchMSCClassRqst(void) 
{
    uint8 requestHandled = CDC_FALSE;
    
    /* Get request data. */
    uint16 value  = CDC_GET_UINT16(CDC_wValueHiReg,  CDC_wValueLoReg);
    uint16 dataLength = CDC_GET_UINT16(CDC_wLengthHiReg, CDC_wLengthLoReg);
       
    /* Check request direction: D2H or H2D. */
    if (0u != (CDC_bmRequestTypeReg & CDC_RQST_DIR_D2H))
    {
        /* Handle direction from device to host. */
        
        if (CDC_MSC_GET_MAX_LUN == CDC_bRequestReg)
        {
            /* Check request fields. */
            if ((value  == CDC_MSC_GET_MAX_LUN_WVALUE) &&
                (dataLength == CDC_MSC_GET_MAX_LUN_WLENGTH))
            {
                /* Reply to Get Max LUN request: setup control read. */
                CDC_currentTD.pData = &CDC_lunCount;
                CDC_currentTD.count =  CDC_MSC_GET_MAX_LUN_WLENGTH;
                
                requestHandled  = CDC_InitControlRead();
            }
        }
    }
    else
    {
        /* Handle direction from host to device. */
        
        if (CDC_MSC_RESET == CDC_bRequestReg)
        {
            /* Check request fields. */
            if ((value  == CDC_MSC_RESET_WVALUE) &&
                (dataLength == CDC_MSC_RESET_WLENGTH))
            {
                /* Handle to Bulk-Only Reset request: no data control transfer. */
                CDC_currentTD.count = CDC_MSC_RESET_WLENGTH;
                
            #ifdef CDC_DISPATCH_MSC_CLASS_MSC_RESET_RQST_CALLBACK
                CDC_DispatchMSCClass_MSC_RESET_RQST_Callback();
            #endif /* (CDC_DISPATCH_MSC_CLASS_MSC_RESET_RQST_CALLBACK) */
                
                requestHandled = CDC_InitNoDataControlTransfer();
            }
        }
    }
    
    return (requestHandled);
}


/*******************************************************************************
* Function Name: CDC_MSC_SetLunCount
****************************************************************************//**
*
*  This function sets the number of logical units supported in the application. 
*  The default number of logical units is set in the component customizer.
*
*  \param lunCount: Count of the logical units. Valid range is between 1 and 16.
*
*
* \globalvars
*  CDC_lunCount - stores number of LUN (logical units).
*
* \reentrant
*  No.
*
*******************************************************************************/
void CDC_MSC_SetLunCount(uint8 lunCount) 
{
    CDC_lunCount = (lunCount - 1u);
}


/*******************************************************************************
* Function Name: CDC_MSC_GetLunCount
****************************************************************************//**
*
*  This function returns the number of logical units.
*
* \return
*   Number of the logical units.
*
* \globalvars
*  CDC_lunCount - stores number of LUN (logical units).
*
* \reentrant
*  No.
*
*******************************************************************************/
uint8 CDC_MSC_GetLunCount(void) 
{
    return (CDC_lunCount + 1u);
}   

#endif /* (CDC_HANDLE_MSC_REQUESTS) */


/* [] END OF FILE */
