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

/*
 * Resource Manager for USB-to-Ethernet bridge
 *
 * this file only contains generic routines that are not specific
 * to any ethernet MAC/PHY hardware; hardware-specific routines
 * are located elsewhere
 *
 * To Do list --
 * 1. it seems that the IP stack does not pad-out illegally-short
 *    ethernet packets; it appears that the ASIX chip will supply
 *    its own padding, but since there's no documentation on this
 *    feature it's not clean exactly when/how it can be relied on;
 *    best would be to modify the IP stack to provide the needed
 *    padding
 * 2. waiting for a new ioctl to be defined for reporting link
 *    status
 * 
 */

#include <ios.h>
#include <ioslibc.h>
#include <priority.h>
#include <iosusb.h>
#include <iop_netif.h>

#include "ax.h"

/* debugging macros */
#ifdef UE_DEBUG
#define UE_LOG(...) printf("UE: " __VA_ARGS__)
#define UE_LOG_CONT(...) printf(__VA_ARGS__)
#define UE_ERROR(...) printf("UE: Error: " __VA_ARGS__)
#else
#define UE_LOG(...)
#define UE_LOG_CONT(...)
#ifdef UE_DEBUG_ERROR
#define UE_ERROR(...) printf("UE: Error: " __VA_ARGS__)
#else
#define UE_ERROR(...) /* printf("UE: Error: " __VA_ARGS__) */
#endif
#endif

/*
 * context info for each usbeth device
 */

typedef struct {
    u8 state;                        /* current state of device */
    IOSFd ipbFd;                     /* handle for IP stack bottom half */
    IPInterfaceStat stats;           /* link statistics; packet counts, etc. */
    NETEthTool defLinkInfo;          /* default link characteristics; speed, etc. */
    NETEthTool linkInfo;             /* link characteristics; speed, etc. */
    NETMcAddrList mcast;             /* multicast settings */
    IOSIobDebugInfo iobufDebugInfo;  /* debug info for iobuf's */
    IOSThreadId rxId;                /* receive thread's id */
    u8 rxRunning;                    /* 1 if receive thread is running */
    IOSMessageQueueId rxMsgQ;        /* message queue for recv operations */
    IOSMessage rxMsgArray[1];        /* message array for recv operations */
    IOSTimerId tmId;                 /* link status timer's id */
    EthHwInfo ethHw;                 /* ethernet hardware info */
} UsbEthContext;

#define USBETH_NUM_DEV 1
UsbEthContext usbEthContext[USBETH_NUM_DEV];

/*
 * usbeth device states
 */
#define USBETH_STATE_CLOSED  0       /* device closed */
#define USBETH_STATE_DOWN    1       /* device open, but link not config'ed */
#define USBETH_STATE_UP      2       /* device open and link config'ed */
#define USBETH_STATE_DROPPED 3       /* link was up, but then dropped out */

