/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 * $RCSfile: x_blk_dev.h,v $
 * $Revision: #1 $
 * $Date: 2015/05/29 $
 * $Author: brianpc.huang $
 *
 * Description:
 *         This header file contains block device related definitions.
 *---------------------------------------------------------------------------*/

#ifndef _X_BLK_DEV_H_
#define _X_BLK_DEV_H_

/*-----------------------------------------------------------------------------
                    include files
-----------------------------------------------------------------------------*/
#include "x_common.h"

/*-----------------------------------------------------------------------------
 *  Constants and type definitions
 *---------------------------------------------------------------------------*/

typedef enum
{
    BLKDEV_COND_FAIL     = -1,
    BLKDEV_COND_READ_OK  =  1,
    BLKDEV_COND_WRITE_OK =  2,
    BLKDEV_COND_ERASE_OK =  3,
    BLKDEV_COND_FLUSH_OK =  4
} BLKDEV_COND_T;

typedef enum
{
    BLKDEV_STATUS_ABSENT = 0,
    BLKDEV_STATUS_FAIL   = 1,
    BLKDEV_STATUS_READY  = 2
} BLKDEV_STATUS_T;

typedef enum
{
    BLKDEV_MEMCARD_CF     = 0,
    BLKDEV_MEMCARD_SD     = 1,
    BLKDEV_MEMCARD_MMC    = 2,
    BLKDEV_MEMCARD_MS     = 3,
    BLKDEV_MEMCARD_MS_PRO = 4,
    BLKDEV_MEMCARD_SM     = 5,
    BLKDEV_MEMCARD_XD     = 6,
    BLKDEV_USB_MASS =   100
} BLKDEV_MEMCARD_VARIETY_T;


/*-----------------------------------------------------------------------------
 * Notify function
 *---------------------------------------------------------------------------*/

typedef VOID (*x_blkdev_nfy_fct)(VOID          *pv_nfy_tag,
                                 BLKDEV_COND_T e_nfy_cond,
                                 VOID          *pv_data);

typedef VOID (*x_blkdev_hotswap_nfy_fct)(VOID            *pv_nfy_tag,
                                         BLKDEV_STATUS_T e_nfy_cond,
                                         VOID            *pv_data);

typedef struct _BLKDEV_NFY_INFO_T
{
    VOID             *pv_tag;
    x_blkdev_nfy_fct  pf_blkdev_nfy;
} BLKDEV_NFY_INFO_T;

typedef struct _BLKDEV_HOTSWAP_NFY_INFO_T
{
    VOID            *pv_tag;
    x_blkdev_hotswap_nfy_fct pf_blkdev_nfy;
} BLKDEV_HOTSWAP_NFY_INFO_T;


/*-----------------------------------------------------------------------------
 * Parameter definitions of get/set types
 *---------------------------------------------------------------------------*/

typedef struct _BLKDEV_IO_DATA_T
{
    UINT64            ui8_blk_num;
    UINT32            ui4_count;
    UINT32            ui4_trans_count;
    BOOL              b_ext_buf;
    VOID              *pv_data;
    UINT32            ui4_handle;         /* for abort function, default is 0x00 */
    BLKDEV_NFY_INFO_T t_nfy_info;         /* for individual notification */
} BLKDEV_IO_DATA_T;

typedef struct _BLKDEV_SECTOR_RANGE_INFO_T
{
    UINT32            ui4_start_sector;
    UINT32            ui4_number_of_sectors;
    UINT32            ui4_handle;         /* for abort function, default is 0x00 */
    BLKDEV_NFY_INFO_T t_nfy_info;         /* for individual notification */
} BLKDEV_SECTOR_RANGE_INFO_T;

typedef struct _BLKDEV_SECTOR_SEG_INFO_T
{
    UINT32            ui4_num_sectors;
    UINT32            ui4_sector_size;
} BLKDEV_SECTOR_SEG_INFO_T;

typedef struct _BLKDEV_SECTOR_TBL_INFO_T
{
    UINT32            ui4_num_entries;
    BLKDEV_SECTOR_SEG_INFO_T *pt_sector_info;
} BLKDEV_SECTOR_TBL_INFO_T;

/* Parameter data structure for I/O control */
typedef struct _BLKDEV_IO_CTRL_T {
    UINT32            u4IoctrlType;       /* I/O control types */
    void              *pvIoData;          /* I/O parameter data pointer */
    UINT32            u4IoDataSz;         /* I/O parameter data size */
    UINT32            ui4_handle;         /* for abort function, default is 0x00 */
    BLKDEV_NFY_INFO_T t_nfy_info;         /* for individual notification */
} BLKDEV_IO_CTRL_T;

