sys/arch/i386/stand/lib/netif/netif_small.c

http://www.minix3.org/ · C · 165 lines · 100 code · 26 blank · 39 comment · 12 complexity · 912ab3bb823b24bf5b661dee42e6a022 MD5 · raw file

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