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);
114 * Add a media configuration to the list of supported media
115 * for a specific interface instance.
118 ifmedia_add(ifm, mword, data, aux)
124 struct ifmedia_entry *entry;
129 printf("ifmedia_add: null ifm\n");
132 printf("Adding entry for ");
133 ifmedia_printword(mword);
137 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
139 panic("ifmedia_add: can't malloc entry");
141 entry->ifm_media = mword;
142 entry->ifm_data = data;
143 entry->ifm_aux = aux;
145 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
149 * Add an array of media configurations to the list of
150 * supported media for a specific interface instance.
153 ifmedia_list_add(ifm, lp, count)
155 struct ifmedia_entry *lp;
160 for (i = 0; i < count; i++)
161 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
166 * Set the default active media.
168 * Called by device-specific code which is assumed to have already
169 * selected the default media in hardware. We do _not_ call the
170 * media-change callback.
173 ifmedia_set(ifm, target)
178 struct ifmedia_entry *match;
180 match = ifmedia_match(ifm, target, ifm->ifm_mask);
183 printf("ifmedia_set: no match for 0x%x/0x%x\n",
184 target, ~ifm->ifm_mask);
185 panic("ifmedia_set");
187 ifm->ifm_cur = match;
191 printf("ifmedia_set: target ");
192 ifmedia_printword(target);
193 printf("ifmedia_set: setting to ");
194 ifmedia_printword(ifm->ifm_cur->ifm_media);
200 * Given a media word, return one suitable for an application
201 * using the original encoding.
204 compat_media(int media)
207 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
208 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
215 * Device-independent media ioctl support function.
218 ifmedia_ioctl(ifp, ifr, ifm, cmd)
224 struct ifmedia_entry *match;
225 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
228 if (ifp == NULL || ifr == NULL || ifm == NULL)
234 * Set the current media.
238 struct ifmedia_entry *oldentry;
240 int newmedia = ifr->ifr_media;
242 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
247 "ifmedia_ioctl: no media found for 0x%x\n",
255 * If no change, we're done.
256 * XXX Automedia may invole software intervention.
257 * Keep going in case the connected media changed.
258 * Similarly, if best match changed (kernel debugger?).
260 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
261 (newmedia == ifm->ifm_media) &&
262 (match == ifm->ifm_cur))
266 * We found a match, now make the driver switch to it.
267 * Make sure to preserve our old media type in case the
268 * driver can't switch.
272 printf("ifmedia_ioctl: switching %s to ",
274 ifmedia_printword(match->ifm_media);
277 oldentry = ifm->ifm_cur;
278 oldmedia = ifm->ifm_media;
279 ifm->ifm_cur = match;
280 ifm->ifm_media = newmedia;
281 error = (*ifm->ifm_change)(ifp);
283 ifm->ifm_cur = oldentry;
284 ifm->ifm_media = oldmedia;
290 * Get list of available media and current media on interface.
295 struct ifmedia_entry *ep;
298 if (ifmr->ifm_count < 0)
301 if (cmd == SIOCGIFMEDIA) {
302 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
303 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
305 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
306 ifm->ifm_cur->ifm_media : IFM_NONE;
308 ifmr->ifm_mask = ifm->ifm_mask;
309 ifmr->ifm_status = 0;
310 (*ifm->ifm_status)(ifp, ifmr);
313 * If there are more interfaces on the list, count
314 * them. This allows the caller to set ifmr->ifm_count
315 * to 0 on the first call to know how much space to
319 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
320 if (i++ < ifmr->ifm_count) {
321 error = copyout(&ep->ifm_media,
322 ifmr->ifm_ulist + i - 1, sizeof(int));
326 if (error == 0 && i > ifmr->ifm_count)
327 error = ifmr->ifm_count ? E2BIG : 0;
340 * Find media entry matching a given ifm word.
343 static struct ifmedia_entry *
344 ifmedia_match(ifm, target, mask)
349 struct ifmedia_entry *match, *next;
354 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
355 if ((next->ifm_media & mask) == (target & mask)) {
356 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
358 printf("ifmedia_match: multiple match for "
359 "0x%x/0x%x\n", target, mask);
370 * Compute the interface `baudrate' from the media, for the interface
371 * metrics (used by routing daemons).
373 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
374 IFM_BAUDRATE_DESCRIPTIONS;
377 ifmedia_baudrate(int mword)
381 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
382 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word))
383 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
391 struct ifmedia_description ifm_type_descriptions[] =
392 IFM_TYPE_DESCRIPTIONS;
394 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
395 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
397 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
398 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
400 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
401 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
403 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
404 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
406 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
407 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
409 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
410 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
412 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
413 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
415 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
416 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
418 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
419 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
421 struct ifmedia_description ifm_subtype_atm_descriptions[] =
422 IFM_SUBTYPE_ATM_DESCRIPTIONS;
424 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
425 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
427 struct ifmedia_description ifm_subtype_shared_descriptions[] =
428 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
430 struct ifmedia_description ifm_shared_option_descriptions[] =
431 IFM_SHARED_OPTION_DESCRIPTIONS;
433 struct ifmedia_type_to_subtype {
434 struct ifmedia_description *subtypes;
435 struct ifmedia_description *options;
436 struct ifmedia_description *modes;
439 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
440 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
442 &ifm_subtype_ethernet_descriptions[0],
443 &ifm_subtype_ethernet_option_descriptions[0],
447 &ifm_subtype_tokenring_descriptions[0],
448 &ifm_subtype_tokenring_option_descriptions[0],
452 &ifm_subtype_fddi_descriptions[0],
453 &ifm_subtype_fddi_option_descriptions[0],
457 &ifm_subtype_ieee80211_descriptions[0],
458 &ifm_subtype_ieee80211_option_descriptions[0],
459 &ifm_subtype_ieee80211_mode_descriptions[0]
462 &ifm_subtype_atm_descriptions[0],
463 &ifm_subtype_atm_option_descriptions[0],
469 * print a media word.
472 ifmedia_printword(ifmw)
475 struct ifmedia_description *desc;
476 struct ifmedia_type_to_subtype *ttos;
479 /* Find the top-level interface type. */
480 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
481 desc->ifmt_string != NULL; desc++, ttos++)
482 if (IFM_TYPE(ifmw) == desc->ifmt_word)
484 if (desc->ifmt_string == NULL) {
485 printf("<unknown type>\n");
488 printf("%s", desc->ifmt_string);
491 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
492 if (IFM_MODE(ifmw) == desc->ifmt_word) {
493 if (desc->ifmt_string != NULL)
494 printf(" mode %s", desc->ifmt_string);
499 * Check for the shared subtype descriptions first, then the
500 * type-specific ones.
502 for (desc = ifm_subtype_shared_descriptions;
503 desc->ifmt_string != NULL; desc++)
504 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
507 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
508 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
510 if (desc->ifmt_string == NULL) {
511 printf(" <unknown subtype>\n");
516 printf(" %s", desc->ifmt_string);
519 * Look for shared options.
521 for (desc = ifm_shared_option_descriptions;
522 desc->ifmt_string != NULL; desc++) {
523 if (ifmw & desc->ifmt_word) {
524 if (seen_option == 0)
526 printf("%s%s", seen_option++ ? "," : "",
532 * Look for subtype-specific options.
534 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
535 if (ifmw & desc->ifmt_word) {
536 if (seen_option == 0)
538 printf("%s%s", seen_option++ ? "," : "",
542 printf("%s\n", seen_option ? ">" : "");
544 #endif /* IFMEDIA_DEBUG */