2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
26 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
28 * This software is derived from information provided by Matt Thomas.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Jonathan Stone
41 * and Jason R. Thorpe for the NetBSD Project.
42 * 4. The names of the authors may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
50 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
52 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * BSD/OS-compatible network interface media selection.
61 * Where it is safe to do so, this code strays slightly from the BSD/OS
62 * design. Software which uses the API (device drivers, basically)
63 * shouldn't notice any difference.
65 * Many thanks to Matt Thomas for providing the information necessary
66 * to implement this interface.
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/malloc.h>
76 #include <net/if_media.h>
79 * Compile-time options:
81 * turn on implementation-level debug printfs.
82 * Useful for debugging newly-ported drivers.
85 static struct ifmedia_entry
*ifmedia_match
__P((struct ifmedia
*ifm
,
86 int flags
, int mask
));
89 int ifmedia_debug
= 0;
90 static void ifmedia_printword
__P((int));
94 * Initialize if_media struct for a specific interface instance.
97 ifmedia_init(ifm
, dontcare_mask
, change_callback
, status_callback
)
100 ifm_change_cb_t change_callback
;
101 ifm_stat_cb_t status_callback
;
104 LIST_INIT(&ifm
->ifm_list
);
107 ifm
->ifm_mask
= dontcare_mask
; /* IF don't-care bits */
108 ifm
->ifm_change
= change_callback
;
109 ifm
->ifm_status
= status_callback
;
113 * Add a media configuration to the list of supported media
114 * for a specific interface instance.
117 ifmedia_add(ifm
, mword
, data
, aux
)
123 register struct ifmedia_entry
*entry
;
128 printf("ifmedia_add: null ifm\n");
131 printf("Adding entry for ");
132 ifmedia_printword(mword
);
136 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
);
138 panic("ifmedia_add: can't malloc entry");
140 entry
->ifm_media
= mword
;
141 entry
->ifm_data
= data
;
142 entry
->ifm_aux
= aux
;
144 LIST_INSERT_HEAD(&ifm
->ifm_list
, entry
, ifm_list
);
148 * Add an array of media configurations to the list of
149 * supported media for a specific interface instance.
152 ifmedia_list_add(ifm
, lp
, count
)
154 struct ifmedia_entry
*lp
;
159 for (i
= 0; i
< count
; i
++)
160 ifmedia_add(ifm
, lp
[i
].ifm_media
, lp
[i
].ifm_data
,
165 * Set the default active media.
167 * Called by device-specific code which is assumed to have already
168 * selected the default media in hardware. We do _not_ call the
169 * media-change callback.
172 ifmedia_set(ifm
, target
)
177 struct ifmedia_entry
*match
;
179 match
= ifmedia_match(ifm
, target
, ifm
->ifm_mask
);
182 printf("ifmedia_set: no match for 0x%x/0x%x\n",
183 target
, ~ifm
->ifm_mask
);
184 panic("ifmedia_set");
186 ifm
->ifm_cur
= match
;
190 printf("ifmedia_set: target ");
191 ifmedia_printword(target
);
192 printf("ifmedia_set: setting to ");
193 ifmedia_printword(ifm
->ifm_cur
->ifm_media
);
199 * Device-independent media ioctl support function.
202 ifmedia_ioctl(ifp
, ifr
, ifm
, cmd
)
208 struct ifmedia_entry
*match
;
209 struct ifmediareq
*ifmr
= (struct ifmediareq
*) ifr
;
210 int error
= 0, sticky
;
212 if (ifp
== NULL
|| ifr
== NULL
|| ifm
== NULL
)
218 * Set the current media.
222 struct ifmedia_entry
*oldentry
;
224 int newmedia
= ifr
->ifr_media
;
226 match
= ifmedia_match(ifm
, newmedia
, ifm
->ifm_mask
);
231 "ifmedia_ioctl: no media found for 0x%x\n",
239 * If no change, we're done.
240 * XXX Automedia may invole software intervention.
241 * Keep going in case the the connected media changed.
242 * Similarly, if best match changed (kernel debugger?).
244 if ((IFM_SUBTYPE(newmedia
) != IFM_AUTO
) &&
245 (newmedia
== ifm
->ifm_media
) &&
246 (match
== ifm
->ifm_cur
))
250 * We found a match, now make the driver switch to it.
251 * Make sure to preserve our old media type in case the
252 * driver can't switch.
256 printf("ifmedia_ioctl: switching %s to ",
258 ifmedia_printword(match
->ifm_media
);
261 oldentry
= ifm
->ifm_cur
;
262 oldmedia
= ifm
->ifm_media
;
263 ifm
->ifm_cur
= match
;
264 ifm
->ifm_media
= newmedia
;
265 error
= (*ifm
->ifm_change
)(ifp
);
267 ifm
->ifm_cur
= oldentry
;
268 ifm
->ifm_media
= oldmedia
;
274 * Get list of available media and current media on interface.
278 struct ifmedia_entry
*ep
;
281 kptr
= NULL
; /* XXX gcc */
283 ifmr
->ifm_active
= ifmr
->ifm_current
= ifm
->ifm_cur
?
284 ifm
->ifm_cur
->ifm_media
: IFM_NONE
;
285 ifmr
->ifm_mask
= ifm
->ifm_mask
;
286 ifmr
->ifm_status
= 0;
287 (*ifm
->ifm_status
)(ifp
, ifmr
);
290 ep
= ifm
->ifm_list
.lh_first
;
292 if (ifmr
->ifm_count
!= 0) {
293 kptr
= (int *) _MALLOC(ifmr
->ifm_count
* sizeof(int),
297 * Get the media words from the interface's list.
299 for (; ep
!= NULL
&& count
< ifmr
->ifm_count
;
300 ep
= ep
->ifm_list
.le_next
, count
++)
301 kptr
[count
] = ep
->ifm_media
;
304 error
= E2BIG
; /* oops! */
308 * If there are more interfaces on the list, count
309 * them. This allows the caller to set ifmr->ifm_count
310 * to 0 on the first call to know how much space to
313 for (; ep
!= NULL
; ep
= ep
->ifm_list
.le_next
)
317 * We do the copyout on E2BIG, because that's
318 * just our way of telling userland that there
319 * are more. This is the behavior I've observed
323 if ((error
== 0 || error
== E2BIG
) && ifmr
->ifm_count
!= 0) {
324 error
= copyout((caddr_t
)kptr
,
325 (caddr_t
)ifmr
->ifm_ulist
,
326 ifmr
->ifm_count
* sizeof(int));
332 if (ifmr
->ifm_count
!= 0)
335 ifmr
->ifm_count
= count
;
347 * Find media entry matching a given ifm word.
350 static struct ifmedia_entry
*
351 ifmedia_match(ifm
, target
, mask
)
356 struct ifmedia_entry
*match
, *next
;
361 for (next
= ifm
->ifm_list
.lh_first
; next
!= NULL
;
362 next
= next
->ifm_list
.le_next
) {
363 if ((next
->ifm_media
& mask
) == (target
& mask
)) {
364 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
366 printf("ifmedia_match: multiple match for "
367 "0x%x/0x%x\n", target
, mask
);
378 struct ifmedia_description ifm_type_descriptions
[] =
379 IFM_TYPE_DESCRIPTIONS
;
381 struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
382 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
384 struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
385 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
387 struct ifmedia_description ifm_subtype_tokenring_descriptions
[] =
388 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS
;
390 struct ifmedia_description ifm_subtype_tokenring_option_descriptions
[] =
391 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS
;
393 struct ifmedia_description ifm_subtype_fddi_descriptions
[] =
394 IFM_SUBTYPE_FDDI_DESCRIPTIONS
;
396 struct ifmedia_description ifm_subtype_fddi_option_descriptions
[] =
397 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS
;
399 struct ifmedia_description ifm_subtype_80211_descriptions
[] =
400 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
402 struct ifmedia_description ifm_subtype_80211_option_descriptions
[] =
403 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
405 struct ifmedia_description ifm_subtype_shared_descriptions
[] =
406 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
408 struct ifmedia_description ifm_shared_option_descriptions
[] =
409 IFM_SHARED_OPTION_DESCRIPTIONS
;
411 struct ifmedia_type_to_subtype
{
412 struct ifmedia_description
*subtypes
;
413 struct ifmedia_description
*options
;
416 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
417 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
419 &ifm_subtype_ethernet_descriptions
[0],
420 &ifm_subtype_ethernet_option_descriptions
[0]
423 &ifm_subtype_tokenring_descriptions
[0],
424 &ifm_subtype_tokenring_option_descriptions
[0]
427 &ifm_subtype_fddi_descriptions
[0],
428 &ifm_subtype_fddi_option_descriptions
[0]
431 &ifm_subtype_80211_descriptions
[0],
432 &ifm_subtype_80211_option_descriptions
[0]
437 * print a media word.
440 ifmedia_printword(ifmw
)
443 struct ifmedia_description
*desc
;
444 struct ifmedia_type_to_subtype
*ttos
;
447 /* Find the top-level interface type. */
448 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
449 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
450 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
452 if (desc
->ifmt_string
== NULL
) {
453 printf("<unknown type>\n");
456 printf(desc
->ifmt_string
);
459 * Check for the shared subtype descriptions first, then the
460 * type-specific ones.
462 for (desc
= ifm_subtype_shared_descriptions
;
463 desc
->ifmt_string
!= NULL
; desc
++)
464 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
467 for (desc
= ttos
->subtypes
; desc
->ifmt_string
!= NULL
; desc
++)
468 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
470 if (desc
->ifmt_string
== NULL
) {
471 printf(" <unknown subtype>\n");
476 printf(" %s", desc
->ifmt_string
);
479 * Look for shared options.
481 for (desc
= ifm_shared_option_descriptions
;
482 desc
->ifmt_string
!= NULL
; desc
++) {
483 if (ifmw
& desc
->ifmt_word
) {
484 if (seen_option
== 0)
486 printf("%s%s", seen_option
++ ? "," : "",
492 * Look for subtype-specific options.
494 for (desc
= ttos
->options
; desc
->ifmt_string
!= NULL
; desc
++) {
495 if (ifmw
& desc
->ifmt_word
) {
496 if (seen_option
== 0)
498 printf("%s%s", seen_option
++ ? "," : "",
502 printf("%s\n", seen_option
? ">" : "");
504 #endif /* IFMEDIA_DEBUG */