/******************************************************************************
*
*  System Name : ARM USB Host Driver Library
*  File Name   : usb_hdrv_rza1.h
*  Contents    : Definitions for USB Host Control 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 __USB_HDRV_RZA1_H__
#define __USB_HDRV_RZA1_H__


/*******************************************************************************
Includes   <System Includes> , "Project Includes"
*******************************************************************************/
#include "r_typedefs.h"
#include "usb_osdef.h"


/*******************************************************************************
Macro definitions
*******************************************************************************/
/* ==== Return value ==== */
#define        USB_SUCCESS                  0
#define        USB_ERROR                    -1
#define        USB_STALL                    -2
#define        USB_TIMEOUT                  -3
#define        USB_NAK_TIMEOUT              -4
#define        USB_DETACH_ERR               -5
#define        USB_SETUP_ERR                -6
#define        USB_CTRL_COM_ERR             -7
#define        USB_COM_ERR                  -8
#define        USB_DEV_ADDR_ERR             -9
#define        USB_ENUMERATION_RETRY_ERR    -100

#define        USB_NOCONNECT                0x0000u
#define        USB_HSCONNECT                0x0001u
#define        USB_FSCONNECT                0x0002u
#define        USB_LSCONNECT                0x0003u

/* ==== Enumeration success ===== */
#define        USB_PROCESS_SUCCESS          0x00000000u
#define        USB_PROCESS_ERROR            0xFFFFFFFFu

/* ==== USB MAX device definition ==== */
#define        USB_MAX_DEVICE               8

#define        USB_DEVICE_0                 0x0000u
#define        USB_DEVICE_1                 0x0001u
#define        USB_DEVICE_2                 0x0002u
#define        USB_DEVICE_3                 0x0003u
#define        USB_DEVICE_4                 0x0004u
#define        USB_DEVICE_5                 0x0005u
#define        USB_DEVICE_6                 0x0006u
#define        USB_DEVICE_7                 0x0007u
#define        USB_DEVICE_8                 0x0008u
#define        USB_DEVICE_9                 0x0009u
#define        USB_DEVICE_10                0x000Au

/* ===== For HCD configuration(USB_HCD_CFG) ===== */
#define        USB_NORMAL_RESPONSE          0
#define        USB_ILLEGAL_RESPONSE         1

#define        USB_HISPEED                  0
#define        USB_FULLSPEED                1
#define        USB_LOWSPEED                 2

/* ===== For generating pipe definition table ===== */
#define        USB_NONE                     (0u)
#define        USB_CNT_ON                   (0x0100u)
#define        USB_CNT_OFF                  (0x0000u)
#define        USB_DBL_ON                   (0x0200u)
#define        USB_DBL_OFF                  (0x0000u)
#define        USB_BUF_SIZE( x )            (uint16_t)((uint16_t)(((x) / 64) - 1) << 10)
#define        USB_BUF_NUMB( x )            (uint16_t)( x )

#define        USB_MAX_PIPE_NO              9u
#define        USB_PIPE0                    (uint16_t)0x0000
#define        USB_PIPE1                    (uint16_t)0x0001
#define        USB_PIPE2                    (uint16_t)0x0002
#define        USB_PIPE3                    (uint16_t)0x0003
#define        USB_PIPE4                    (uint16_t)0x0004
#define        USB_PIPE5                    (uint16_t)0x0005
#define        USB_PIPE6                    (uint16_t)0x0006
#define        USB_PIPE7                    (uint16_t)0x0007
#define        USB_PIPE8                    (uint16_t)0x0008
#define        USB_PIPE9                    (uint16_t)0x0009
#define        USB_PIPE_TBL_END             (uint16_t)0xFFFF
#define        USB_PIPE_TBL_UNUSED          (uint16_t)0xFFFE

#define        USB_CUSE                     (uint16_t)0
#define        USB_D0USE                    (uint16_t)1
#define        USB_D0DMA                    (uint16_t)2
#define        USB_D1USE                    (uint16_t)3
#define        USB_D1DMA                    (uint16_t)4

#define        USB_FIFO_LITTLE              (uint16_t)0
#define        USB_FIFO_BIG                 (uint16_t)1

/* ==== For USB standard request function ==== */
/* CLEAR_FEATURE / SET_FEATURE */
#define        USB_REQ_ENDPOINT_HALT           0x0000u
#define        USB_REQ_DEVICE_REMOTE_WAKEUP    0x0001u
#define        USB_REQ_TEST_MODE               0x0002u

/* GET_STATUS */
#define        USB_REQ_DEVICE                  0x0000u
#define        USB_REQ_INTERFACE               0x0001u
#define        USB_REQ_ENDPOINT                0x0002u

