openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

wrap.c (4747B)


      1 /* $Id: //depot/blt/srv/network/device/ne/wrap.c#1 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 /*
     30  * limitations:  can only support one card for now.
     31  */
     32 
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <blt/qsem.h>
     36 #include <blt/network/eth.h>
     37 #include <blt/network/mbuf.h>
     38 #include "ne2000.h"
     39 #include "wrap.h"
     40 
     41 #define NE_IRQ 3
     42 
     43 void probe (void);
     44 static void isr (void);
     45 static void receive (void *cb, packet_data *data);
     46 static void send (int num, struct mbuf *data);
     47 
     48 static eth_drv_t driver = { "ne", send, NULL };
     49 
     50 static int maxdev = 0;
     51 static int __irq;
     52 static qsem_t *__mutex;
     53 static ne_card_t *__card;
     54 static eth_dev_t *__dev;
     55 
     56 int _init (void)
     57 {
     58 	register_eth_driver (&driver);
     59 	return 0;
     60 }
     61 
     62 void _fini (void)
     63 {
     64 }
     65 
     66 void probe (void)
     67 {
     68 	int iobase;
     69 	ne_card_t *card;
     70 	eth_dev_t *dev;
     71 
     72 	if (iobase = nic_detect (0))
     73 	{
     74 		card = malloc (sizeof (ne_card_t));
     75 		card->nic.iobase = 0;
     76 		card->irq = NE_IRQ;
     77 		card->mutex = qsem_create (1);
     78 		nic_init (&card->nic, iobase, card->prom, NULL);
     79 		printf ("network: ne0 at port 0x%x, irq %d, mac = %X:%X:%X:%X:%X:%X\n",
     80 			card->nic.iobase, card->irq, card->prom[0], card->prom[1],
     81 			card->prom[2], card->prom[3], card->prom[4], card->prom[5]);
     82 		dev = malloc (sizeof (eth_dev_t));
     83 		dev->dev_driver = &driver;
     84 		dev->dev_num = maxdev++;
     85 		memcpy (dev->dev_addr, card->prom, sizeof (dev->dev_addr));
     86 		dev->dev_data = card;
     87 
     88 		register_eth_device (dev);
     89 		nic_register_notify (&card->nic, receive, NULL);
     90 		nic_start (&card->nic, 0);
     91 
     92 		__card = card;
     93 		__irq = card->irq;
     94 		__mutex = card->mutex;
     95 		__dev = dev;
     96 
     97 		thr_create (isr, 0, "network:ne:isr");
     98 	}
     99 	else
    100 		printf ("network: ne: probe failed.\n");
    101 }
    102 
    103 int config (int num, const char *prot, int state, const char *data)
    104 {
    105 	eth_config (__dev, prot, state, data);
    106 	return 0;
    107 }
    108 
    109 static void isr (void)
    110 {
    111 	os_handle_irq (__irq);
    112 	for (;;)
    113 	{
    114 		os_sleep_irq ();
    115 		qsem_acquire (__mutex);
    116 		nic_isr (&__card->nic);
    117 		qsem_release (__mutex);
    118 	}
    119 }
    120 
    121 static void receive (void *cb, packet_data *data)
    122 {
    123 	struct mbuf *mbuf, *header;
    124 
    125 	mbuf = mget ();
    126 	mbuf->m_type = MT_DATA;
    127 	mbuf->m_len = data->len;
    128 	if (data->len < MLEN)
    129 	{
    130 		mbuf->m_data = mbuf->m_databuf;
    131 		memcpy (mbuf->m_data, data->ptr, data->len);
    132 	}
    133 	free_buffer_data (data);
    134 
    135 	header = mget ();
    136 	header->m_type = MT_IFADDR;
    137 	header->m_len = 3;
    138 	*((eth_dev_t **) header->m_data) = __dev;
    139 	header->m_next = mbuf;
    140 
    141 	eth_input (header);
    142 }
    143 
    144 struct _pbuf {
    145     packet_buffer pb;    /* 16 */
    146     packet_data pd;      /* 8 */
    147     int n;
    148 } pbuf;
    149 
    150 static void send (int num, struct mbuf *data)
    151 {
    152 	int res;
    153 	packet_buffer *pb;
    154 
    155 	printf ("send! %d\n", data->m_next->m_len);
    156 #if 0
    157 	pb = malloc (sizeof (packet_buffer));
    158 	pb->count = 1;
    159 	pb->len = data->m_next->m_len;
    160 	pb->buf = alloc_buffer_data (pb->len);
    161 	pb->buf->len = data->m_next->m_len;
    162 	memcpy (pb->buf->ptr, data->m_next->m_data, pb->len);
    163 	qsem_acquire (__mutex);
    164 	res = nic_send_packet (&__card->nic, pb);
    165 	qsem_release (__mutex);
    166 #else
    167 	pbuf.pb.count = 1;
    168 	pbuf.pb.buf = &pbuf.pd;
    169 	pbuf.pd.ptr = malloc (1000);
    170 	memcpy (pbuf.pd.ptr, data->m_next->m_data, data->m_next->m_len);
    171 	pbuf.pb.len = pbuf.pd.len = data->m_next->m_len + 100;
    172 	qsem_acquire (__mutex);
    173 	res = nic_send_packet (&__card->nic, &pbuf.pb);
    174 	qsem_release (__mutex);
    175 #endif
    176 	printf ("sent %d\n", res);
    177 	mput (data->m_next);
    178 	mput (data);
    179 }
    180