/*
 *              Copyright (C) 2005, BroadOn Communications Corp.
 *
 * These coded instructions, statements, and computer programs contain
 * unpublished  proprietary information of BroadOn Communications Corp.,
 * and  are protected by Federal copyright law. They may not be disclosed
 * to  third  parties or copied or duplicated in any form, in whole or in
 * part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#ifndef __USB_MSD_H
#define __USB_MSD_H

#define MSD_VENDOR_ID                 0x1527
#define MSD_PRODUCT_ID                0xBCDD
#define MSD_VERSION_ID                0x0001

#define MSD_WRITE_EP                  2
#define MSD_READ_EP                   3
#define MSD_MAX_PKT_SIZE              64

#define MSD_STR_NUM                   3

/* 9(1 configuration) + 9(1 interface) + 2*7 (2 ep) = 32 */
#define MSD_CFG_DESC_SIZE             32

/* Please make sure buf size > 512 for write-only mbr */
#define MSD_READ_BUF_SIZE             2048

/* * * * * MSD ch9 class reques * * * * */
#define MSD_CH9_RESET                 0xff
#define MSD_CH9_MAX_LUN               0xfe

#define MSD_CBW_SIGNATURE             0x43425355
#define MSD_CSW_SIGNATURE             0x53425355

#define MSD_CBW_FLAGS_IN              0x80

#define MSD_CSW_STATUS_GOOD           0
#define MSD_CSW_STATUS_FAIL           1
#define MSD_CSW_STATUS_PHASE_ERR      2

/* * * * Data structure for CBW CSW * * * * */
typedef struct {
    u32 signature;
    u32 tag;
    u32 dataTransferLen;
    u8  flags;
    u8  lun;
    u8  cbLen;
    u8  cb[16];
} USBMsdCBW;

typedef struct {
    u32 signature;
    u32 tag;
    u32 dataResidue;
    u8  status;
} USBMsdCSW;

/* * * * * SPC2 command supported in MSD * * * * */
#define SPC2_INQUIRY                  0x12
#define SPC2_FORMAT_CAP               0x23
#define SPC2_CAPACITY10               0x25
#define SPC2_READ10                   0x28
#define SPC2_WRITE10                  0x2A
#define SPC2_MODESENSE6               0x1A
#define SPC2_MODESENSE10              0x5A
#define SPC2_REQUEST_SENSE            0x03
#define SPC2_TEST_UNIT_READY          0

/* * * * * SPC2 FORMAT CAP * * * * * * */ 
#define SPC2_FORMAT_CAP_DESC_FRMT     3

/* * * * * SPC2 Sense key Error code * * * * */
#define SPC2_NO_SENSE                 0

#define MSD_SENSE_DATA_SIZE           18
#define SPC2_SENSE_CURRENT            0x70

#define SPC2_ASC_INVALID_COMMAND      0x20
#define SPC2_ASCQ_INVALID_COMMAND     0

#define SPC2_ASC_OUT_OF_RANGE         0x21
#define SPC2_ASCQ_OUT_OF_RANGE        0

#define SPC2_ASC_WRITE_PROTECTED      0x27
#define SPC2_ASCQ_WRITE_PROTECTED     0

/* * * * * SPC2 Command Ops structure * * * * */

/* we did not use any since ARM need u16/u32 has to be aligned at halfword/word boundary */

/* * * * * SPC2 Inquiry Command Response structure * * * * */
typedef struct {
    u8 devType;
    u8 rmb;
    u8 version;
    u8 respondDataFormat;
    u8 additionalLen;
    u8 sccstp;
    u8 bqueetc;
    u8 cmdQue;
    char vendorId[8];
    char productId[16];
    char productRev[4];
} SPC2InquiryResponse;

/* * * * NC Card MSD ctrl  data structure * * * */
#define USB_MSD_STATE_READY         0x0       /* MSD Ready to recv CBW */
#define USB_MSD_STATE_SEND_CSW      0x1       /* MSD send CSW data */ 
#define USB_MSD_STATE_DATA_IN       0x2       /* IN Data state */
#define USB_MSD_STATE_DATA_OUT      0x3       /* OUT Data state */

/* * * *  MSD ctrl data structure * * * * * * */
typedef struct {
    IOSFd fd;                                 /* Flash image file descriptor */
    u32 state;                                /* MSD state */
    u32 blkNum;                               /* # of MSD blocks */
    u32 blkSize;                              /* bytes per block */
    u32 pos;                                  /* read/write position */
    u32 len;                                  /* read/write length */
    u32 lba;                                  /* request lba */
    u8  recv[MSD_MAX_PKT_SIZE] DCACHE_ALIGN;  /* rcv buffer */
    u8  send[MSD_MAX_PKT_SIZE*2] DCACHE_ALIGN;  /* send buffer*/
    u8  *data;                                /* should be shared memory */
    USBMsdCBW cbw;                            /* current CBW */
    USBMsdCSW csw;                            /* current CSW */
} USBMsdCtrl;

#define MIN3(a, b, c)   ((a)>(b) ? ((b)>(c) ?(c):(b)) : ((a)>(c)?(c):(a)))

int isValidCbw(USBMsdCBW *cbw, int len);
int isMeaningfulCbw(USBMsdCBW *cbw);
IOSError msdHandleCommand(USBMsdCBW *cbw, USBMsdCSW *csw);
IOSError msdSetDeviceType(u8 newType);
u8 msdGetDeviceType();

#endif
