syntheth.c

来自「开放源码实时操作系统源码.」· C语言 代码 · 共 462 行 · 第 1/2 页

C
462
字号
synth_eth_recv(struct eth_drv_sc* sc, struct eth_drv_sg* sg_list, int sg_len)
{
    synth_eth*      eth     = (synth_eth*)(sc->driver_private);
    unsigned char*  buf     = eth->rx_data;
    int             len     = eth->rx_len;
    int             i;

    for (i = 0; i < sg_len; i++) {
        if (0 == sg_list[i].buf) {
            break;
        } else if (len <= sg_list[i].len) {
            memcpy((void*)sg_list[i].buf, buf, len);
            break;
        } else {
            memcpy((void*)sg_list[i].buf, buf, sg_list[i].len);
            buf += sg_list[i].len;
            len -= sg_list[i].len;
        }
    }
}

// The ISR does not have to do anything, the DSR does the real work.
static cyg_uint32
synth_eth_isr(cyg_vector_t vector, cyg_addrword_t data)
{
    cyg_drv_interrupt_acknowledge(vector);
    return CYG_ISR_CALL_DSR;
}

// The DSR also does not have to do very much. The data argument is
// actually the eth_drv_sc structure, which must match the vector.
// Interrupts only go off when there are pending receives, so set the
// rx_pending flag and call the generic DSR to do the real work.
static void
synth_eth_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
{
    struct eth_drv_sc* sc = (struct eth_drv_sc*) data;
    synth_eth* eth        = (synth_eth*)(sc->driver_private);
    CYG_ASSERT(eth->interrupt == vector, "Interrupt vectors cannot change during a run");

    eth->rx_pending = 1;
    eth_drv_dsr(vector, count, data);
}

// ----------------------------------------------------------------------------
// Delivery. This is invoked by a thread inside the TCP/IP stack, or by
// the poll function.
static void
synth_eth_deliver(struct eth_drv_sc* sc)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);

    if (eth->tx_done) {
        eth->tx_done = 0;
        (*sc->funs->eth_drv->tx_done)(sc, eth->tx_key, 1);
    }
    while (eth->rx_pending) {
        int more = 1;
        eth->rx_pending = 0;

        while (more && eth->up && synth_auxiliary_running) {
            synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_RX, 0, 0, (void*) 0, 0,
                                    &more, eth->rx_data, &(eth->rx_len), ETHERNET_MAXTU);
            CYG_LOOP_INVARIANT(!more || (0 != eth->rx_len), "Auxiliary must send at least one packet if several are available");
            if (eth->rx_len > 0) {
                CYG_ASSERT((eth->rx_len >= ETHERNET_MINTU) && (eth->rx_len <= ETHERNET_MAXTU), "Only normal-sized ethernet packets are supported");
                // Inform higher-level code that data is available.
                // This should result in a call to recv() with a
                // suitable sg_list. If out of memory, recv()
                // will see a null pointer.
                (*sc->funs->eth_drv->recv)(sc, eth->rx_len);
            }
        };
    }
}


// ----------------------------------------------------------------------------
// Polling support. Transmits are relatively straightforward because
// all the hard work is handled by send(). Receives are rather more
// complicated because interrupts are disabled so we never know when
// there is really pending data. However deliver() will do the right
// thing even if there is no data, so simply faking up an interrupt
// is enough. This does mean extra traffic between application and
// auxiliary, but polling does rather imply that.
static void
synth_eth_poll(struct eth_drv_sc* sc)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);
    if (synth_auxiliary_running && eth->up) {
        eth->rx_pending = 1;
    }
    synth_eth_deliver(sc);
}

static int
synth_eth_intvector(struct eth_drv_sc* sc)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);
    return eth->interrupt;
}