/* Standard Descriptor type */
#define        USB_DEVICE_DESC                       0x0100u
#define        USB_CONFIGURATION_DESC                0x0200u
#define        USB_STRING_DESC                       0x0300u
#define        USB_INTERFACE_DESC                    0x0400u
#define        USB_ENDPOINT_DESC                     0x0500u
#define        USB_DEVICE_QUALIFIER_DESC             0x0600u
#define        USB_OTHER_SPEED_CONFIGURATION_DESC    0x0700u
#define        USB_INTERFACE_POWER_DESC              0x0800u

#define        USB_PIPE_IN                     0
#define        USB_PIPE_OUT                    1
#define        USB_PIPE_ERROR                  0xFFFF

/* ==== Command for electrical test activation ==== */
#define        USB_CMD_UACT_OFF          0
#define        USB_CMD_UACT_ON           1
#define        USB_CMD_SINGLE_SETUP      2
#define        USB_CMD_SINGLE_DATA       3
#define        USB_CMD_SINGLE_STATUS     4

#define        USB_CMD_TEST_NORMAL       0x0000u
#define        USB_CMD_TEST_J            0x0009u
#define        USB_CMD_TEST_K            0x000Au
#define        USB_CMD_TEST_SE0_NAK      0x000Bu
#define        USB_CMD_TEST_PACKET       0x000Cu
#define        USB_CMD_TEST_FORCE        0x000Du

/* ==== USB specification define ==== */

/* For identifying USB manager task */
#define        USB_MM_DETACH             0u
#define        USB_MM_ATTACH             1u
#define        USB_MM_ENUMERATION        2u
#define        USB_MM_HS_OR_FS_DETECT    4u
#define        USB_MM_DEFAULT            40u
#define        USB_MM_SET_CONFIGURE      10u
#define        USB_MM_SUSPENDED          20u
#define        USB_MM_DISABLEDET         30u
#define        USB_MM_DETACH_PROC        100u
#define        USB_MM_ATTACH_PROC        200u
#define        USB_MM_SUSPEND_PROC       21u
#define        USB_MM_RESUME_PROC        22u

/* Descriptor type    Define */
#define        USB_DT_DEVICE                       (uint8_t)0x01
#define        USB_DT_CONFIGURATION                (uint8_t)0x02
#define        USB_DT_STRING                       (uint8_t)0x03
#define        USB_DT_INTERFACE                    (uint8_t)0x04
#define        USB_DT_ENDPOINT                     (uint8_t)0x05
#define        USB_DT_DEVICE_QUALIFIER             (uint8_t)0x06
#define        USB_DT_OTHER_SPEED_CONFIGURATION    (uint8_t)0x07
#define        USB_DT_INTERFACE_POWER              (uint8_t)0x08

/* Interface class Define */
#define        USB_IF_CLS_NOT                      (uint8_t)0x00
#define        USB_IF_CLS_AUDIO                    (uint8_t)0x01
#define        USB_IF_CLS_CDC_CTRL                 (uint8_t)0x02
#define        USB_IF_CLS_HID                      (uint8_t)0x03
#define        USB_IF_CLS_PHYSICAL                 (uint8_t)0x05
#define        USB_IF_CLS_IMAGE                    (uint8_t)0x06
#define        USB_IF_CLS_PRINTER                  (uint8_t)0x07
#define        USB_IF_CLS_MASS                     (uint8_t)0x08
#define        USB_IF_CLS_HUB                      (uint8_t)0x09
#define        USB_IF_CLS_CDC_DATA                 (uint8_t)0x0A
#define        USB_IF_CLS_CRAD                     (uint8_t)0x0B
#define        USB_IF_CLS_CONTENT                  (uint8_t)0x0D
#define        USB_IF_CLS_VIDEO                    (uint8_t)0x0E
#define        USB_IF_CLS_DIAG                     (uint8_t)0xDC
#define        USB_IF_CLS_WIRELESS                 (uint8_t)0xE0
#define        USB_IF_CLS_APL                      (uint8_t)0xFE
#define        USB_IF_CLS_VENDOR                   (uint8_t)0xFF
#define        USB_IF_CLS_HELE                     (uint8_t)0xAA

/* Endpoint Descriptor    Define */
#define        USB_EP_DIR_MASK                     (uint8_t)0x80
#define        USB_EP_IN                           (uint8_t)0x80
#define        USB_EP_OUT                          (uint8_t)0x00
#define        USB_EP_NUM_MASK                     (uint8_t)0x0F
#define        USB_EP_USG_MASK                     (uint8_t)0x30
#define        USB_EP_SYNC_MASK                    (uint8_t)0x0C
#define        USB_EP_TRNS_MASK                    (uint8_t)0x03
#define        USB_EP_CTRL                         (uint8_t)0x00
#define        USB_EP_ISO                          (uint8_t)0x01
#define        USB_EP_BULK                         (uint8_t)0x02
#define        USB_EP_INT                          (uint8_t)0x03


