No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / ofppc / stand / ofwboot / netif_of.c
bloba29cb56a7291090e592bedb6bbae4fab29e8a663
1 /* $NetBSD: netif_of.c,v 1.8 2009/01/12 07:49:57 tsutsui Exp $ */
3 /*
4 * Copyright (C) 1995 Wolfgang Solfrank.
5 * Copyright (C) 1995 TooLs GmbH.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Open Firmware does most of the job for interfacing to the hardware,
36 * so it is easiest to just replace the netif module with
37 * this adaptation to the PROM network interface.
39 * Note: this is based in part on sys/arch/sparc/stand/netif_sun.c
42 #include <sys/param.h>
43 #include <sys/socket.h>
45 #include <net/if.h>
46 #include <net/if_ether.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
51 #include <lib/libsa/stand.h>
52 #include <lib/libsa/net.h>
54 #include "ofdev.h"
55 #include "openfirm.h"
57 #include "netif_of.h"
59 static struct iodesc sdesc;
61 struct iodesc *
62 socktodesc(int sock)
64 if (sock != 0)
65 return NULL;
66 return &sdesc;
69 int
70 netif_of_open(struct of_dev *op)
72 struct iodesc *io;
74 #ifdef NETIF_DEBUG
75 printf("netif_open...");
76 #endif
77 /* find a free socket */
78 io = &sdesc;
79 if (io->io_netif) {
80 #ifdef NETIF_DEBUG
81 printf("device busy\n");
82 #endif
83 errno = ENFILE;
84 return -1;
86 memset(io, 0, sizeof *io);
88 io->io_netif = (void *)op;
90 /* Put our ethernet address in io->myea */
91 OF_getprop(OF_instance_to_package(op->handle),
92 "mac-address", io->myea, sizeof io->myea);
94 #ifdef NETIF_DEBUG
95 printf("OK\n");
96 #endif
97 return 0;
100 void
101 netif_of_close(int fd)
103 struct iodesc *io;
105 #ifdef NETIF_DEBUG
106 printf("netif_close(%x)...", fd);
107 #endif
109 #ifdef NETIF_DEBUG
110 if (fd != 0) {
111 printf("EBADF\n");
112 return;
114 #endif
116 io = &sdesc;
117 io->io_netif = NULL;
119 #ifdef NETIF_DEBUG
120 printf("OK\n");
121 #endif
125 * Send a packet. The ether header is already there.
126 * Return the length sent (or -1 on error).
128 ssize_t
129 netif_put(struct iodesc *desc, void *pkt, size_t len)
131 struct of_dev *op;
132 ssize_t rv;
133 size_t sendlen;
135 op = (struct of_dev *)desc->io_netif;
137 #ifdef NETIF_DEBUG
139 struct ether_header *eh;
141 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
142 desc, pkt, len);
143 eh = pkt;
144 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
145 printf("src: %s ", ether_sprintf(eh->ether_shost));
146 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
148 #endif
150 sendlen = len;
151 if (sendlen < 60) {
152 sendlen = 60;
153 #ifdef NETIF_DEBUG
154 printf("netif_put: length padded to %d\n", sendlen);
155 #endif
158 rv = OF_write(op->handle, pkt, sendlen);
160 #ifdef NETIF_DEBUG
161 printf("netif_put: xmit returned %d\n", rv);
162 #endif
164 return rv;
168 * Receive a packet, including the ether header.
169 * Return the total length received (or -1 on error).
171 ssize_t
172 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
174 struct of_dev *op;
175 int tick0, tmo_ms;
176 int len;
178 op = (struct of_dev *)desc->io_netif;
180 #ifdef NETIF_DEBUG
181 printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
182 pkt, maxlen, timo);
183 #endif
185 tmo_ms = timo * 1000;
186 tick0 = OF_milliseconds();
188 do {
189 len = OF_read(op->handle, pkt, maxlen);
190 } while ((len == -2 || len == 0) &&
191 (OF_milliseconds() - tick0 < tmo_ms));
193 #ifdef NETIF_DEBUG
194 printf("netif_get: received len=%d\n", len);
195 #endif
197 if (len < 12)
198 return -1;
200 #ifdef NETIF_DEBUG
202 struct ether_header *eh = pkt;
204 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
205 printf("src: %s ", ether_sprintf(eh->ether_shost));
206 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
208 #endif
210 return len;
214 * Shouldn't really be here, but is used solely for networking, so...
216 satime_t
217 getsecs(void)
219 return OF_milliseconds() / 1000;