Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sparc / stand / ofwboot / netif_of.c
bloba785924a9173347049918113be182a905c7eca81
1 /* $NetBSD: netif_of.c,v 1.6 2009/01/12 11:32:44 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>
53 #include <lib/libsa/netif.h>
55 #include <machine/promlib.h>
57 #include "ofdev.h"
59 static struct netif netif_of;
61 struct iodesc sockets[SOPEN_MAX];
63 struct iodesc *
64 socktodesc(int sock)
66 if (sock != 0)
67 return NULL;
68 return sockets;
71 int
72 netif_open(void *machdep_hint)
74 struct of_dev *op = machdep_hint;
75 struct iodesc *io;
76 int fd, error;
77 char addr[32];
79 #ifdef NETIF_DEBUG
80 printf("netif_open...");
81 #endif
82 /* find a free socket */
83 io = sockets;
84 if (io->io_netif) {
85 #ifdef NETIF_DEBUG
86 printf("device busy\n");
87 #endif
88 errno = ENFILE;
89 return -1;
91 memset(io, 0, sizeof *io);
93 netif_of.nif_devdata = op;
94 io->io_netif = &netif_of;
96 /* Put our ethernet address in io->myea */
97 _prom_getprop(prom_instance_to_package(op->handle),
98 "mac-address", io->myea, sizeof io->myea);
100 #ifdef NETIF_DEBUG
101 printf("OK\n");
102 #endif
103 return 0;
107 netif_close(int fd)
109 struct iodesc *io;
110 struct netif *ni;
112 #ifdef NETIF_DEBUG
113 printf("netif_close(%x)...", fd);
114 #endif
115 if (fd != 0) {
116 #ifdef NETIF_DEBUG
117 printf("EBADF\n");
118 #endif
119 errno = EBADF;
120 return -1;
123 io = &sockets[fd];
124 ni = io->io_netif;
125 if (ni != NULL) {
126 ni->nif_devdata = NULL;
127 io->io_netif = NULL;
129 #ifdef NETIF_DEBUG
130 printf("OK\n");
131 #endif
132 return 0;
136 * Send a packet. The ether header is already there.
137 * Return the length sent (or -1 on error).
139 ssize_t
140 netif_put(struct iodesc *desc, void *pkt, size_t len)
142 struct of_dev *op;
143 ssize_t rv;
144 size_t sendlen;
146 op = ((struct netif *)desc->io_netif)->nif_devdata;
148 #ifdef NETIF_DEBUG
150 struct ether_header *eh;
152 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
153 desc, pkt, len);
154 eh = pkt;
155 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
156 printf("src: %s ", ether_sprintf(eh->ether_shost));
157 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
159 #endif
161 sendlen = len;
162 if (sendlen < 60) {
163 sendlen = 60;
164 #ifdef NETIF_DEBUG
165 printf("netif_put: length padded to %d\n", sendlen);
166 #endif
169 rv = prom_write(op->handle, pkt, sendlen);
171 #ifdef NETIF_DEBUG
172 printf("netif_put: xmit returned %d\n", rv);
173 #endif
175 if (rv > len)
176 rv = len;
178 return rv;
182 * Receive a packet, including the ether header.
183 * Return the total length received (or -1 on error).
185 ssize_t
186 netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
188 struct of_dev *op;
189 int tick0, tmo_ms;
190 int len;
192 op = ((struct netif *)desc->io_netif)->nif_devdata;
194 #ifdef NETIF_DEBUG
195 printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
196 pkt, maxlen, timo);
197 #endif
199 tmo_ms = timo * 1000;
200 tick0 = prom_ticks();
202 do {
203 len = prom_read(op->handle, pkt, maxlen);
204 } while ((len == -2 || len == 0) &&
205 (prom_ticks() - tick0 < tmo_ms));
207 #ifdef NETIF_DEBUG
208 printf("netif_get: received len=%d\n", len);
209 #endif
211 if (len < 12)
212 return -1;
214 #ifdef NETIF_DEBUG
216 struct ether_header *eh = pkt;
218 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
219 printf("src: %s ", ether_sprintf(eh->ether_shost));
220 printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
222 #endif
224 return len;
228 * Shouldn't really be here, but is used solely for networking, so...
230 satime_t
231 getsecs(void)
234 return prom_ticks() / 1000;