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 | |
---|
61 | #include <rtems/rtems_mii_ioctl.h> |
---|
62 | |
---|
63 | #include <stdlib.h> |
---|
64 | #include <errno.h> |
---|
65 | #include <stdio.h> |
---|
66 | #include <string.h> |
---|
67 | #include <ctype.h> |
---|
68 | |
---|
69 | static struct ifmedia_description shared_media_strings[] = |
---|
70 | IFM_SUBTYPE_SHARED_DESCRIPTIONS; |
---|
71 | static struct ifmedia_description ethern_media_strings[] = |
---|
72 | IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; |
---|
73 | static struct ifmedia_description eth_al_media_strings[] = |
---|
74 | IFM_SUBTYPE_ETHERNET_ALIASES; |
---|
75 | |
---|
76 | static const char * |
---|
77 | find_desc (int tag, struct ifmedia_description *list) |
---|
78 | { |
---|
79 | while (list->ifmt_string && tag != list->ifmt_word) |
---|
80 | list++; |
---|
81 | return list->ifmt_string; |
---|
82 | } |
---|
83 | |
---|
84 | #define WHATPRINT(buf,sz,fmt...) \ |
---|
85 | ( (sz) > 0 ? snprintf((buf),(sz),fmt) : fprintf((buf) ? (FILE*)(buf) : stdout, fmt) ) |
---|
86 | |
---|
87 | int |
---|
88 | rtems_ifmedia2str (int media, char *buf, int bufsz) |
---|
89 | { |
---|
90 | const char *mdesc; |
---|
91 | const char *dupdesc = 0; |
---|
92 | |
---|
93 | /* only ethernet supported, so far */ |
---|
94 | if (IFM_ETHER != IFM_TYPE (media)) |
---|
95 | return -1; |
---|
96 | |
---|
97 | if (!(mdesc = find_desc (IFM_SUBTYPE (media), shared_media_strings))) |
---|
98 | mdesc = find_desc (IFM_SUBTYPE (media), ethern_media_strings); |
---|
99 | |
---|
100 | if (!mdesc) |
---|
101 | return -1; |
---|
102 | |
---|
103 | if (IFM_NONE != IFM_SUBTYPE (media)) |
---|
104 | dupdesc = IFM_FDX & media ? " full-duplex" : " half-duplex"; |
---|
105 | |
---|
106 | return WHATPRINT (buf, bufsz, |
---|
107 | "Ethernet [phy instance: %" PRId32 "]: (link %s, autoneg %s) -- media: %s%s", |
---|
108 | (int32_t) IFM_INST (media), |
---|
109 | IFM_LINK_OK & media ? "ok" : "down", |
---|
110 | IFM_ANEG_DIS & media ? "off" : "on", |
---|
111 | mdesc, dupdesc ? dupdesc : ""); |
---|
112 | } |
---|
113 | |
---|
114 | static int |
---|
115 | find_tag (const char *desc, struct ifmedia_description *list) |
---|
116 | { |
---|
117 | while (list->ifmt_string) { |
---|
118 | if (strstr (desc, list->ifmt_string)) |
---|
119 | return list->ifmt_word; |
---|
120 | list++; |
---|
121 | } |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /* convert a string to a media word |
---|
127 | * RETURNS: 0 on failure; valid results have always at least IFM_ETHER set |
---|
128 | */ |
---|
129 | int |
---|
130 | rtems_str2ifmedia (const char *str, int phy) |
---|
131 | { |
---|
132 | int sub, opt = 0; |
---|
133 | char *chpt; |
---|
134 | |
---|
135 | if (!strncmp (str, "auto", 4)) { |
---|
136 | sub = IFM_AUTO; |
---|
137 | } else if ((sub = find_tag (str, ethern_media_strings)) < 0) { |
---|
138 | if ((sub = find_tag (str, eth_al_media_strings)) < 0) { |
---|
139 | /* allow more */ |
---|
140 | |
---|
141 | /* if no number, 0 is returned which will not pass the test */ |
---|
142 | switch (strtol (str, &chpt, 10)) { |
---|
143 | case 10: |
---|
144 | sub = IFM_10_T; |
---|
145 | break; |
---|
146 | case 100: |
---|
147 | sub = IFM_100_TX; |
---|
148 | break; |
---|
149 | case 1000: |
---|
150 | sub = IFM_1000_T; |
---|
151 | break; |
---|
152 | default: |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | /* need 'b' or 'base' */ |
---|
157 | if ('b' != *chpt++) |
---|
158 | return 0; |
---|
159 | if (!strncmp (chpt, "ase", 3)) |
---|
160 | chpt += 3; |
---|
161 | if (toupper ((int)*chpt++) != 'T') |
---|
162 | return 0; |
---|
163 | if (IFM_100_TX == sub && toupper ((int)*chpt++) != 'X') |
---|
164 | return 0; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | if (strstr (str, "full") || strstr (str, "FDX") || strstr (str, "fdx")) |
---|
169 | opt |= IFM_FDX; |
---|
170 | |
---|
171 | return IFM_MAKEWORD (IFM_ETHER, sub, opt, phy); |
---|
172 | } |
---|