/******************************************************************************
*
*  System Name : ARM USB Mass Storage Library
*  File Name   : usb0_hmscd.h
*  Contents    : Definitions for USB Mass Storage Class Driver
*  Version     : 0.00.01
*  Device      : ARM
*  Tool-Chain  : 
*  OS          : 
*
*  Note        : 
*                     
*  Copyright (C) 2014 Renesas Electronics Corporation and 
*  Renesas Solutions Corp. All rights reserved.
*
*  History     : Feb.12,2014 Ver.0.00.01
******************************************************************************/
#ifndef __USB0_HMSCD_H__
#define __USB0_HMSCD_H__


/*******************************************************************************
Includes   <System Includes> , "Project Includes"
*******************************************************************************/


/*******************************************************************************
Macro definitions
*******************************************************************************/
/* USB Mass Storage Request */
#define USB_HMSCD_MASS_STORAGE_GET_MAX_LUN    0xfea1
#define USB_HMSCD_MASS_STORAGE_RESET          0xff21

/* Class driver status */
#define USB_HMSCD_DEV_DET      0x00
#define USB_HMSCD_DEV_ATT      0x01

#define MSCD_SFF8020I          (uint8_t)0x02
#define MSCD_ATAPI             (uint8_t)0x05
#define MSCD_SCSI              (uint8_t)0x06
#define MSCD_BOT               (uint8_t)0x50
#define MSCD_TOTAL_EP          (uint8_t)0x02
#define MSCD_UNKNOWN_SUB       (uint8_t)0xFF

#define MSDC_CBW_LENGTH        31
#define MSDC_CBW_SIG           0x43425355
#define MSDC_CSW_SIG           0x53425355
#define MSDC_CBW_CB_LENGTH     12
#define MSDC_CSW_LENGTH        13


/*******************************************************************************
Typedef definitions
*******************************************************************************/
typedef struct
{
    uint32_t    cbw_sig;
    uint32_t    cbw_tag;
    uint8_t     cbw_length_lo;
    uint8_t     cbw_length_ml;
    uint8_t     cbw_length_mh;
    uint8_t     cbw_length_hi;

    uint8_t     b7CBWFlags;
    uint8_t     cbw_lun;
    uint8_t     cbw_cb_length;

    uint8_t     cbw_cb[16];

} USB_HMSCD_CBW_t;

typedef struct
{
    uint32_t    csw_sig;
    uint32_t    csw_tag;
    uint8_t     csw_data_res_lo;
    uint8_t     csw_data_res_ml;
    uint8_t     csw_data_res_mh;
    uint8_t     csw_data_res_hi;
    uint8_t     csw_status;

} USB_HMSCD_CSW_t;

typedef struct{
    uint32_t               csw_data_residue;
    uint32_t               cbw_tag;
    USB_HMSCD_REGIST_t     *mscd_regist;
    uint16_t               mscd_out_pipe;
    uint16_t               mscd_in_pipe;
    uint16_t               mscd_dev_status;
    uint16_t               interface_num;
    uint8_t                cbw_command_type;
} USB_HMSCD_GLOBAL_t;


/*******************************************************************************
Variable Externs
*******************************************************************************/
extern USB_HMSCD_GLOBAL_t    g_usb0_hmscd_global[ USB_HMSCD_MAX_ADDRESS + 1 ];
extern USB_HMSCD_CBW_t       g_usb0_hmscd_cbw[ USB_HMSCD_MAX_ADDRESS + 1 ];
extern USB_HMSCD_CSW_t       g_usb0_hmscd_csw[ USB_HMSCD_MAX_ADDRESS + 1 ];
extern uint32_t              g_usb0_hmscd_work[ USB_HMSCD_MAX_ADDRESS + 1 ][ 32 / sizeof(uint32_t) ];


/*******************************************************************************
Functions Prototypes
*******************************************************************************/
void        usb0_hmscd_cbw_init( uint16_t device_address );
int32_t     usb0_hmscd_check_bot_descriptor( uint16_t device_address, uint8_t* );
int32_t     usb0_hmscd_pipe_info( uint16_t device_address,uint8_t* Table, uint16_t length  );
int32_t     usb0_hmscd_no_data( uint16_t device_address,uint32_t *bytes,uint8_t *cswstatus );
int32_t     usb0_hmscd_data_in( uint16_t device_address,uint8_t *buffer, 
                                uint32_t size,uint32_t *bytes,uint8_t *cswstatus );
int32_t     usb0_hmscd_data_out( uint16_t device_address,uint8_t *buffer, 
                                 uint32_t size,uint32_t *bytes,uint8_t *cswstatus );
void        usb0_hmscd_cbw_tag_count( uint16_t device_address );
int32_t     usb0_hmscd_check_csw( uint16_t device_address,uint8_t *cswstatus );
void        usb0_hmscd_set_dev_sts( uint16_t device_address, uint16_t data );
uint16_t    usb0_hmscd_get_dev_sts( uint16_t device_address );
int32_t     usb0_hmscd_send_cbw( uint16_t device_address );
int32_t     usb0_hmscd_get_data( uint16_t device_address,uint8_t *buffer, 
                                 uint32_t size,uint32_t *bytes );
int32_t     usb0_hmscd_send_data( uint16_t device_address,uint8_t *buffer, 
                                  uint32_t size,uint32_t *bytes );
int32_t     usb0_hmscd_get_csw( uint16_t device_address,uint8_t *cswstatus );


#endif
