xref: /netbsd/sys/arch/i386/stand/lib/netif/netif_small.c (revision bf9ec67e)
1 /*	$NetBSD: netif_small.c,v 1.6 2001/07/07 22:57:58 perry Exp $	*/
2 
3 /* minimal netif - for boot ROMs we don't have to select between
4   several interfaces, and we have to save space
5 
6   hacked from netbsd:sys/arch/mvme68k/stand/libsa/netif.c
7  */
8 
9 /*
10  * Copyright (c) 1995 Gordon W. Ross
11  * All rights reserved.
12  *
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.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  * 4. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *      This product includes software developed by Gordon W. Ross
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #ifdef _STANDALONE
42 #include <lib/libkern/libkern.h>
43 #else
44 #include <string.h>
45 #endif
46 
47 #include <net/if.h>
48 #include <net/if_ether.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 
53 #include <lib/libsa/stand.h>
54 #include <lib/libsa/net.h>
55 
56 #include "netif_small.h"
57 #include "etherdrv.h"
58 
59 #ifdef NETIF_DEBUG
60 int netif_debug=1;
61 #endif
62 
63 /* we allow for one socket only */
64 static struct iodesc iosocket;
65 
66 struct iodesc *
67 socktodesc(sock)
68 	int sock;
69 {
70 	if (sock != 0) {
71 		return(NULL);
72 	}
73 	return (&iosocket);
74 }
75 
76 int
77 netif_open()
78 {
79 	struct iodesc *io;
80 
81 	io = &iosocket;
82 	if (io->io_netif) {
83 #ifdef	DEBUG
84 		printf("netif_open: device busy\n");
85 #endif
86 		return (-1);
87 	}
88 	memset(io, 0, sizeof(*io));
89 
90 	if(!EtherInit(io->myea)) {
91 		printf("EtherInit failed\n");
92 		return(-1);
93 	}
94 
95 	io->io_netif = (void*)1; /* mark busy */
96 
97 	return(0);
98 }
99 
100 int
101 netif_close(fd)
102 	int fd;
103 {
104 	struct iodesc *io;
105 
106 	if (fd != 0) {
107 		errno = EBADF;
108 		return(-1);
109 	}
110 
111 	io = &iosocket;
112 	if(io->io_netif) {
113 		EtherStop();
114 		io->io_netif = NULL;
115 	}
116 	return(0);
117 }
118 
119 /*
120  * Send a packet.  The ether header is already there.
121  * Return the length sent (or -1 on error).
122  */
123 int
124 netif_put(desc, pkt, len)
125 	struct iodesc *desc;
126 	void *pkt;
127 	size_t len;
128 {
129 #ifdef NETIF_DEBUG
130 	if (netif_debug) {
131 		struct ether_header *eh;
132 
133 		printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
134 			   desc, pkt, len);
135 		eh = pkt;
136 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
137 		printf("src: %s ", ether_sprintf(eh->ether_shost));
138 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
139 	}
140 #endif
141 	return(EtherSend(pkt, len));
142 }
143 
144 /*
145  * Receive a packet, including the ether header.
146  * Return the total length received (or -1 on error).
147  */
148 int
149 netif_get(desc, pkt, maxlen, timo)
150 	struct iodesc *desc;
151 	void *pkt;
152 	size_t maxlen;
153 	time_t timo;
154 {
155 	int len;
156 	time_t t;
157 
158 #ifdef NETIF_DEBUG
159 	if (netif_debug)
160 		printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
161 			   pkt, maxlen, timo);
162 #endif
163 
164 	t = getsecs();
165 	len = 0;
166 	while (((getsecs() - t) < timo) && !len) {
167 	    len = EtherReceive(pkt, maxlen);
168 	}
169 
170 #ifdef NETIF_DEBUG
171 	if (netif_debug) {
172 		struct ether_header *eh = pkt;
173 
174 		printf("dst: %s ", ether_sprintf(eh->ether_dhost));
175 		printf("src: %s ", ether_sprintf(eh->ether_shost));
176 		printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
177 	}
178 #endif
179 
180 	return len;
181 }
182