/* 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.
 *
 *****************************************************************************/


#ifndef _MUSB_BT_IF_H_
#define _MUSB_BT_IF_H_

#define MUSB_BT_DEVICE_NUM   (1)

/** Successful status */
#define BT_STATUS_OK			(0)
/** error status */
#define BT_STATUS_ERROR		(0x80)
/** error: NACK limit reached */
#define BT_STATUS_NACK_LIMIT		(BT_STATUS_ERROR | 0x02)
/** error: general transmit error */
#define BT_STATUS_TRANSMIT_ERROR (BT_STATUS_ERROR | 0x08)
/** error: general receive error */
#define BT_STATUS_RECEIVE_ERROR	(BT_STATUS_ERROR | 0x09)
/** error: received USB stall handshake */
#define BT_STATUS_STALLED		(BT_STATUS_ERROR | 0x17)
/** error: transfer aborted because peer was removed from bus */
#define BT_STATUS_PEER_REMOVED	(BT_STATUS_ERROR | 0x20)

/** 
 * MUSB_BtIoctlCmdType.
 * HFI Ioctl command Type.
 */
typedef enum
{
    MUSB_BT_IOCTL_GET_DEVICE_NUM,     // total number of Bluetooth devices on our system.
    MUSB_BT_IOCTL_GET_DEVICE_STATUS,
    MUSB_BT_IOCTL_SET_HOTSWAP_NFY,
    MUSB_BT_IOCTL_GET_IO_REQ_NUM,
    
    MUSB_BT_IOCTL_END
} MUSB_BtIoctlCmdType;

/**
 * Bluetooth Control/Bulk/Interrupt/Isochronous IRP completion handler.
 * A completion handler for control transfers.
 * @param 1 pCompleteParam from user's define.
 * @param 2 IRP pointer
 */
struct _MUSB_BT_IO_T;

typedef void (*MUSB_pfBtIOComplete)(void* pBtIOCompleteParam, 
    struct _MUSB_BT_IO_T *prBtIOData);

/* Traffic types in bmAttributes */
#define MUSB_BT_XFER_CONTROL	0
#define MUSB_BT_XFER_ISOC		1
#define MUSB_BT_XFER_BULK		2
#define MUSB_BT_XFER_INT		3

typedef struct _MUSB_BT_ISO_T 
{
    UINT32 dwDataLength;		/* expected length */
    UINT32 dwActualDataLength;
    UINT32 dwStatus;
}MUSB_BT_ISO_T;

typedef struct _MUSB_BT_IO_REQ_NUM_T
{
    BOOL                    b_is_tx;
    UINT8                   ui1_traffic_type;           /*I: 0 ~ 3: select transfer type.*/
    UINT32                  ui4_io_request_in_queue;    /*O: num of uncompleted request in driver queue*/
}MUSB_BT_IO_REQ_NUM_T;

typedef struct _MUSB_BT_IO_T
{
    /*0 ~ 3: select transfer type.*/
    UINT8 bTrafficType;           

    /*1: Tx, 0: Rx */
    BOOL  bIsTx;                     

    /* Only for bTrafficType == MUSB_BT_XFER_CONTROL */
    UINT8* pSetupPacket;      

    /* For all transfer type, including ISO. */
    UINT8* pDataBuffer;

    /* For Bulk/Interrupt/Control transfer.
        If bTrafficType == MUSB_BT_XFER_CONTROL, 
        dwDataLength = the size of data at data phase.
        It does not include setup packet.
        Do not use by ISO transfer.
    */
    UINT32 dwDataLength;     
    UINT32 dwActualDataLength;
    UINT32 dwStatus;              /*0: ok, others: fail.*/

    /* number of ISO packets */
    UINT32 dwNofPackets;        
    /* assign every ISO packets transfer information */
    MUSB_BT_ISO_T *prISOpacket; 
    
    MUSB_pfBtIOComplete pfBtIOComplete;
    void* pBtIOCompleteParam;
    
} MUSB_BT_IO_T;

/**
 * Element Meaning
 * 
 * @param MaxUnits Number of maximum units the driver can handle.
 * @param pfIoCtl Pointer to the device IoCtl function.
 * @param pfRWBurst Pointer to the device read/write multiple sector function. (optional)
 * @param pfWriteProtect Pointer to the device write protect checking function. (optional)
 * @param pfInitDevice Pointer to the device driver initialization function. (optional)
*/
typedef struct 
{
    UINT32 MaxUnits;
    UINT32 (*pfIoCtl) (UINT32 u4Index, UINT32 u4Cmd, UINT32 u4Aux,void *pBuffer);
    UINT32 (*pfBtIOTransfer) (UINT32 u4Index, MUSB_BT_IO_T *prBtIOData);
    UINT32 (*pfInitDevice) (UINT32 u4Index);
} MU_BT_DEVICE_TYPE;

/**
 * Get a pointer to the bluetooth handling driver
 */
extern MU_BT_DEVICE_TYPE *MUSB_BtDriverGet(void);

#endif /* _MUSB_BT_IF_H_ */
