⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ne2kif.c

📁 自已写的lwip+ucos程序已调通
💻 C
📖 第 1 页 / 共 2 页
字号:
	 //packetLength muse >=64 (see 802.3)

    if ((p->tot_len) < 64)
   {
       padLength = 64 - (p->tot_len);
       packetLength = 64;

   }
   	
   	//yangye 2003-1-21
	//don't close nic,just close receive interrupt	
	outb(CMD_PAGE2 | CMD_NODMA | CMD_RUN, NE_CR);
	isr = inb(NE_IMR);
	isr &= ~ISR_PRX;
      	outb(CMD_PAGE0 | CMD_NODMA | CMD_RUN, NE_CR);
	outb(isr, NE_IMR);
	
	outb(ISR_RDC, NE_ISR);	
	//yangye 2003-1-21
	//or close nic(CMD_STOP) during receive ???
	//outb(CMD_PAGE0 | CMD_NODMA | CMD_STOP, NE_CR);
	
	
    // Amount to send
	outb(packetLength & 0xff, NE_RBCR0);
	outb(packetLength >> 8, NE_RBCR1);

    // Address on NIC to store
	outb(XMIT_START & 0xff, NE_RSAR0);
	outb(XMIT_START >> 8, NE_RSAR1);
	// Write command to start
	outb(CMD_PAGE0 | CMD_WRITE | CMD_RUN, NE_CR);    

/*
 * Write packet to ring buffers.
 */
   for(q = p; q != NULL; q = q->next) {
    /* Send the data from the pbuf to the interface, one pbuf at a
       time. The size of the data in each pbuf is kept in the ->len
       variable. */
	ne2k_copyout(q->len, q->payload);
    }

	while(padLength-- > 0){	
		outb(0, NE_DMA); 	// Write padding for undersized packets
	}
    
	//  Wait for remote dma to complete - ISR Bit 6 clear if busy
	while((u8_t)(inb(NE_ISR) & ISR_RDC) == 0 );
		
	outb(ISR_RDC, NE_ISR);     //clear RDC
/*
 * Issue the transmit command.(start local dma)
 */
 	outb(XMIT_START >> 8, NE_TPSR);
	outb(packetLength & 0xff, NE_TBCR0);
	outb(packetLength >> 8, NE_TBCR1);	
	//  Start transmission (and shut off remote dma)
	//  and reopen nic(CMD_RUN)
	outb(CMD_PAGE0 | CMD_NODMA | CMD_XMIT | CMD_RUN, NE_CR);
		
	//yangye 2003-1-21
	//reopen receive interrupt
	outb(CMD_PAGE2 | CMD_NODMA | CMD_RUN, NE_CR);
	isr = inb(NE_IMR);
	isr |= ISR_PRX;
     	outb(CMD_PAGE0 | CMD_NODMA | CMD_RUN, NE_CR);
	outb(isr, NE_IMR);
	return ERR_OK;
}


/**
 * Read a packet into a pbuf chain.
 */
static struct pbuf * 
low_level_receive(struct RTL8019if *rtl8019if)
{
	u16_t  packetLength,len;

	u8_t PDHeader[18];   // Temp storage for ethernet headers
	
	struct pbuf * p;
	struct pbuf * q;
	u8_t * payload;
	
// uHALr_printf("is low_level-receive statr\n");

	outb(ISR_RDC, NE_ISR);
	outb(0x0f, NE_RBCR1); 	/* See controller manual , use send packet command */
	outb(CMD_PAGE0 | CMD_SEND | CMD_RUN, NE_CR);

	//get the first 18 bytes from nic 
	ne2k_copyin(18,PDHeader);

	//  Store real length, set len to packet length - header
	packetLength = ((unsigned) PDHeader[2] | (PDHeader[3] << 8 ));

   	//verify if the packet is an IP packet or ARP packet
	if((PDHeader[3]>0x06)||(PDHeader[16] != 8)||(PDHeader[17] != 0 && PDHeader[17] != 6))
        {
		 uHALr_printf("check  verify is packet is an ip packeet or arp packet \n");
	     ne2k_discard(packetLength-14);
             return NULL;
        }  
    	
	  /* We allocate a pbuf chain of pbufs from the pool. */

	p = pbuf_alloc(PBUF_LINK, packetLength, PBUF_POOL);
	if(p != NULL) {
		/* We iterate over the pbuf chain until we have read the entire
      		 packet into the pbuf. */
           	// This assumes a minimum pbuf size of 14 ... a good assumption
//uHALr_printf("WE ALLOCATE APBUF CHAIN OF PBUFS FROM THE PLLO \n");
           	memcpy(p->payload, PDHeader + 4, 14);
	
		for(q = p; q != NULL; q= q->next){
     		   /* Read enough bytes to fill this pbuf in the chain. The
         	  avaliable data in the pbuf is given by the q->len
         	  variable. */
      		  /* read data into(q->payload, q->len); */
     		  // First 14 bytes are already there, skip them
      	
		  payload = q->payload;
	//uHALr_printf(payload);	  
		  len = q->len;

		  if (q == p){
	//uHALr_printf("q====p");
           	    payload += 14;
		    len -=14;
           	  }
		ne2k_copyin(len,payload);
      		
		}//for
	} else {
    /* no more PBUF resource, Discard packet in buffer. */
	ne2k_discard(packetLength-14);
  	}
	return p;
}