// ----------------------------------------------------------------------------
// ioctl()'s.
//
// SET_MAC_ADDRESS is not currently implemented, and probably should
// not be implemented because the underlying ethernet device may not
// support it.
//
// SET_MC_ALL is supported if the underlying hardware does. This is
// needed for IPV6 support. More selective multicasting via
// SET_MC_LIST is not supported, because it imposes too heavy a
// requirement on the underlying Linux device. SET_MC_LIST can be
// used to disable multicast support.
//
// GET_IF_STATS_UD and GET_IF_STATS are not currently implemented
static int
synth_eth_ioctl(struct eth_drv_sc* sc, unsigned long key, void* data, int data_length)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);
    int result = EINVAL;
    
    switch(key) {
      case ETH_DRV_SET_MC_ALL:
        {
            if (eth->multi_supported) {
                synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_MULTIALL, 1, 0, (unsigned char*) 0, 0,
                                       (void*)0, (unsigned char*)0, (int*)0, 0);
                result = 0;
            }
            break;
        }
      case ETH_DRV_SET_MC_LIST:
        {
            struct eth_drv_mc_list* mcl = (struct eth_drv_mc_list*) data;
            if (eth->multi_supported && (0 == mcl->len)) {
                synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_MULTIALL, 0, 0, (unsigned char*) 0, 0,
                                       (void*)0, (unsigned char*)0, (int*)0, 0);
                result = 0;
            }
            break;
        }
      default:
        break;
    }
    
    return result;
}

// ----------------------------------------------------------------------------
// Starting and stopping an interface. This includes restarting in
// promiscuous mode.
static void
synth_eth_start(struct eth_drv_sc* sc, unsigned char* enaddr, int flags)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);

    eth->up         = 0;
    eth->rx_pending = 0;
    
    if ((-1 != eth->synth_id) && synth_auxiliary_running) {
        synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_START, flags & IFF_PROMISC, 0, (void*) 0, 0,
                                (int*)0, (void*) 0, (int*) 0, 0);
    }
    eth->up = 1;
    if (enaddr != (unsigned char*)0) {
        memcpy(enaddr, eth->MAC, 6);
    }
}

static void
synth_eth_stop(struct eth_drv_sc* sc)
{
    synth_eth* eth = (synth_eth*)(sc->driver_private);

    eth->up         = 0;
    if ((-1 != eth->synth_id) && synth_auxiliary_running) {
        synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_STOP, 0, 0, (void*) 0, 0,
                                (int*) 0, (void*) 0, (int*) 0, 0);
    }
    eth->rx_pending = 0;
}


// ----------------------------------------------------------------------------
// Initialization.
//
// This requires instantiating a device of class ethernet with a specific
// device name held in the eth_drv_sc structure. No additional device data
// is needed. If a device can be instantiated then the interrupt vector
// and the MAC address can be obtained, and an interrupt handler can be
// installed.
static bool
synth_eth_init(struct cyg_netdevtab_entry* ndp)
{
    bool result = false;
    struct eth_drv_sc* sc   = (struct eth_drv_sc*)(ndp->device_instance);
    struct synth_eth*  eth  = (struct synth_eth*)(sc->driver_private);

    if (synth_auxiliary_running) {
        eth->synth_id = synth_auxiliary_instantiate("devs/eth/synth/ecosynth", SYNTH_MAKESTRING(CYGPKG_DEVS_ETH_ECOSYNTH), "ethernet",
                                                    sc->dev_name, (const char*) 0);
        
        if (-1 != eth->synth_id) {
            unsigned char data[7];
            result = true;
            synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_GETPARAMS, 0, 0, (const unsigned char*) 0, 0,
                                    (int *)&(eth->interrupt), data, 0, 7);
            memcpy(eth->MAC, data, 6);
            eth->multi_supported = data[6];
            cyg_drv_interrupt_create(eth->interrupt,
                                     0,
                                     (CYG_ADDRWORD) sc,
                                     &synth_eth_isr,
                                     &synth_eth_dsr,
                                     &(eth->interrupt_handle),
                                     &(eth->interrupt_data));
            cyg_drv_interrupt_attach(eth->interrupt_handle);
            cyg_drv_interrupt_unmask(eth->interrupt);
        }
    }
    (*sc->funs->eth_drv->init)(sc, eth->MAC);

#ifdef CYGPKG_NET
    if (eth->multi_supported) {
      sc->sc_arpcom.ac_if.if_flags |= IFF_ALLMULTI;
    }
#endif    
    return result;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?