#define USB_HCD_TRANSFER_NORMAL                    0
#define USB_HCD_TRANSFER_CONTINUE                  1
#define USB_HCD_TRANSFER_RELOAD                    2


/* ==== DMA control for user defined function ==== */
enum
{
    USB_D0FIFO = 0,
    USB_D1FIFO,
    USB_CFIFO,
    USB_D0FIFO_BIG,
    USB_D1FIFO_BIG,
    USB_CFIFO_BIG
};
enum
{
    USB_FIFO2BUF = 0,
    USB_BUF2FIFO
};

enum
{
    USB_ISO_8BIT = 0,
    USB_ISO_16BIT,
    USB_ISO_32BIT
};


/* ==== Definition for MBX ==== */
enum
{
    USB_HCD_CONTROL_END = 0,     /* 0 */
    USB_HCD_DATA_NONE,           /* 1 */
    USB_HCD_DATA_WAIT,           /* 2 */
    USB_HCD_DATA_OK,             /* 3 */
    USB_HCD_DATA_ERR,            /* 4 */
    USB_HCD_DATA_STALL,          /* 5 */
    USB_HCD_DATA_STOP,           /* 6 */
    USB_HCD_DATA_OVR,            /* 7 */
    USB_HCD_DATA_DETACH,         /* 8 */
    USB_HCD_SETUP_ERR,           /* 9 */
    USB_HCD_CTRL_COM_ERR,        /* 10 */
    USB_HCD_PIPE_COM_ERR,        /* 11 */
    USB_HCD_PIPE_ISO_COM_ERR,    /* 12 */
};

/* ==== Definition for HCD ==== */
enum
{
    USB_HCD_CTRL = 0,
    USB_HCD_BULK,
    USB_HCD_INT,
    USB_HCD_ISO
};

/* ==== Definition for HCD ==== */
enum
{
    USB_CTRL_TRNS_IDLE = 0,    /* 0 */
    USB_CTRL_TRNS_SETUP_ND,    /* 1 */
    USB_CTRL_TRNS_SETUP_WT,    /* 2 */
    USB_CTRL_TRNS_SETUP_RD,    /* 3 */
    USB_CTRL_TRNS_DATA_WT,     /* 4 */
    USB_CTRL_TRNS_DATA_RD,     /* 5 */
    USB_CTRL_STATUS_RD,        /* 6 */
    USB_CTRL_STATUS_WT,        /* 7 */
    USB_DATA_WT,               /* 8 */
    USB_DATA_RD                /* 9 */
};


/*******************************************************************************
Typedef definitions
*******************************************************************************/
typedef    void        ( *USB_CALLBACK_HMGR )( uint32_t, uint32_t );
typedef    int32_t     ( *USB_CALLBACK_HCD )( uint16_t result );
typedef    int32_t     ( *USB_CALLBACK_HCD_REQUEST )( void *hcd_request_result );

typedef struct
{
    uint16_t    fbcfg_fwait;
    uint16_t    pipe_err_cnt[USB_MAX_PIPE_NO + 1];
    uint16_t    detect_time;
    uint16_t    control_mode;
    uint16_t    speed_mode;
    uint32_t    control_timeout;
    uint32_t    enu_retry;
    uint32_t    bus_reset_width;
    uint32_t    bus_reset_wait_time;
} USB_HCD_CONFIG_t;

typedef struct
{
    USB_CALLBACK_HMGR    device_powered_proc;
    USB_CALLBACK_HMGR    device_default_proc;
    USB_CALLBACK_HMGR    device_address_proc;
    USB_CALLBACK_HMGR    device_config_proc;
    USB_CALLBACK_HMGR    device_detach_proc;
    USB_CALLBACK_HMGR    device_attach_proc;
    USB_CALLBACK_HMGR    device_suspend_proc;
    USB_CALLBACK_HMGR    device_resume_proc;
} USB_HMGR_REGIST_t;


typedef struct
{
    uint16_t    device_address;
    uint16_t    speed_mode;
} USB_HOST_DEVICE_CFG_INFO_t;

typedef struct
{
    uint16_t    hub_address;
    uint16_t    hub_port;
    uint16_t    device_speed;
    uint16_t    root_port;
} USB_HOST_DEVICE_ADDRESS_CFG_t;

