/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
* Copyright (C) 2014 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* File Name    : usb_hmscd_fsi.h
* $Rev: 682 $
* $Date:: 2014-01-23 09:15:07 +0900#$
* Description  : USB Sample Program
*******************************************************************************/
#ifndef __USB_HMSCD_FSI_H__
#define __USB_HMSCD_FSI_H__


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


/*******************************************************************************
Macro definitions
*******************************************************************************/
#define        USB_HMSCD_FSI_TEST_UNIT_DISABLE    0
#define        USB_HMSCD_FSI_TEST_UNIT_ENABLE     1

#define        USB_HMSCD_FSI_INQUIRY_COUNT        2

#define        USB_HMSCD_FSI_USB_MAX_UNIT         4
#define        USB_HMSCD_FSI_MAX_DRIVE            20
#define        USB_HMSCD_FSI_MAX_PARTITION        4
#define        USB_HMSCD_FSI_USB_DRIVE            0
#define        USB_HMSCD_FSI_MAX_DEVICE_NUM       10
#define        USB_HMSCD_FSI_WORK_SIZE            4096u

/* Partision check */
#define        USB_HMSCD_FSI_PBR_ADDS             1
#define        USB_HMSCD_FSI_MBR_ADDR             2
#define        USB_HMSCD_FSI_EMBR_ADDR            3
#define        USB_HMSCD_FSI_BOOT_SIG             (uint16_t)0xaa55
#define        USB_HMSCD_FSI_NOP_CODE             (uint8_t)0x90
#define        USB_HMSCD_FSI_JMP_CODE1            (uint8_t)0xeb
#define        USB_HMSCD_FSI_JMP_CODE2            (uint8_t)0xe9

/* Partition type */
#define        USB_HMSCD_FSI_PT_NONE              (uint8_t)0x00
#define        USB_HMSCD_FSI_PT_FAT12A            (uint8_t)0x01
#define        USB_HMSCD_FSI_PT_FAT16A            (uint8_t)0x04
#define        USB_HMSCD_FSI_PT_EPRTA             (uint8_t)0x05
#define        USB_HMSCD_FSI_PT_FAT16B            (uint8_t)0x06
#define        USB_HMSCD_FSI_PT_FAT32A            (uint8_t)0x0B
#define        USB_HMSCD_FSI_PT_FAT32X            (uint8_t)0x0C
#define        USB_HMSCD_FSI_PT_FAT16X            (uint8_t)0x0E
#define        USB_HMSCD_FSI_PT_EPRTB             (uint8_t)0x0F
#define        USB_HMSCD_FSI_PT_EPRTC             (uint8_t)0x85

#define        USB_HMSCD_FSI_PT_FAT12             (uint8_t)0x01
#define        USB_HMSCD_FSI_PT_FAT16             (uint8_t)0x04
#define        USB_HMSCD_FSI_PT_FAT32             (uint8_t)0x0B
#define        USB_HMSCD_FSI_PT_EPRT              (uint8_t)0x05


/*******************************************************************************
Typedef definitions
*******************************************************************************/
enum
{
    USB_HMSCD_FSI_DEVICE_NOTREADY = 0,
    USB_HMSCD_FSI_DEVICE_CHANGE_TO_READY,
    USB_HMSCD_FSI_DEVICE_READY
};

typedef struct
{
    int32_t    ready;
    int32_t    type;
} USB_HMSCD_FSI_DEVICE_STATUS_t;

typedef struct
{
    USB_HMSCD_FSI_DEVICE_STATUS_t    device[USB_HMSCD_FSI_USB_MAX_UNIT];
    int32_t                          mount_drive[USB_HMSCD_FSI_USB_MAX_UNIT];
    int32_t                          max_lun;
} USB_HMSCD_FSI_DEVICE_INFO_t;

typedef struct
{
    int32_t     ready;
    uint16_t    device_address;
    uint16_t    unit_number;
    uint32_t    pbr_lsn;
    uint32_t    mbr_lsn;
    uint32_t    sector_size;
} USB_HMSCD_FSI_PBR_INFO_t;

typedef struct
{
    uint8_t    jmp_code;
    uint8_t    jmp_addr;
    uint8_t    nop_code;
    uint8_t    bsr_code[443];
    uint8_t    partition_table[64];
    uint8_t    signature[2];
} USB_HMSCD_FSI_MBR_t;

typedef struct
{
    uint8_t    active_flag;
    uint8_t    start_head;
    uint8_t    start_cs[2];
    uint8_t    partition_type;
    uint8_t    stop_head;
    uint8_t    stop_cs[2];
    uint8_t    start_sector_num[4];
    uint8_t    partition_sector[4];
} USB_HMSCD_FSI_PT_TABLE_t;

typedef struct
{
    uint8_t    jmp_code;
    uint8_t    jmp_addr;
    uint8_t    nop_code;
    uint8_t    name[8];
    uint8_t    sector_size[2];
    uint8_t    cluster_size;
    uint8_t    reserved_sector[2];
    uint8_t    fat_count;
    uint8_t    root_dir_top[2];
    uint8_t    total_sector0[2];
    uint8_t    media_type;
    uint8_t    fat_sector[2];
    uint8_t    track_sector[2];
    uint8_t    cylinder_sector[2];
    uint8_t    offset_sector[4];
    uint8_t    total_sector1[4];
    uint8_t    fat_sig_data[474];
    uint8_t    signature[2];
} USB_HMSCD_FSI_PBR_t;

typedef struct
{
    uint8_t    drive_num;
    uint8_t    Reserve;
    uint8_t    boot_sig;
    uint8_t    volume_sirial[4];
    uint8_t    volume_label[11];
    uint8_t    file_system_type[8];
} USB_HMSCD_FSI_FAT_12_16_t;

typedef struct
{
    uint8_t    fat_sector[4];
    uint8_t    extened_flag[2];
    uint8_t    file_system_version[2];
    uint8_t    root_dir_cluster[4];
    uint8_t    fs_info_sector[2];
    uint8_t    bak_boot_sector[2];
    uint8_t    reserved12[12];
    uint8_t    drive_num;
    uint8_t    Reserve;
    uint8_t    boot_sig;
    uint8_t    volume_sirial[4];
    uint8_t    volume_label[11];
    uint8_t    file_system_type[8];
} USB_HMSCD_FSI_FAT_32_t;


/*******************************************************************************
Variable Externs
*******************************************************************************/


/*******************************************************************************
Functions Prototypes
*******************************************************************************/
int32_t    usb0_fsi_dev_open( int32_t side );
int32_t    usb0_fsi_dev_close( int32_t side );
int32_t    usb0_fsi_dev_read_sector( int32_t side, uint8_t *buffer, 
                                     uint32_t sector_number, int32_t sector_count );
int32_t    usb0_fsi_dev_write_sector( int32_t side, uint8_t *buffer, 
                                      uint32_t sector_number, int32_t sector_count );
void       usb0_fsi_init(void);
void       usb0_fsi_clear(uint16_t device_address);
void       usb0_fsi_drv_clear(uint16_t device_address, uint16_t unit_number);
void       usb0_fsi_drive_search( uint16_t device_address );


#endif /* __USB_HMSCD_FSI_H__ */

/* End of File */