/* usbeth RM thread stack, stack size, priority */
#define USBETH_STACK_SIZE  (32 * 1024)
#define USBETH_PRIORITY  60
const u8 _initStack[USBETH_STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = IOS_PRIORITY_USBETH;

/* usbeth receive thread stack, stack size, priority */
#define USBETH_RECV_STACK_SIZE  (4 * 1024)
#define USBETH_RECV_PRIORITY  (IOS_PRIORITY_USBETH - 1)
static u8 _recvStack[USBETH_RECV_STACK_SIZE];

/* queue size for usbeth RM */
#define USBETH_MSGQ_SIZE  64

/*
 * Ethernet packet types (unicast versus multicast) are distinguished
 * by the lsb of the first octet of the destination address; note that
 * broadcast is a special case of multicast in which the destination
 * address is ff:ff:ff:ff:ff:ff
 */
#define ETH_ADDR_UNICAST(da)  ( ((da)&0x1) == 0 )  /* lsb of 1st DA byte */
#define ETH_ADDR_MULTICAST(da)  ((da)&0x1)  /* lsb of 1st DA byte */

/*
 * these min/max ethernet packet lengths do not including the FCS
 * since hardware automatically adds it and strips it off; ditto
 * for the preamble
 *
 * also, hardware automatically pads short packets, so a legal-length
 * packet goes out over the wire so long as a complete header is
 * provided
 *
 * NOTE: if VLAN support is desired, then add 4 to ETH_MAX_PACKET_SIZE
 */
#define ETH_MIN_PACKET_SIZE  (ETH_HLEN)
#define ETH_MAX_PACKET_SIZE  (ETH_HLEN+1500)

/*
 * print the contents of a packet to stdout, prepending a
 * user-supplied message
 */
#ifdef DUMP_PACKET
static void
dumpPacket( u8 *pkt, u32 length, char *msg )
{
    int i, j, k;

    printf("\n");
    printf("%s: dumping packet: length = %d\n", msg, length);
    for (i=0; i<length; i+= 16) {
        printf("%06d ", i);
        k = length - i;
        if ( k > 16 )
            k = 16;
        for ( j=0; j<k; j++ ) {
            printf("%02x ", pkt[i+j]);
        }
        printf("\n");
    }
}
#endif

/*
 * usbEthDiscardPacket -- determine whether received packets should
 * be discarded as a result of the current multicast filter settings
 *
 * returns 1 if packet should be discarded; IOS_ERROR_OK (0) if
 * packet should NOT be discarded
 */
static
IOSError usbEthDiscardPacket(NETMcAddrList *mcast, u8 *da)
{
    IOSError rv = IOS_ERROR_OK;
    s32 i;
    s32 found;

    UE_LOG("%s: da = %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
           da[0], da[1], da[2], da[3], da[4], da[5]);
    
    if ( ETH_ADDR_UNICAST(da[0]) ) {
        /* never discard unicast packets because -
         * if promiscuous mode, we keep all packets;
         * if not promiscuous mode, ethernet hardware automatically
         * discards all unicast packets except for those specifically
         * addressed to it;
         */
        UE_LOG("%s: keeping unicast packet\n", __FUNCTION__);
    } else {
        
        if ( da[0] == 0xff && da[1] == 0xff && da[2] == 0xff &&
             da[3] == 0xff && da[4] == 0xff && da[5] == 0xff ) {
            /* never discard broadcast packets */
            UE_LOG("%s: keeping broadcast packet\n", __FUNCTION__);
        } else {
            /* don't discard multicast packets if
             * (1) the NET_IF_ALLMULTI flag is set
             * OR
             * (2) its on the user's list of multicast packets to keep
             */
            UE_LOG("%s: multicast\n", __FUNCTION__);

            if ( mcast->flags & NET_IF_ALLMULTI ) {
                UE_LOG("%s: keeping multicast packet (allmulti)\n", __FUNCTION__);
            } else {
                found = 0;
                for (i=0; i<mcast->count; i++) {
                    if ( memcmp( da, mcast->mcAddr[i], ETH_ALEN ) == 0 ) {
                        found = 1;
                        break;
                    }
                }

                if ( found ) {
                    UE_LOG("%s: keeping multicast packet (match=%d)\n", __FUNCTION__, i);
                } else {
                    UE_LOG("%s: discarding multicast filter (no match)\n", __FUNCTION__);
                    rv = 1;
                }
            }
        }
    }

    return rv;
}

/*
 * usbEthRecvThread -- packet receiver thread
 *
 * standalone thread which receives packets for the ethernet
 * MAC/PHY, filters them, and passes them on to the IP stack.
 */
static
void usbEthRecvThread(u32 arg)
{
    UsbEthContext *uec = (UsbEthContext *)arg;

    IOSError rv;
    IOSMessage msg;
    u8 *ethHdr;
    u16 pktLen;
    IOSIobuf *iob;
    u8 *data;

    UE_LOG("%s: entry\n", __FUNCTION__);

    /*
     * loop, receiving packets and sending them to the IP stack
     */
    while (1) {

        /* check for message from main thread */
        rv = IOS_ReceiveMessage(uec->rxMsgQ, &msg, IOS_MESSAGE_NOBLOCK);
        if ( rv == IOS_ERROR_OK ) {
            /* only valid request is to exit */
            UE_LOG("%s: received stop message\n", __FUNCTION__);
            break;
        } else if ( rv != IOS_ERROR_QEMPTY ) {
            UE_ERROR("%s: unable to receive message, rv = %d\n", __FUNCTION__, rv);
            /* ignore error, fall through */
        }            

        /* recv a new packet */
        rv = axRecvPacket(&uec->ethHw, &ethHdr, &pktLen);
        if (rv < 0) {
            uec->stats.inErrors++;
            UE_LOG("%s: received bad packet, skipping, rv = %d\n", __FUNCTION__, rv);
            continue;
        }

        UE_LOG("%s: pktLen = %d (%x)\n", __FUNCTION__, pktLen, pktLen);
        if ( pktLen < ETH_HLEN ) {
            if (pktLen == 0) {
                UE_LOG("%s: received zero-length packet\n", __FUNCTION__);
            } else {
                uec->stats.inErrors++;
                UE_ERROR("%s: invalid packet length = %d\n", __FUNCTION__, pktLen);
            }
            continue;
        }

        /* filter packet */
        if ( usbEthDiscardPacket(&uec->mcast, ethHdr) ) {
            uec->stats.inDiscards++;
            UE_LOG("%s: discarding packet\n", __FUNCTION__);
#ifdef DUMP_PACKET
            dumpPacket(ethHdr, pktLen, "usbEthRecvThread: DROP");
#endif
            continue;
        }
        
        /* obtain an iobuf to hold the new packet */
        iob = IOS_AllocIob(POOL_DEFAULT, pktLen+NET_IP_ALIGN, uec->iobufDebugInfo);
        if (!iob) {
            uec->stats.inDiscards++;
            UE_ERROR("%s: allocating iobuf failed\n", __FUNCTION__);
            continue;
        }

        /* copy the new packet into the iobuf */
        data = IOS_PutIob(iob, pktLen+NET_IP_ALIGN);
        if (!data) {
            uec->stats.inDiscards++;
            UE_ERROR("%s: iobuf too small\n", __FUNCTION__);
            if ( (rv = IOS_FreeIob(iob)) != IOS_ERROR_OK ) {
               UE_ERROR("%s: too small: failed to free iob @ 0x%x, rv = %d\n",
                        __FUNCTION__, (u32)iob, rv);
               /* ignore error, fall through */
            }
            continue;
        }
            
        u8 *src = ethHdr;
        u8 *dst = data;
        memcpy((void *)dst, (const void *)src, ETH_HLEN);
        dst += ETH_HLEN;
        src += ETH_HLEN;
        memset((void *)dst, 0, NET_IP_ALIGN);
        dst += NET_IP_ALIGN;
        memcpy((void *)dst, (const void *)src, (int) pktLen - ETH_HLEN );

        /* optionally, dump the packet contents */
#ifdef DUMP_PACKET
        dumpPacket(data, pktLen, "usbEthRecvThread");
#endif

        /* send packet to IP stack */
        IOSError rv;
        IOSIoVector v[2];
        u32 ifID = NET_IF_ETH_USB;
        
        v[0].base   = (u8 *) &ifID;
        v[0].length = sizeof(ifID);
        v[1].base   = (u8 *) iob;
        v[1].length = sizeof(IOSIobuf);

        if ( (rv = IOS_Ioctlv(uec->ipbFd, NET_IP_RECV_PKT, sizeof(v)/sizeof(v[0]), 0, v)) < 0 ) {
            uec->stats.inErrors++;
            UE_ERROR("%s: NET_IP_RECV_PKT failed, rv = %d\n", __FUNCTION__, rv);
            if ( (rv = IOS_FreeIob(iob)) != IOS_ERROR_OK ) {
                UE_ERROR("%s: unable to free packet iob = 0x%x, rv = %d\n",
                         __FUNCTION__, (u32)iob, rv);
            }
        }
        
        /* update statistics */
        if ( ETH_ADDR_UNICAST(ethHdr[0]) ) {
            uec->stats.inUcastPackets++;
        } else {
            uec->stats.inNonUcastPackets++;
        }
        
    }
    
    UE_LOG("%s: exiting\n", __FUNCTION__);
    return;
}

/*
 * usbEthStartRecvThread -- start thread to read received packets from
 * ethernet MAC/PHY and forward them to IP stack
 *
 * return IOS_ERROR_OK if thread is started successfully, else error code
 */
static IOSError
usbEthStartRecvThread(UsbEthContext *uec)
{
    IOSError rv = IOS_ERROR_OK;

    /*
     * create receive message queue
     */
    if ( uec->rxMsgQ < 0 ) {
        uec->rxMsgQ = rv = IOS_CreateMessageQueue(uec->rxMsgArray, sizeof(uec->rxMsgArray)/sizeof(IOSMessage));
        if ( rv < 0 ) {
            UE_ERROR("%s: unable to create recv message queue, rv = %d\n",
                     __FUNCTION__, rv);
            goto out;
        }
    }

    /*
     * create receive thread
     */
    if ( uec->rxId < 0 ) {
        UE_LOG("%s: creating thread\n", __FUNCTION__);
        uec->rxId = rv = IOS_CreateThread(usbEthRecvThread,
                                          uec,
                                          _recvStack + USBETH_RECV_STACK_SIZE,
                                          USBETH_RECV_STACK_SIZE,
                                          USBETH_RECV_PRIORITY,
                                          IOS_THREAD_CREATE_JOINABLE);
        
        if (rv < 0) {
            UE_ERROR("%s: could not create recv thread, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
    }
    
    /*
     * start receive thread
     */
    if ( ! uec->rxRunning ) {
        UE_LOG("%s: starting thread\n", __FUNCTION__);
        if ( (rv = IOS_StartThread(uec->rxId)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: could not start recv thread, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
        uec->rxRunning = 1;
    }

out:
    UE_LOG("%s: exit\n", __FUNCTION__);
    return rv;
}

/*
 * usbEthStopRecvThread -- stop thread receiving packets from ethernet
 * MAC/PHY
 *
 * returns IOS_ERROR_OK on success, else an error code
 *
 * Note that the receive thread performed blocking reads from the
 * ethernet hardware.  If closing the ethernet device does not terminate 
 * the read (as is currently the case), then the thread must be manually
 * terminated because otherwise it can block indefinitely waiting for
 * a receive packets (which may not be forthcoming).  If (in the future)
 * the ethernet driver is modified to terminate any outstanding trans-
 * actions when a close is requested, then the receive thread can be sent
 * a message instructing it to exit cleanly.
 */
static IOSError
usbEthStopRecvThread(UsbEthContext *uec)
{
    IOSError rv = IOS_ERROR_OK;

    if ( uec->rxRunning ) {

        /* send stop message to receive thread
         *
         * since there's only one message (the "stop" message), the content
         * of the message doesn't matter; simply send the address of the UE
         * context
         */
        UE_LOG("%s: sending stop message\n", __FUNCTION__);
        if ((rv = IOS_SendMessage(uec->rxMsgQ, (u32)uec, IOS_MESSAGE_BLOCK)) != IOS_ERROR_OK) {
            UE_ERROR("%s: unable to send stop message, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }

        /* recv thread may be blocking on a bulk read from the ethernet
         * hardware; force ethernet to complete the read, even if no
         * data is returned
         */
        UE_LOG("%s: forcing bulk read to complete\n", __FUNCTION__);
        if ( (rv = axForceReadCompletion(&uec->ethHw)) < 0 ) {
            UE_ERROR("%s: unable to force read completion, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
        
        /* wait for receive thread to exit */
        UE_LOG("%s: waiting for recv thread to exit\n", __FUNCTION__);
        if ( (rv = IOS_JoinThread(uec->rxId, 0x0)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: unable to join receive thread, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }

        UE_LOG("%s: recv thread has exited properly\n", __FUNCTION__);
        
        uec->rxId = -1;
        uec->rxRunning = 0;

    } else if ( uec->rxId >= 0 ) {

        u32 val;
        
        /* thread was created but never started; destroy it */
        if ( (rv = IOS_DestroyThread(uec->rxId, &val)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: unable to kill recv thread, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
        uec->rxId = -1;
    }

    /* destroy message queue */
    if ( uec->rxMsgQ >= 0 ) {
        if ( (rv = IOS_DestroyMessageQueue(uec->rxMsgQ)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: unable to destroy recv message queue, rv = %d\n",
                        __FUNCTION__, rv);
            goto out;
        }
        uec->rxMsgQ = -1;
    }
    
out:
    UE_LOG("%s: exit\n", __FUNCTION__);
    return rv;
}

/*
 * usbEthNotify -- notify IP stack of a status change
 *
 * return IOS_ERROR_OK on success, else error code
 */
static IOSError
usbEthNotify(UsbEthContext *uec, u32 notifyID)
{
    IOSIoVector v[2];
    IOSError rv;
    
    u32 ifID = NET_IF_ETH_USB;

    v[0].base   = (u8 *) &ifID;
    v[0].length = sizeof(ifID);
    v[1].base   = (u8 *) &notifyID;
    v[1].length = sizeof(notifyID);

    if ( (rv = IOS_Ioctlv(uec->ipbFd, NET_IP_NOTIFY, 2, 0, v)) < 0 ) {
        UE_ERROR("%s: IP stack notify %d, rv = %d\n", __FUNCTION__, notifyID, rv);
        /* ignore error, fall through */
    }

    return rv;
}

/*
 * usbEthDevUp -- configure ethernet link, start packet receive thread
 *
 * return IOS_ERROR_OK on success, else error code
 */
static IOSError
usbEthDevUp(UsbEthContext *uec)
{
    IOSError rv;

    UE_LOG("%s: entry\n", __FUNCTION__);

    /* check current state */
    if ( uec->state != USBETH_STATE_DOWN ) {
        UE_ERROR("%s: device isn't down, can't bring up\n", __FUNCTION__);
        rv = IOS_ERROR_NOTREADY;
        goto out;
    }
    
    /* initialize the mac/phy hardware multicast filter */
    if ( (rv = axConfigMulticast(&uec->ethHw, &uec->mcast)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: usbEthConfigMulticast failed, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }

    /* configure the mac/phy */
    if ( (rv = axUp(&uec->ethHw, &uec->linkInfo)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to start up phy/mac, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }

    /* start the receive thread */
    UE_LOG("%s: start receive thread\n", __FUNCTION__);
    if ( (rv = usbEthStartRecvThread(uec)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to start receive thread, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }

    /* start link status timer */
    UE_LOG("%s: start timer (1 sec heartbeat)\n", __FUNCTION__);
    if ( (rv = IOS_RestartTimer(uec->tmId, 0, 1000000)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to start timer, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* set internal state */
    uec->state = USBETH_STATE_UP;

    /* notify IP stack */
    if ( (rv = usbEthNotify(uec, NET_NOTIFY_DEV_UP)) < 0 ) {
        UE_ERROR("%s: notify device up, rv = %d\n", __FUNCTION__, rv);
        /* ignore error, fall through */
        rv = IOS_ERROR_OK;
    }
    
out:
    return rv;
}
    
/*
 * usbEthDevDown -- shut down ethernet link, stop packet receive thread
 *
 * return IOS_ERROR_OK on success, else error code
 */
static IOSError
usbEthDevDown(UsbEthContext *uec)
{
    IOSError rv;

    UE_LOG("%s: entry\n", __FUNCTION__);

    /* check current state */
    if ( ! (uec->state == USBETH_STATE_UP || uec->state == USBETH_STATE_DROPPED) ) {
        UE_ERROR("%s: device isn't up, can't bring down\n", __FUNCTION__);
        rv = IOS_ERROR_NOTREADY;
        goto out;
    }
    
    /* turn off RX operations in the MAC/PHY hardware and
     * shut down the MAC/PHY
     */
    
    if ( (rv = axDown(&uec->ethHw)) < 0 ) {
        UE_ERROR("%s: unable to shut down the ethernet MAC/PHY, rv = %d\n", __FUNCTION__, rv);
        rv = IOS_ERROR_FAIL_INTERNAL;
        goto out;
    }
        
    /* stop link status timer */
    UE_LOG("%s: stop timer (1 sec heartbeat)\n", __FUNCTION__);
    if ( (rv = IOS_StopTimer(uec->tmId)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to stop timer, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* set internal state */
    uec->state = USBETH_STATE_DOWN;

    /* notify IP stack */
    if ( (rv = usbEthNotify(uec, NET_NOTIFY_DEV_DOWN)) < 0 ) {
        UE_ERROR("%s: notify device down, rv = %d\n", __FUNCTION__, rv);
        /* ignore error, fall through */
        rv = IOS_ERROR_OK;
    }
    
out:
    return rv;
}

/*
 * usbEthClose -- close the usbeth device
 *
 * return IOS_ERROR_OK on success, else error code
 */
static IOSError
usbEthClose(IOSResourceRequest *req)
{
    IOSError rv = IOS_ERROR_OK;
    UsbEthContext *uec = (UsbEthContext *) req->handle;

    UE_LOG("%s: entry\n", __FUNCTION__);

    /* if device is up, shut it down */
    if ( uec->state == USBETH_STATE_UP || uec->state == USBETH_STATE_DROPPED ) {
        if ( (rv = usbEthDevDown(uec)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: cannot shut down device, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
    }

    /* stop read thread */
    if ( (rv = usbEthStopRecvThread(uec)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to stop receive thread, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }

    /* close the ethernet device */
    if ( (rv = axClose(&uec->ethHw)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to close ethernet MAC/PHY, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* close connection to IP stack */
    if ( uec->ipbFd >= 0 && (rv = IOS_Close(uec->ipbFd)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to close IP stack connection, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    uec->ipbFd = -1;
    
    /* destroy timer */
    if ( uec->tmId >= 0 && (rv = IOS_DestroyTimer(uec->tmId)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: unable to destroy timer %d, rv = %d\n", __FUNCTION__, uec->tmId, rv);
        goto out;
    }

    uec->tmId = -1;
    
    /* mark as closed */
    uec->state = USBETH_STATE_CLOSED;
    
out:
    return rv;
}

/*
 * usbEthLinkStatus -- check link status and notify IP stack when
 *                     link goes up or down
 *
 * return IOS_ERROR_OK if link is up, IOS_ERROR_NOLINK if link is down,
 * else error code
 */
static IOSError
usbEthLinkStatus(UsbEthContext *uec)
{
    IOSError rv, rv2;

    UE_LOG("%s: checking link status\n", __FUNCTION__);

    /* sanity check -- only check status when link is up or dropped */
    if ( uec->state == USBETH_STATE_CLOSED || uec->state == USBETH_STATE_DOWN ) {
        UE_ERROR("%s: invalid state = %d\n", __FUNCTION__, uec->state);
        rv = IOS_ERROR_OK;
        goto out;
    }
    
    /* check link status */
    rv = axLinkStatus(&uec->ethHw);

    if ( rv == IOS_ERROR_OK ) {
        if ( uec->state == USBETH_STATE_DROPPED ) {
            UE_LOG("%s: link is back up\n", __FUNCTION__);
            /* notify stack that link is back up */
            if ( (rv2 = usbEthNotify(uec, NET_NOTIFY_DEV_UP)) == IOS_ERROR_OK ) {
                /* mark link as up */
                uec->state = USBETH_STATE_UP;
            } else {
                /* don't mark link as up so that notify will be re-attempted */
                UE_ERROR("%s: notify device up, rv = %d\n", __FUNCTION__, rv2);
                /* ignore error, fall through */
            }
            /* ignore error, fall through */
        }
        
    } else {

        if ( uec->state == USBETH_STATE_UP ) {
            UE_LOG("%s: link went down\n", __FUNCTION__);
            /* notify stack that link went down */
            if ( (rv2 = usbEthNotify(uec, NET_NOTIFY_DEV_DOWN)) == IOS_ERROR_OK ) {
                /* mark link as down */
                uec->state = USBETH_STATE_DROPPED;
            } else {
                /* don't mark link as down so that notify will be re-attempted */
                UE_ERROR("%s: notify device down, rv = %d\n", __FUNCTION__, rv2);
                /* ignore error, fall through */
            }
        }

        rv = IOS_ERROR_NO_LINK;

    }

out:
    return rv;
}

/*
 * usbEthOpen -- open usbeth device
 *
 * return IOS_ERROR_OK on success, else error code
 */ 
static IOSError
usbEthOpen(IOSResourceRequest *req, IOSMessageQueueId qid)
{
    IOSResourceOpen *args = &req->args.open;
    IOSError rv = IOS_ERROR_OK;
    UsbEthContext *uec = (UsbEthContext *)0x0;

    /* check user-specified path against paths handled by this RM */
    UE_LOG("%s: opening \"%s\"\n", __FUNCTION__, args->path);
    if ( strncmp(args->path, NET_RM_USBETH_TOP, sizeof(NET_RM_USBETH_TOP)) != 0 ) {
        UE_ERROR("%s: %s is not a usbeth device\n", __FUNCTION__, args->path);
        rv = IOS_ERROR_NOEXISTS;
        goto out;
    }

    /* determine if device already open */
    if ( usbEthContext[0].state != USBETH_STATE_CLOSED ) {
        UE_ERROR("%s: %s is already open\n", __FUNCTION__, args->path);
        rv = IOS_ERROR_MAX;
        goto out;
    }
 
    /* initialize context */
    uec = &usbEthContext[0];

    uec->state = USBETH_STATE_CLOSED;
    uec->ipbFd = -1;
    memset(&uec->stats, 0, sizeof(uec->stats));
    memset(&uec->defLinkInfo, 0, sizeof(uec->defLinkInfo));
    memset(&uec->linkInfo, 0, sizeof(uec->linkInfo));
    memset(&uec->mcast, 0, sizeof(uec->mcast));
    /* uec->iobufDebugInfo = 0; */  /* struct not defined yet */
    uec->rxId = -1;
    uec->rxRunning = 0;
    uec->rxMsgQ = -1;
    uec->tmId = -1;

    /* open the ethernet hardware driver */
    UE_LOG("%s: opening ethernet hardware\n", __FUNCTION__);
    if ( (rv = axOpen(&uec->ethHw)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: open of ethernet hardware failed, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* initialize the ethernet hardware */
    UE_LOG("%s: initializing ethernet hardware\n", __FUNCTION__);
    if ( (rv = axInit(&uec->ethHw, &uec->defLinkInfo)) < 0 ) {
        UE_ERROR("%s: initialize of ethernet hardware failed, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    memcpy(&uec->linkInfo, &uec->defLinkInfo, sizeof(NETEthTool));

    /* allocate timer */
    uec->tmId = rv = IOS_CreateTimer((IOSTime)0, (IOSTime)0, qid, (IOSMessage) uec);
    if ( rv < 0 ) {
        UE_ERROR("%s: unable to create timer, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* set state */
    UE_LOG("%s: usbeth open success!\n", __FUNCTION__);
    uec->state = USBETH_STATE_DOWN;
    
    /* open IP bottom-half device */
    if ( (uec->ipbFd = rv = IOS_Open(NET_RM_IP_BOT, 0)) < 0 ) {
        UE_ERROR("%s: %s open failed with rv = %d\n", __FUNCTION__, NET_RM_IP_BOT, rv);
        goto out;
    }
    UE_LOG("%s: ip success!\n", __FUNCTION__);

    rv = (IOSError) uec;
    
out:
    if ( uec != 0x0 && rv < 0 ) {
        IOSError rv2;

        if ( uec->tmId >= 0 ) {
            if ( (rv2 = IOS_DestroyTimer(uec->tmId)) != IOS_ERROR_OK ) {
                UE_ERROR("%s: unable to destroy timer %d, rv = %d\n",
                         __FUNCTION__, uec->tmId, rv2);
                /* ignore error, fall through */
            }
        }

        if ( uec->ipbFd >= 0 ) {
            if ( (rv2 = IOS_Close(uec->ipbFd)) < 0 ) {
                UE_ERROR("%s: unable to close %s, rv = %d\n", __FUNCTION__,
                         NET_RM_IP_BOT, rv2);
                /* ignore error, fall through */
            }
        }

        if ( (rv2 = axClose(&uec->ethHw)) < 0 ) {
            UE_ERROR("%s: unable to close ethernet hardware, rv = %d\n",
                     __FUNCTION__, rv2);
            /* ignore error, fall through */
        }
    }
    UE_LOG("%s: exiting with rv = %d (0x%x)\n", __FUNCTION__, rv, rv);
    return rv;
}

/*
 * usbEthIoctlv -- perform IOCTLV operations
 *
 * return IOS_ERROR_OK on success, else error code
 */
static IOSError
usbEthIoctlv(IOSResourceRequest *req)
{
    IOSResourceIoctlv *ioctlv;
    IOSError rv = IOS_ERROR_INVALID;
    UsbEthContext *uec = (UsbEthContext *) req->handle;
    IOSIobuf *iob;
    
    ioctlv = &req->args.ioctlv;

    UE_LOG("%s: cmd=%d\n", __FUNCTION__, ioctlv->cmd);

    switch (ioctlv->cmd) {
    case NET_DEV_XMIT_PKT:
        UE_LOG("%s: NET_DEV_XMIT_PKT\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 1 || ioctlv->writeCount != 0 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != sizeof(IOSIobuf) ) {
            goto out;
        }

        /* check driver state */
        if ( !(uec->state == USBETH_STATE_UP || uec->state == USBETH_STATE_DROPPED) ) {
            IOSError rv2;
            
            UE_ERROR("%s: link not established, can't send packet\n", __FUNCTION__);
            uec->stats.outErrors++;
            rv = IOS_ERROR_NOTREADY;

            /* notify IP stack that xmit failed */
            if ( (rv2 = usbEthNotify(uec, NET_NOTIFY_DEV_XMIT_ERR)) < 0 ) {
                UE_ERROR("%s: notify of xmit error failed (1), rv = %d\n", __FUNCTION__, rv2);
                /* ignore error, fall through */
            }

            goto out;
        }

        /* re-check link status if most-recent check indicated that link was down */
        if ( uec->state == USBETH_STATE_DROPPED && (rv = usbEthLinkStatus(uec)) < 0 ) {
            IOSError rv2;
            
            UE_ERROR("%s: link down or unable to read status, rv = %d\n", __FUNCTION__, rv);
            uec->stats.outErrors++;
            
            /* notify IP stack that xmit failed */
            if ( (rv2 = usbEthNotify(uec, NET_NOTIFY_DEV_XMIT_ERR)) < 0 ) {
                UE_ERROR("%s: notify of xmit error failed (2), rv = %d\n", __FUNCTION__, rv2);
                /* ignore error, fall through */
            }

            goto out;
        }
        
        /* check packet size */
        iob = (IOSIobuf *) ioctlv->vector[0].base;
        if ( iob->dataLen < ETH_MIN_PACKET_SIZE || iob->dataLen > ETH_MAX_PACKET_SIZE ) {
            UE_ERROR("%s: invalid packet size = %d\n", __FUNCTION__, iob->dataLen);
            uec->stats.outErrors++;

            /* notify IP stack that xmit failed */
            if ( (rv = usbEthNotify(uec, NET_NOTIFY_DEV_XMIT_ERR)) < 0 ) {
                UE_ERROR("%s: notify of xmit error failed (3), rv = %d\n", __FUNCTION__, rv);
                /* ignore error, fall through */
            }
            
            rv = IOS_ERROR_INVALID_SIZE;
            goto out;
        } 

        /* optionally, dump packets contents to stdout */
#ifdef DUMP_PACKET
        dumpPacket( iob->data, iob->dataLen, "NET_DEV_XMIT_PKT");
#endif
        
        /* write packet to ethernet hardware */
        rv = axXmitPacket(&uec->ethHw, iob);
        if ( rv == IOS_ERROR_FAIL_ALLOC ) {
            uec->stats.outDiscards++;
        } else if ( rv < 0 ) {
            uec->stats.outErrors++;
        }
        if( rv < 0  ) {
            UE_ERROR("%s: WriteBlkMsg failed, rv = %d\n", __FUNCTION__, rv);

            /* notify IP stack that xmit failed */
            if ( (rv = usbEthNotify(uec, NET_NOTIFY_DEV_XMIT_ERR)) < 0 ) {
                UE_ERROR("%s: notify xmit error failed, rv = %d\n", __FUNCTION__, rv);
                /* ignore error, fall through */
            }

            rv = IOS_ERROR_FAIL_INTERNAL;
            goto out;
        }
        
        /* update sent-packet statistics */
        ETHHeader *eh = (ETHHeader *) iob->data;
#ifdef DUMP_PACKET
        dumpPacket( iob->data, iob->dataLen, "NET_DEV_XMIT_PKT2");
#endif
        
        if ( ETH_ADDR_UNICAST(eh->dst[0]) ) {
            uec->stats.outUcastPackets++;
        } else {
            uec->stats.outNonUcastPackets++;
        }

        /* free buffer that packets used to occupy */
        if ( (rv = IOS_FreeIob(iob)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: failed to free iob @ 0x%x, rv = %d\n",
                     __FUNCTION__, (u32)iob, rv);
            /* ignore error, fall through */
        }

        rv = IOS_ERROR_OK;
        break;
        
    case NET_DEV_UP:
        UE_LOG("%s: NET_DEV_UP\n", __FUNCTION__);
        rv = usbEthDevUp(uec);
        UE_LOG("%s: usbEthDevUp exited with rv = %d\n", __FUNCTION__, rv);
        break;
        
    case NET_DEV_DOWN:
        UE_LOG("%s: NET_DEV_DOWN\n", __FUNCTION__);
        rv = usbEthDevDown(uec);
        UE_LOG("%s: usbEthDevDown exited with rv = %d\n", __FUNCTION__, rv);
        break;

    case NET_DEV_SET_MULTICAST_LIST:
        UE_LOG("%s: NET_DEV_SET_MULTICAST_LIST\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 1 || ioctlv->writeCount != 0 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != sizeof(NETMcAddrList) ) {
            goto out;
        }
        
        /* check multicast settings for validity */
        NETMcAddrList *mcast = (NETMcAddrList *)ioctlv->vector[0].base;
        if ( mcast->count > NET_MAX_MC_ADDR ) {
            UE_ERROR("%s: invalid multicast address count = %d\n", __FUNCTION__,
                     mcast->count);
            rv = IOS_ERROR_INVALID_SIZE;
            goto out;
        }

        /* copy multicast info into device context */
        memcpy(&uec->mcast, mcast, sizeof(NETMcAddrList));

        /* if link is up, reconfigure hardware multicast filter */
        if ( uec->state == USBETH_STATE_UP || uec->state == USBETH_STATE_DROPPED ) {
            if ( (rv = axConfigMulticast(&uec->ethHw, &uec->mcast)) != IOS_ERROR_OK ) {
                UE_ERROR("%s: usbEthConfigMulticast failed, rv = %d\n", __FUNCTION__, rv);
                goto out;
            }
        } 
        rv = IOS_ERROR_OK;
        break;

    case NET_DEV_SET_MAC_ADDRESS:
        UE_LOG("%s: NET_DEV_SET_MAC_ADDRESS\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 1 || ioctlv->writeCount != 0 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != ETH_ALEN ) {
            goto out;
        }

        /* set hardware MAC address as specified */
        if ( (rv = axWriteMac(&uec->ethHw, ioctlv->vector[0].base)) < 0 ) {
#if defined(UE_DEBUG) || defined(UE_DEBUG_ERROR)
            u8 *mac = ioctlv->vector[0].base;
#endif
            UE_ERROR("%s: unable to write MAC address (%d-%d-%d-%d-%d-%d, 0x%x-%x-%x-%x-%x-%x), rv = %d\n",
                     __FUNCTION__,
                     mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
                     mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
                     rv);
            rv = IOS_ERROR_FAIL_INTERNAL;
            goto out;
        }
        rv = IOS_ERROR_OK;
        break;
        
    case NET_DEV_GET_MAC_ADDRESS:
        UE_LOG("%s: NET_DEV_GET_MAC_ADDRESS\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 0 || ioctlv->writeCount != 1 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != ETH_ALEN ) {
            goto out;
        }
        
        /* get MAC address from hardware */
        if ( (rv = axReadMac(&uec->ethHw, ioctlv->vector[0].base)) < 0 ) {
            UE_ERROR("%s: unable to read MAC address, rv = %d\n", __FUNCTION__, rv);
            rv = IOS_ERROR_FAIL_INTERNAL;
            goto out;
        }
        rv = IOS_ERROR_OK;
        break;
        
    case NET_DEV_GET_STATS:
        UE_LOG("%s: NET_DEV_GET_STATS\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 0 || ioctlv->writeCount != 1 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != sizeof(IPInterfaceStat) ) {
            goto out;
        }
        
        /* copy statistics into user's buffer */
        memcpy(ioctlv->vector[0].base, &uec->stats, sizeof(IPInterfaceStat));
        rv = IOS_ERROR_OK;
        break;

    case NET_DEV_SET_ETHTOOL:
        UE_LOG("%s: NET_DEV_SET_ETHTOOL\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 1 || ioctlv->writeCount != 0 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != sizeof(NETEthTool) ) {
            UE_ERROR("%s: rc=%d, wc=%d, base=0x%x, len=%d(%d)\n",
                     __FUNCTION__, ioctlv->readCount, ioctlv->writeCount,
                     (u32) ioctlv->vector[0].base,
                     ioctlv->vector[0].length, sizeof(NETEthTool));
            goto out;
        }

        /* check that link is down */
        if ( uec->state != USBETH_STATE_DOWN ) {
            UE_ERROR("%s: link isn't down, can't set ethtool\n");
            rv = IOS_ERROR_NOTREADY;
            goto out;
        }
        
        /* check link config settings for validity */
        NETEthTool *et = (NETEthTool *)ioctlv->vector[0].base;
        NETEthTool *det = &uec->defLinkInfo;
        if ( et->supported != det->supported ) {
            UE_ERROR("%s: 'supported' field cannot be modified, must be set to 0x%x\n",
                     __FUNCTION__, det->supported);
            rv = IOS_ERROR_INVALID_FLAG;
            goto out;
        }

        if ( et->advertising & ~et->supported ) {
            UE_ERROR("%s: cannot advertise features (0x%x) that aren't supported (0x%x)\n",
                     __FUNCTION__, et->advertising, et->supported);
            rv = IOS_ERROR_INVALID_FLAG;
            goto out;
        }
        
        if ( et->autoneg ) {
            if ( !(det->supported & NET_SUPPORTED_AUTONEG) ) {
                UE_ERROR("%s: autoneg selected, but not supported\n", __FUNCTION__);
                rv = IOS_ERROR_INVALID_FLAG;
                goto out;
            } else if ( !(et->advertising & NET_SUPPORTED_AUTONEG) ) {
                UE_ERROR("%s: autoneg selected, but not advertised\n", __FUNCTION__);
                rv = IOS_ERROR_INVALID_FLAG;
                goto out;
            }
        } else {
            if ( et->speed == NET_SPEED_10 ) {
                if ( et->duplex == NET_DUPLEX_HALF && !(det->supported & NET_SUPPORTED_10BASET_HALF) ) {
                    UE_ERROR("%s: 10Mbps half-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } else if ( et->duplex == NET_DUPLEX_FULL && !(det->supported & NET_SUPPORTED_10BASET_FULL) ) {
                    UE_ERROR("%s: 10Mbps full-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } 
            } else if ( et->speed == NET_SPEED_100 ) {
                if ( et->duplex == NET_DUPLEX_HALF && !(det->supported & NET_SUPPORTED_100BASET_HALF) ) {
                    UE_ERROR("%s: 100Mbps half-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } else if ( et->duplex == NET_DUPLEX_FULL && !(det->supported & NET_SUPPORTED_100BASET_FULL) ) {
                    UE_ERROR("%s: 100Mbps full-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } 
            } else if ( et->speed == NET_SPEED_1000 ) {
                if ( et->duplex == NET_DUPLEX_HALF && !(det->supported & NET_SUPPORTED_1000BASET_HALF) ) {
                    UE_ERROR("%s: 1000Mbps half-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } else if ( et->duplex == NET_DUPLEX_FULL && !(det->supported & NET_SUPPORTED_1000BASET_FULL) ) {
                    UE_ERROR("%s: 1000Mbps full-duplex selected, but not supported\n", __FUNCTION__);
                    rv = IOS_ERROR_INVALID_FLAG;
                    goto out;
                } 
            } else {
                UE_ERROR("%s: no autoneg and no speed selected\n", __FUNCTION__);
                rv = IOS_ERROR_INVALID_FLAG;
                goto out;
            }
        }
        
        /* copy desired link config info into device context */
        memcpy(&uec->linkInfo, ioctlv->vector[0].base, sizeof(NETEthTool));
        rv = IOS_ERROR_OK;
        break;
        
    case NET_DEV_GET_ETHTOOL:
        UE_LOG("%s: NET_DEV_GET_ETHTOOL\n", __FUNCTION__);
        /* check input parameters */
        if ( ioctlv->readCount != 0 || ioctlv->writeCount != 1 ||
             ioctlv->vector[0].base == 0x0 ||
             ioctlv->vector[0].length != sizeof(NETEthTool) ) {
            goto out;
        }
        
        /* copy link config info into user's buffer */
        memcpy(ioctlv->vector[0].base, &uec->linkInfo, sizeof(NETEthTool));
        rv = IOS_ERROR_OK;
        break;
        
    default:
        UE_ERROR("%s: unknown command = %d (0x%x)\n", __FUNCTION__, ioctlv->cmd, ioctlv->cmd);
        break;
    }
    
out:
    return rv;
}

/*
 * main
 *
 * allocate resources for usbeth Resource Manager (RM), register the
 * RM, then enter an infinite loop processing requests
 */
int main(void)
{   
    IOSMessageQueueId qid;
    IOSMessage msgQ[USBETH_MSGQ_SIZE];
    IOSMessage msg;
    IOSError rv;
    
    UE_LOG("%s: *** USB Ethernet RM Starting ***\n", __FUNCTION__);

    /* Create a RM message queue for usb-eth interface layer */
    if ( (qid = rv = IOS_CreateMessageQueue(msgQ, USBETH_MSGQ_SIZE)) < 0 ) {
        UE_ERROR("%s: IOS_CreateMessageQueue failed, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }
    
    /* Register RM */
    if ( (rv = IOS_RegisterResourceManager(NET_RM_USBETH_TOP, qid)) != IOS_ERROR_OK ) {
        UE_ERROR("%s: RegisterResourceManager failed, rv = %d\n", __FUNCTION__, rv);
        goto out;
    }

    /* Sit in a loop processing requests */
    while (1) {

        /* block waiting for a request */
        UE_LOG("%s: waiting for msg\n", __FUNCTION__);
        if ( (rv = IOS_ReceiveMessage(qid, &msg, IOS_MESSAGE_BLOCK)) != IOS_ERROR_OK ) {
            UE_ERROR("%s: IOS_ReceiveMessage failed, rv = %d\n", __FUNCTION__, rv);
            goto out;
        }
        UE_LOG("%s: received message = 0x%08x\n", __FUNCTION__, (u32)msg);

        if ( msg == (IOSMessage) &usbEthContext[0] ) {

            UE_LOG("%s: calling link status, msg=0x%x, state = %d\n",
                   __FUNCTION__, msg, usbEthContext[0].state);
            if ( (rv = usbEthLinkStatus(&usbEthContext[0])) != IOS_ERROR_OK ) {
                /* ignore error, fall through */
            }
            UE_LOG("%s: link status return, rv = %d\n", __FUNCTION__, rv);
            
           
        } else {
            
            IOSResourceRequest *req = (IOSResourceRequest *)msg;
            IOSResourceOpen *openArgs;
            
            /* determine the type of request and process it */
            switch (req->cmd) {
            case IOS_OPEN:
                UE_LOG("%s: IOS_OPEN\n", __FUNCTION__);
                openArgs = (IOSResourceOpen *) &(req->args.open);
                if (openArgs->uid != IOS_NET_PROCESS) {
                    rv = IOS_ERROR_ACCESS;
                } else {
                    rv = usbEthOpen(req, qid);
                }
                UE_LOG("%s: IOS_OPEN returning rv = %d\n", __FUNCTION__, rv);
                break;
            case IOS_CLOSE:
                UE_LOG("%s: IOS_CLOSE\n", __FUNCTION__);
                rv = usbEthClose(req);
                UE_LOG("%s: IOS_CLOSE returning rv = %d\n", __FUNCTION__, rv);
                break;
            case IOS_IOCTLV:
                UE_LOG("%s: IOS_IOCTLV\n", __FUNCTION__);
                rv = usbEthIoctlv(req);
                UE_LOG("%s: IOS_IOCTLV returning rv = %d\n", __FUNCTION__, rv);
                break;
            default:
                rv = IOS_ERROR_INVALID;
                UE_LOG("%s: unknown command = %d (0x%x) returning rv = %d\n", __FUNCTION__, req->cmd, req->cmd, rv);
                break;
            }
            IOS_ResourceReply(req, rv);
        }
    }

out:
    UE_LOG("%s: exiting, rv = %d\n", __FUNCTION__, rv);
    return rv;
}