typedef struct
{
    uint16_t    pipe_number;
    uint16_t    pipe_cfg;
    uint16_t    pipe_buf;
    uint16_t    pipe_max_pktsize;
    uint16_t    pipe_cycle;
    uint16_t    fifo_port;
    uint16_t    fifo_endian;
} USB_HOST_CFG_PIPETBL_t;

typedef struct
{
    uint32_t    fifo;
    uint32_t    buffer;
    uint32_t    bytes;
    uint32_t    dir;
    uint32_t    size;
} USB_HOST_DMA_t;

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    void                        *transfer_address;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
    uint32_t                    transfer_length;
    int32_t                     status;
    uint16_t                    pipe_number;
    uint16_t                    *setup_packet;
    uint16_t                    error_count;
} USB_HCD_REQUEST_t;

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    void                        *transfer_address;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
    uint32_t                    transfer_length;
    int32_t                     status;
    uint16_t                    pipe_number;
    uint16_t                    *setup_packet;
    uint16_t                    error_count;
    uint32_t                    reload_length;
    void                        *reload_address;
    uint16_t                    bit_size;
} USB_HCD_REQUEST_ISO_t;

typedef    int32_t    ( *USB_CALLBACK_INT_MSG )( uint16_t  pipe,
                                                 uint32_t  rcv_length,
                                                 uint32_t  *next_length,
                                                 void **next_address,
                                                 uint32_t  *total_length );
typedef    int32_t    ( *USB_CALLBACK_ZERO_MSG )( uint16_t pipe,
                                                  uint32_t rcv_length,
                                                  uint8_t  *data,
                                                  uint32_t *length );

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    uint16_t                    transfer_type;
    USB_HOST_CFG_PIPETBL_t      *pipe_table;
    int32_t                     status;
    uint32_t                    transfer_length;
    void                        *transfer_address;
    uint16_t                    zero_length_packet;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
    USB_CALLBACK_INT_MSG        int_proc;
} USB_HCD_BULK_TRANSFER_t;

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    uint16_t                    transfer_type;
    USB_HOST_CFG_PIPETBL_t      *pipe_table;
    int32_t                     status;
    uint32_t                    transfer_length;
    void                        *transfer_address;
    uint32_t                    reload_length;
    void                        *reload_address;
    uint16_t                    bit_size;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
    USB_CALLBACK_INT_MSG        int_proc;
    USB_CALLBACK_ZERO_MSG       zero_proc;
} USB_HCD_ISO_TRANSFER_t;

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    uint16_t                    transfer_type;
    USB_HOST_CFG_PIPETBL_t      *pipe_table;
    int32_t                     status;
    uint32_t                    transfer_length;
    void                        *transfer_address;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
} USB_HCD_INT_TRANSFER_t;

typedef struct
{
    uint32_t                    msg_header;
    uint16_t                    msg_info;
    uint16_t                    devadr;
    uint16_t                    transfer_type;
    uint16_t                    *setup_packet;
    int32_t                     status;
    uint32_t                    transfer_length;
    void                        *transfer_address;
    USB_CALLBACK_HCD_REQUEST    complete_proc;
} USB_HCD_CTRL_TRANSFER_t;

typedef struct
{
    uint16_t    int_num;
    uint16_t    brdy_enable;
    uint16_t    nrdy_enable;
    uint16_t    bemp_enable;
    uint16_t    brdy_status;
    uint16_t    nrdy_status;
    uint16_t    bemp_status;
} USB_HCD_INT_INFO_t;

typedef struct
{
    uint32_t               msg_header;
    uint16_t               msg_info;
    void                   *transfer_address;
    USB_CALLBACK_HCD       complete_proc;
    uint16_t               pipe_number;
    int32_t                result;
    USB_HCD_INT_INFO_t     int_info;
} USB_HCD_INFO_t;

typedef struct
{
    uint32_t    msg_header;
    uint16_t    msg_info;
    uint16_t    result;
} USB_HMGR_MESSAGE_t;

typedef union
{
    USB_HCD_REQUEST_t          usb_hcd_request;
    USB_HCD_REQUEST_ISO_t      usb_hcd_request_iso;
    USB_HCD_INFO_t             usb_hcd_info;
    USB_HCD_BULK_TRANSFER_t    usb_hcd_bulk_transfer;
    USB_HCD_ISO_TRANSFER_t     usb_hcd_iso_transfer;
    USB_HCD_INT_TRANSFER_t     usb_hcd_int_transfer;
    USB_HCD_CTRL_TRANSFER_t    usb_hcd_cntl_transfer;
} USB_HCD_MESSAGE_t;


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


/*******************************************************************************
Functions Prototypes
*******************************************************************************/


#include "usb0_hdrv_rza1.h"


#endif /* __USB_HDRV_RZA1_H__ */

/* End of file */
