1 /* $NetBSD: netif_small.c,v 1.12 2009/10/21 23:12:09 snj 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 *
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.
32 */
33
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
41
42 #include <net/if.h>
43 #include <net/if_ether.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47
48 #include <lib/libsa/stand.h>
49 #include <lib/libsa/net.h>
50
51 #include "netif_small.h"
52 #include "etherdrv.h"
53
54 #ifdef NETIF_DEBUG
55 int netif_debug = 1;
56 #endif
57
58 /* we allow for one socket only */
59 static struct iodesc iosocket;
60
61 struct iodesc *
socktodesc(int sock)62 socktodesc(int sock)
63 {
64 if (sock != 0) {
65 return NULL;
66 }
67 return &iosocket;
68 }
69
70 int
netif_open(void)71 netif_open(void)
72 {
73 struct iodesc *io;
74
75 io = &iosocket;
76 if (io->io_netif) {
77 #ifdef NETIF_DEBUG
78 printf("netif_open: device busy\n");
79 #endif
80 return -1;
81 }
82 memset(io, 0, sizeof(*io));
83
84 if (!EtherInit(io->myea)) {
85 printf("EtherInit failed\n");
86 return -1;
87 }
88
89 io->io_netif = (void*)1; /* mark busy */
90
91 return 0;
92 }
93
94 void
netif_close(int fd)95 netif_close(int fd)
96 {
97 struct iodesc *io;
98
99 if (fd != 0) {
100 return;
101 }
102
103 io = &iosocket;
104 if (io->io_netif) {
105 EtherStop();
106 io->io_netif = NULL;
107 }
108 }
109
110 /*
111 * Send a packet. The ether header is already there.
112 * Return the length sent (or -1 on error).
113 */
114 int
netif_put(struct iodesc * desc,void * pkt,size_t len)115 netif_put(struct iodesc *desc, void *pkt, size_t len)
116 {
117 #ifdef NETIF_DEBUG
118 if (netif_debug) {
119 struct ether_header *eh;
120
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);
127 }
128 #endif
129 return EtherSend(pkt, len);
130 }
131
132 /*
133 * Receive a packet, including the ether header.
134 * Return the total length received (or -1 on error).
135 */
136 int
netif_get(struct iodesc * desc,void * pkt,size_t maxlen,saseconds_t timo)137 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
138 {
139 int len;
140 satime_t t;
141
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
147
148 t = getsecs();
149 len = 0;
150 while (((getsecs() - t) < timo) && !len) {
151 len = EtherReceive(pkt, maxlen);
152 }
153
154 #ifdef NETIF_DEBUG
155 if (netif_debug) {
156 struct ether_header *eh = pkt;
157
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);
161 }
162 #endif
163
164 return len;
165 }
166