source: rtems/cpukit/libnetworking/rtems/rtems_mii_ioctl.c @ 05cdf2a8

4.104.114.84.95
Last change on this file since 05cdf2a8 was 05cdf2a8, checked in by Till Straumann <strauman@…>, on 01/17/07 at 06:15:19

2007-01-16 Till Straumann <strauman@…>

  • libnetworking/rtems/rtems_mii_ioctl.c,
  • libnetworking/rtems/rtems_mii_ioctl.h,
  • libnetworking/rtems/rtems_mii_ioctl_kern.c: Added SLAC/Stanford Authorship Note / Copyright + Liability Disclaimer.
  • Property mode set to 100644
File size: 5.0 KB
Line 
1/* $Id$ */
2
3/* Simple (default) implementation for SIOCGIFMEDIA/SIOCSIFMEDIA
4 * to be used by ethernet drivers [from their ioctl].
5 *
6 * USERSPACE UTILITIES
7 *
8 * NOTE: This much simpler than the BSD ifmedia API
9 */
10
11/*
12 * Authorship
13 * ----------
14 * This software was created by
15 *     Till Straumann <strauman@slac.stanford.edu>, 2005,
16 *         Stanford Linear Accelerator Center, Stanford University.
17 *
18 * Acknowledgement of sponsorship
19 * ------------------------------
20 * This software was produced by
21 *     the Stanford Linear Accelerator Center, Stanford University,
22 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
23 *
24 * Government disclaimer of liability
25 * ----------------------------------
26 * Neither the United States nor the United States Department of Energy,
27 * nor any of their employees, makes any warranty, express or implied, or
28 * assumes any legal liability or responsibility for the accuracy,
29 * completeness, or usefulness of any data, apparatus, product, or process
30 * disclosed, or represents that its use would not infringe privately owned
31 * rights.
32 *
33 * Stanford disclaimer of liability
34 * --------------------------------
35 * Stanford University makes no representations or warranties, express or
36 * implied, nor assumes any liability for the use of this software.
37 *
38 * Stanford disclaimer of copyright
39 * --------------------------------
40 * Stanford University, owner of the copyright, hereby disclaims its
41 * copyright and all other rights in this software.  Hence, anyone may
42 * freely use it for any purpose without restriction. 
43 *
44 * Maintenance of notices
45 * ----------------------
46 * In the interest of clarity regarding the origin and status of this
47 * SLAC software, this and all the preceding Stanford University notices
48 * are to remain affixed to any copy or derivative of this software made
49 * or distributed by the recipient and are to be affixed to any copy of
50 * software made or distributed by the recipient that contains a copy or
51 * derivative of this software.
52 *
53 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
54 */
55
56#include <rtems.h>
57#include <inttypes.h>
58
59#undef _KERNEL
60#undef KERNEL
61
62#include <rtems/rtems_mii_ioctl.h>
63
64#include <stdlib.h>
65#include <errno.h>
66#include <stdio.h>
67#include <string.h>
68#include <ctype.h>
69
70static struct ifmedia_description shared_media_strings[] =
71  IFM_SUBTYPE_SHARED_DESCRIPTIONS;
72static struct ifmedia_description ethern_media_strings[] =
73  IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
74static struct ifmedia_description eth_al_media_strings[] =
75  IFM_SUBTYPE_ETHERNET_ALIASES;
76
77static const char *
78find_desc (int tag, struct ifmedia_description *list)
79{
80  while (list->ifmt_string && tag != list->ifmt_word)
81    list++;
82  return list->ifmt_string;
83}
84
85#define WHATPRINT(buf,sz,fmt...)        \
86        ( (sz) > 0 ? snprintf((buf),(sz),fmt) : fprintf((buf) ? (FILE*)(buf) : stdout, fmt) )
87
88int
89rtems_ifmedia2str (int media, char *buf, int bufsz)
90{
91  const char *mdesc;
92  const char *dupdesc = 0;
93
94  /* only ethernet supported, so far */
95  if (IFM_ETHER != IFM_TYPE (media))
96    return -1;
97
98  if (!(mdesc = find_desc (IFM_SUBTYPE (media), shared_media_strings)))
99    mdesc = find_desc (IFM_SUBTYPE (media), ethern_media_strings);
100
101  if (!mdesc)
102    return -1;
103
104  if (IFM_NONE != IFM_SUBTYPE (media))
105    dupdesc = IFM_FDX & media ? " full-duplex" : " half-duplex";
106
107  return WHATPRINT (buf, bufsz,
108                    "Ethernet [phy instance: %" PRId32 "]: (link %s, autoneg %s) -- media: %s%s",
109                    (int32_t) IFM_INST (media),
110                    IFM_LINK_OK & media ? "ok" : "down",
111                    IFM_ANEG_DIS & media ? "off" : "on",
112                    mdesc, dupdesc ? dupdesc : "");
113}
114
115static int
116find_tag (const char *desc, struct ifmedia_description *list)
117{
118  while (list->ifmt_string) {
119    if (strstr (desc, list->ifmt_string))
120      return list->ifmt_word;
121    list++;
122  }
123  return -1;
124}
125
126
127/* convert a string to a media word
128 * RETURNS: 0 on failure; valid results have always at least IFM_ETHER set
129 */
130int
131rtems_str2ifmedia (const char *str, int phy)
132{
133  int sub, opt = 0;
134  char *chpt;
135
136  if (!strncmp (str, "auto", 4)) {
137    sub = IFM_AUTO;
138  } else if ((sub = find_tag (str, ethern_media_strings)) < 0) {
139    if ((sub = find_tag (str, eth_al_media_strings)) < 0) {
140      /* allow more */
141
142      /* if no number, 0 is returned which will not pass the test */
143      switch (strtol (str, &chpt, 10)) {
144      case 10:
145        sub = IFM_10_T;
146        break;
147      case 100:
148        sub = IFM_100_TX;
149        break;
150      case 1000:
151        sub = IFM_1000_T;
152        break;
153      default:
154        return 0;
155      }
156
157      /* need 'b' or 'base' */
158      if ('b' != *chpt++)
159        return 0;
160      if (!strncmp (chpt, "ase", 3))
161        chpt += 3;
162      if (toupper (*chpt++) != 'T')
163        return 0;
164      if (IFM_100_TX == sub && toupper (*chpt++) != 'X')
165        return 0;
166    }
167  }
168
169  if (strstr (str, "full") || strstr (str, "FDX") || strstr (str, "fdx"))
170    opt |= IFM_FDX;
171
172  return IFM_MAKEWORD (IFM_ETHER, sub, opt, phy);
173}
Note: See TracBrowser for help on using the repository browser.