/*-----------------------------------------------------------------------------------*/
/*
 * ethernetif_init():
 *
 * Should be called at the beginning of the program to set up the
 * network interface. It calls the function low_level_init() to do the
 * actual setup of the hardware.
 *
 *///  WARNING: must close all interrupts during init!!!!
/*-----------------------------------------------------------------------------------*/
#if 0
void ne2k_init(struct netif *netif)
{
  struct RTL8019if *rtl8019if;

  rtl8019if = mem_malloc(sizeof(struct RTL8019if));
  netif->state = rtl8019if;
  netif->name[0] = 'e';
  netif->name[1] = 't';
  netif->output = ne2k_send_packet;
  
  
  
  low_level_init(netif);
  etharp_init();
}
#endif

err_t ne2k_init(struct netif *netif)
{
  struct RTL8019if *rtl8019if;
  rtl8019if = mem_malloc(sizeof(struct RTL8019if));
  netif->state = rtl8019if;
  netif->name[0] = 'e';
  netif->name[1] = 't';
  netif->output = ne2k_send_packet;
  
  low_level_init(netif);
  etharp_init();
  return ERR_OK;
}
err_t ne2k_send_packet(struct netif *netif, struct pbuf *p,
		  struct ip_addr *ipaddr)
{
  struct RTL8019if *rtl8019if;
  struct pbuf *q;
  struct eth_hdr *ethhdr;
  struct eth_addr *dest, mcastaddr;
  struct ip_addr *queryaddr;
  err_t err;
  u8_t i;
  
  rtl8019if = netif->state;

  /* Make room for Ethernet header. */
  if(pbuf_header(p, 14) != 0) {
    /* The pbuf_header() call shouldn't fail, but we allocate an extra
       pbuf just in case. */
    q = pbuf_alloc(PBUF_LINK, 14, PBUF_RAM);
    if(q == NULL) {
      return ERR_MEM;
    }
    pbuf_chain(q, p);
    p = q;
  }

  /* Construct Ethernet header. Start with looking up deciding which
     MAC address to use as a destination address. Broadcasts and
     multicasts are special, all other addresses are looked up in the
     ARP table. */
  queryaddr = ipaddr;
  if(ip_addr_isany(ipaddr) || ip_addr_isbroadcast(ipaddr, &(netif->netmask))) {
    dest = (struct eth_addr *)&ethbroadcast;
  } 
  else if(ip_addr_ismulticast(ipaddr)) {
    /* Hash IP multicast address to MAC address. */
    mcastaddr.addr[0] = 0x01;
    mcastaddr.addr[1] = 0x0;
    mcastaddr.addr[2] = 0x5e;
    mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
    mcastaddr.addr[4] = ip4_addr3(ipaddr);
    mcastaddr.addr[5] = ip4_addr4(ipaddr);
    dest = &mcastaddr;
  } else {

    if(ip_addr_maskcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
      /* Use destination IP address if the destination is on the same
         subnet as we are. */
      queryaddr = ipaddr;
    } else {
      /* Otherwise we use the default router as the address to send
         the Ethernet frame to. */
      queryaddr = &(netif->gw);
    }
    dest = arp_lookup(queryaddr);
  }


  /* If the arp_lookup() didn't find an address, we send out an ARP
     query for the IP address. */
  if(dest == NULL) {
    q = arp_query(netif, rtl8019if->ethaddr, queryaddr);
    if(q != NULL) {
	  low_level_send(rtl8019if, q);
      pbuf_free(q);
      return err;
    }
     
    return ERR_MEM;
  }
  ethhdr = p->payload;

  for(i = 0; i < 6; i++) {
    ethhdr->dest.addr[i] = dest->addr[i];
    ethhdr->src.addr[i] = rtl8019if->ethaddr->addr[i];
  }
  
  ethhdr->type = htons(ETHTYPE_IP);
  
  return low_level_send(rtl8019if, p);
	
}


/**
 * Read a packet, clearing overflows.
 */
void  ne2k_recv_packet(struct netif *netif)
{
  struct RTL8019if *rtl8019if;
  struct eth_hdr *ethhdr;
  struct pbuf *aaap;
	

  rtl8019if = netif->state;
 
  aaap = low_level_receive(rtl8019if);
 
  
  if(aaap == NULL) {
	uHALr_printf("is p terurn\n");

    return;
  }
  ethhdr = aaap->payload;
  uHALr_printf("ethhdr= %x\n",htons(ethhdr->type));
  switch(htons(ethhdr->type)) {
  case ETHTYPE_IP:
    uHALr_printf("this  pack is ip pack\n");
    etharp_ip_input(netif, aaap);
    //uHALr_printf("is qhyarp aaaaaaaaakaaaaaaaaaaaaaaaaaaaajjjjjjjjjjjjjjjjjjjjjjjjjjj pack arriver over\n");

    pbuf_header(aaap, -14);
    netif->input(aaap, netif);
    break;

  case ETHTYPE_ARP:
       uHALr_printf("this pack is arp pack\n");

    aaap = etharp_arp_input(netif, rtl8019if->ethaddr, aaap);

    if(aaap != NULL) {

	low_level_send(rtl8019if, aaap);
	pbuf_free(aaap);
    }
    break;
  default:
    pbuf_free(aaap);
    break;
  }
}

⌨️ 快捷键说明

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