1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
2 /* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks 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>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
60 * Compile-time options:
62 * turn on implementation-level debug kprintfs.
63 * Useful for debugging newly-ported drivers.
66 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
70 int ifmedia_debug = 0;
71 static void ifmedia_printword (int);
75 * Initialize if_media struct for a specific interface instance.
78 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
79 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
82 LIST_INIT(&ifm->ifm_list);
84 ifm->ifm_media = IFM_NONE;
85 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
86 ifm->ifm_change = change_callback;
87 ifm->ifm_status = status_callback;
91 ifmedia_removeall(struct ifmedia *ifm)
93 struct ifmedia_entry *entry;
95 while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
96 LIST_REMOVE(entry, ifm_list);
97 kfree(entry, M_IFADDR);
100 ifm->ifm_media = IFM_NONE;
104 * Add a media configuration to the list of supported media
105 * for a specific interface instance.
108 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
110 struct ifmedia_entry *entry;
115 kprintf("ifmedia_add: null ifm\n");
118 kprintf("Adding entry for ");
119 ifmedia_printword(mword);
123 entry = kmalloc(sizeof(*entry), M_IFADDR, M_INTWAIT);
124 entry->ifm_media = mword;
125 entry->ifm_data = data;
126 entry->ifm_aux = aux;
128 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
132 ifmedia_add_nodup(struct ifmedia *ifm, int mword, int data, void *aux)
134 struct ifmedia_entry *match;
136 match = ifmedia_match(ifm, mword, ifm->ifm_mask);
139 ifmedia_add(ifm, mword, data, aux);
144 * Add an array of media configurations to the list of
145 * supported media for a specific interface instance.
148 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp,
153 for (i = 0; i < count; i++)
154 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
159 * Set the default active media.
161 * Called by device-specific code which is assumed to have already
162 * selected the default media in hardware. We do _not_ call the
163 * media-change callback.
166 ifmedia_set(struct ifmedia *ifm, int target)
168 struct ifmedia_entry *match;
170 match = ifmedia_match(ifm, target, ifm->ifm_mask);
172 kprintf("ifmedia_set: no match for 0x%x/0x%x\n",
173 target, ~ifm->ifm_mask);
174 panic("ifmedia_set");
176 ifm->ifm_cur = match;
177 ifm->ifm_media = target;
181 kprintf("ifmedia_set: target ");
182 ifmedia_printword(target);
183 kprintf("ifmedia_set: setting to ");
184 ifmedia_printword(ifm->ifm_cur->ifm_media);
190 ifmedia_tryset(struct ifmedia *ifm, int target)
192 struct ifmedia_entry *match;
194 match = ifmedia_match(ifm, target, ifm->ifm_mask);
197 ifmedia_set(ifm, target);
202 * Given a media word, return one suitable for an application
203 * using the original encoding.
206 compat_media(int media)
209 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
210 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
217 * Device-independent media ioctl support function, typically called
218 * from the device network ioctl procedure.
221 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
222 struct ifmedia *ifm, u_long cmd)
224 struct ifmedia_entry *match;
225 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
226 int error = 0, sticky;
228 if (ifp == NULL || ifr == NULL || ifm == NULL)
231 ASSERT_IFNET_SERIALIZED_ALL(ifp);
235 * Set the current media.
239 struct ifmedia_entry *oldentry;
241 int newmedia = ifr->ifr_media;
243 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
248 "ifmedia_ioctl: no media found for 0x%x\n",
256 * If no change, we're done.
257 * XXX Automedia may invole software intervention.
258 * Keep going in case the the connected media changed.
259 * Similarly, if best match changed (kernel debugger?).
261 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
262 (newmedia == ifm->ifm_media) &&
263 (match == ifm->ifm_cur))
267 * We found a match, now make the driver switch to it.
268 * Make sure to preserve our old media type in case the
269 * driver can't switch.
273 kprintf("ifmedia_ioctl: switching %s to ",
275 ifmedia_printword(match->ifm_media);
278 oldentry = ifm->ifm_cur;
279 oldmedia = ifm->ifm_media;
280 ifm->ifm_cur = match;
281 ifm->ifm_media = newmedia;
282 error = (*ifm->ifm_change)(ifp);
284 ifm->ifm_cur = oldentry;
285 ifm->ifm_media = oldmedia;
291 * Get list of available media and current media on interface.
296 struct ifmedia_entry *ep;
298 int usermax; /* user requested max */
300 kptr = NULL; /* XXX gcc */
304 * ifm_cur may not contain certain media options, e.g.
305 * flow control options, so ifm_media should be used
308 if (cmd == SIOCGIFMEDIA) {
309 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
310 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
312 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
313 ifm->ifm_cur->ifm_media : IFM_NONE;
315 ifmr->ifm_mask = ifm->ifm_mask;
316 ifmr->ifm_status = 0;
317 (*ifm->ifm_status)(ifp, ifmr);
323 * If there are more interfaces on the list, count
324 * them. This allows the caller to set ifmr->ifm_count
325 * to 0 on the first call to know how much space to
328 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
332 * Don't allow the user to ask for too many
333 * or a negative number.
335 if (ifmr->ifm_count > usermax)
336 ifmr->ifm_count = usermax;
337 else if (ifmr->ifm_count < 0)
340 if (ifmr->ifm_count != 0) {
341 kptr = (int *)kmalloc(ifmr->ifm_count * sizeof(int),
345 * Get the media words from the interface's list.
347 ep = LIST_FIRST(&ifm->ifm_list);
348 for (; ep != NULL && count < ifmr->ifm_count;
349 ep = LIST_NEXT(ep, ifm_list), count++)
350 kptr[count] = ep->ifm_media;
353 error = E2BIG; /* oops! */
359 * We do the copyout on E2BIG, because that's
360 * just our way of telling userland that there
361 * are more. This is the behavior I've observed
365 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
366 error = copyout((caddr_t)kptr,
367 (caddr_t)ifmr->ifm_ulist,
368 ifmr->ifm_count * sizeof(int));
374 if (ifmr->ifm_count != 0)
377 ifmr->ifm_count = count;
389 * Find media entry matching a given ifm word.
392 static struct ifmedia_entry *
393 ifmedia_match(struct ifmedia *ifm, int target, int mask)
395 struct ifmedia_entry *match, *next;
400 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
401 if ((next->ifm_media & mask) == (target & mask)) {
402 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
404 kprintf("ifmedia_match: multiple match for "
405 "0x%x/0x%x\n", target, mask);
415 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
416 IFM_BAUDRATE_DESCRIPTIONS;
419 ifmedia_baudrate(int mword)
423 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
424 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word))
425 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
433 ifmedia_str2ethfc(const char *str)
435 if (strcmp(str, IFM_ETH_FC_FULL) == 0)
436 return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
437 if (strcmp(str, IFM_ETH_FC_RXPAUSE) == 0)
438 return IFM_ETH_RXPAUSE;
439 if (strcmp(str, IFM_ETH_FC_TXPAUSE) == 0)
440 return IFM_ETH_TXPAUSE;
442 if (strcmp(str, IFM_ETH_FC_FORCE_FULL) == 0)
443 return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
444 if (strcmp(str, IFM_ETH_FC_FORCE_RXPAUSE) == 0)
445 return (IFM_ETH_RXPAUSE | IFM_ETH_FORCEPAUSE);
446 if (strcmp(str, IFM_ETH_FC_FORCE_TXPAUSE) == 0)
447 return (IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
448 if (strcmp(str, IFM_ETH_FC_FORCE_NONE) == 0)
449 return IFM_ETH_FORCEPAUSE;
455 struct ifmedia_description ifm_type_descriptions[] =
456 IFM_TYPE_DESCRIPTIONS;
458 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
459 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
461 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
462 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
464 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
465 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
467 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
468 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
470 struct ifmedia_description ifm_subtype_shared_descriptions[] =
471 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
473 struct ifmedia_description ifm_shared_option_descriptions[] =
474 IFM_SHARED_OPTION_DESCRIPTIONS;
476 struct ifmedia_type_to_subtype {
477 struct ifmedia_description *subtypes;
478 struct ifmedia_description *options;
481 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
482 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
484 &ifm_subtype_ethernet_descriptions[0],
485 &ifm_subtype_ethernet_option_descriptions[0]
488 &ifm_subtype_ieee80211_descriptions[0],
489 &ifm_subtype_ieee80211_option_descriptions[0]
494 * print a media word.
497 ifmedia_printword(int ifmw)
499 struct ifmedia_description *desc;
500 struct ifmedia_type_to_subtype *ttos;
503 /* Find the top-level interface type. */
504 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
505 desc->ifmt_string != NULL; desc++, ttos++)
506 if (IFM_TYPE(ifmw) == desc->ifmt_word)
508 if (desc->ifmt_string == NULL) {
509 kprintf("<unknown type>\n");
512 kprintf("%s", desc->ifmt_string);
515 * Check for the shared subtype descriptions first, then the
516 * type-specific ones.
518 for (desc = ifm_subtype_shared_descriptions;
519 desc->ifmt_string != NULL; desc++)
520 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
523 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
524 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
526 if (desc->ifmt_string == NULL) {
527 kprintf(" <unknown subtype>\n");
532 kprintf(" %s", desc->ifmt_string);
535 * Look for shared options.
537 for (desc = ifm_shared_option_descriptions;
538 desc->ifmt_string != NULL; desc++) {
539 if (ifmw & desc->ifmt_word) {
540 if (seen_option == 0)
542 kprintf("%s%s", seen_option++ ? "," : "",
548 * Look for subtype-specific options.
550 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
551 if (ifmw & desc->ifmt_word) {
552 if (seen_option == 0)
554 kprintf("%s%s", seen_option++ ? "," : "",
558 kprintf("%s\n", seen_option ? ">" : "");
560 #endif /* IFMEDIA_DEBUG */