/*
 *              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 __NC_USB_MUX_H_
#define __NC_USB_MUX_H_

  /* Read: NC read from PC, as USB is OUT Transaction */
  /* Write: NC write to PC, as USB is IN Transaction */
#define MUX_WRITE_EP           2
#define MUX_READ_EP            3
#define MUX_MAX_OPEN           64

typedef struct {
    IOSResourceRequest *req;    /* RM request */  

    u8 *buf;                   /* buffer supply by NC app */
    int len, pos;              /* length and position */
    int busLen;                /* bus length and pos, specified by Header */
                               /* As Raymond suggest, if len < busLen, return error*/
    u16 inUse;                 /* valid ? */
} USBMuxTrans;                  

#define MAX_MUX_READ_BITSTREAM     2
#define MAX_MUX_WRITE_BITSTREAM    2   

typedef struct {
    USBMuxTrans wr[MAX_MUX_WRITE_BITSTREAM]; /* write transaction */     
    USBMuxTrans rd[MAX_MUX_READ_BITSTREAM];  /* read transaction */
    int curRead;                             /* current rd transaction */
    int curWrite;                            /* current wr transaction */
    IOSResourceRequest *waitReady;           /* wait ready request */

    u8 ref;                                  /* reference count */  
    u8 devId;                                /* device id */
    u8 stall;                                /* because of packet drop */
} USBMuxDevDesc;

typedef struct {
    USBStreamHeader rdHd DCACHE_ALIGN;    /* read hd buffer */
    USBStreamHeader wrHd DCACHE_ALIGN;    /* write hd buffer */ 
    u8 ctrlWrBuf[16] DCACHE_ALIGN;        /* Ctrl ep wite buffer */
    u8 ctrlRdBuf[16] DCACHE_ALIGN;        /* Ctrl ep read buffer */

    u16 dropBusLen;                       /* drop packet length */
    u16 dropBusPos;                       /* drop packet pos */

    u8 curRdDev;                          /* current read device */
    u8 curWrDev;                          /* current write device */
    u8 status;                            /* device status */
    u8 iso;                               /* ep type */
    u8 inUse;                             /* 0 -- no open stream */
} USBMuxCtrl;

void muxInitData();

IOSError muxOpen(IOSResourceRequest *req);
IOSError muxClose(IOSResourceRequest *req);
IOSError muxRead(IOSResourceRequest *req);
IOSError muxWrite(IOSResourceRequest *req);
IOSError muxIoctl(IOSResourceRequest *req);

IOSError muxNextWrite();
void    muxEnumDone();
IOSError muxInit(int iso);
IOSError muxExit();
IOSError muxReset();
IOSError muxSuspend();
IOSError muxUnplug();
IOSError muxEp0Recv(int evt, int len);
IOSError muxEp0Send(int evt, int len);
IOSError muxEp1Recv(int evt, int len);
IOSError muxEp1Send(int evt, int len);
IOSError muxEp2Send(int evt, int len);
IOSError muxEp3Recv(int evt, int len);

#endif

