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

📄 rt_3c905x.c

📁 最新rtlinux内核源码
💻 C
字号:
/* * Copyright (c) 2001, Swedish Institute of Computer Science. * All rights reserved.  * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * 3. Neither the name of the Institute nor the names of its contributors  *    may be used to endorse or promote products derived from this software  *    without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  * SUCH DAMAGE.  * *//*********************************************************************************//* This file is based in one file part of the lwIP TCP/IP stack. The file is:    *//* ethernetif.c                                                                  *//* which author is: Adam Dunkels <adam@sics.se>                                  *//*                                                                               *//* This file has been modified by Sergio Perez Alca駃z <serpeal@upvnet.upv.es>   *//*            Departamento de Inform醫ica de Sistemas y Computadores             *//*            Universidad Polit閏nica de Valencia                                *//*            Valencia (Spain)                                                   *//*                                                                               *//* The RTL-lwIP project has been supported by the Spanish Government Research    *//* Office (CICYT) under grant TIC2002-04123-C03-03                               *//*                                                                               *//* Copyright (c) March, 2003 SISTEMAS DE TIEMPO REAL EMPOTRADOS, FIABLES Y       *//* DISTRIBUIDOS BASADOS EN COMPONENTES                                           *//*                                                                               *//*  This program is free software; you can redistribute it and/or modify         *//*  it under the terms of the GNU General Public License as published by         *//*  the Free Software Foundation; either version 2 of the License, or            *//*  (at your option) any later version.                                          *//*                                                                               *//*  This program is distributed in the hope that it will be useful,              *//*  but WITHOUT ANY WARRANTY; without even the implied warrabnty of              *//*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *//*  GNU General Public License for more details.                                 *//*                                                                               *//*  You should have received a copy of the GNU General Public License            *//*  along with this program; if not, write to the Free Software                  *//*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    *//*                                                                               *//*  Linking RTL-lwIP statically or dynamically with other modules is making a    *//*  combined work based on RTL-lwIP.  Thus, the terms and conditions of the GNU  *//*  General Public License cover the whole combination.                          *//*                                                                               *//*  As a special exception, the copyright holders of RTL-lwIP give you           *//*  permission to link RTL-lwIP with independent modules that communicate with   *//*  RTL-lwIP solely through the interfaces, regardless of the license terms of   *//*  these independent modules, and to copy and distribute the resulting combined *//*  work under terms of your choice, provided that every copy of the combined    *//*  work is accompanied by a complete copy of the source code of RTL-lwIP (the   *//*  version of RTL-lwIP used to produce the combined work), being distributed    *//*  under the terms of the GNU General Public License plus this exception.  An   *//*  independent module is a module which is not derived from or based on         *//*  RTL-lwIP.                                                                    *//*                                                                               *//*  Note that people who make modified versions of RTL-lwIP are not obligated to *//*  grant this special exception for their modified versions; it is their choice *//*  whether to do so.  The GNU General Public License gives permission to        *//*  release a modified version without this exception; this exception also makes *//*  it possible to release a modified version which carries forward this         *//*  exception.                                                                   *//*********************************************************************************/#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/sys.h"#include "netif/etharp.h"#include "netif/rt_3c905x_exports.h"#include "netif/ethernetif.h"#include <unistd.h>#include "bcopy.h"#include <signal.h>#include <rtl_sema.h>#define IFNAME0 'e'#define IFNAME1 't'#define COM3_SIGNAL RTL_SIGUSR2ethernetif_thread_t com3_thread;struct rt_3c905cif {  struct eth_addr *ethaddr;};static const struct eth_addr rt_3c905cif_ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};static int rt_3c905cif_fd;static struct netif *rt_3c905cif_netif;/* Forward declarations. */static void rt_3c905cif_input(struct pbuf *p, struct netif *netif);static err_t rt_3c905cif_output(struct netif *netif, struct pbuf *p,			       struct ip_addr *ipaddr);static void rt_3c905cif_ethernetif_thread(void *arg);/*-----------------------------------------------------------------------------------*/static voidrt_3com905cif_low_level_init(struct netif *netif){  struct rt_3c905cif *rt_3c905cif;  char dev_name[]={"/dev/eth0"};    rt_3c905cif = netif->state;    /* Do whatever else is needed to initialize interface. */    if((rt_3c905cif_fd=open(dev_name,0)) == -1)    rtl_printf("ERROR OPENING /dev/%s0\n",COM3_905C_NAME);  /* Obtain MAC address from network interface. */  ioctl(rt_3c905cif_fd, 2, (unsigned long) rt_3c905cif->ethaddr->addr);  /* We set an IP filter to the ethernet card */  ioctl(rt_3c905cif_fd, 1, (unsigned long) netif->ip_addr.addr);//  rtl_printf("ADRESS: %x:%x:%x:%x:%x:%x \n",rt_3c905cif->ethaddr->addr[0],rt_3c905cif->ethaddr->addr[1],rt_3c905cif->ethaddr->addr[2],rt_3c905cif->ethaddr->addr[3],rt_3c905cif->ethaddr->addr[4],rt_3c905cif->ethaddr->addr[5]);  sys_thread_new(rt_3c905cif_ethernetif_thread, netif, 0);  return ;}/*-----------------------------------------------------------------------------------*/static void rt_3c905cif_ethernetif_thread(void *arg){  struct sched_param p;  p . sched_priority = 100000;  pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);  do{    rt_3c905cif_input(NULL, rt_3c905cif_netif);    }while(1);}/*-----------------------------------------------------------------------------------*//* * rt_3com905cif_low_level_output(): * * Should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf * might be chained. * *//*-----------------------------------------------------------------------------------*/err_trt_3com905cif_low_level_output(struct netif *rt_3c905cif, struct pbuf *p){  struct pbuf *q;  unsigned char buf[1536];  unsigned char *bufptr;    //initiate transfer;  bufptr = buf;      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. */    bcopy(q->payload, bufptr, q->len);    bufptr += q->len;  }  //signal that packet should be sent;  {    int tmp;    int counter=0;     while((tmp = write(rt_3c905cif_fd,buf,p->tot_len)) == -1){      counter++;      usleep(1);      }  }    return ERR_OK;}/*-----------------------------------------------------------------------------------*//* * rt_3com905cif_low_level_input(): * * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * *//*-----------------------------------------------------------------------------------*/static struct pbuf *rt_3com905cif_low_level_input(struct rt_3c905cif *rt_3c905cif){  struct pbuf *p, *q;  struct memory receive_buffer;  unsigned char *bufptr;  u16_t len;    /* Obtain the size of the packet and put it into the "len"     variable. */  len = read(rt_3c905cif_fd,(void *) &receive_buffer,1536);    /* We allocate a pbuf chain of pbufs from the pool. */  p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);    if(p != NULL) {    /* We iterate over the pbuf chain until we have read the entire       packet into the pbuf. */    bufptr = receive_buffer.mem;    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. */      bcopy(bufptr, q->payload, q->len);      bufptr += q->len;    }  } else {    rtl_printf("ERROR:Not enough memory!!!\n");    sys_arch_close();  }  return p;  }/*-----------------------------------------------------------------------------------*//* * rt_3c905cif_output(): * * This function is called by the TCP/IP stack when an IP packet * should be sent. It calls the function called rt_3com905cif_low_level_output() to * do the actuall transmission of the packet. * *//*-----------------------------------------------------------------------------------*/static err_trt_3c905cif_output(struct netif *netif, struct pbuf *p,		  struct ip_addr *ipaddr){    p = etharp_output(netif, ipaddr, p);  if(p != NULL) {    rt_3com905cif_low_level_output(netif, p);  }  return ERR_OK;}/*-----------------------------------------------------------------------------------*//* * rt_3c905cif_input(): * * This function should be called when a packet is ready to be read * from the interface. It uses the function rt_3com905cif_low_level_input() that * should handle the actual reception of bytes from the network * interface. * *//*-----------------------------------------------------------------------------------*/static voidrt_3c905cif_input(struct pbuf *o, struct netif *netif)//(struct netif *netif){  /* Ethernet protocol layer */  struct eth_hdr *ethhdr;  struct rt_3c905cif *rt_3c905cif = netif->state;  struct pbuf *q = NULL , *p = NULL;    p = rt_3com905cif_low_level_input(rt_3c905cif);  ethhdr = p->payload;    switch(htons(ethhdr->type)) {  case ETHTYPE_IP:    q = etharp_ip_input(netif, p);    pbuf_header(p, -14);    netif->input(p, netif);    break;  case ETHTYPE_ARP:    q = etharp_arp_input(netif, rt_3c905cif->ethaddr, p);    break;  default:    pbuf_free(p);    break;  }  if(q != NULL) {    rt_3com905cif_low_level_output(netif, q);    pbuf_free(q);  }}/*-----------------------------------------------------------------------------------*/void rt_3com905cif_etharp_timer(int signo){  etharp_tmr();  sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)rt_3com905cif_etharp_timer, NULL);}/*-----------------------------------------------------------------------------------*//* * rt_3c905cif_init(): * * Should be called at the beginning of the program to set up the * network interface. It calls the function rt_3com905cif_low_level_init() to do the * actual setup of the hardware. * *//*-----------------------------------------------------------------------------------*/err_trt_3c905cif_init(struct netif *netif){  struct rt_3c905cif *rt_3c905cif;      rt_3c905cif = mem_malloc(sizeof(struct rt_3c905cif));  netif->state = rt_3c905cif;  netif->name[0] = IFNAME0;  netif->name[1] = IFNAME1;  netif->output = rt_3c905cif_output;  netif->linkoutput = rt_3com905cif_low_level_output;    rt_3c905cif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);  netif->hwaddr_len = 6;  rt_3c905cif_netif = netif;   rt_3com905cif_low_level_init(netif);  etharp_init();  sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)rt_3com905cif_etharp_timer, NULL);  return ERR_OK;}/*-----------------------------------------------------------------------------------*/void rt_3c905cif_close(void){  /* Unregistering the thread */  ioctl(rt_3c905cif_fd, 4, (unsigned long) &com3_thread);  /* Closing the 3Com905C-X card */  close(rt_3c905cif_fd);}

⌨️ 快捷键说明

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