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