2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
24 /* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks Exp $ */
28 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
30 * This software is derived from information provided by Matt Thomas.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by Jonathan Stone
43 * and Jason R. Thorpe for the NetBSD Project.
44 * 4. The names of the authors may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
54 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * BSD/OS-compatible network interface media selection.
63 * Where it is safe to do so, this code strays slightly from the BSD/OS
64 * design. Software which uses the API (device drivers, basically)
65 * shouldn't notice any difference.
67 * Many thanks to Matt Thomas for providing the information necessary
68 * to implement this interface.
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/socket.h>
74 #include <sys/sockio.h>
75 #include <sys/malloc.h>
78 #include <net/if_media.h>
81 * Compile-time options:
83 * turn on implementation-level debug printfs.
84 * Useful for debugging newly-ported drivers.
87 static struct ifmedia_entry
*ifmedia_match(struct ifmedia
*ifm
,
91 int ifmedia_debug
= 0;
92 static void ifmedia_printword(int);
96 * Initialize if_media struct for a specific interface instance.
99 ifmedia_init(ifm
, dontcare_mask
, change_callback
, status_callback
)
102 ifm_change_cb_t change_callback
;
103 ifm_stat_cb_t status_callback
;
106 LIST_INIT(&ifm
->ifm_list
);
109 ifm
->ifm_mask
= dontcare_mask
; /* IF don't-care bits */
110 ifm
->ifm_change
= change_callback
;
111 ifm
->ifm_status
= status_callback
;
115 ifmedia_removeall(ifm
)
118 struct ifmedia_entry
*entry
;
120 for (entry
= LIST_FIRST(&ifm
->ifm_list
); entry
;
121 entry
= LIST_FIRST(&ifm
->ifm_list
)) {
122 LIST_REMOVE(entry
, ifm_list
);
123 FREE(entry
, M_IFADDR
);
128 * Add a media configuration to the list of supported media
129 * for a specific interface instance.
132 ifmedia_add(ifm
, mword
, data
, aux
)
138 register struct ifmedia_entry
*entry
;
143 printf("ifmedia_add: null ifm\n");
146 printf("Adding entry for ");
147 ifmedia_printword(mword
);
151 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_NOWAIT
);
153 panic("ifmedia_add: can't malloc entry");
155 entry
->ifm_media
= mword
;
156 entry
->ifm_data
= data
;
157 entry
->ifm_aux
= aux
;
159 LIST_INSERT_HEAD(&ifm
->ifm_list
, entry
, ifm_list
);
163 * Add an array of media configurations to the list of
164 * supported media for a specific interface instance.
167 ifmedia_list_add(ifm
, lp
, count
)
169 struct ifmedia_entry
*lp
;
174 for (i
= 0; i
< count
; i
++)
175 ifmedia_add(ifm
, lp
[i
].ifm_media
, lp
[i
].ifm_data
,
180 * Set the default active media.
182 * Called by device-specific code which is assumed to have already
183 * selected the default media in hardware. We do _not_ call the
184 * media-change callback.
187 ifmedia_set(ifm
, target
)
192 struct ifmedia_entry
*match
;
194 match
= ifmedia_match(ifm
, target
, ifm
->ifm_mask
);
197 printf("ifmedia_set: no match for 0x%x/0x%x\n",
198 target
, ~ifm
->ifm_mask
);
199 panic("ifmedia_set");
201 ifm
->ifm_cur
= match
;
205 printf("ifmedia_set: target ");
206 ifmedia_printword(target
);
207 printf("ifmedia_set: setting to ");
208 ifmedia_printword(ifm
->ifm_cur
->ifm_media
);
214 * Device-independent media ioctl support function.
223 struct ifmedia_entry
*match
;
224 struct ifmediareq
*ifmr
= (struct ifmediareq
*) ifr
;
225 int error
= 0, sticky
;
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 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.
293 struct ifmedia_entry
*ep
;
295 int usermax
; /* user requested max */
297 kptr
= NULL
; /* XXX gcc */
299 ifmr
->ifm_active
= ifmr
->ifm_current
= ifm
->ifm_cur
?
300 ifm
->ifm_cur
->ifm_media
: IFM_NONE
;
301 ifmr
->ifm_mask
= ifm
->ifm_mask
;
302 ifmr
->ifm_status
= 0;
303 (*ifm
->ifm_status
)(ifp
, ifmr
);
309 * If there are more interfaces on the list, count
310 * them. This allows the caller to set ifmr->ifm_count
311 * to 0 on the first call to know how much space to
314 LIST_FOREACH(ep
, &ifm
->ifm_list
, ifm_list
)
318 * Don't allow the user to ask for too many
319 * or a negative number.
321 if (ifmr
->ifm_count
> usermax
)
322 ifmr
->ifm_count
= usermax
;
323 else if (ifmr
->ifm_count
< 0)
326 if (ifmr
->ifm_count
!= 0) {
327 kptr
= (int *) _MALLOC(ifmr
->ifm_count
* sizeof(int),
331 * Get the media words from the interface's list.
333 ep
= LIST_FIRST(&ifm
->ifm_list
);
334 for (; ep
!= NULL
&& count
< ifmr
->ifm_count
;
335 ep
= LIST_NEXT(ep
, ifm_list
), count
++)
336 kptr
[count
] = ep
->ifm_media
;
339 error
= E2BIG
; /* oops! */
345 * We do the copyout on E2BIG, because that's
346 * just our way of telling userland that there
347 * are more. This is the behavior I've observed
351 if ((error
== 0 || error
== E2BIG
) && ifmr
->ifm_count
!= 0) {
352 error
= copyout((caddr_t
)kptr
,
353 CAST_USER_ADDR_T(ifmr
->ifm_ulist
),
354 ifmr
->ifm_count
* sizeof(int));
360 if (ifmr
->ifm_count
!= 0)
363 ifmr
->ifm_count
= count
;
375 * Find media entry matching a given ifm word.
378 static struct ifmedia_entry
*
379 ifmedia_match(ifm
, target
, mask
)
384 struct ifmedia_entry
*match
, *next
;
389 LIST_FOREACH(next
, &ifm
->ifm_list
, ifm_list
) {
390 if ((next
->ifm_media
& mask
) == (target
& mask
)) {
391 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
393 printf("ifmedia_match: multiple match for "
394 "0x%x/0x%x\n", target
, mask
);
405 struct ifmedia_description ifm_type_descriptions
[] =
406 IFM_TYPE_DESCRIPTIONS
;
408 struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
409 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
411 struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
412 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
414 struct ifmedia_description ifm_subtype_tokenring_descriptions
[] =
415 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS
;
417 struct ifmedia_description ifm_subtype_tokenring_option_descriptions
[] =
418 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS
;
420 struct ifmedia_description ifm_subtype_fddi_descriptions
[] =
421 IFM_SUBTYPE_FDDI_DESCRIPTIONS
;
423 struct ifmedia_description ifm_subtype_fddi_option_descriptions
[] =
424 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS
;
426 struct ifmedia_description ifm_subtype_80211_descriptions
[] =
427 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
429 struct ifmedia_description ifm_subtype_80211_option_descriptions
[] =
430 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
432 struct ifmedia_description ifm_subtype_shared_descriptions
[] =
433 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
435 struct ifmedia_description ifm_shared_option_descriptions
[] =
436 IFM_SHARED_OPTION_DESCRIPTIONS
;
438 struct ifmedia_type_to_subtype
{
439 struct ifmedia_description
*subtypes
;
440 struct ifmedia_description
*options
;
443 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
444 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
446 &ifm_subtype_ethernet_descriptions
[0],
447 &ifm_subtype_ethernet_option_descriptions
[0]
450 &ifm_subtype_tokenring_descriptions
[0],
451 &ifm_subtype_tokenring_option_descriptions
[0]
454 &ifm_subtype_fddi_descriptions
[0],
455 &ifm_subtype_fddi_option_descriptions
[0]
458 &ifm_subtype_80211_descriptions
[0],
459 &ifm_subtype_80211_option_descriptions
[0]
464 * print a media word.
467 ifmedia_printword(ifmw
)
470 struct ifmedia_description
*desc
;
471 struct ifmedia_type_to_subtype
*ttos
;
474 /* Find the top-level interface type. */
475 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
476 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
477 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
479 if (desc
->ifmt_string
== NULL
) {
480 printf("<unknown type>\n");
483 printf(desc
->ifmt_string
);
486 * Check for the shared subtype descriptions first, then the
487 * type-specific ones.
489 for (desc
= ifm_subtype_shared_descriptions
;
490 desc
->ifmt_string
!= NULL
; desc
++)
491 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
494 for (desc
= ttos
->subtypes
; desc
->ifmt_string
!= NULL
; desc
++)
495 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
497 if (desc
->ifmt_string
== NULL
) {
498 printf(" <unknown subtype>\n");
503 printf(" %s", desc
->ifmt_string
);
506 * Look for shared options.
508 for (desc
= ifm_shared_option_descriptions
;
509 desc
->ifmt_string
!= NULL
; desc
++) {
510 if (ifmw
& desc
->ifmt_word
) {
511 if (seen_option
== 0)
513 printf("%s%s", seen_option
++ ? "," : "",
519 * Look for subtype-specific options.
521 for (desc
= ttos
->options
; desc
->ifmt_string
!= NULL
; desc
++) {
522 if (ifmw
& desc
->ifmt_word
) {
523 if (seen_option
== 0)
525 printf("%s%s", seen_option
++ ? "," : "",
529 printf("%s\n", seen_option
? ">" : "");
531 #endif /* IFMEDIA_DEBUG */