1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
2 /* $Id: if_media.c,v 1.5 1998/02/06 12:13:48 eivind 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_media.h>
59 * Compile-time options:
61 * turn on implementation-level debug printfs.
62 * Useful for debugging newly-ported drivers.
65 static struct ifmedia_entry *ifmedia_match __P((struct ifmedia *ifm,
66 int flags, int mask));
69 int ifmedia_debug = 0;
70 static void ifmedia_printword __P((int));
74 * Initialize if_media struct for a specific interface instance.
77 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
80 ifm_change_cb_t change_callback;
81 ifm_stat_cb_t status_callback;
84 LIST_INIT(&ifm->ifm_list);
87 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
88 ifm->ifm_change = change_callback;
89 ifm->ifm_status = status_callback;
93 * Add a media configuration to the list of supported media
94 * for a specific interface instance.
97 ifmedia_add(ifm, mword, data, aux)
103 register struct ifmedia_entry *entry;
108 printf("ifmedia_add: null ifm\n");
111 printf("Adding entry for ");
112 ifmedia_printword(mword);
116 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
118 panic("ifmedia_add: can't malloc entry");
120 entry->ifm_media = mword;
121 entry->ifm_data = data;
122 entry->ifm_aux = aux;
124 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
128 * Add an array of media configurations to the list of
129 * supported media for a specific interface instance.
132 ifmedia_list_add(ifm, lp, count)
134 struct ifmedia_entry *lp;
139 for (i = 0; i < count; i++)
140 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
145 * Set the default active media.
147 * Called by device-specific code which is assumed to have already
148 * selected the default media in hardware. We do _not_ call the
149 * media-change callback.
152 ifmedia_set(ifm, target)
157 struct ifmedia_entry *match;
159 match = ifmedia_match(ifm, target, ifm->ifm_mask);
162 printf("ifmedia_set: no match for 0x%x/0x%x\n",
163 target, ~ifm->ifm_mask);
164 panic("ifmedia_set");
166 ifm->ifm_cur = match;
170 printf("ifmedia_set: target ");
171 ifmedia_printword(target);
172 printf("ifmedia_set: setting to ");
173 ifmedia_printword(ifm->ifm_cur->ifm_media);
179 * Device-independent media ioctl support function.
182 ifmedia_ioctl(ifp, ifr, ifm, cmd)
188 struct ifmedia_entry *match;
189 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
190 int error = 0, sticky;
192 if (ifp == NULL || ifr == NULL || ifm == NULL)
198 * Set the current media.
202 struct ifmedia_entry *oldentry;
204 int newmedia = ifr->ifr_media;
206 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
211 "ifmedia_ioctl: no media found for 0x%x\n",
219 * If no change, we're done.
220 * XXX Automedia may invole software intervention.
221 * Keep going in case the the connected media changed.
222 * Similarly, if best match changed (kernel debugger?).
224 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
225 (newmedia == ifm->ifm_media) &&
226 (match == ifm->ifm_cur))
230 * We found a match, now make the driver switch to it.
231 * Make sure to preserve our old media type in case the
232 * driver can't switch.
236 printf("ifmedia_ioctl: switching %s to ",
238 ifmedia_printword(match->ifm_media);
241 oldentry = ifm->ifm_cur;
242 oldmedia = ifm->ifm_media;
243 ifm->ifm_cur = match;
244 ifm->ifm_media = newmedia;
245 error = (*ifm->ifm_change)(ifp);
247 ifm->ifm_cur = oldentry;
248 ifm->ifm_media = oldmedia;
254 * Get list of available media and current media on interface.
258 struct ifmedia_entry *ep;
261 kptr = NULL; /* XXX gcc */
263 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
264 ifm->ifm_cur->ifm_media : IFM_NONE;
265 ifmr->ifm_mask = ifm->ifm_mask;
266 ifmr->ifm_status = 0;
267 (*ifm->ifm_status)(ifp, ifmr);
270 ep = ifm->ifm_list.lh_first;
272 if (ifmr->ifm_count != 0) {
273 kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
277 * Get the media words from the interface's list.
279 for (; ep != NULL && count < ifmr->ifm_count;
280 ep = ep->ifm_list.le_next, count++)
281 kptr[count] = ep->ifm_media;
284 error = E2BIG; /* oops! */
288 * If there are more interfaces on the list, count
289 * them. This allows the caller to set ifmr->ifm_count
290 * to 0 on the first call to know how much space to
293 for (; ep != NULL; ep = ep->ifm_list.le_next)
297 * We do the copyout on E2BIG, because that's
298 * just our way of telling userland that there
299 * are more. This is the behavior I've observed
303 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
304 error = copyout((caddr_t)kptr,
305 (caddr_t)ifmr->ifm_ulist,
306 ifmr->ifm_count * sizeof(int));
312 if (ifmr->ifm_count != 0)
315 ifmr->ifm_count = count;
327 * Find media entry matching a given ifm word.
330 static struct ifmedia_entry *
331 ifmedia_match(ifm, target, mask)
336 struct ifmedia_entry *match, *next;
341 for (next = ifm->ifm_list.lh_first; next != NULL;
342 next = next->ifm_list.le_next) {
343 if ((next->ifm_media & mask) == (target & mask)) {
344 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
346 printf("ifmedia_match: multiple match for "
347 "0x%x/0x%x\n", target, mask);
358 struct ifmedia_description ifm_type_descriptions[] =
359 IFM_TYPE_DESCRIPTIONS;
361 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
362 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
364 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
365 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
367 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
368 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
370 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
371 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
373 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
374 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
376 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
377 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
379 struct ifmedia_description ifm_subtype_shared_descriptions[] =
380 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
382 struct ifmedia_description ifm_shared_option_descriptions[] =
383 IFM_SHARED_OPTION_DESCRIPTIONS;
385 struct ifmedia_type_to_subtype {
386 struct ifmedia_description *subtypes;
387 struct ifmedia_description *options;
390 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
391 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
393 &ifm_subtype_ethernet_descriptions[0],
394 &ifm_subtype_ethernet_option_descriptions[0]
397 &ifm_subtype_tokenring_descriptions[0],
398 &ifm_subtype_tokenring_option_descriptions[0]
401 &ifm_subtype_fddi_descriptions[0],
402 &ifm_subtype_fddi_option_descriptions[0]
407 * print a media word.
410 ifmedia_printword(ifmw)
413 struct ifmedia_description *desc;
414 struct ifmedia_type_to_subtype *ttos;
417 /* Find the top-level interface type. */
418 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
419 desc->ifmt_string != NULL; desc++, ttos++)
420 if (IFM_TYPE(ifmw) == desc->ifmt_word)
422 if (desc->ifmt_string == NULL) {
423 printf("<unknown type>\n");
426 printf(desc->ifmt_string);
429 * Check for the shared subtype descriptions first, then the
430 * type-specific ones.
432 for (desc = ifm_subtype_shared_descriptions;
433 desc->ifmt_string != NULL; desc++)
434 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
437 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
438 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
440 if (desc->ifmt_string == NULL) {
441 printf(" <unknown subtype>\n");
446 printf(" %s", desc->ifmt_string);
449 * Look for shared options.
451 for (desc = ifm_shared_option_descriptions;
452 desc->ifmt_string != NULL; desc++) {
453 if (ifmw & desc->ifmt_word) {
454 if (seen_option == 0)
456 printf("%s%s", seen_option++ ? "," : "",
462 * Look for subtype-specific options.
464 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
465 if (ifmw & desc->ifmt_word) {
466 if (seen_option == 0)
468 printf("%s%s", seen_option++ ? "," : "",
472 printf("%s\n", seen_option ? ">" : "");
474 #endif /* IFMEDIA_DEBUG */