/* Parameter data structure for abort */
typedef struct _BLKDEV_ABORT_T {
    UINT32            u4Handle;           /* handle of I/O data */
    BOOL              fgEnable;           /* enable or disable driver abort state */
} BLKDEV_ABORT_T;

/* Parameter data structure for flush command */
typedef struct _BLKDEV_FLUSH_DATA_T
{
    UINT32            ui4_handle;         /* for abort function, default is 0x00 */
    BLKDEV_NFY_INFO_T t_nfy_info;         /* for individual notification */
} BLKDEV_FLUSH_DATA_T;

/* Parameter data structure for standby command */
typedef struct _BLKDEV_STANDBY_DATA_T
{
    UINT32            ui4_StandbyTimer;
    UINT32            ui4_handle;         /* for abort function, default is 0x00 */
    BLKDEV_NFY_INFO_T t_nfy_info;         /* for individual notification */
} BLKDEV_STANDBY_DATA_T;

/*-----------------------------------------------------------------------------
 * Block device flags
 *---------------------------------------------------------------------------*/
#define BLKDEV_FLAG_STANDBY ((UINT32) 0x00010000)
#define BLKDEV_FLAG_HOTSWAP ((UINT32) 0x00020000)
#define BLKDEV_FLAG_ASYNC_READ  ((UINT32) 0x00030000)
#define BLKDEV_FLAG_ASYNC_WRITE ((UINT32) 0x00040000)

/*-----------------------------------------------------------------------------
 * IO control types
 *---------------------------------------------------------------------------*/
#define BLK_DEV_IO_CTRL_TYPE_ENABLE_WP ((UINT32) 0x100)

/*-----------------------------------------------------------------------------
 * Get types
 *---------------------------------------------------------------------------*/
#define BLKDEV_GET_TYPE_SIZE \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 0))
#define BLKDEV_GET_TYPE_NUMBER_OF_SECTORS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 1))
#define BLKDEV_GET_TYPE_SECTOR_SIZE \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 2))
#define BLKDEV_GET_TYPE_MAX_TRANS_SECTORS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 3))
#define BLKDEV_GET_TYPE_MAX_IO_REQUEST \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 4))
#define BLKDEV_GET_TYPE_SECTOR_TBL_INFO \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 5))
#define BLKDEV_GET_TYPE_ALIGNMENT \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 6))
#define BLKDEV_GET_TYPE_STANDBY \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 7))
#define BLKDEV_GET_TYPE_STATUS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 8))
#define BLKDEV_GET_TYPE_MEMCARD_VARIETY \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 9))
#define BLKDEV_GET_TYPE_MODEL \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 10))
#define BLKDEV_GET_TYPE_BEST_TRANS_SECTORS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 11))    /* default value: 1 */
#define BLKDEV_GET_TYPE_MIN_READ_SECTORS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 12))    /* default value: 1 */
#define BLKDEV_GET_TYPE_MIN_WRITE_SECTORS \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 13))    /* default value: 1 */
#define BLKDEV_GET_TYPE_WRITE_PROTECT \
     (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 14))    
#define BLKDEV_GET_TYPE_PROPRIETARY_PROPERTY \
    (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T) 0x80))  /* get type value over 0x80 should be defined in device proprietary header file */

/*-----------------------------------------------------------------------------
 * Set types
 *---------------------------------------------------------------------------*/
#define BLKDEV_SET_TYPE_READ \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 0))
#define BLKDEV_SET_TYPE_WRITE \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 1))
#define BLKDEV_SET_TYPE_ERASE \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 2))
#define BLKDEV_SET_TYPE_FLUSH \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 3))
#define BLKDEV_SET_TYPE_NFY_FCT \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 4))
#define BLKDEV_SET_TYPE_STANDBY \
	  (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 5))
#define BLKDEV_SET_TYPE_HOTSWAP_NFY_FCT \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 6))
#define BLKDEV_SET_TYPE_IO_CTRL \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 7))
#define BLKDEV_SET_TYPE_ABORT \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 8))

#define BLKDEV_SET_TYPE_PROPRIETARY_PROPERTY \
    (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 0x80))  /* set type value over 0x80 should be defined in device proprietary header file */

#endif /* _X_BLK_DEV_H_ */
