Annotation of sys/arch/macppc/stand/netif_of.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: netif_of.c,v 1.4 2003/10/16 04:30:09 drahn Exp $ */
! 2: /* $NetBSD: netif_of.c,v 1.1 1997/04/16 20:29:19 thorpej Exp $ */
! 3:
! 4: /*
! 5: * Copyright (C) 1995 Wolfgang Solfrank.
! 6: * Copyright (C) 1995 TooLs GmbH.
! 7: * All rights reserved.
! 8: *
! 9: * Redistribution and use in source and binary forms, with or without
! 10: * modification, are permitted provided that the following conditions
! 11: * are met:
! 12: * 1. Redistributions of source code must retain the above copyright
! 13: * notice, this list of conditions and the following disclaimer.
! 14: * 2. Redistributions in binary form must reproduce the above copyright
! 15: * notice, this list of conditions and the following disclaimer in the
! 16: * documentation and/or other materials provided with the distribution.
! 17: * 3. All advertising materials mentioning features or use of this software
! 18: * must display the following acknowledgement:
! 19: * This product includes software developed by TooLs GmbH.
! 20: * 4. The name of TooLs GmbH may not be used to endorse or promote products
! 21: * derived from this software without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
! 24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! 25: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! 26: * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! 27: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! 28: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! 29: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! 30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! 31: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! 32: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 33: */
! 34:
! 35: /*
! 36: * Open Firmware does most of the job for interfacing to the hardware,
! 37: * so it is easiest to just replace the netif module with
! 38: * this adaptation to the PROM network interface.
! 39: *
! 40: * Note: this is based in part on sys/arch/sparc/stand/netif_sun.c
! 41: */
! 42:
! 43: #include <sys/param.h>
! 44: #include <sys/socket.h>
! 45:
! 46: #if 0 /* XXX thorpej */
! 47: #include <string.h>
! 48: #include <time.h>
! 49: #endif
! 50:
! 51: #include <net/if.h>
! 52:
! 53: #include <netinet/in.h>
! 54: #include <netinet/if_ether.h>
! 55: #include <netinet/in_systm.h>
! 56:
! 57: #include <lib/libsa/stand.h>
! 58: #include <lib/libsa/net.h>
! 59: #include <lib/libsa/netif.h>
! 60:
! 61: #include <macppc/stand/ofdev.h>
! 62: #include <macppc/stand/openfirm.h>
! 63:
! 64: static struct netif netif_of;
! 65:
! 66: struct iodesc sockets[SOPEN_MAX];
! 67:
! 68: struct iodesc *
! 69: socktodesc(int sock)
! 70: {
! 71: if (sock != 0)
! 72: return NULL;
! 73: return sockets;
! 74: }
! 75:
! 76: int
! 77: netif_open(void *machdep_hint)
! 78: {
! 79: struct of_dev *op = machdep_hint;
! 80: struct iodesc *io;
! 81: int fd, error;
! 82: char addr[32];
! 83:
! 84: #ifdef NETIF_DEBUG
! 85: printf("netif_open...");
! 86: #endif
! 87: /* find a free socket */
! 88: io = sockets;
! 89: if (io->io_netif) {
! 90: #ifdef NETIF_DEBUG
! 91: printf("device busy\n");
! 92: #endif
! 93: errno = ENFILE;
! 94: return -1;
! 95: }
! 96: bzero(io, sizeof *io);
! 97:
! 98: netif_of.nif_devdata = op;
! 99: io->io_netif = &netif_of;
! 100:
! 101: /* Put our ethernet address in io->myea */
! 102: OF_getprop(OF_instance_to_package(op->handle),
! 103: "local-mac-address", io->myea, sizeof io->myea) == -1 &&
! 104: OF_getprop(OF_instance_to_package(op->handle),
! 105: "mac-address", io->myea, sizeof io->myea);
! 106:
! 107: #ifdef NETIF_DEBUG
! 108: printf("OK\n");
! 109: #endif
! 110: return 0;
! 111: }
! 112:
! 113: int
! 114: netif_close(int fd)
! 115: {
! 116: struct iodesc *io;
! 117: struct netif *ni;
! 118:
! 119: #ifdef NETIF_DEBUG
! 120: printf("netif_close(%x)...", fd);
! 121: #endif
! 122: if (fd != 0) {
! 123: #ifdef NETIF_DEBUG
! 124: printf("EBADF\n");
! 125: #endif
! 126: errno = EBADF;
! 127: return -1;
! 128: }
! 129:
! 130: io = &sockets[fd];
! 131: ni = io->io_netif;
! 132: if (ni != NULL) {
! 133: ni->nif_devdata = NULL;
! 134: io->io_netif = NULL;
! 135: }
! 136: #ifdef NETIF_DEBUG
! 137: printf("OK\n");
! 138: #endif
! 139: return 0;
! 140: }
! 141:
! 142: /*
! 143: * Send a packet. The ether header is already there.
! 144: * Return the length sent (or -1 on error).
! 145: */
! 146: ssize_t
! 147: netif_put(struct iodesc *desc, void *pkt, size_t len)
! 148: {
! 149: struct of_dev *op;
! 150: ssize_t rv;
! 151: size_t sendlen;
! 152:
! 153: op = desc->io_netif->nif_devdata;
! 154:
! 155: #ifdef NETIF_DEBUG
! 156: {
! 157: struct ether_header *eh;
! 158:
! 159: printf("netif_put: desc=0x%x pkt=0x%x len=%d\n",
! 160: desc, pkt, len);
! 161: eh = pkt;
! 162: printf("dst: %s ", ether_sprintf(eh->ether_dhost));
! 163: printf("src: %s ", ether_sprintf(eh->ether_shost));
! 164: printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
! 165: }
! 166: #endif
! 167:
! 168: sendlen = len;
! 169: if (sendlen < 60) {
! 170: sendlen = 60;
! 171: #ifdef NETIF_DEBUG
! 172: printf("netif_put: length padded to %d\n", sendlen);
! 173: #endif
! 174: }
! 175:
! 176: if (op->dmabuf) {
! 177: bcopy(pkt, op->dmabuf, sendlen);
! 178: pkt = op->dmabuf;
! 179: }
! 180: rv = OF_write(op->handle, pkt, sendlen);
! 181:
! 182: #ifdef NETIF_DEBUG
! 183: printf("netif_put: xmit returned %d\n", rv);
! 184: #endif
! 185:
! 186: return rv;
! 187: }
! 188:
! 189: /*
! 190: * Receive a packet, including the ether header.
! 191: * Return the total length received (or -1 on error).
! 192: */
! 193: ssize_t
! 194: netif_get(struct iodesc *desc, void *pkt, size_t maxlen, time_t timo)
! 195: {
! 196: struct of_dev *op;
! 197: int tick0, tmo_ms;
! 198: int len;
! 199:
! 200: op = desc->io_netif->nif_devdata;
! 201:
! 202: #ifdef NETIF_DEBUG
! 203: printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
! 204: pkt, maxlen, timo);
! 205: #endif
! 206:
! 207: tmo_ms = timo * 1000;
! 208: tick0 = OF_milliseconds();
! 209:
! 210: do {
! 211: len = OF_read(op->handle, pkt, maxlen);
! 212: } while ((len == -2 || len == 0) &&
! 213: ((OF_milliseconds() - tick0) < tmo_ms));
! 214:
! 215: #ifdef NETIF_DEBUG
! 216: printf("netif_get: received len=%d\n", len);
! 217: #endif
! 218:
! 219: if (len < 12)
! 220: return -1;
! 221:
! 222: #ifdef NETIF_DEBUG
! 223: {
! 224: struct ether_header *eh = pkt;
! 225:
! 226: printf("dst: %s ", ether_sprintf(eh->ether_dhost));
! 227: printf("src: %s ", ether_sprintf(eh->ether_shost));
! 228: printf("type: 0x%x\n", eh->ether_type & 0xFFFF);
! 229: }
! 230: #endif
! 231:
! 232: return len;
! 233: }
! 234:
! 235: /*
! 236: * Shouldn't really be here, but is used solely for networking, so...
! 237: */
! 238: time_t
! 239: getsecs()
! 240: {
! 241: return OF_milliseconds() / 1000;
! 242: }
CVSweb