2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
26 /* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks Exp $ */
30 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
32 * This software is derived from information provided by Matt Thomas.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by Jonathan Stone
45 * and Jason R. Thorpe for the NetBSD Project.
46 * 4. The names of the authors may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
56 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * BSD/OS-compatible network interface media selection.
65 * Where it is safe to do so, this code strays slightly from the BSD/OS
66 * design. Software which uses the API (device drivers, basically)
67 * shouldn't notice any difference.
69 * Many thanks to Matt Thomas for providing the information necessary
70 * to implement this interface.
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/socket.h>
76 #include <sys/sockio.h>
77 #include <sys/malloc.h>
80 #include <net/if_media.h>
83 * Compile-time options:
85 * turn on implementation-level debug printfs.
86 * Useful for debugging newly-ported drivers.
89 static struct ifmedia_entry
*ifmedia_match
__P((struct ifmedia
*ifm
,
90 int flags
, int mask
));
93 int ifmedia_debug
= 0;
94 static void ifmedia_printword
__P((int));
98 * Initialize if_media struct for a specific interface instance.
101 ifmedia_init(ifm
, dontcare_mask
, change_callback
, status_callback
)
104 ifm_change_cb_t change_callback
;
105 ifm_stat_cb_t status_callback
;
108 LIST_INIT(&ifm
->ifm_list
);
111 ifm
->ifm_mask
= dontcare_mask
; /* IF don't-care bits */
112 ifm
->ifm_change
= change_callback
;
113 ifm
->ifm_status
= status_callback
;
117 ifmedia_removeall(ifm
)
120 struct ifmedia_entry
*entry
;
122 for (entry
= LIST_FIRST(&ifm
->ifm_list
); entry
;
123 entry
= LIST_FIRST(&ifm
->ifm_list
)) {
124 LIST_REMOVE(entry
, ifm_list
);
125 FREE(entry
, M_IFADDR
);
130 * Add a media configuration to the list of supported media
131 * for a specific interface instance.
134 ifmedia_add(ifm
, mword
, data
, aux
)
140 register struct ifmedia_entry
*entry
;
145 printf("ifmedia_add: null ifm\n");
148 printf("Adding entry for ");
149 ifmedia_printword(mword
);
153 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_NOWAIT
);
155 panic("ifmedia_add: can't malloc entry");
157 entry
->ifm_media
= mword
;
158 entry
->ifm_data
= data
;
159 entry
->ifm_aux
= aux
;
161 LIST_INSERT_HEAD(&ifm
->ifm_list
, entry
, ifm_list
);
165 * Add an array of media configurations to the list of
166 * supported media for a specific interface instance.
169 ifmedia_list_add(ifm
, lp
, count
)
171 struct ifmedia_entry
*lp
;
176 for (i
= 0; i
< count
; i
++)
177 ifmedia_add(ifm
, lp
[i
].ifm_media
, lp
[i
].ifm_data
,
182 * Set the default active media.
184 * Called by device-specific code which is assumed to have already
185 * selected the default media in hardware. We do _not_ call the
186 * media-change callback.
189 ifmedia_set(ifm
, target
)
194 struct ifmedia_entry
*match
;
196 match
= ifmedia_match(ifm
, target
, ifm
->ifm_mask
);
199 printf("ifmedia_set: no match for 0x%x/0x%x\n",
200 target
, ~ifm
->ifm_mask
);
201 panic("ifmedia_set");
203 ifm
->ifm_cur
= match
;
207 printf("ifmedia_set: target ");
208 ifmedia_printword(target
);
209 printf("ifmedia_set: setting to ");
210 ifmedia_printword(ifm
->ifm_cur
->ifm_media
);
216 * Device-independent media ioctl support function.
219 ifmedia_ioctl(ifp
, ifr
, ifm
, cmd
)
225 struct ifmedia_entry
*match
;
226 struct ifmediareq
*ifmr
= (struct ifmediareq
*) ifr
;
227 int error
= 0, sticky
;
229 if (ifp
== NULL
|| ifr
== NULL
|| ifm
== NULL
)
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 printf("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.
295 struct ifmedia_entry
*ep
;
297 int usermax
; /* user requested max */
299 kptr
= NULL
; /* XXX gcc */
301 ifmr
->ifm_active
= ifmr
->ifm_current
= ifm
->ifm_cur
?
302 ifm
->ifm_cur
->ifm_media
: IFM_NONE
;
303 ifmr
->ifm_mask
= ifm
->ifm_mask
;
304 ifmr
->ifm_status
= 0;
305 (*ifm
->ifm_status
)(ifp
, ifmr
);
311 * If there are more interfaces on the list, count
312 * them. This allows the caller to set ifmr->ifm_count
313 * to 0 on the first call to know how much space to
316 LIST_FOREACH(ep
, &ifm
->ifm_list
, ifm_list
)
320 * Don't allow the user to ask for too many
321 * or a negative number.
323 if (ifmr
->ifm_count
> usermax
)
324 ifmr
->ifm_count
= usermax
;
325 else if (ifmr
->ifm_count
< 0)
328 if (ifmr
->ifm_count
!= 0) {
329 kptr
= (int *) _MALLOC(ifmr
->ifm_count
* sizeof(int),
333 * Get the media words from the interface's list.
335 ep
= LIST_FIRST(&ifm
->ifm_list
);
336 for (; ep
!= NULL
&& count
< ifmr
->ifm_count
;
337 ep
= LIST_NEXT(ep
, ifm_list
), count
++)
338 kptr
[count
] = ep
->ifm_media
;
341 error
= E2BIG
; /* oops! */
347 * We do the copyout on E2BIG, because that's
348 * just our way of telling userland that there
349 * are more. This is the behavior I've observed
353 if ((error
== 0 || error
== E2BIG
) && ifmr
->ifm_count
!= 0) {
354 error
= copyout((caddr_t
)kptr
,
355 (caddr_t
)ifmr
->ifm_ulist
,
356 ifmr
->ifm_count
* sizeof(int));
362 if (ifmr
->ifm_count
!= 0)
365 ifmr
->ifm_count
= count
;
377 * Find media entry matching a given ifm word.
380 static struct ifmedia_entry
*
381 ifmedia_match(ifm
, target
, mask
)
386 struct ifmedia_entry
*match
, *next
;
391 LIST_FOREACH(next
, &ifm
->ifm_list
, ifm_list
) {
392 if ((next
->ifm_media
& mask
) == (target
& mask
)) {
393 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
395 printf("ifmedia_match: multiple match for "
396 "0x%x/0x%x\n", target
, mask
);
407 struct ifmedia_description ifm_type_descriptions
[] =
408 IFM_TYPE_DESCRIPTIONS
;
410 struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
411 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
413 struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
414 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
416 struct ifmedia_description ifm_subtype_tokenring_descriptions
[] =
417 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS
;
419 struct ifmedia_description ifm_subtype_tokenring_option_descriptions
[] =
420 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS
;
422 struct ifmedia_description ifm_subtype_fddi_descriptions
[] =
423 IFM_SUBTYPE_FDDI_DESCRIPTIONS
;
425 struct ifmedia_description ifm_subtype_fddi_option_descriptions
[] =
426 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS
;
428 struct ifmedia_description ifm_subtype_80211_descriptions
[] =
429 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
431 struct ifmedia_description ifm_subtype_80211_option_descriptions
[] =
432 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
434 struct ifmedia_description ifm_subtype_shared_descriptions
[] =
435 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
437 struct ifmedia_description ifm_shared_option_descriptions
[] =
438 IFM_SHARED_OPTION_DESCRIPTIONS
;
440 struct ifmedia_type_to_subtype
{
441 struct ifmedia_description
*subtypes
;
442 struct ifmedia_description
*options
;
445 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
446 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
448 &ifm_subtype_ethernet_descriptions
[0],
449 &ifm_subtype_ethernet_option_descriptions
[0]
452 &ifm_subtype_tokenring_descriptions
[0],
453 &ifm_subtype_tokenring_option_descriptions
[0]
456 &ifm_subtype_fddi_descriptions
[0],
457 &ifm_subtype_fddi_option_descriptions
[0]
460 &ifm_subtype_80211_descriptions
[0],
461 &ifm_subtype_80211_option_descriptions
[0]
466 * print a media word.
469 ifmedia_printword(ifmw
)
472 struct ifmedia_description
*desc
;
473 struct ifmedia_type_to_subtype
*ttos
;
476 /* Find the top-level interface type. */
477 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
478 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
479 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
481 if (desc
->ifmt_string
== NULL
) {
482 printf("<unknown type>\n");
485 printf(desc
->ifmt_string
);
488 * Check for the shared subtype descriptions first, then the
489 * type-specific ones.
491 for (desc
= ifm_subtype_shared_descriptions
;
492 desc
->ifmt_string
!= NULL
; desc
++)
493 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
496 for (desc
= ttos
->subtypes
; desc
->ifmt_string
!= NULL
; desc
++)
497 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
499 if (desc
->ifmt_string
== NULL
) {
500 printf(" <unknown subtype>\n");
505 printf(" %s", desc
->ifmt_string
);
508 * Look for shared options.
510 for (desc
= ifm_shared_option_descriptions
;
511 desc
->ifmt_string
!= NULL
; desc
++) {
512 if (ifmw
& desc
->ifmt_word
) {
513 if (seen_option
== 0)
515 printf("%s%s", seen_option
++ ? "," : "",
521 * Look for subtype-specific options.
523 for (desc
= ttos
->options
; desc
->ifmt_string
!= NULL
; desc
++) {
524 if (ifmw
& desc
->ifmt_word
) {
525 if (seen_option
== 0)
527 printf("%s%s", seen_option
++ ? "," : "",
531 printf("%s\n", seen_option
? ">" : "");
533 #endif /* IFMEDIA_DEBUG */