if_media.c revision 1.14 1 1.14 drochner /* $NetBSD: if_media.c,v 1.14 2001/05/18 08:56:27 drochner Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.2 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2 thorpej * NASA Ames Research Center.
10 1.2 thorpej *
11 1.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2 thorpej * modification, are permitted provided that the following conditions
13 1.2 thorpej * are met:
14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2 thorpej * must display the following acknowledgement:
21 1.2 thorpej * This product includes software developed by the NetBSD
22 1.2 thorpej * Foundation, Inc. and its contributors.
23 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2 thorpej * contributors may be used to endorse or promote products derived
25 1.2 thorpej * from this software without specific prior written permission.
26 1.2 thorpej *
27 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.2 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Copyright (c) 1997
42 1.1 thorpej * Jonathan Stone and Jason R. Thorpe. All rights reserved.
43 1.1 thorpej *
44 1.1 thorpej * This software is derived from information provided by Matt Thomas.
45 1.1 thorpej *
46 1.1 thorpej * Redistribution and use in source and binary forms, with or without
47 1.1 thorpej * modification, are permitted provided that the following conditions
48 1.1 thorpej * are met:
49 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
50 1.1 thorpej * notice, this list of conditions and the following disclaimer.
51 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
53 1.1 thorpej * documentation and/or other materials provided with the distribution.
54 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
55 1.1 thorpej * must display the following acknowledgement:
56 1.1 thorpej * This product includes software developed by Jonathan Stone
57 1.1 thorpej * and Jason R. Thorpe for the NetBSD Project.
58 1.1 thorpej * 4. The names of the authors may not be used to endorse or promote products
59 1.1 thorpej * derived from this software without specific prior written permission.
60 1.1 thorpej *
61 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
62 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
64 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
65 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
66 1.1 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 1.1 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
68 1.1 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69 1.1 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.1 thorpej * SUCH DAMAGE.
72 1.1 thorpej */
73 1.1 thorpej
74 1.1 thorpej /*
75 1.1 thorpej * BSD/OS-compatible network interface media selection.
76 1.1 thorpej *
77 1.1 thorpej * Where it is safe to do so, this code strays slightly from the BSD/OS
78 1.1 thorpej * design. Software which uses the API (device drivers, basically)
79 1.1 thorpej * shouldn't notice any difference.
80 1.1 thorpej *
81 1.1 thorpej * Many thanks to Matt Thomas for providing the information necessary
82 1.1 thorpej * to implement this interface.
83 1.1 thorpej */
84 1.1 thorpej
85 1.1 thorpej #include <sys/param.h>
86 1.1 thorpej #include <sys/systm.h>
87 1.1 thorpej #include <sys/errno.h>
88 1.1 thorpej #include <sys/ioctl.h>
89 1.1 thorpej #include <sys/socket.h>
90 1.1 thorpej #include <sys/malloc.h>
91 1.1 thorpej
92 1.1 thorpej #include <net/if.h>
93 1.1 thorpej #include <net/if_media.h>
94 1.1 thorpej #include <net/netisr.h>
95 1.1 thorpej
96 1.1 thorpej /*
97 1.1 thorpej * Compile-time options:
98 1.1 thorpej * IFMEDIA_DEBUG:
99 1.1 thorpej * turn on implementation-level debug printfs.
100 1.1 thorpej * Useful for debugging newly-ported drivers.
101 1.1 thorpej */
102 1.1 thorpej
103 1.1 thorpej #ifdef IFMEDIA_DEBUG
104 1.1 thorpej int ifmedia_debug = 0;
105 1.1 thorpej static void ifmedia_printword __P((int));
106 1.1 thorpej #endif
107 1.1 thorpej
108 1.1 thorpej /*
109 1.1 thorpej * Initialize if_media struct for a specific interface instance.
110 1.1 thorpej */
111 1.1 thorpej void
112 1.1 thorpej ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
113 1.1 thorpej struct ifmedia *ifm;
114 1.1 thorpej int dontcare_mask;
115 1.1 thorpej ifm_change_cb_t change_callback;
116 1.1 thorpej ifm_stat_cb_t status_callback;
117 1.1 thorpej {
118 1.1 thorpej
119 1.7 thorpej TAILQ_INIT(&ifm->ifm_list);
120 1.1 thorpej ifm->ifm_cur = NULL;
121 1.1 thorpej ifm->ifm_media = 0;
122 1.1 thorpej ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
123 1.1 thorpej ifm->ifm_change = change_callback;
124 1.1 thorpej ifm->ifm_status = status_callback;
125 1.1 thorpej }
126 1.1 thorpej
127 1.1 thorpej /*
128 1.1 thorpej * Add a media configuration to the list of supported media
129 1.1 thorpej * for a specific interface instance.
130 1.1 thorpej */
131 1.1 thorpej void
132 1.1 thorpej ifmedia_add(ifm, mword, data, aux)
133 1.1 thorpej struct ifmedia *ifm;
134 1.1 thorpej int mword;
135 1.1 thorpej int data;
136 1.1 thorpej void *aux;
137 1.1 thorpej {
138 1.11 augustss struct ifmedia_entry *entry;
139 1.1 thorpej
140 1.1 thorpej #ifdef IFMEDIA_DEBUG
141 1.1 thorpej if (ifmedia_debug) {
142 1.1 thorpej if (ifm == NULL) {
143 1.1 thorpej printf("ifmedia_add: null ifm\n");
144 1.1 thorpej return;
145 1.1 thorpej }
146 1.1 thorpej printf("Adding entry for ");
147 1.1 thorpej ifmedia_printword(mword);
148 1.1 thorpej }
149 1.1 thorpej #endif
150 1.1 thorpej
151 1.1 thorpej entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
152 1.1 thorpej if (entry == NULL)
153 1.1 thorpej panic("ifmedia_add: can't malloc entry");
154 1.1 thorpej
155 1.1 thorpej entry->ifm_media = mword;
156 1.1 thorpej entry->ifm_data = data;
157 1.1 thorpej entry->ifm_aux = aux;
158 1.1 thorpej
159 1.7 thorpej TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
160 1.1 thorpej }
161 1.1 thorpej
162 1.1 thorpej /*
163 1.1 thorpej * Add an array of media configurations to the list of
164 1.1 thorpej * supported media for a specific interface instance.
165 1.1 thorpej */
166 1.1 thorpej void
167 1.1 thorpej ifmedia_list_add(ifm, lp, count)
168 1.1 thorpej struct ifmedia *ifm;
169 1.1 thorpej struct ifmedia_entry *lp;
170 1.1 thorpej int count;
171 1.1 thorpej {
172 1.1 thorpej int i;
173 1.1 thorpej
174 1.1 thorpej for (i = 0; i < count; i++)
175 1.1 thorpej ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
176 1.1 thorpej lp[i].ifm_aux);
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej /*
180 1.1 thorpej * Set the default active media.
181 1.1 thorpej *
182 1.1 thorpej * Called by device-specific code which is assumed to have already
183 1.1 thorpej * selected the default media in hardware. We do _not_ call the
184 1.1 thorpej * media-change callback.
185 1.1 thorpej */
186 1.1 thorpej void
187 1.1 thorpej ifmedia_set(ifm, target)
188 1.1 thorpej struct ifmedia *ifm;
189 1.1 thorpej int target;
190 1.1 thorpej
191 1.1 thorpej {
192 1.1 thorpej struct ifmedia_entry *match;
193 1.1 thorpej
194 1.1 thorpej match = ifmedia_match(ifm, target, ifm->ifm_mask);
195 1.1 thorpej
196 1.1 thorpej if (match == NULL) {
197 1.1 thorpej printf("ifmedia_set: no match for 0x%x/0x%x\n",
198 1.1 thorpej target, ~ifm->ifm_mask);
199 1.1 thorpej panic("ifmedia_set");
200 1.1 thorpej }
201 1.1 thorpej ifm->ifm_cur = match;
202 1.1 thorpej
203 1.1 thorpej #ifdef IFMEDIA_DEBUG
204 1.1 thorpej if (ifmedia_debug) {
205 1.1 thorpej printf("ifmedia_set: target ");
206 1.1 thorpej ifmedia_printword(target);
207 1.1 thorpej printf("ifmedia_set: setting to ");
208 1.1 thorpej ifmedia_printword(ifm->ifm_cur->ifm_media);
209 1.1 thorpej }
210 1.1 thorpej #endif
211 1.1 thorpej }
212 1.1 thorpej
213 1.1 thorpej /*
214 1.1 thorpej * Device-independent media ioctl support function.
215 1.1 thorpej */
216 1.1 thorpej int
217 1.1 thorpej ifmedia_ioctl(ifp, ifr, ifm, cmd)
218 1.1 thorpej struct ifnet *ifp;
219 1.1 thorpej struct ifreq *ifr;
220 1.1 thorpej struct ifmedia *ifm;
221 1.1 thorpej u_long cmd;
222 1.1 thorpej {
223 1.1 thorpej struct ifmedia_entry *match;
224 1.1 thorpej struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
225 1.1 thorpej int error = 0, sticky;
226 1.1 thorpej
227 1.1 thorpej if (ifp == NULL || ifr == NULL || ifm == NULL)
228 1.1 thorpej return(EINVAL);
229 1.1 thorpej
230 1.1 thorpej switch (cmd) {
231 1.1 thorpej
232 1.1 thorpej /*
233 1.1 thorpej * Set the current media.
234 1.1 thorpej */
235 1.1 thorpej case SIOCSIFMEDIA:
236 1.1 thorpej {
237 1.1 thorpej struct ifmedia_entry *oldentry;
238 1.1 thorpej int oldmedia;
239 1.1 thorpej int newmedia = ifr->ifr_media;
240 1.1 thorpej
241 1.1 thorpej match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
242 1.1 thorpej if (match == NULL) {
243 1.1 thorpej #ifdef IFMEDIA_DEBUG
244 1.1 thorpej if (ifmedia_debug) {
245 1.1 thorpej printf(
246 1.1 thorpej "ifmedia_ioctl: no media found for 0x%x\n",
247 1.1 thorpej newmedia);
248 1.1 thorpej }
249 1.1 thorpej #endif
250 1.13 joda return (EINVAL);
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej /*
254 1.1 thorpej * If no change, we're done.
255 1.14 drochner * XXX Automedia may involve software intervention.
256 1.10 soren * Keep going in case the connected media changed.
257 1.1 thorpej * Similarly, if best match changed (kernel debugger?).
258 1.1 thorpej */
259 1.1 thorpej if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
260 1.5 thorpej (newmedia == ifm->ifm_media) &&
261 1.1 thorpej (match == ifm->ifm_cur))
262 1.1 thorpej return 0;
263 1.1 thorpej
264 1.1 thorpej /*
265 1.1 thorpej * We found a match, now make the driver switch to it.
266 1.1 thorpej * Make sure to preserve our old media type in case the
267 1.1 thorpej * driver can't switch.
268 1.1 thorpej */
269 1.1 thorpej #ifdef IFMEDIA_DEBUG
270 1.1 thorpej if (ifmedia_debug) {
271 1.1 thorpej printf("ifmedia_ioctl: switching %s to ",
272 1.1 thorpej ifp->if_xname);
273 1.1 thorpej ifmedia_printword(match->ifm_media);
274 1.1 thorpej }
275 1.1 thorpej #endif
276 1.1 thorpej oldentry = ifm->ifm_cur;
277 1.1 thorpej oldmedia = ifm->ifm_media;
278 1.1 thorpej ifm->ifm_cur = match;
279 1.1 thorpej ifm->ifm_media = newmedia;
280 1.1 thorpej error = (*ifm->ifm_change)(ifp);
281 1.1 thorpej if (error) {
282 1.1 thorpej ifm->ifm_cur = oldentry;
283 1.1 thorpej ifm->ifm_media = oldmedia;
284 1.1 thorpej }
285 1.1 thorpej break;
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej /*
289 1.1 thorpej * Get list of available media and current media on interface.
290 1.1 thorpej */
291 1.1 thorpej case SIOCGIFMEDIA:
292 1.1 thorpej {
293 1.1 thorpej struct ifmedia_entry *ep;
294 1.1 thorpej int *kptr, count;
295 1.1 thorpej
296 1.1 thorpej kptr = NULL; /* XXX gcc */
297 1.1 thorpej
298 1.1 thorpej ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
299 1.1 thorpej ifm->ifm_cur->ifm_media : IFM_NONE;
300 1.1 thorpej ifmr->ifm_mask = ifm->ifm_mask;
301 1.1 thorpej ifmr->ifm_status = 0;
302 1.1 thorpej (*ifm->ifm_status)(ifp, ifmr);
303 1.1 thorpej
304 1.1 thorpej count = 0;
305 1.7 thorpej ep = TAILQ_FIRST(&ifm->ifm_list);
306 1.1 thorpej
307 1.1 thorpej if (ifmr->ifm_count != 0) {
308 1.1 thorpej kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
309 1.1 thorpej M_TEMP, M_WAITOK);
310 1.1 thorpej
311 1.1 thorpej /*
312 1.1 thorpej * Get the media words from the interface's list.
313 1.1 thorpej */
314 1.1 thorpej for (; ep != NULL && count < ifmr->ifm_count;
315 1.7 thorpej ep = TAILQ_NEXT(ep, ifm_list), count++)
316 1.1 thorpej kptr[count] = ep->ifm_media;
317 1.1 thorpej
318 1.1 thorpej if (ep != NULL)
319 1.1 thorpej error = E2BIG; /* oops! */
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej /*
323 1.1 thorpej * If there are more interfaces on the list, count
324 1.1 thorpej * them. This allows the caller to set ifmr->ifm_count
325 1.1 thorpej * to 0 on the first call to know how much space to
326 1.1 thorpej * callocate.
327 1.1 thorpej */
328 1.7 thorpej for (; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
329 1.1 thorpej count++;
330 1.1 thorpej
331 1.1 thorpej /*
332 1.1 thorpej * We do the copyout on E2BIG, because that's
333 1.1 thorpej * just our way of telling userland that there
334 1.1 thorpej * are more. This is the behavior I've observed
335 1.1 thorpej * under BSD/OS 3.0
336 1.1 thorpej */
337 1.1 thorpej sticky = error;
338 1.1 thorpej if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
339 1.1 thorpej error = copyout((caddr_t)kptr,
340 1.1 thorpej (caddr_t)ifmr->ifm_ulist,
341 1.1 thorpej ifmr->ifm_count * sizeof(int));
342 1.1 thorpej }
343 1.1 thorpej
344 1.1 thorpej if (error == 0)
345 1.1 thorpej error = sticky;
346 1.1 thorpej
347 1.1 thorpej if (ifmr->ifm_count != 0)
348 1.1 thorpej free(kptr, M_TEMP);
349 1.1 thorpej
350 1.1 thorpej ifmr->ifm_count = count;
351 1.1 thorpej break;
352 1.1 thorpej }
353 1.1 thorpej
354 1.1 thorpej default:
355 1.1 thorpej return (EINVAL);
356 1.1 thorpej }
357 1.1 thorpej
358 1.1 thorpej return (error);
359 1.1 thorpej }
360 1.1 thorpej
361 1.1 thorpej /*
362 1.1 thorpej * Find media entry matching a given ifm word.
363 1.1 thorpej */
364 1.1 thorpej struct ifmedia_entry *
365 1.1 thorpej ifmedia_match(ifm, target, mask)
366 1.1 thorpej struct ifmedia *ifm;
367 1.1 thorpej int target;
368 1.1 thorpej int mask;
369 1.1 thorpej {
370 1.1 thorpej struct ifmedia_entry *match, *next;
371 1.1 thorpej
372 1.1 thorpej match = NULL;
373 1.1 thorpej mask = ~mask;
374 1.1 thorpej
375 1.7 thorpej for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
376 1.7 thorpej next = TAILQ_NEXT(next, ifm_list)) {
377 1.1 thorpej if ((next->ifm_media & mask) == (target & mask)) {
378 1.1 thorpej #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
379 1.1 thorpej if (match) {
380 1.1 thorpej printf("ifmedia_match: multiple match for "
381 1.1 thorpej "0x%x/0x%x\n", target, mask);
382 1.1 thorpej }
383 1.1 thorpej #endif
384 1.1 thorpej match = next;
385 1.1 thorpej }
386 1.1 thorpej }
387 1.1 thorpej
388 1.1 thorpej return match;
389 1.8 thorpej }
390 1.8 thorpej
391 1.8 thorpej /*
392 1.8 thorpej * Delete all media for a given instance.
393 1.8 thorpej */
394 1.8 thorpej void
395 1.8 thorpej ifmedia_delete_instance(ifm, inst)
396 1.8 thorpej struct ifmedia *ifm;
397 1.8 thorpej int inst;
398 1.8 thorpej {
399 1.8 thorpej struct ifmedia_entry *ife, *nife;
400 1.8 thorpej
401 1.8 thorpej for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
402 1.8 thorpej ife = nife) {
403 1.8 thorpej nife = TAILQ_NEXT(ife, ifm_list);
404 1.8 thorpej if (inst == IFM_INST_ANY ||
405 1.8 thorpej inst == IFM_INST(ife->ifm_media)) {
406 1.8 thorpej TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
407 1.8 thorpej free(ife, M_DEVBUF);
408 1.8 thorpej }
409 1.8 thorpej }
410 1.9 thorpej }
411 1.9 thorpej
412 1.9 thorpej /*
413 1.9 thorpej * Compute the interface `baudrate' from the media, for the interface
414 1.9 thorpej * metrics (used by routing daemons).
415 1.9 thorpej */
416 1.12 jdolecek static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
417 1.9 thorpej IFM_BAUDRATE_DESCRIPTIONS;
418 1.9 thorpej
419 1.9 thorpej int
420 1.9 thorpej ifmedia_baudrate(mword)
421 1.9 thorpej int mword;
422 1.9 thorpej {
423 1.9 thorpej int i;
424 1.9 thorpej
425 1.9 thorpej for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
426 1.9 thorpej if ((mword & (IFM_NMASK|IFM_TMASK)) ==
427 1.9 thorpej ifmedia_baudrate_descriptions[i].ifmb_word)
428 1.9 thorpej return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
429 1.9 thorpej }
430 1.9 thorpej
431 1.9 thorpej /* Not known. */
432 1.9 thorpej return (0);
433 1.1 thorpej }
434 1.1 thorpej
435 1.1 thorpej #ifdef IFMEDIA_DEBUG
436 1.2 thorpej
437 1.12 jdolecek static const struct ifmedia_description ifm_type_descriptions[] =
438 1.1 thorpej IFM_TYPE_DESCRIPTIONS;
439 1.1 thorpej
440 1.12 jdolecek static const struct ifmedia_description ifm_subtype_descriptions[] =
441 1.2 thorpej IFM_SUBTYPE_DESCRIPTIONS;
442 1.1 thorpej
443 1.12 jdolecek static const struct ifmedia_description ifm_option_descriptions[] =
444 1.2 thorpej IFM_OPTION_DESCRIPTIONS;
445 1.1 thorpej
446 1.1 thorpej /*
447 1.1 thorpej * print a media word.
448 1.1 thorpej */
449 1.1 thorpej static void
450 1.1 thorpej ifmedia_printword(ifmw)
451 1.1 thorpej int ifmw;
452 1.1 thorpej {
453 1.12 jdolecek const struct ifmedia_description *desc;
454 1.1 thorpej int seen_option = 0;
455 1.1 thorpej
456 1.2 thorpej /* Print the top-level interface type. */
457 1.2 thorpej for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
458 1.2 thorpej desc++) {
459 1.1 thorpej if (IFM_TYPE(ifmw) == desc->ifmt_word)
460 1.1 thorpej break;
461 1.1 thorpej }
462 1.2 thorpej if (desc->ifmt_string == NULL)
463 1.2 thorpej printf("<unknown type> ");
464 1.2 thorpej else
465 1.2 thorpej printf("%s ", desc->ifmt_string);
466 1.2 thorpej
467 1.2 thorpej /* Print the subtype. */
468 1.2 thorpej for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
469 1.2 thorpej desc++) {
470 1.2 thorpej if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
471 1.2 thorpej IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
472 1.1 thorpej break;
473 1.1 thorpej }
474 1.2 thorpej if (desc->ifmt_string == NULL)
475 1.2 thorpej printf("<unknown subtype>");
476 1.2 thorpej else
477 1.2 thorpej printf("%s", desc->ifmt_string);
478 1.2 thorpej
479 1.2 thorpej /* Print any options. */
480 1.2 thorpej for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
481 1.2 thorpej desc++) {
482 1.2 thorpej if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
483 1.2 thorpej (ifmw & desc->ifmt_word) != 0 &&
484 1.2 thorpej (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
485 1.1 thorpej if (seen_option == 0)
486 1.1 thorpej printf(" <");
487 1.3 enami printf("%s%s", seen_option ? "," : "",
488 1.1 thorpej desc->ifmt_string);
489 1.2 thorpej seen_option |= IFM_OPTIONS(desc->ifmt_word);
490 1.1 thorpej }
491 1.1 thorpej }
492 1.1 thorpej printf("%s\n", seen_option ? ">" : "");
493 1.1 thorpej }
494 1.2 thorpej
495 1.1 thorpej #endif /* IFMEDIA_DEBUG */
496