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 <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/sysctl.h>
58 #include <net/if_media.h>
61 * Compile-time options:
63 * turn on implementation-level debug printfs.
64 * Useful for debugging newly-ported drivers.
67 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
71 int ifmedia_debug = 0;
72 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
73 0, "if_media debugging msgs");
74 static void ifmedia_printword(int);
78 * Initialize if_media struct for a specific interface instance.
81 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
84 ifm_change_cb_t change_callback;
85 ifm_stat_cb_t status_callback;
88 LIST_INIT(&ifm->ifm_list);
91 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
92 ifm->ifm_change = change_callback;
93 ifm->ifm_status = status_callback;
97 ifmedia_removeall(ifm)
100 struct ifmedia_entry *entry;
102 for (entry = LIST_FIRST(&ifm->ifm_list); entry;
103 entry = LIST_FIRST(&ifm->ifm_list)) {
104 LIST_REMOVE(entry, ifm_list);
105 free(entry, M_IFADDR);
110 * Add a media configuration to the list of supported media
111 * for a specific interface instance.
114 ifmedia_add(ifm, mword, data, aux)
120 register struct ifmedia_entry *entry;
125 printf("ifmedia_add: null ifm\n");
128 printf("Adding entry for ");
129 ifmedia_printword(mword);
133 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
135 panic("ifmedia_add: can't malloc entry");
137 entry->ifm_media = mword;
138 entry->ifm_data = data;
139 entry->ifm_aux = aux;
141 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
145 * Add an array of media configurations to the list of
146 * supported media for a specific interface instance.
149 ifmedia_list_add(ifm, lp, count)
151 struct ifmedia_entry *lp;
156 for (i = 0; i < count; i++)
157 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
162 * Set the default active media.
164 * Called by device-specific code which is assumed to have already
165 * selected the default media in hardware. We do _not_ call the
166 * media-change callback.
169 ifmedia_set(ifm, target)
174 struct ifmedia_entry *match;
176 match = ifmedia_match(ifm, target, ifm->ifm_mask);
179 printf("ifmedia_set: no match for 0x%x/0x%x\n",
180 target, ~ifm->ifm_mask);
181 panic("ifmedia_set");
183 ifm->ifm_cur = match;
187 printf("ifmedia_set: target ");
188 ifmedia_printword(target);
189 printf("ifmedia_set: setting to ");
190 ifmedia_printword(ifm->ifm_cur->ifm_media);
196 * Device-independent media ioctl support function.
199 ifmedia_ioctl(ifp, ifr, ifm, cmd)
205 struct ifmedia_entry *match;
206 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
207 int error = 0, sticky;
209 if (ifp == NULL || ifr == NULL || ifm == NULL)
215 * Set the current media.
219 struct ifmedia_entry *oldentry;
221 int newmedia = ifr->ifr_media;
223 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
228 "ifmedia_ioctl: no media found for 0x%x\n",
236 * If no change, we're done.
237 * XXX Automedia may invole software intervention.
238 * Keep going in case the the connected media changed.
239 * Similarly, if best match changed (kernel debugger?).
241 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
242 (newmedia == ifm->ifm_media) &&
243 (match == ifm->ifm_cur))
247 * We found a match, now make the driver switch to it.
248 * Make sure to preserve our old media type in case the
249 * driver can't switch.
253 printf("ifmedia_ioctl: switching %s to ",
255 ifmedia_printword(match->ifm_media);
258 oldentry = ifm->ifm_cur;
259 oldmedia = ifm->ifm_media;
260 ifm->ifm_cur = match;
261 ifm->ifm_media = newmedia;
262 error = (*ifm->ifm_change)(ifp);
264 ifm->ifm_cur = oldentry;
265 ifm->ifm_media = oldmedia;
271 * Get list of available media and current media on interface.
275 struct ifmedia_entry *ep;
277 int usermax; /* user requested max */
279 kptr = NULL; /* XXX gcc */
281 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
282 ifm->ifm_cur->ifm_media : IFM_NONE;
283 ifmr->ifm_mask = ifm->ifm_mask;
284 ifmr->ifm_status = 0;
285 (*ifm->ifm_status)(ifp, ifmr);
291 * If there are more interfaces on the list, count
292 * them. This allows the caller to set ifmr->ifm_count
293 * to 0 on the first call to know how much space to
296 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
300 * Don't allow the user to ask for too many
301 * or a negative number.
303 if (ifmr->ifm_count > usermax)
304 ifmr->ifm_count = usermax;
305 else if (ifmr->ifm_count < 0)
308 if (ifmr->ifm_count != 0) {
309 kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
315 * Get the media words from the interface's list.
317 ep = LIST_FIRST(&ifm->ifm_list);
318 for (; ep != NULL && count < ifmr->ifm_count;
319 ep = LIST_NEXT(ep, ifm_list), count++)
320 kptr[count] = ep->ifm_media;
323 error = E2BIG; /* oops! */
329 * We do the copyout on E2BIG, because that's
330 * just our way of telling userland that there
331 * are more. This is the behavior I've observed
335 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
336 error = copyout((caddr_t)kptr,
337 (caddr_t)ifmr->ifm_ulist,
338 ifmr->ifm_count * sizeof(int));
344 if (ifmr->ifm_count != 0)
347 ifmr->ifm_count = count;
359 * Find media entry matching a given ifm word.
362 static struct ifmedia_entry *
363 ifmedia_match(ifm, target, mask)
368 struct ifmedia_entry *match, *next;
373 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
374 if ((next->ifm_media & mask) == (target & mask)) {
375 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
377 printf("ifmedia_match: multiple match for "
378 "0x%x/0x%x\n", target, mask);
389 * Compute the interface `baudrate' from the media, for the interface
390 * metrics (used by routing daemons).
392 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
393 IFM_BAUDRATE_DESCRIPTIONS;
396 ifmedia_baudrate(int mword)
400 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
401 if ((mword & (IFM_NMASK|IFM_TMASK)) ==
402 ifmedia_baudrate_descriptions[i].ifmb_word)
403 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
411 struct ifmedia_description ifm_type_descriptions[] =
412 IFM_TYPE_DESCRIPTIONS;
414 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
415 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
417 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
418 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
420 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
421 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
423 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
424 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
426 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
427 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
429 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
430 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
432 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
433 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
435 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
436 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
438 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
439 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
441 struct ifmedia_description ifm_subtype_atm_descriptions[] =
442 IFM_SUBTYPE_ATM_DESCRIPTIONS;
444 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
445 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
447 struct ifmedia_description ifm_subtype_shared_descriptions[] =
448 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
450 struct ifmedia_description ifm_shared_option_descriptions[] =
451 IFM_SHARED_OPTION_DESCRIPTIONS;
453 struct ifmedia_type_to_subtype {
454 struct ifmedia_description *subtypes;
455 struct ifmedia_description *options;
456 struct ifmedia_description *modes;
459 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
460 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
462 &ifm_subtype_ethernet_descriptions[0],
463 &ifm_subtype_ethernet_option_descriptions[0],
467 &ifm_subtype_tokenring_descriptions[0],
468 &ifm_subtype_tokenring_option_descriptions[0],
472 &ifm_subtype_fddi_descriptions[0],
473 &ifm_subtype_fddi_option_descriptions[0],
477 &ifm_subtype_ieee80211_descriptions[0],
478 &ifm_subtype_ieee80211_option_descriptions[0],
479 &ifm_subtype_ieee80211_mode_descriptions[0]
482 &ifm_subtype_atm_descriptions[0],
483 &ifm_subtype_atm_option_descriptions[0],
489 * print a media word.
492 ifmedia_printword(ifmw)
495 struct ifmedia_description *desc;
496 struct ifmedia_type_to_subtype *ttos;
499 /* Find the top-level interface type. */
500 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
501 desc->ifmt_string != NULL; desc++, ttos++)
502 if (IFM_TYPE(ifmw) == desc->ifmt_word)
504 if (desc->ifmt_string == NULL) {
505 printf("<unknown type>\n");
508 printf(desc->ifmt_string);
511 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
512 if (IFM_MODE(ifmw) == desc->ifmt_word) {
513 if (desc->ifmt_string != NULL)
514 printf(" mode %s", desc->ifmt_string);
519 * Check for the shared subtype descriptions first, then the
520 * type-specific ones.
522 for (desc = ifm_subtype_shared_descriptions;
523 desc->ifmt_string != NULL; desc++)
524 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
527 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
528 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
530 if (desc->ifmt_string == NULL) {
531 printf(" <unknown subtype>\n");
536 printf(" %s", desc->ifmt_string);
539 * Look for shared options.
541 for (desc = ifm_shared_option_descriptions;
542 desc->ifmt_string != NULL; desc++) {
543 if (ifmw & desc->ifmt_word) {
544 if (seen_option == 0)
546 printf("%s%s", seen_option++ ? "," : "",
552 * Look for subtype-specific options.
554 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
555 if (ifmw & desc->ifmt_word) {
556 if (seen_option == 0)
558 printf("%s%s", seen_option++ ? "," : "",
562 printf("%s\n", seen_option ? ">" : "");
564 #endif /* IFMEDIA_DEBUG */