unstack - fix ipcvecs
[minix.git] / sys / arch / i386 / stand / lib / netif / netif_small.c
blob0a46233df8e24d4087dd2dd35381469f907f3d06
1 /* $NetBSD: netif_small.c,v 1.12 2009/10/21 23:12:09 snj Exp $ */
3 /* minimal netif - for boot ROMs we don't have to select between
4 several interfaces, and we have to save space
6 hacked from netbsd:sys/arch/mvme68k/stand/libsa/netif.c
7 */
9 /*
10 * Copyright (c) 1995 Gordon W. Ross
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #ifdef _STANDALONE
37 #include <lib/libkern/libkern.h>
38 #else
39 #include <string.h>
40 #endif
42 #include <net/if.h>
43 #include <net/if_ether.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
48 #include <lib/libsa/stand.h>
49 #include <lib/libsa/net.h>
51 #include "netif_small.h"
52 #include "etherdrv.h"
54 #ifdef NETIF_DEBUG
55 int netif_debug = 1;
56 #endif
58 /* we allow for one socket only */
59 static struct iodesc iosocket;
61 struct iodesc *
62 socktodesc(int sock)
64 if (sock != 0) {
65 return NULL;
67 return &iosocket;
70 int
71 netif_open(void)
73 struct iodesc *io;
75 io = &iosocket;
76 if (io->io_netif) {
77 #ifdef NETIF_DEBUG
78 printf("netif_open: device busy\n");
79 #endif
80 return -1;
82 memset(io, 0, sizeof(*io));
84 if (!EtherInit(io->myea)) {
85 printf("EtherInit failed\n");
86 return -1;
89 io->io_netif = (void*)1; /* mark busy */
91 return 0;
94 void
95 netif_close(int fd)
97 struct iodesc *io;
99 if (fd != 0) {
100 return;
103 io = &iosocket;
104 if (io->io_netif) {
105 EtherStop();
106 io->io_netif = NULL;
111 * Send a packet. The ether header is already there.
112 * Return the length sent (or -1 on error).
115 netif_put(struct iodesc *desc, void *pkt, size_t len)
117 #ifdef NETIF_DEBUG
118 if (netif_debug) {
119 struct ether_header *eh;
121 printf("netif_put: desc=%p pkt=%p len=%d\n",
122 desc, pkt, len);
123 eh = pkt;
124 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
125 printf("src: %s ", ether_sprintf(eh->ether_shost));
126 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
128 #endif
129 return EtherSend(pkt, len);
133 * Receive a packet, including the ether header.
134 * Return the total length received (or -1 on error).
137 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
139 int len;
140 satime_t t;
142 #ifdef NETIF_DEBUG
143 if (netif_debug)
144 printf("netif_get: pkt=%p, maxlen=%d, tmo=%d\n",
145 pkt, maxlen, timo);
146 #endif
148 t = getsecs();
149 len = 0;
150 while (((getsecs() - t) < timo) && !len) {
151 len = EtherReceive(pkt, maxlen);
154 #ifdef NETIF_DEBUG
155 if (netif_debug) {
156 struct ether_header *eh = pkt;
158 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
159 printf("src: %s ", ether_sprintf(eh->ether_shost));
160 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
162 #endif
164 return len;