1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
6 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
8 * This software is derived from information provided by Matt Thomas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Jonathan Stone
21 * and Jason R. Thorpe for the NetBSD Project.
22 * 4. The names of the authors may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * BSD/OS-compatible network interface media selection.
41 * Where it is safe to do so, this code strays slightly from the BSD/OS
42 * design. Software which uses the API (device drivers, basically)
43 * shouldn't notice any difference.
45 * Many thanks to Matt Thomas for providing the information necessary
46 * to implement this interface.
49 #include "opt_ifmedia.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/sysctl.h>
60 #include <net/if_media.h>
63 * Compile-time options:
65 * turn on implementation-level debug printfs.
66 * Useful for debugging newly-ported drivers.
69 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
73 #include <net/if_var.h>
74 int ifmedia_debug = 0;
75 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
76 0, "if_media debugging msgs");
77 static void ifmedia_printword(int);
81 * Initialize if_media struct for a specific interface instance.
84 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
87 ifm_change_cb_t change_callback;
88 ifm_stat_cb_t status_callback;
91 LIST_INIT(&ifm->ifm_list);
94 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
95 ifm->ifm_change = change_callback;
96 ifm->ifm_status = status_callback;
100 ifmedia_removeall(ifm)
103 struct ifmedia_entry *entry;
105 for (entry = LIST_FIRST(&ifm->ifm_list); entry;
106 entry = LIST_FIRST(&ifm->ifm_list)) {
107 LIST_REMOVE(entry, ifm_list);
108 free(entry, M_IFADDR);
113 * Add a media configuration to the list of supported media
114 * for a specific interface instance.
117 ifmedia_add(ifm, mword, data, aux)
123 register struct ifmedia_entry *entry;
128 printf("ifmedia_add: null ifm\n");
131 printf("Adding entry for ");
132 ifmedia_printword(mword);
136 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
138 panic("ifmedia_add: can't malloc entry");
140 entry->ifm_media = mword;
141 entry->ifm_data = data;
142 entry->ifm_aux = aux;
144 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
148 * Add an array of media configurations to the list of
149 * supported media for a specific interface instance.
152 ifmedia_list_add(ifm, lp, count)
154 struct ifmedia_entry *lp;
159 for (i = 0; i < count; i++)
160 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
165 * Set the default active media.
167 * Called by device-specific code which is assumed to have already
168 * selected the default media in hardware. We do _not_ call the
169 * media-change callback.
172 ifmedia_set(ifm, target)
177 struct ifmedia_entry *match;
179 match = ifmedia_match(ifm, target, ifm->ifm_mask);
182 printf("ifmedia_set: no match for 0x%x/0x%x\n",
183 target, ~ifm->ifm_mask);
184 panic("ifmedia_set");
186 ifm->ifm_cur = match;
190 printf("ifmedia_set: target ");
191 ifmedia_printword(target);
192 printf("ifmedia_set: setting to ");
193 ifmedia_printword(ifm->ifm_cur->ifm_media);
199 * Given a media word, return one suitable for an application
200 * using the original encoding.
203 compat_media(int media)
206 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
207 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
214 * Device-independent media ioctl support function.
217 ifmedia_ioctl(ifp, ifr, ifm, cmd)
223 struct ifmedia_entry *match;
224 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
227 if (ifp == NULL || ifr == NULL || ifm == NULL)
233 * Set the current media.
237 struct ifmedia_entry *oldentry;
239 int newmedia = ifr->ifr_media;
241 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
246 "ifmedia_ioctl: no media found for 0x%x\n",
254 * If no change, we're done.
255 * XXX Automedia may invole software intervention.
256 * Keep going in case the connected media changed.
257 * Similarly, if best match changed (kernel debugger?).
259 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
260 (newmedia == ifm->ifm_media) &&
261 (match == ifm->ifm_cur))
265 * We found a match, now make the driver switch to it.
266 * Make sure to preserve our old media type in case the
267 * driver can't switch.
271 printf("ifmedia_ioctl: switching %s to ",
273 ifmedia_printword(match->ifm_media);
276 oldentry = ifm->ifm_cur;
277 oldmedia = ifm->ifm_media;
278 ifm->ifm_cur = match;
279 ifm->ifm_media = newmedia;
280 error = (*ifm->ifm_change)(ifp);
282 ifm->ifm_cur = oldentry;
283 ifm->ifm_media = oldmedia;
289 * Get list of available media and current media on interface.
294 struct ifmedia_entry *ep;
297 if (ifmr->ifm_count < 0)
300 if (cmd == SIOCGIFMEDIA) {
301 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
302 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
304 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
305 ifm->ifm_cur->ifm_media : IFM_NONE;
307 ifmr->ifm_mask = ifm->ifm_mask;
308 ifmr->ifm_status = 0;
309 (*ifm->ifm_status)(ifp, ifmr);
312 * If there are more interfaces on the list, count
313 * them. This allows the caller to set ifmr->ifm_count
314 * to 0 on the first call to know how much space to
318 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
319 if (i++ < ifmr->ifm_count) {
320 error = copyout(&ep->ifm_media,
321 ifmr->ifm_ulist + i - 1, sizeof(int));
325 if (error == 0 && i > ifmr->ifm_count)
326 error = ifmr->ifm_count ? E2BIG : 0;
339 * Find media entry matching a given ifm word.
342 static struct ifmedia_entry *
343 ifmedia_match(ifm, target, mask)
348 struct ifmedia_entry *match, *next;
353 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
354 if ((next->ifm_media & mask) == (target & mask)) {
355 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
357 printf("ifmedia_match: multiple match for "
358 "0x%x/0x%x\n", target, mask);
369 * Compute the interface `baudrate' from the media, for the interface
370 * metrics (used by routing daemons).
372 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
373 IFM_BAUDRATE_DESCRIPTIONS;
376 ifmedia_baudrate(int mword)
380 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
381 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word))
382 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
390 struct ifmedia_description ifm_type_descriptions[] =
391 IFM_TYPE_DESCRIPTIONS;
393 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
394 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
396 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
397 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
399 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
400 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
402 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
403 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
405 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
406 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
408 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
409 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
411 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
412 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
414 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
415 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
417 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
418 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
420 struct ifmedia_description ifm_subtype_atm_descriptions[] =
421 IFM_SUBTYPE_ATM_DESCRIPTIONS;
423 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
424 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
426 struct ifmedia_description ifm_subtype_shared_descriptions[] =
427 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
429 struct ifmedia_description ifm_shared_option_descriptions[] =
430 IFM_SHARED_OPTION_DESCRIPTIONS;
432 struct ifmedia_type_to_subtype {
433 struct ifmedia_description *subtypes;
434 struct ifmedia_description *options;
435 struct ifmedia_description *modes;
438 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
439 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
441 &ifm_subtype_ethernet_descriptions[0],
442 &ifm_subtype_ethernet_option_descriptions[0],
446 &ifm_subtype_tokenring_descriptions[0],
447 &ifm_subtype_tokenring_option_descriptions[0],
451 &ifm_subtype_fddi_descriptions[0],
452 &ifm_subtype_fddi_option_descriptions[0],
456 &ifm_subtype_ieee80211_descriptions[0],
457 &ifm_subtype_ieee80211_option_descriptions[0],
458 &ifm_subtype_ieee80211_mode_descriptions[0]
461 &ifm_subtype_atm_descriptions[0],
462 &ifm_subtype_atm_option_descriptions[0],
468 * print a media word.
471 ifmedia_printword(ifmw)
474 struct ifmedia_description *desc;
475 struct ifmedia_type_to_subtype *ttos;
478 /* Find the top-level interface type. */
479 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
480 desc->ifmt_string != NULL; desc++, ttos++)
481 if (IFM_TYPE(ifmw) == desc->ifmt_word)
483 if (desc->ifmt_string == NULL) {
484 printf("<unknown type>\n");
487 printf("%s", desc->ifmt_string);
490 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
491 if (IFM_MODE(ifmw) == desc->ifmt_word) {
492 if (desc->ifmt_string != NULL)
493 printf(" mode %s", desc->ifmt_string);
498 * Check for the shared subtype descriptions first, then the
499 * type-specific ones.
501 for (desc = ifm_subtype_shared_descriptions;
502 desc->ifmt_string != NULL; desc++)
503 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
506 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
507 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
509 if (desc->ifmt_string == NULL) {
510 printf(" <unknown subtype>\n");
515 printf(" %s", desc->ifmt_string);
518 * Look for shared options.
520 for (desc = ifm_shared_option_descriptions;
521 desc->ifmt_string != NULL; desc++) {
522 if (ifmw & desc->ifmt_word) {
523 if (seen_option == 0)
525 printf("%s%s", seen_option++ ? "," : "",
531 * Look for subtype-specific options.
533 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
534 if (ifmw & desc->ifmt_word) {
535 if (seen_option == 0)
537 printf("%s%s", seen_option++ ? "," : "",
541 printf("%s\n", seen_option ? ">" : "");
543 #endif /* IFMEDIA_DEBUG */