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

#include <sc/sc.h>
#include <sc/ios.h>
#include <ioslibc.h>
#include "usb.h"
#include "usbdev.h"
#include "mux.h"
#include "ch9.h"
#include "iso.h"

unsigned char isoDevDesc[18] DCACHE_ALIGN =
{
    sizeof(isoDevDesc),         /* Length */
    1,                       /* Type of descriptor = device */
    U16_LOW_BYTE(USB_VER), 
    U16_HIGH_BYTE(USB_VER),  /* Usb version */
    0x00,                    /* Device Class */
    0x00,                    /* Subclass */
    0x00,                    /* No class specific protocols */
    8,                       /* Maximum packet size */
    U16_LOW_BYTE(ISO_VENDOR_ID), 
    U16_HIGH_BYTE(ISO_VENDOR_ID),
    U16_LOW_BYTE(ISO_PRODUCT_ID), 
    U16_HIGH_BYTE(ISO_PRODUCT_ID),
    U16_LOW_BYTE(ISO_VERSION_ID), 
    U16_HIGH_BYTE(ISO_VERSION_ID),
    1,                       /* Manufacturer string index */
    2,                       /* Product string index */
    0,                       /* Serial number string index */
    1                        /* Number of configurations */
};

unsigned char isoCfgDesc[ISO_CFG_DESC_SIZE] DCACHE_ALIGN = 
{
    /* * Configuration descriptor * */    
    9,                       /* Configuration Descriptor - always 9 bytes */
    2,                       /* "Configuration" type of descriptor */
    U16_LOW_BYTE(ISO_CFG_DESC_SIZE), 
    U16_HIGH_BYTE(ISO_CFG_DESC_SIZE),
    1,                       /* # of Interfaces  */
    1,                       /* Configuration Value */
    0,                       /* Configuration Description String Index */
    0x80,                    /* Attributes:  Self-powered */
    0,                       /* self-power device */

    /* Interface */
    9,                       /* Interface 0 Descriptor - always 9 bytes */
    4,                       /* "Interface" type of descriptor */
    0,                       /* Number of this interface */
    0,                       /* Alternate Setting */
    2,                       /* Number of endpoints on this interface */
    0xff,                    /* Interface Class: Vendor specific */
    0xff,                    /* Interface Subclass: Vendor specific */
    0x0,                     /* Interface Protocol */
    0,                       /* Interface Description */
   
    /* Ep 2 (config as iso in) */  
    7,                       /* Interface 0 Descriptor - always 7 bytes */
    5,                       /* ep type of descriptor */
    0x82,                    /* ep2, bit 7 -- Rx(IN) */
    0x1,                     /* 0=Control 1=Isochronous 2=Bulk 3=Interrupt */
    U16_LOW_BYTE(ISO_MAX_PKT_SIZE),
    U16_HIGH_BYTE(ISO_MAX_PKT_SIZE),
    0,                       /* Pulling interval */

    /* Ep 3 (config as iso out) */  
    7,                       /* Interface 0 Descriptor - always 7 bytes */
    5,                       /* ep type of descriptor */
    0x3,                     /* ep3, bit 7 -- Tx(Out) */
    0x1,                     /* 0=Control 1=Isochronous 2=Bulk 3=Interrupt */
    U16_LOW_BYTE(ISO_MAX_PKT_SIZE),
    U16_HIGH_BYTE(ISO_MAX_PKT_SIZE),
    0                        /* Pulling interval */
};

   /* Languages supported = American English (0x0409) */
unsigned char ISO_STR_0[4] DCACHE_ALIGN = 
       {sizeof(ISO_STR_0), 0x03, 0x09, 0x04};         

unsigned char ISO_STR_1[20] DCACHE_ALIGN =
{
    sizeof(ISO_STR_1), 0x03,
    'i', 0, 'Q', 0, 'u', 0, 'e', 0, ' ', 0, 'L', 0, 't', 0, 'd', 0,
    '.', 0
};
        
static unsigned char ISO_STR_2[28] = {sizeof(ISO_STR_2), 0x03,
      'i',0,'Q',0,'u',0,'e',0,' ',0,'N',0,'e',0,'t',0,
      ' ', 0, 'C',0,'a',0, 'r', 0, 'd', 0};

static unsigned char  ISO_STR_n[34] DCACHE_ALIGN =
{
    sizeof(ISO_STR_n), 0x03,
    'B',0,'A',0,'D',0,' ',0,'S',0,'T',0,'R',0,'I',0,
    'N',0,'G',0,' ',0,'I',0,'n',0,'d',0,'e',0,'x',0
};

unsigned char * ISO_USB_STRING_DESC[ISO_STR_NUM+2] = 
{   
    (unsigned char *) ISO_STR_0, 
    (unsigned char *) ISO_STR_1, 
    (unsigned char *) ISO_STR_2, 
    (unsigned char *) ISO_STR_n, 
};

USBDeviceFuncs usbIsoDevFuncs = {
    muxOpen,
    muxClose,
    muxIoctl,
    muxRead,
    muxWrite,

    muxReset,
    muxInit,
    muxExit,
    muxSuspend,
    muxUnplug,
    muxEp0Recv,
    muxEp0Send,       
    muxEp1Recv,
    muxEp1Send,
    muxEp2Send,
    muxEp3Recv,
};

u8*
isoGetDevDesc(int *len)
{
    *len = sizeof(isoDevDesc);

    return isoDevDesc;
}

u8*
isoGetConfDesc(int *len)
{
    *len = sizeof(isoCfgDesc);

    return isoCfgDesc;
}

u8*
isoGetDescString(int n, int *len)
{
    if (n > ISO_STR_NUM) n = 0;

    *len =  sizeof(ISO_USB_STRING_DESC[n][0]);

    return ISO_USB_STRING_DESC[n];
}

Ch9CallBack isoCh9Callback = {
    isoGetDevDesc,
    isoGetConfDesc,
    isoGetDescString,
    muxEnumDone
};


