source: rtems/cpukit/libnetworking/rtems/rtems_mii_ioctl.c @ e131ac0

4.104.114.84.95
Last change on this file since e131ac0 was e131ac0, checked in by Till Straumann <strauman@…>, on 11/03/05 at 03:09:45

2005-11-02 straumanatslacdotstanford.edu

  • libnetworking/Makefile.am, libnetworking/preinstall.am: Added simple implementation of ethernet media ioctl SIOCSIFMEDIA/SIOCGIFMEDIA for mii compliant phys.
  • libnetworking/rtems/rtems_mii_ioctl.c, libnetworking/rtems/rtems_mii_ioctl.h, libnetworking/rtems/rtems_mii_ioctl_kern.c: New files.
  • Property mode set to 100644
File size: 3.1 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/* Author: Till Straumann, <straumanatslacdotstandorddotedu>, 2005 */
12
13#include <rtems.h>
14
15#undef _KERNEL
16#undef KERNEL
17
18#include <rtems/rtems_mii_ioctl.h>
19
20#include <stdlib.h>
21#include <errno.h>
22#include <stdio.h>
23#include <string.h>
24#include <ctype.h>
25
26static struct ifmedia_description shared_media_strings[] =
27  IFM_SUBTYPE_SHARED_DESCRIPTIONS;
28static struct ifmedia_description ethern_media_strings[] =
29  IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
30static struct ifmedia_description eth_al_media_strings[] =
31  IFM_SUBTYPE_ETHERNET_ALIASES;
32
33static const char *
34find_desc (int tag, struct ifmedia_description *list)
35{
36  while (list->ifmt_string && tag != list->ifmt_word)
37    list++;
38  return list->ifmt_string;
39}
40
41#define WHATPRINT(buf,sz,fmt...)        \
42        ( (sz) > 0 ? snprintf((buf),(sz),fmt) : fprintf((buf) ? (FILE*)(buf) : stdout, fmt) )
43
44int
45rtems_ifmedia2str (int media, char *buf, int bufsz)
46{
47  const char *mdesc;
48  const char *dupdesc = 0;
49
50  /* only ethernet supported, so far */
51  if (IFM_ETHER != IFM_TYPE (media))
52    return -1;
53
54  if (!(mdesc = find_desc (IFM_SUBTYPE (media), shared_media_strings)))
55    mdesc = find_desc (IFM_SUBTYPE (media), ethern_media_strings);
56
57  if (!mdesc)
58    return -1;
59
60  if (IFM_NONE != IFM_SUBTYPE (media))
61    dupdesc = IFM_FDX & media ? " full-duplex" : " half-duplex";
62
63  return WHATPRINT (buf, bufsz,
64                    "Ethernet [phy instance: %i]: (link %s, autoneg %s) -- media: %s%s",
65                    IFM_INST (media),
66                    IFM_LINK_OK & media ? "ok" : "down",
67                    IFM_ANEG_DIS & media ? "off" : "on",
68                    mdesc, dupdesc ? dupdesc : "");
69}
70
71static int
72find_tag (const char *desc, struct ifmedia_description *list)
73{
74  while (list->ifmt_string) {
75    if (strstr (desc, list->ifmt_string))
76      return list->ifmt_word;
77    list++;
78  }
79  return -1;
80}
81
82
83/* convert a string to a media word
84 * RETURNS: 0 on failure; valid results have always at least IFM_ETHER set
85 */
86int
87rtems_str2ifmedia (const char *str, int phy)
88{
89  int sub, opt = 0;
90  char *chpt;
91
92  if (!strncmp (str, "auto", 4)) {
93    sub = IFM_AUTO;
94  } else if ((sub = find_tag (str, ethern_media_strings)) < 0) {
95    if ((sub = find_tag (str, eth_al_media_strings)) < 0) {
96      /* allow more */
97
98      /* if no number, 0 is returned which will not pass the test */
99      switch (strtol (str, &chpt, 10)) {
100      case 10:
101        sub = IFM_10_T;
102        break;
103      case 100:
104        sub = IFM_100_TX;
105        break;
106      case 1000:
107        sub = IFM_1000_T;
108        break;
109      default:
110        return 0;
111      }
112
113      /* need 'b' or 'base' */
114      if ('b' != *chpt++)
115        return 0;
116      if (!strncmp (chpt, "ase", 3))
117        chpt += 3;
118      if (toupper (*chpt++) != 'T')
119        return 0;
120      if (IFM_100_TX == sub && toupper (*chpt++) != 'X')
121        return 0;
122    }
123  }
124
125  if (strstr (str, "full") || strstr (str, "FDX") || strstr (str, "fdx"))
126    opt |= IFM_FDX;
127
128  return IFM_MAKEWORD (IFM_ETHER, sub, opt, phy);
129}
Note: See TracBrowser for help on using the repository browser.