2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
31 /* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks Exp $ */
35 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
37 * This software is derived from information provided by Matt Thomas.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by Jonathan Stone
50 * and Jason R. Thorpe for the NetBSD Project.
51 * 4. The names of the authors may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
59 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
61 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * BSD/OS-compatible network interface media selection.
70 * Where it is safe to do so, this code strays slightly from the BSD/OS
71 * design. Software which uses the API (device drivers, basically)
72 * shouldn't notice any difference.
74 * Many thanks to Matt Thomas for providing the information necessary
75 * to implement this interface.
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/socket.h>
81 #include <sys/sockio.h>
82 #include <sys/malloc.h>
85 #include <net/if_media.h>
88 * Compile-time options:
90 * turn on implementation-level debug printfs.
91 * Useful for debugging newly-ported drivers.
94 static struct ifmedia_entry
*ifmedia_match(struct ifmedia
*ifm
,
98 int ifmedia_debug
= 0;
99 static void ifmedia_printword(int);
103 * Initialize if_media struct for a specific interface instance.
106 ifmedia_init(ifm
, dontcare_mask
, change_callback
, status_callback
)
109 ifm_change_cb_t change_callback
;
110 ifm_stat_cb_t status_callback
;
113 LIST_INIT(&ifm
->ifm_list
);
116 ifm
->ifm_mask
= dontcare_mask
; /* IF don't-care bits */
117 ifm
->ifm_change
= change_callback
;
118 ifm
->ifm_status
= status_callback
;
122 ifmedia_removeall(ifm
)
125 struct ifmedia_entry
*entry
;
127 for (entry
= LIST_FIRST(&ifm
->ifm_list
); entry
;
128 entry
= LIST_FIRST(&ifm
->ifm_list
)) {
129 LIST_REMOVE(entry
, ifm_list
);
130 FREE(entry
, M_IFADDR
);
135 * Add a media configuration to the list of supported media
136 * for a specific interface instance.
139 ifmedia_add(ifm
, mword
, data
, aux
)
145 register struct ifmedia_entry
*entry
;
150 printf("ifmedia_add: null ifm\n");
153 printf("Adding entry for ");
154 ifmedia_printword(mword
);
158 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_NOWAIT
);
160 panic("ifmedia_add: can't malloc entry");
162 entry
->ifm_media
= mword
;
163 entry
->ifm_data
= data
;
164 entry
->ifm_aux
= aux
;
166 LIST_INSERT_HEAD(&ifm
->ifm_list
, entry
, ifm_list
);
170 * Add an array of media configurations to the list of
171 * supported media for a specific interface instance.
174 ifmedia_list_add(ifm
, lp
, count
)
176 struct ifmedia_entry
*lp
;
181 for (i
= 0; i
< count
; i
++)
182 ifmedia_add(ifm
, lp
[i
].ifm_media
, lp
[i
].ifm_data
,
187 * Set the default active media.
189 * Called by device-specific code which is assumed to have already
190 * selected the default media in hardware. We do _not_ call the
191 * media-change callback.
194 ifmedia_set(ifm
, target
)
199 struct ifmedia_entry
*match
;
201 match
= ifmedia_match(ifm
, target
, ifm
->ifm_mask
);
204 printf("ifmedia_set: no match for 0x%x/0x%x\n",
205 target
, ~ifm
->ifm_mask
);
206 panic("ifmedia_set");
208 ifm
->ifm_cur
= match
;
212 printf("ifmedia_set: target ");
213 ifmedia_printword(target
);
214 printf("ifmedia_set: setting to ");
215 ifmedia_printword(ifm
->ifm_cur
->ifm_media
);
221 * Device-independent media ioctl support function.
230 struct ifmedia_entry
*match
;
231 struct ifmediareq
*ifmr
= (struct ifmediareq
*) ifr
;
232 int error
= 0, sticky
;
234 if (ifp
== NULL
|| ifr
== NULL
|| ifm
== NULL
)
240 * Set the current media.
244 struct ifmedia_entry
*oldentry
;
246 int newmedia
= ifr
->ifr_media
;
248 match
= ifmedia_match(ifm
, newmedia
, ifm
->ifm_mask
);
253 "ifmedia_ioctl: no media found for 0x%x\n",
261 * If no change, we're done.
262 * XXX Automedia may invole software intervention.
263 * Keep going in case the the connected media changed.
264 * Similarly, if best match changed (kernel debugger?).
266 if ((IFM_SUBTYPE(newmedia
) != IFM_AUTO
) &&
267 (newmedia
== ifm
->ifm_media
) &&
268 (match
== ifm
->ifm_cur
))
272 * We found a match, now make the driver switch to it.
273 * Make sure to preserve our old media type in case the
274 * driver can't switch.
278 printf("ifmedia_ioctl: switching %s to ",
280 ifmedia_printword(match
->ifm_media
);
283 oldentry
= ifm
->ifm_cur
;
284 oldmedia
= ifm
->ifm_media
;
285 ifm
->ifm_cur
= match
;
286 ifm
->ifm_media
= newmedia
;
287 error
= (*ifm
->ifm_change
)(ifp
);
289 ifm
->ifm_cur
= oldentry
;
290 ifm
->ifm_media
= oldmedia
;
296 * Get list of available media and current media on interface.
300 struct ifmedia_entry
*ep
;
302 int usermax
; /* user requested max */
304 kptr
= NULL
; /* XXX gcc */
306 ifmr
->ifm_active
= ifmr
->ifm_current
= ifm
->ifm_cur
?
307 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
);
316 * If there are more interfaces on the list, count
317 * them. This allows the caller to set ifmr->ifm_count
318 * to 0 on the first call to know how much space to
321 LIST_FOREACH(ep
, &ifm
->ifm_list
, ifm_list
)
325 * Don't allow the user to ask for too many
326 * or a negative number.
328 if (ifmr
->ifm_count
> usermax
)
329 ifmr
->ifm_count
= usermax
;
330 else if (ifmr
->ifm_count
< 0)
333 if (ifmr
->ifm_count
!= 0) {
334 kptr
= (int *) _MALLOC(ifmr
->ifm_count
* sizeof(int),
338 * Get the media words from the interface's list.
340 ep
= LIST_FIRST(&ifm
->ifm_list
);
341 for (; ep
!= NULL
&& count
< ifmr
->ifm_count
;
342 ep
= LIST_NEXT(ep
, ifm_list
), count
++)
343 kptr
[count
] = ep
->ifm_media
;
346 error
= E2BIG
; /* oops! */
352 * We do the copyout on E2BIG, because that's
353 * just our way of telling userland that there
354 * are more. This is the behavior I've observed
358 if ((error
== 0 || error
== E2BIG
) && ifmr
->ifm_count
!= 0) {
359 error
= copyout((caddr_t
)kptr
,
360 CAST_USER_ADDR_T(ifmr
->ifm_ulist
),
361 ifmr
->ifm_count
* sizeof(int));
367 if (ifmr
->ifm_count
!= 0)
370 ifmr
->ifm_count
= count
;
382 * Find media entry matching a given ifm word.
385 static struct ifmedia_entry
*
386 ifmedia_match(ifm
, target
, mask
)
391 struct ifmedia_entry
*match
, *next
;
396 LIST_FOREACH(next
, &ifm
->ifm_list
, ifm_list
) {
397 if ((next
->ifm_media
& mask
) == (target
& mask
)) {
398 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
400 printf("ifmedia_match: multiple match for "
401 "0x%x/0x%x\n", target
, mask
);
412 struct ifmedia_description ifm_type_descriptions
[] =
413 IFM_TYPE_DESCRIPTIONS
;
415 struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
416 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
418 struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
419 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
421 struct ifmedia_description ifm_subtype_tokenring_descriptions
[] =
422 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS
;
424 struct ifmedia_description ifm_subtype_tokenring_option_descriptions
[] =
425 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS
;
427 struct ifmedia_description ifm_subtype_fddi_descriptions
[] =
428 IFM_SUBTYPE_FDDI_DESCRIPTIONS
;
430 struct ifmedia_description ifm_subtype_fddi_option_descriptions
[] =
431 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS
;
433 struct ifmedia_description ifm_subtype_80211_descriptions
[] =
434 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
436 struct ifmedia_description ifm_subtype_80211_option_descriptions
[] =
437 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
439 struct ifmedia_description ifm_subtype_shared_descriptions
[] =
440 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
442 struct ifmedia_description ifm_shared_option_descriptions
[] =
443 IFM_SHARED_OPTION_DESCRIPTIONS
;
445 struct ifmedia_type_to_subtype
{
446 struct ifmedia_description
*subtypes
;
447 struct ifmedia_description
*options
;
450 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
451 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
453 &ifm_subtype_ethernet_descriptions
[0],
454 &ifm_subtype_ethernet_option_descriptions
[0]
457 &ifm_subtype_tokenring_descriptions
[0],
458 &ifm_subtype_tokenring_option_descriptions
[0]
461 &ifm_subtype_fddi_descriptions
[0],
462 &ifm_subtype_fddi_option_descriptions
[0]
465 &ifm_subtype_80211_descriptions
[0],
466 &ifm_subtype_80211_option_descriptions
[0]
471 * print a media word.
474 ifmedia_printword(ifmw
)
477 struct ifmedia_description
*desc
;
478 struct ifmedia_type_to_subtype
*ttos
;
481 /* Find the top-level interface type. */
482 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
483 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
484 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
486 if (desc
->ifmt_string
== NULL
) {
487 printf("<unknown type>\n");
490 printf(desc
->ifmt_string
);
493 * Check for the shared subtype descriptions first, then the
494 * type-specific ones.
496 for (desc
= ifm_subtype_shared_descriptions
;
497 desc
->ifmt_string
!= NULL
; desc
++)
498 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
501 for (desc
= ttos
->subtypes
; desc
->ifmt_string
!= NULL
; desc
++)
502 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
504 if (desc
->ifmt_string
== NULL
) {
505 printf(" <unknown subtype>\n");
510 printf(" %s", desc
->ifmt_string
);
513 * Look for shared options.
515 for (desc
= ifm_shared_option_descriptions
;
516 desc
->ifmt_string
!= NULL
; desc
++) {
517 if (ifmw
& desc
->ifmt_word
) {
518 if (seen_option
== 0)
520 printf("%s%s", seen_option
++ ? "," : "",
526 * Look for subtype-specific options.
528 for (desc
= ttos
->options
; desc
->ifmt_string
!= NULL
; desc
++) {
529 if (ifmw
& desc
->ifmt_word
) {
530 if (seen_option
== 0)
532 printf("%s%s", seen_option
++ ? "," : "",
536 printf("%s\n", seen_option
? ">" : "");
538 #endif /* IFMEDIA_DEBUG */