3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
46 #include <sys/module.h>
47 #if __FreeBSD_version < 502113
48 #include <sys/sysctl.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/route.h>
60 #include <machine/bus_memio.h>
61 #include <machine/bus_pio.h>
62 #include <machine/bus.h>
63 #include <machine/resource.h>
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_ioctl.h>
70 #include <dev/wi/if_wavelan_ieee.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pcivar.h>
75 #include <compat/ndis/pe_var.h>
76 #include <compat/ndis/resource_var.h>
77 #include <compat/ndis/ntoskrnl_var.h>
78 #include <compat/ndis/hal_var.h>
79 #include <compat/ndis/ndis_var.h>
80 #include <compat/ndis/cfg_var.h>
81 #include <dev/if_ndis/if_ndisvar.h>
86 #include "ndis_driver_data.h"
88 int ndis_attach (device_t);
89 int ndis_detach (device_t);
90 int ndis_suspend (device_t);
91 int ndis_resume (device_t);
92 void ndis_shutdown (device_t);
94 int ndisdrv_modevent (module_t, int, void *);
96 static __stdcall void ndis_txeof (ndis_handle,
97 ndis_packet *, ndis_status);
98 static __stdcall void ndis_rxeof (ndis_handle,
99 ndis_packet **, uint32_t);
100 static __stdcall void ndis_linksts (ndis_handle,
101 ndis_status, void *, uint32_t);
102 static __stdcall void ndis_linksts_done (ndis_handle);
104 /* We need to wrap these functions for amd64. */
106 static funcptr ndis_txeof_wrap;
107 static funcptr ndis_rxeof_wrap;
108 static funcptr ndis_linksts_wrap;
109 static funcptr ndis_linksts_done_wrap;
111 static void ndis_intr (void *);
112 static void ndis_intrtask (void *);
113 static void ndis_tick (void *);
114 static void ndis_ticktask (void *);
115 static void ndis_start (struct ifnet *);
116 static void ndis_starttask (void *);
117 static int ndis_ioctl (struct ifnet *, u_long, caddr_t);
118 static int ndis_wi_ioctl_get (struct ifnet *, u_long, caddr_t);
119 static int ndis_wi_ioctl_set (struct ifnet *, u_long, caddr_t);
120 static int ndis_80211_ioctl_get (struct ifnet *, u_long, caddr_t);
121 static int ndis_80211_ioctl_set (struct ifnet *, u_long, caddr_t);
122 static void ndis_init (void *);
123 static void ndis_stop (struct ndis_softc *);
124 static void ndis_watchdog (struct ifnet *);
125 static int ndis_ifmedia_upd (struct ifnet *);
126 static void ndis_ifmedia_sts (struct ifnet *, struct ifmediareq *);
127 static int ndis_get_assoc (struct ndis_softc *, ndis_wlan_bssid_ex **);
128 static int ndis_probe_offload (struct ndis_softc *);
129 static int ndis_set_offload (struct ndis_softc *);
130 static void ndis_getstate_80211 (struct ndis_softc *);
131 static void ndis_setstate_80211 (struct ndis_softc *);
132 static void ndis_media_status (struct ifnet *, struct ifmediareq *);
134 static void ndis_setmulti (struct ndis_softc *);
135 static void ndis_map_sclist (void *, bus_dma_segment_t *,
136 int, bus_size_t, int);
138 static int ndisdrv_loaded = 0;
141 * This routine should call windrv_load() once for each driver
142 * image. This will do the relocation and dynalinking for the
143 * image, and create a Windows driver object which will be
144 * saved in our driver database.
148 ndisdrv_modevent(mod, cmd, arg)
158 if (ndisdrv_loaded > 1)
160 windrv_load(mod, (vm_offset_t)drv_data, 0);
161 windrv_wrap((funcptr)ndis_rxeof, &ndis_rxeof_wrap);
162 windrv_wrap((funcptr)ndis_txeof, &ndis_txeof_wrap);
163 windrv_wrap((funcptr)ndis_linksts, &ndis_linksts_wrap);
164 windrv_wrap((funcptr)ndis_linksts_done,
165 &ndis_linksts_done_wrap);
169 if (ndisdrv_loaded > 0)
171 windrv_unload(mod, (vm_offset_t)drv_data, 0);
172 windrv_unwrap(ndis_rxeof_wrap);
173 windrv_unwrap(ndis_txeof_wrap);
174 windrv_unwrap(ndis_linksts_wrap);
175 windrv_unwrap(ndis_linksts_done_wrap);
178 windrv_unwrap(ndis_rxeof_wrap);
179 windrv_unwrap(ndis_txeof_wrap);
180 windrv_unwrap(ndis_linksts_wrap);
181 windrv_unwrap(ndis_linksts_done_wrap);
192 * Program the 64-bit multicast hash filter.
196 struct ndis_softc *sc;
199 struct ifmultiaddr *ifma;
200 int len, mclistsz, error;
203 ifp = &sc->arpcom.ac_if;
205 if (!NDIS_INITIALIZED(sc))
208 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
209 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
210 len = sizeof(sc->ndis_filter);
211 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
212 &sc->ndis_filter, &len);
214 device_printf (sc->ndis_dev,
215 "set filter failed: %d\n", error);
219 if (TAILQ_EMPTY(&ifp->if_multiaddrs))
222 len = sizeof(mclistsz);
223 ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
225 mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
227 if (mclist == NULL) {
228 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
232 sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
235 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
236 if (ifma->ifma_addr->sa_family != AF_LINK)
238 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
239 mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
241 if (len > mclistsz) {
242 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
243 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
248 len = len * ETHER_ADDR_LEN;
249 error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
251 device_printf (sc->ndis_dev, "set mclist failed: %d\n", error);
252 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
253 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
257 free(mclist, M_TEMP);
259 len = sizeof(sc->ndis_filter);
260 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
261 &sc->ndis_filter, &len);
263 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
270 struct ndis_softc *sc;
272 ndis_task_offload *nto;
273 ndis_task_offload_hdr *ntoh;
274 ndis_task_tcpip_csum *nttc;
278 ifp = &sc->arpcom.ac_if;
280 if (!NDIS_INITIALIZED(sc))
283 /* See if there's anything to set. */
285 error = ndis_probe_offload(sc);
289 if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
292 len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
293 sizeof(ndis_task_tcpip_csum);
295 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
300 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
301 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
302 ntoh->ntoh_offset_firsttask = sizeof(ndis_task_offload_hdr);
303 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
304 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
305 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
307 nto = (ndis_task_offload *)((char *)ntoh +
308 ntoh->ntoh_offset_firsttask);
310 nto->nto_vers = NDIS_TASK_OFFLOAD_VERSION;
311 nto->nto_len = sizeof(ndis_task_offload);
312 nto->nto_task = NDIS_TASK_TCPIP_CSUM;
313 nto->nto_offset_nexttask = 0;
314 nto->nto_taskbuflen = sizeof(ndis_task_tcpip_csum);
316 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
318 if (ifp->if_capenable & IFCAP_TXCSUM)
319 nttc->nttc_v4tx = sc->ndis_v4tx;
321 if (ifp->if_capenable & IFCAP_RXCSUM)
322 nttc->nttc_v4rx = sc->ndis_v4rx;
324 error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
331 ndis_probe_offload(sc)
332 struct ndis_softc *sc;
334 ndis_task_offload *nto;
335 ndis_task_offload_hdr *ntoh;
336 ndis_task_tcpip_csum *nttc = NULL;
338 int len, error, dummy;
340 ifp = &sc->arpcom.ac_if;
343 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
348 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
353 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
354 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
355 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
356 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
357 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
359 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
366 if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
371 nto = (ndis_task_offload *)((char *)ntoh +
372 ntoh->ntoh_offset_firsttask);
375 switch (nto->nto_task) {
376 case NDIS_TASK_TCPIP_CSUM:
377 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
379 /* Don't handle these yet. */
380 case NDIS_TASK_IPSEC:
381 case NDIS_TASK_TCP_LARGESEND:
385 if (nto->nto_offset_nexttask == 0)
387 nto = (ndis_task_offload *)((char *)nto +
388 nto->nto_offset_nexttask);
396 sc->ndis_v4tx = nttc->nttc_v4tx;
397 sc->ndis_v4rx = nttc->nttc_v4rx;
399 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_IP_CSUM)
400 sc->ndis_hwassist |= CSUM_IP;
401 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
402 sc->ndis_hwassist |= CSUM_TCP;
403 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
404 sc->ndis_hwassist |= CSUM_UDP;
406 if (sc->ndis_hwassist)
407 ifp->if_capabilities |= IFCAP_TXCSUM;
409 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_IP_CSUM)
410 ifp->if_capabilities |= IFCAP_RXCSUM;
411 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
412 ifp->if_capabilities |= IFCAP_RXCSUM;
413 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
414 ifp->if_capabilities |= IFCAP_RXCSUM;
421 * Attach the interface. Allocate softc structures, do ifmedia
422 * setup and ethernet/BPF attach.
428 u_char eaddr[ETHER_ADDR_LEN];
429 struct ndis_softc *sc;
432 struct ifnet *ifp = NULL;
437 sc = device_get_softc(dev);
439 mtx_init(&sc->ndis_mtx, "ndis softc lock",
440 MTX_NETWORK_LOCK, MTX_DEF);
443 * Hook interrupt early, since calling the driver's
444 * init routine may trigger an interrupt.
447 error = bus_setup_intr(dev, sc->ndis_irq, INTR_TYPE_NET | INTR_MPSAFE,
448 ndis_intr, sc, &sc->ndis_intrhand);
451 device_printf(dev, "couldn't set up irq\n");
455 if (sc->ndis_iftype == PCMCIABus) {
456 error = ndis_alloc_amem(sc);
458 device_printf(dev, "failed to allocate "
459 "attribute memory\n");
464 sc->ndis_regvals = ndis_regvals;
466 #if __FreeBSD_version < 502113
467 sysctl_ctx_init(&sc->ndis_ctx);
470 /* Create sysctl registry nodes */
471 ndis_create_sysctls(sc);
474 * Create a new functional device object for this
475 * device. This is what creates the miniport block
476 * for this device instance.
480 drv = windrv_lookup((vm_offset_t)img);
481 pdo = windrv_find_pdo(drv, dev);
482 if (NdisAddDevice(drv, pdo) != STATUS_SUCCESS) {
483 device_printf(dev, "failed to create FDO!\n");
488 /* Tell the user what version of the API the driver is using. */
489 device_printf(dev, "NDIS API version: %d.%d\n",
490 sc->ndis_chars->nmc_version_major,
491 sc->ndis_chars->nmc_version_minor);
493 /* Do resource conversion. */
494 ndis_convert_res(sc);
496 /* Install our RX and TX interrupt handlers. */
497 sc->ndis_block->nmb_senddone_func = ndis_txeof_wrap;
498 sc->ndis_block->nmb_pktind_func = ndis_rxeof_wrap;
500 /* Call driver's init routine. */
501 if (ndis_init_nic(sc)) {
502 device_printf (dev, "init handler failed\n");
508 * Get station address from the driver.
511 ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
513 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
516 * Figure out if we're allowed to use multipacket sends
517 * with this driver, and if so, how many.
520 if (sc->ndis_chars->nmc_sendsingle_func &&
521 sc->ndis_chars->nmc_sendmulti_func == NULL) {
522 sc->ndis_maxpkts = 1;
524 len = sizeof(sc->ndis_maxpkts);
525 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
526 &sc->ndis_maxpkts, &len);
529 sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
530 sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
532 /* Allocate a pool of ndis_packets for TX encapsulation. */
534 NdisAllocatePacketPool(&i, &sc->ndis_txpool,
535 sc->ndis_maxpkts, PROTOCOL_RESERVED_SIZE_IN_PACKET);
537 if (i != NDIS_STATUS_SUCCESS) {
538 sc->ndis_txpool = NULL;
539 device_printf(dev, "failed to allocate TX packet pool");
544 sc->ndis_txpending = sc->ndis_maxpkts;
547 /* Get supported oid list. */
548 ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
550 /* If the NDIS module requested scatter/gather, init maps. */
555 * See if the OID_802_11_CONFIGURATION OID is
556 * supported by this driver. If it is, then this an 802.11
557 * wireless driver, and we should set up media for wireless.
559 for (i = 0; i < sc->ndis_oidcnt; i++) {
560 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
566 /* Check for task offload support. */
567 ndis_probe_offload(sc);
569 ifp = &sc->arpcom.ac_if;
571 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
572 ifp->if_mtu = ETHERMTU;
573 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
574 ifp->if_ioctl = ndis_ioctl;
575 ifp->if_start = ndis_start;
576 ifp->if_watchdog = ndis_watchdog;
577 ifp->if_init = ndis_init;
578 ifp->if_baudrate = 10000000;
579 #if __FreeBSD_version < 502114
580 ifp->if_snd.ifq_maxlen = 50;
582 IFQ_SET_MAXLEN(&ifp->if_snd, 50);
583 ifp->if_snd.ifq_drv_maxlen = 25;
584 IFQ_SET_READY(&ifp->if_snd);
586 ifp->if_capenable = ifp->if_capabilities;
587 ifp->if_hwassist = sc->ndis_hwassist;
590 if (sc->ndis_80211) {
591 struct ieee80211com *ic = (void *)&sc->ic;
592 ndis_80211_rates_ex rates;
593 struct ndis_80211_nettype_list *ntl;
598 ic->ic_phytype = IEEE80211_T_DS;
599 ic->ic_opmode = IEEE80211_M_STA;
600 ic->ic_caps = IEEE80211_C_IBSS;
601 ic->ic_state = IEEE80211_S_ASSOC;
602 ic->ic_modecaps = (1<<IEEE80211_MODE_AUTO);
604 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
608 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
609 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
616 for (i = 0; i < ntl->ntl_items; i++) {
617 switch (ntl->ntl_type[i]) {
618 case NDIS_80211_NETTYPE_11FH:
619 case NDIS_80211_NETTYPE_11DS:
620 ic->ic_modecaps |= (1<<IEEE80211_MODE_11B);
622 case NDIS_80211_NETTYPE_11OFDM5:
623 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
625 case NDIS_80211_NETTYPE_11OFDM24:
626 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
635 bzero((char *)&rates, len);
636 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
637 (void *)rates, &len);
639 device_printf (dev, "get rates failed: 0x%x\n", r);
641 * Since the supported rates only up to 8 can be supported,
642 * if this is not 802.11b we're just going to be faking it
646 #define TESTSETRATE(x, y) \
649 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) { \
650 if (ic->ic_sup_rates[x].rs_rates[i] == (y)) \
653 if (i == ic->ic_sup_rates[x].rs_nrates) { \
654 ic->ic_sup_rates[x].rs_rates[i] = (y); \
655 ic->ic_sup_rates[x].rs_nrates++; \
659 #define SETRATE(x, y) \
660 ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
662 ic->ic_sup_rates[x].rs_nrates++
664 ic->ic_curmode = IEEE80211_MODE_AUTO;
665 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A))
666 ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
667 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B))
668 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
669 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G))
670 ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
671 for (i = 0; i < len; i++) {
672 switch (rates[i] & IEEE80211_RATE_VAL) {
678 if (!(ic->ic_modecaps &
679 (1<<IEEE80211_MODE_11B))) {
680 /* Lazy-init 802.11b. */
682 (1<<IEEE80211_MODE_11B);
683 ic->ic_sup_rates[IEEE80211_MODE_11B].
686 SETRATE(IEEE80211_MODE_11B, rates[i]);
687 INCRATE(IEEE80211_MODE_11B);
690 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
691 SETRATE(IEEE80211_MODE_11A, rates[i]);
692 INCRATE(IEEE80211_MODE_11A);
694 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
695 SETRATE(IEEE80211_MODE_11G, rates[i]);
696 INCRATE(IEEE80211_MODE_11G);
703 * If the hardware supports 802.11g, it most
704 * likely supports 802.11b and all of the
705 * 802.11b and 802.11g speeds, so maybe we can
706 * just cheat here. Just how in the heck do
707 * we detect turbo modes, though?
709 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
710 TESTSETRATE(IEEE80211_MODE_11B,
711 IEEE80211_RATE_BASIC|2);
712 TESTSETRATE(IEEE80211_MODE_11B,
713 IEEE80211_RATE_BASIC|4);
714 TESTSETRATE(IEEE80211_MODE_11B,
715 IEEE80211_RATE_BASIC|11);
716 TESTSETRATE(IEEE80211_MODE_11B,
717 IEEE80211_RATE_BASIC|22);
719 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
720 TESTSETRATE(IEEE80211_MODE_11G, 47);
721 TESTSETRATE(IEEE80211_MODE_11G, 72);
722 TESTSETRATE(IEEE80211_MODE_11G, 96);
723 TESTSETRATE(IEEE80211_MODE_11G, 108);
725 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
726 TESTSETRATE(IEEE80211_MODE_11A, 47);
727 TESTSETRATE(IEEE80211_MODE_11A, 72);
728 TESTSETRATE(IEEE80211_MODE_11A, 96);
729 TESTSETRATE(IEEE80211_MODE_11A, 108);
734 * Taking yet more guesses here.
736 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
739 if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
740 chanflag |= IEEE80211_CHAN_G;
742 chanflag |= IEEE80211_CHAN_B;
743 if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
745 chanflag = IEEE80211_CHAN_A;
748 ic->ic_channels[i].ic_freq =
749 ieee80211_ieee2mhz(i, chanflag);
750 ic->ic_channels[i].ic_flags = chanflag;
754 r = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &i);
755 if (arg != NDIS_80211_WEPSTAT_NOTSUPPORTED)
756 ic->ic_caps |= IEEE80211_C_WEP;
758 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
760 ic->ic_caps |= IEEE80211_C_PMGT;
761 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
762 ieee80211_ifattach(ic);
763 ieee80211_media_init(ic, ieee80211_media_change,
765 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
766 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
768 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
770 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
771 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
772 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
773 ifmedia_add(&sc->ifmedia,
774 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
775 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
776 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
777 ether_ifattach(ifp, eaddr);
780 /* Override the status handler so we can detect link changes. */
781 sc->ndis_block->nmb_status_func = ndis_linksts_wrap;
782 sc->ndis_block->nmb_statusdone_func = ndis_linksts_done_wrap;
787 /* We're done talking to the NIC for now; halt it. */
794 * Shutdown hardware and free up resources. This can be called any
795 * time after the mutex has been initialized. It is called in both
796 * the error case in attach and the normal detach case so it needs
797 * to be careful about only freeing resources that have actually been
804 struct ndis_softc *sc;
808 sc = device_get_softc(dev);
809 KASSERT(mtx_initialized(&sc->ndis_mtx),
810 ("ndis mutex not initialized"));
812 ifp = &sc->arpcom.ac_if;
813 ifp->if_flags &= ~IFF_UP;
815 if (device_is_attached(dev)) {
819 ieee80211_ifdetach(&sc->ic);
825 bus_generic_detach(dev);
827 if (sc->ndis_intrhand)
828 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
830 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
832 bus_release_resource(dev, SYS_RES_IOPORT,
833 sc->ndis_io_rid, sc->ndis_res_io);
834 if (sc->ndis_res_mem)
835 bus_release_resource(dev, SYS_RES_MEMORY,
836 sc->ndis_mem_rid, sc->ndis_res_mem);
837 if (sc->ndis_res_altmem)
838 bus_release_resource(dev, SYS_RES_MEMORY,
839 sc->ndis_altmem_rid, sc->ndis_res_altmem);
841 if (sc->ndis_iftype == PCMCIABus)
845 ndis_destroy_dma(sc);
847 if (sc->ndis_txarray)
848 free(sc->ndis_txarray, M_DEVBUF);
851 ifmedia_removeall(&sc->ifmedia);
853 if (sc->ndis_txpool != NULL)
854 NdisFreePacketPool(sc->ndis_txpool);
856 ndis_unload_driver(sc);
858 /* Destroy the PDO for this device. */
860 drv = windrv_lookup((vm_offset_t)drv_data);
862 panic("couldn't find driver object");
863 windrv_destroy_pdo(drv, dev);
865 if (sc->ndis_iftype == PCIBus)
866 bus_dma_tag_destroy(sc->ndis_parent_tag);
868 #if __FreeBSD_version < 502113
869 sysctl_ctx_free(&sc->ndis_ctx);
872 mtx_destroy(&sc->ndis_mtx);
881 struct ndis_softc *sc;
884 sc = device_get_softc(dev);
885 ifp = &sc->arpcom.ac_if;
888 if (NDIS_INITIALIZED(sc))
899 struct ndis_softc *sc;
902 sc = device_get_softc(dev);
903 ifp = &sc->arpcom.ac_if;
905 if (NDIS_INITIALIZED(sc))
912 * A frame has been uploaded: pass the resulting mbuf chain up to
913 * the higher level protocols.
915 * When handling received NDIS packets, the 'status' field in the
916 * out-of-band portion of the ndis_packet has special meaning. In the
917 * most common case, the underlying NDIS driver will set this field
918 * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
919 * take posession of it. We then change the status field to
920 * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
921 * and that we will return it at some point in the future via the
922 * return packet handler.
924 * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
925 * this means the driver is running out of packet/buffer resources and
926 * wants to maintain ownership of the packet. In this case, we have to
927 * copy the packet data into local storage and let the driver keep the
930 __stdcall static void
931 ndis_rxeof(adapter, packets, pktcnt)
933 ndis_packet **packets;
936 struct ndis_softc *sc;
937 ndis_miniport_block *block;
940 ndis_tcpip_csum *csum;
945 block = (ndis_miniport_block *)adapter;
946 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
947 ifp = &sc->arpcom.ac_if;
949 for (i = 0; i < pktcnt; i++) {
951 /* Stash the softc here so ptom can use it. */
953 if (ndis_ptom(&m0, p)) {
954 device_printf (sc->ndis_dev, "ptom failed\n");
955 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
956 ndis_return_packet(sc, p);
958 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
959 m = m_dup(m0, M_DONTWAIT);
961 * NOTE: we want to destroy the mbuf here, but
962 * we don't actually want to return it to the
963 * driver via the return packet handler. By
964 * bumping np_refcnt, we can prevent the
965 * ndis_return_packet() routine from actually
975 p->np_oob.npo_status = NDIS_STATUS_PENDING;
976 m0->m_pkthdr.rcvif = ifp;
979 /* Deal with checksum offload. */
981 if (ifp->if_capenable & IFCAP_RXCSUM &&
982 p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
984 p->np_ext.npe_info[ndis_tcpipcsum_info];
985 csum = (ndis_tcpip_csum *)&s;
986 if (csum->u.ntc_rxflags &
987 NDIS_RXCSUM_IP_PASSED)
988 m0->m_pkthdr.csum_flags |=
989 CSUM_IP_CHECKED|CSUM_IP_VALID;
990 if (csum->u.ntc_rxflags &
991 (NDIS_RXCSUM_TCP_PASSED |
992 NDIS_RXCSUM_UDP_PASSED)) {
993 m0->m_pkthdr.csum_flags |=
994 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
995 m0->m_pkthdr.csum_data = 0xFFFF;
999 (*ifp->if_input)(ifp, m0);
1007 * A frame was downloaded to the chip. It's safe for us to clean up
1010 __stdcall static void
1011 ndis_txeof(adapter, packet, status)
1012 ndis_handle adapter;
1013 ndis_packet *packet;
1017 struct ndis_softc *sc;
1018 ndis_miniport_block *block;
1023 block = (ndis_miniport_block *)adapter;
1024 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1025 ifp = &sc->arpcom.ac_if;
1028 idx = packet->np_txidx;
1030 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
1032 ndis_free_packet(packet);
1036 sc->ndis_txarray[idx] = NULL;
1037 sc->ndis_txpending++;
1039 if (status == NDIS_STATUS_SUCCESS)
1044 ifp->if_flags &= ~IFF_OACTIVE;
1047 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
1052 __stdcall static void
1053 ndis_linksts(adapter, status, sbuf, slen)
1054 ndis_handle adapter;
1059 ndis_miniport_block *block;
1062 block->nmb_getstat = status;
1067 __stdcall static void
1068 ndis_linksts_done(adapter)
1069 ndis_handle adapter;
1071 ndis_miniport_block *block;
1072 struct ndis_softc *sc;
1076 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1077 ifp = &sc->arpcom.ac_if;
1080 if (!NDIS_INITIALIZED(sc)) {
1085 switch (block->nmb_getstat) {
1086 case NDIS_STATUS_MEDIA_CONNECT:
1087 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1088 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
1090 case NDIS_STATUS_MEDIA_DISCONNECT:
1092 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1106 struct ndis_softc *sc;
1111 ifp = &sc->arpcom.ac_if;
1113 irql = KeRaiseIrql(DISPATCH_LEVEL);
1117 ndis_enable_intr(sc);
1126 struct ndis_softc *sc;
1128 int is_our_intr = 0;
1131 ndis_miniport_interrupt *intr;
1134 ifp = &sc->arpcom.ac_if;
1135 intr = sc->ndis_block->nmb_interrupt;
1137 if (sc->ndis_block->nmb_miniportadapterctx == NULL)
1140 KeAcquireSpinLock(&intr->ni_dpccountlock, &irql);
1141 if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE)
1142 ndis_isr(sc, &is_our_intr, &call_isr);
1144 ndis_disable_intr(sc);
1147 KeReleaseSpinLock(&intr->ni_dpccountlock, irql);
1149 if ((is_our_intr || call_isr))
1150 ndis_sched(ndis_intrtask, ifp->if_softc, NDIS_SWI);
1159 struct ndis_softc *sc;
1165 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1166 sc->ndis_stat_ch = timeout(ndis_tick, sc, hz *
1167 sc->ndis_block->nmb_checkforhangsecs);
1178 struct ndis_softc *sc;
1179 __stdcall ndis_checkforhang_handler hangfunc;
1181 ndis_media_state linkstate;
1186 hangfunc = sc->ndis_chars->nmc_checkhang_func;
1188 if (hangfunc != NULL) {
1189 rval = hangfunc(sc->ndis_block->nmb_miniportadapterctx);
1196 len = sizeof(linkstate);
1197 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1198 (void *)&linkstate, &len);
1202 if (sc->ndis_link == 0 && linkstate == nmc_connected) {
1203 device_printf(sc->ndis_dev, "link up\n");
1207 ndis_getstate_80211(sc);
1209 #ifdef LINK_STATE_UP
1210 sc->arpcom.ac_if.if_link_state = LINK_STATE_UP;
1211 rt_ifmsg(&(sc->arpcom.ac_if));
1212 #endif /* LINK_STATE_UP */
1215 if (sc->ndis_link == 1 && linkstate == nmc_disconnected) {
1216 device_printf(sc->ndis_dev, "link down\n");
1218 #ifdef LINK_STATE_DOWN
1219 sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN;
1220 rt_ifmsg(&(sc->arpcom.ac_if));
1221 #endif /* LINK_STATE_DOWN */
1230 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1232 bus_dma_segment_t *segs;
1238 struct ndis_sc_list *sclist;
1241 if (error || arg == NULL)
1246 sclist->nsl_frags = nseg;
1248 for (i = 0; i < nseg; i++) {
1249 sclist->nsl_elements[i].nse_addr.np_quad = segs[i].ds_addr;
1250 sclist->nsl_elements[i].nse_len = segs[i].ds_len;
1263 #if __FreeBSD_version < 502114
1264 if (ifp->if_snd.ifq_head != NULL)
1266 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1273 * Main transmit routine. To make NDIS drivers happy, we need to
1274 * transform mbuf chains into NDIS packets and feed them to the
1275 * send packet routines. Most drivers allow you to send several
1276 * packets at once (up to the maxpkts limit). Unfortunately, rather
1277 * that accepting them in the form of a linked list, they expect
1278 * a contiguous array of pointers to packets.
1280 * For those drivers which use the NDIS scatter/gather DMA mechanism,
1281 * we need to perform busdma work here. Those that use map registers
1282 * will do the mapping themselves on a buffer by buffer basis.
1289 struct ndis_softc *sc;
1290 struct mbuf *m = NULL;
1291 ndis_packet **p0 = NULL, *p = NULL;
1292 ndis_tcpip_csum *csum;
1293 int pcnt = 0, status;
1299 if (!sc->ndis_link || ifp->if_flags & IFF_OACTIVE) {
1304 p0 = &sc->ndis_txarray[sc->ndis_txidx];
1306 while(sc->ndis_txpending) {
1307 #if __FreeBSD_version < 502114
1308 IF_DEQUEUE(&ifp->if_snd, m);
1310 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1315 NdisAllocatePacket(&status,
1316 &sc->ndis_txarray[sc->ndis_txidx], sc->ndis_txpool);
1318 if (status != NDIS_STATUS_SUCCESS)
1321 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1322 #if __FreeBSD_version >= 502114
1323 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1326 #if __FreeBSD_version < 502114
1327 IF_PREPEND(&ifp->if_snd, m);
1333 * Save pointer to original mbuf
1334 * so we can free it later.
1337 p = sc->ndis_txarray[sc->ndis_txidx];
1338 p->np_txidx = sc->ndis_txidx;
1340 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1343 * Do scatter/gather processing, if driver requested it.
1346 bus_dmamap_load_mbuf(sc->ndis_ttag,
1347 sc->ndis_tmaps[sc->ndis_txidx], m,
1348 ndis_map_sclist, &p->np_sclist, BUS_DMA_NOWAIT);
1349 bus_dmamap_sync(sc->ndis_ttag,
1350 sc->ndis_tmaps[sc->ndis_txidx],
1351 BUS_DMASYNC_PREREAD);
1352 p->np_ext.npe_info[ndis_sclist_info] = &p->np_sclist;
1355 /* Handle checksum offload. */
1357 if (ifp->if_capenable & IFCAP_TXCSUM &&
1358 m->m_pkthdr.csum_flags) {
1359 csum = (ndis_tcpip_csum *)
1360 &p->np_ext.npe_info[ndis_tcpipcsum_info];
1361 csum->u.ntc_txflags = NDIS_TXCSUM_DO_IPV4;
1362 if (m->m_pkthdr.csum_flags & CSUM_IP)
1363 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_IP;
1364 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1365 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_TCP;
1366 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1367 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_UDP;
1368 p->np_private.npp_flags = NDIS_PROTOCOL_ID_TCP_IP;
1372 sc->ndis_txpending--;
1377 * If there's a BPF listener, bounce a copy of this frame
1384 * The array that p0 points to must appear contiguous,
1385 * so we must not wrap past the end of sc->ndis_txarray[].
1386 * If it looks like we're about to wrap, break out here
1387 * so the this batch of packets can be transmitted, then
1388 * wait for txeof to ask us to send the rest.
1391 if (sc->ndis_txidx == 0)
1400 if (sc->ndis_txpending == 0)
1401 ifp->if_flags |= IFF_OACTIVE;
1404 * Set a timeout in case the chip goes out to lunch.
1410 if (sc->ndis_maxpkts == 1)
1411 ndis_send_packet(sc, p);
1413 ndis_send_packets(sc, p0, pcnt);
1422 struct ndis_softc *sc = xsc;
1423 struct ifnet *ifp = &sc->arpcom.ac_if;
1427 * Avoid reintializing the link unnecessarily.
1428 * This should be dealt with in a better way by
1429 * fixing the upper layer modules so they don't
1430 * call ifp->if_init() quite as often.
1432 if (sc->ndis_link && sc->ndis_skip)
1436 * Cancel pending I/O and free all RX/TX buffers.
1439 if (ndis_init_nic(sc))
1442 /* Init our MAC address */
1444 /* Program the packet filter */
1446 sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1448 if (ifp->if_flags & IFF_BROADCAST)
1449 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1451 if (ifp->if_flags & IFF_PROMISC)
1452 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1454 i = sizeof(sc->ndis_filter);
1456 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1457 &sc->ndis_filter, &i);
1460 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1463 * Program the multicast filter, if necessary.
1467 /* Setup task offload. */
1468 ndis_set_offload(sc);
1470 /* Enable interrupts. */
1471 ndis_enable_intr(sc);
1474 ndis_setstate_80211(sc);
1479 sc->ndis_txpending = sc->ndis_maxpkts;
1482 ifp->if_flags |= IFF_RUNNING;
1483 ifp->if_flags &= ~IFF_OACTIVE;
1488 * Some drivers don't set this value. The NDIS spec says
1489 * the default checkforhang timeout is "approximately 2
1490 * seconds." We use 3 seconds, because it seems for some
1491 * drivers, exactly 2 seconds is too fast.
1494 if (sc->ndis_block->nmb_checkforhangsecs == 0)
1495 sc->ndis_block->nmb_checkforhangsecs = 3;
1497 sc->ndis_stat_ch = timeout(ndis_tick, sc,
1498 hz * sc->ndis_block->nmb_checkforhangsecs);
1504 * Set media options.
1507 ndis_ifmedia_upd(ifp)
1510 struct ndis_softc *sc;
1514 if (NDIS_INITIALIZED(sc))
1521 * Report current media status.
1524 ndis_ifmedia_sts(ifp, ifmr)
1526 struct ifmediareq *ifmr;
1528 struct ndis_softc *sc;
1529 uint32_t media_info;
1530 ndis_media_state linkstate;
1533 ifmr->ifm_status = IFM_AVALID;
1534 ifmr->ifm_active = IFM_ETHER;
1537 if (!NDIS_INITIALIZED(sc))
1540 len = sizeof(linkstate);
1541 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1542 (void *)&linkstate, &len);
1544 len = sizeof(media_info);
1545 error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
1546 (void *)&media_info, &len);
1548 if (linkstate == nmc_connected)
1549 ifmr->ifm_status |= IFM_ACTIVE;
1551 switch(media_info) {
1553 ifmr->ifm_active |= IFM_10_T;
1556 ifmr->ifm_active |= IFM_100_TX;
1559 ifmr->ifm_active |= IFM_1000_T;
1562 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
1570 ndis_setstate_80211(sc)
1571 struct ndis_softc *sc;
1573 struct ieee80211com *ic;
1574 ndis_80211_ssid ssid;
1575 ndis_80211_config config;
1577 int i, rval = 0, len;
1582 ifp = &sc->arpcom.ac_if;
1584 if (!NDIS_INITIALIZED(sc))
1587 /* Set network infrastructure mode. */
1590 if (ic->ic_opmode == IEEE80211_M_IBSS)
1591 arg = NDIS_80211_NET_INFRA_IBSS;
1593 arg = NDIS_80211_NET_INFRA_BSS;
1595 rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
1598 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
1602 #ifdef IEEE80211_F_PRIVACY
1603 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1605 if (ic->ic_wep_mode >= IEEE80211_WEP_ON) {
1607 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1608 if (ic->ic_nw_keys[i].wk_keylen) {
1609 bzero((char *)&wep, sizeof(wep));
1610 wep.nw_keylen = ic->ic_nw_keys[i].wk_keylen;
1612 /* 5 and 13 are the only valid key lengths */
1613 if (ic->ic_nw_keys[i].wk_keylen < 5)
1615 else if (ic->ic_nw_keys[i].wk_keylen > 5 &&
1616 ic->ic_nw_keys[i].wk_keylen < 13)
1620 wep.nw_length = (sizeof(uint32_t) * 3)
1622 if (i == ic->ic_def_txkey)
1623 wep.nw_keyidx |= NDIS_80211_WEPKEY_TX;
1624 bcopy(ic->ic_nw_keys[i].wk_key,
1625 wep.nw_keydata, wep.nw_length);
1627 rval = ndis_set_info(sc,
1628 OID_802_11_ADD_WEP, &wep, &len);
1630 device_printf(sc->ndis_dev,
1631 "set wepkey failed: %d\n", rval);
1634 arg = NDIS_80211_WEPSTAT_ENABLED;
1636 rval = ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1638 device_printf(sc->ndis_dev,
1639 "enable WEP failed: %d\n", rval);
1640 #ifndef IEEE80211_F_WEPON
1642 if (ic->ic_wep_mode != IEEE80211_WEP_8021X &&
1643 ic->ic_wep_mode != IEEE80211_WEP_ON)
1644 arg = NDIS_80211_PRIVFILT_ACCEPTALL;
1648 arg = NDIS_80211_PRIVFILT_8021XWEP;
1650 rval = ndis_set_info(sc, OID_802_11_PRIVACY_FILTER, &arg, &len);
1651 #ifdef IEEE80211_WEP_8021X /*IEEE80211_F_WEPON*/
1652 /* Accept that we only have "shared" and 802.1x modes. */
1654 if (arg == NDIS_80211_PRIVFILT_ACCEPTALL)
1655 ic->ic_wep_mode = IEEE80211_WEP_MIXED;
1657 ic->ic_wep_mode = IEEE80211_WEP_8021X;
1660 arg = NDIS_80211_AUTHMODE_OPEN;
1662 arg = NDIS_80211_WEPSTAT_DISABLED;
1664 ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1665 arg = NDIS_80211_AUTHMODE_OPEN;
1669 rval = ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
1673 device_printf (sc->ndis_dev, "set auth failed: %d\n", rval);
1677 /* Set network type. */
1681 switch (ic->ic_curmode) {
1682 case IEEE80211_MODE_11A:
1683 arg = NDIS_80211_NETTYPE_11OFDM5;
1685 case IEEE80211_MODE_11B:
1686 arg = NDIS_80211_NETTYPE_11DS;
1688 case IEEE80211_MODE_11G:
1689 arg = NDIS_80211_NETTYPE_11OFDM24;
1692 device_printf(sc->ndis_dev, "unknown mode: %d\n",
1698 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
1701 device_printf (sc->ndis_dev,
1702 "set nettype failed: %d\n", rval);
1706 len = sizeof(config);
1707 bzero((char *)&config, len);
1708 config.nc_length = len;
1709 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1710 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1713 * Some drivers expect us to initialize these values, so
1714 * provide some defaults.
1716 if (config.nc_beaconperiod == 0)
1717 config.nc_beaconperiod = 100;
1718 if (config.nc_atimwin == 0)
1719 config.nc_atimwin = 100;
1720 if (config.nc_fhconfig.ncf_dwelltime == 0)
1721 config.nc_fhconfig.ncf_dwelltime = 200;
1723 if (rval == 0 && ic->ic_ibss_chan != IEEE80211_CHAN_ANYC) {
1726 chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
1727 chanflag = config.nc_dsconfig > 2500000 ? IEEE80211_CHAN_2GHZ :
1728 IEEE80211_CHAN_5GHZ;
1729 if (chan != ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0)) {
1730 config.nc_dsconfig =
1731 ic->ic_ibss_chan->ic_freq * 1000;
1732 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1733 len = sizeof(config);
1734 config.nc_length = len;
1735 config.nc_fhconfig.ncf_length =
1736 sizeof(ndis_80211_config_fh);
1737 rval = ndis_set_info(sc, OID_802_11_CONFIGURATION,
1740 device_printf(sc->ndis_dev, "couldn't change "
1741 "DS config to %ukHz: %d\n",
1742 config.nc_dsconfig, rval);
1745 device_printf(sc->ndis_dev, "couldn't retrieve "
1746 "channel info: %d\n", rval);
1748 /* Set SSID -- always do this last. */
1751 bzero((char *)&ssid, len);
1752 ssid.ns_ssidlen = ic->ic_des_esslen;
1753 if (ssid.ns_ssidlen == 0) {
1754 ssid.ns_ssidlen = 1;
1756 bcopy(ic->ic_des_essid, ssid.ns_ssid, ssid.ns_ssidlen);
1757 rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
1760 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
1766 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1768 struct ieee80211com *ic = &((struct ndis_softc *)ifp->if_softc)->ic;
1769 struct ieee80211_node *ni = NULL;
1771 imr->ifm_status = IFM_AVALID;
1772 imr->ifm_active = IFM_IEEE80211;
1773 if (ic->ic_state == IEEE80211_S_RUN)
1774 imr->ifm_status |= IFM_ACTIVE;
1775 imr->ifm_active |= IFM_AUTO;
1776 switch (ic->ic_opmode) {
1777 case IEEE80211_M_STA:
1779 /* calculate rate subtype */
1780 imr->ifm_active |= ieee80211_rate2media(ic,
1781 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1783 case IEEE80211_M_IBSS:
1785 /* calculate rate subtype */
1786 imr->ifm_active |= ieee80211_rate2media(ic,
1787 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1788 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1790 case IEEE80211_M_AHDEMO:
1791 /* should not come here */
1793 case IEEE80211_M_HOSTAP:
1794 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1796 case IEEE80211_M_MONITOR:
1797 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1800 switch (ic->ic_curmode) {
1801 case IEEE80211_MODE_11A:
1802 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
1804 case IEEE80211_MODE_11B:
1805 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
1807 case IEEE80211_MODE_11G:
1808 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
1810 case IEEE80211_MODE_TURBO_A:
1811 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
1812 | IFM_IEEE80211_TURBO;
1818 ndis_get_assoc(sc, assoc)
1819 struct ndis_softc *sc;
1820 ndis_wlan_bssid_ex **assoc;
1822 ndis_80211_bssid_list_ex *bl;
1823 ndis_wlan_bssid_ex *bs;
1824 ndis_80211_macaddr bssid;
1830 len = sizeof(bssid);
1831 error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
1833 device_printf(sc->ndis_dev, "failed to get bssid\n");
1837 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1838 if (error != ENOSPC) {
1839 device_printf(sc->ndis_dev, "bssid_list failed\n");
1843 bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
1844 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1847 device_printf(sc->ndis_dev, "bssid_list failed\n");
1851 bs = (ndis_wlan_bssid_ex *)&bl->nblx_bssid[0];
1852 for (i = 0; i < bl->nblx_items; i++) {
1853 if (bcmp(bs->nwbx_macaddr, bssid, sizeof(bssid)) == 0) {
1854 *assoc = malloc(bs->nwbx_len, M_TEMP, M_NOWAIT);
1855 if (*assoc == NULL) {
1859 bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
1863 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
1871 ndis_getstate_80211(sc)
1872 struct ndis_softc *sc;
1874 struct ieee80211com *ic;
1875 ndis_80211_ssid ssid;
1876 ndis_80211_config config;
1877 ndis_wlan_bssid_ex *bs;
1878 int rval, len, i = 0;
1883 ifp = &sc->arpcom.ac_if;
1885 if (!NDIS_INITIALIZED(sc))
1889 ic->ic_state = IEEE80211_S_RUN;
1891 ic->ic_state = IEEE80211_S_ASSOC;
1895 * If we're associated, retrieve info on the current bssid.
1897 if ((rval = ndis_get_assoc(sc, &bs)) == 0) {
1898 switch(bs->nwbx_nettype) {
1899 case NDIS_80211_NETTYPE_11FH:
1900 case NDIS_80211_NETTYPE_11DS:
1901 ic->ic_curmode = IEEE80211_MODE_11B;
1903 case NDIS_80211_NETTYPE_11OFDM5:
1904 ic->ic_curmode = IEEE80211_MODE_11A;
1906 case NDIS_80211_NETTYPE_11OFDM24:
1907 ic->ic_curmode = IEEE80211_MODE_11G;
1910 device_printf(sc->ndis_dev,
1911 "unknown nettype %d\n", arg);
1914 IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid, bs->nwbx_macaddr);
1920 bzero((char *)&ssid, len);
1921 rval = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
1924 device_printf (sc->ndis_dev, "get ssid failed: %d\n", rval);
1925 bcopy(ssid.ns_ssid, ic->ic_bss->ni_essid, ssid.ns_ssidlen);
1926 ic->ic_bss->ni_esslen = ssid.ns_ssidlen;
1929 rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
1931 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
1934 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
1935 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
1936 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1937 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1938 IEEE80211_RATE_VAL) == arg / 5000)
1943 if (i == ic->ic_bss->ni_rates.rs_nrates &&
1944 ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
1945 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11G];
1946 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1947 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1948 IEEE80211_RATE_VAL) == arg / 5000)
1953 if (i == ic->ic_bss->ni_rates.rs_nrates)
1954 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
1957 ic->ic_bss->ni_txrate = i;
1959 if (ic->ic_caps & IEEE80211_C_PMGT) {
1961 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
1964 device_printf(sc->ndis_dev,
1965 "get power mode failed: %d\n", rval);
1966 if (arg == NDIS_80211_POWERMODE_CAM)
1967 ic->ic_flags &= ~IEEE80211_F_PMGTON;
1969 ic->ic_flags |= IEEE80211_F_PMGTON;
1972 len = sizeof(config);
1973 bzero((char *)&config, len);
1974 config.nc_length = len;
1975 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1976 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1980 chan = ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0);
1981 if (chan < 0 || chan >= IEEE80211_CHAN_MAX) {
1982 if (ifp->if_flags & IFF_DEBUG)
1983 device_printf(sc->ndis_dev, "current channel "
1984 "(%uMHz) out of bounds\n",
1985 config.nc_dsconfig / 1000);
1986 ic->ic_bss->ni_chan = &ic->ic_channels[1];
1988 ic->ic_bss->ni_chan = &ic->ic_channels[chan];
1990 device_printf(sc->ndis_dev, "couldn't retrieve "
1991 "channel info: %d\n", rval);
1995 rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1998 device_printf (sc->ndis_dev,
1999 "get wep status failed: %d\n", rval);
2001 if (arg == NDIS_80211_WEPSTAT_ENABLED)
2002 ic->ic_flags |= IEEE80211_F_WEPON;
2004 ic->ic_flags &= ~IEEE80211_F_WEPON;
2010 ndis_ioctl(ifp, command, data)
2015 struct ndis_softc *sc = ifp->if_softc;
2016 struct ifreq *ifr = (struct ifreq *) data;
2023 if (ifp->if_flags & IFF_UP) {
2024 if (ifp->if_flags & IFF_RUNNING &&
2025 ifp->if_flags & IFF_PROMISC &&
2026 !(sc->ndis_if_flags & IFF_PROMISC)) {
2028 NDIS_PACKET_TYPE_PROMISCUOUS;
2029 i = sizeof(sc->ndis_filter);
2030 error = ndis_set_info(sc,
2031 OID_GEN_CURRENT_PACKET_FILTER,
2032 &sc->ndis_filter, &i);
2033 } else if (ifp->if_flags & IFF_RUNNING &&
2034 !(ifp->if_flags & IFF_PROMISC) &&
2035 sc->ndis_if_flags & IFF_PROMISC) {
2037 ~NDIS_PACKET_TYPE_PROMISCUOUS;
2038 i = sizeof(sc->ndis_filter);
2039 error = ndis_set_info(sc,
2040 OID_GEN_CURRENT_PACKET_FILTER,
2041 &sc->ndis_filter, &i);
2045 if (ifp->if_flags & IFF_RUNNING)
2048 sc->ndis_if_flags = ifp->if_flags;
2058 if (sc->ndis_80211) {
2059 error = ieee80211_ioctl(&sc->ic, command, data);
2060 if (error == ENETRESET) {
2061 ndis_setstate_80211(sc);
2066 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
2069 ifp->if_capenable = ifr->ifr_reqcap;
2070 if (ifp->if_capenable & IFCAP_TXCSUM)
2071 ifp->if_hwassist = sc->ndis_hwassist;
2073 ifp->if_hwassist = 0;
2074 ndis_set_offload(sc);
2078 error = ndis_80211_ioctl_get(ifp, command, data);
2084 error = ndis_80211_ioctl_set(ifp, command, data);
2088 case SIOCGIFGENERIC:
2089 case SIOCSIFGENERIC:
2090 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
2091 if (command == SIOCGIFGENERIC)
2092 error = ndis_wi_ioctl_get(ifp, command, data);
2094 error = ndis_wi_ioctl_set(ifp, command, data);
2097 if (error != ENOTTY)
2101 if (sc->ndis_80211) {
2102 error = ieee80211_ioctl(&sc->ic, command, data);
2103 if (error == ENETRESET) {
2104 ndis_setstate_80211(sc);
2108 error = ether_ioctl(ifp, command, data);
2113 /*NDIS_UNLOCK(sc);*/
2119 ndis_wi_ioctl_get(ifp, command, data)
2126 struct ndis_softc *sc;
2127 ndis_80211_bssid_list_ex *bl;
2128 ndis_wlan_bssid_ex *wb;
2129 struct wi_apinfo *api;
2130 int error, i, j, len, maxaps;
2133 ifr = (struct ifreq *)data;
2134 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2138 switch (wreq.wi_type) {
2139 case WI_RID_READ_APS:
2141 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2144 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2146 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2147 if (error != ENOSPC)
2149 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
2150 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2155 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
2156 maxaps = MIN(maxaps, bl->nblx_items);
2157 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
2158 *(int *)&wreq.wi_val = maxaps;
2159 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
2160 wb = bl->nblx_bssid;
2162 bzero(api, sizeof(*api));
2163 bcopy(&wb->nwbx_macaddr, &api->bssid,
2164 sizeof(api->bssid));
2165 api->namelen = wb->nwbx_ssid.ns_ssidlen;
2166 bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
2167 if (wb->nwbx_privacy)
2168 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
2169 /* XXX Where can we get noise information? */
2170 api->signal = wb->nwbx_rssi + 149; /* XXX */
2171 api->quality = api->signal;
2173 ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2175 /* In "auto" infrastructure mode, this is useless. */
2176 if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
2177 api->capinfo |= IEEE80211_CAPINFO_IBSS;
2178 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
2179 j = sizeof(ndis_80211_rates_ex);
2180 /* handle other extended things */
2182 j = sizeof(ndis_80211_rates);
2183 for (i = api->rate = 0; i < j; i++)
2184 api->rate = MAX(api->rate, 5 *
2185 (wb->nwbx_supportedrates[i] & 0x7f));
2187 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2190 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2200 ndis_wi_ioctl_set(ifp, command, data)
2207 struct ndis_softc *sc;
2211 error = suser(curthread);
2216 ifr = (struct ifreq *)data;
2217 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2221 switch (wreq.wi_type) {
2222 case WI_RID_SCAN_APS:
2223 case WI_RID_SCAN_REQ: /* arguments ignored */
2226 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2237 ndis_80211_ioctl_get(struct ifnet *ifp, u_long command, caddr_t data)
2239 struct ndis_softc *sc;
2240 struct ieee80211req *ireq;
2241 ndis_80211_bssid_list_ex *bl;
2242 ndis_wlan_bssid_ex *wb;
2243 struct ieee80211req_scan_result *sr, *bsr;
2244 int error, len, i, j;
2248 ireq = (struct ieee80211req *) data;
2250 switch (ireq->i_type) {
2251 case IEEE80211_IOC_SCAN_RESULTS:
2253 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2254 if (error != ENOSPC)
2256 bl = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
2257 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2262 sr = bsr = malloc(ireq->i_len, M_DEVBUF, M_WAITOK | M_ZERO);
2263 wb = bl->nblx_bssid;
2265 for (i = 0; i < bl->nblx_items; i++) {
2267 * Check if we have enough space left for this ap
2269 j = roundup(sizeof(*sr) + wb->nwbx_ssid.ns_ssidlen
2270 + wb->nwbx_ielen - sizeof(struct ndis_80211_fixed_ies),
2272 if (len + j > ireq->i_len)
2274 bcopy(&wb->nwbx_macaddr, &sr->isr_bssid, sizeof(sr->isr_bssid));
2275 if (wb->nwbx_privacy)
2276 sr->isr_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2277 sr->isr_rssi = wb->nwbx_rssi + 200;
2278 sr->isr_freq = wb->nwbx_config.nc_dsconfig / 1000;
2279 sr->isr_intval = wb->nwbx_config.nc_beaconperiod;
2280 switch (wb->nwbx_netinfra) {
2281 case NDIS_80211_NET_INFRA_IBSS:
2282 sr->isr_capinfo |= IEEE80211_CAPINFO_IBSS;
2284 case NDIS_80211_NET_INFRA_BSS:
2285 sr->isr_capinfo |= IEEE80211_CAPINFO_ESS;
2288 for (j = 0; j < sizeof(sr->isr_rates); j++) {
2289 /* XXX - check units */
2290 if (wb->nwbx_supportedrates[j] == 0)
2292 sr->isr_rates[j] = wb->nwbx_supportedrates[j] & 0x7f;
2295 sr->isr_ssid_len = wb->nwbx_ssid.ns_ssidlen;
2296 cp = (char *)sr + sizeof(*sr);
2297 bcopy(&wb->nwbx_ssid.ns_ssid, cp, sr->isr_ssid_len);
2298 cp += sr->isr_ssid_len;
2299 sr->isr_ie_len = wb->nwbx_ielen
2300 - sizeof(struct ndis_80211_fixed_ies);
2301 bcopy((char *)wb->nwbx_ies + sizeof(struct ndis_80211_fixed_ies),
2302 cp, sr->isr_ie_len);
2303 sr->isr_len = roundup(sizeof(*sr) + sr->isr_ssid_len
2304 + sr->isr_ie_len, sizeof(uint32_t));
2306 sr = (struct ieee80211req_scan_result *)((char *)sr + sr->isr_len);
2307 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2310 error = copyout(bsr, ireq->i_data, len);
2312 free(bsr, M_DEVBUF);
2315 error = ieee80211_ioctl(&sc->ic, command, data);
2322 ndis_80211_ioctl_set(struct ifnet *ifp, u_long command, caddr_t data)
2324 struct ndis_softc *sc;
2325 struct ieee80211req *ireq;
2329 ireq = (struct ieee80211req *) data;
2331 switch (ireq->i_type) {
2332 case IEEE80211_IOC_SCAN_REQ:
2334 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, NULL, &len);
2335 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2336 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
2339 error = ieee80211_ioctl(&sc->ic, command, data);
2340 if (error == ENETRESET) {
2341 ndis_setstate_80211(sc);
2353 struct ndis_softc *sc;
2359 device_printf(sc->ndis_dev, "watchdog timeout\n");
2362 ndis_sched((void(*)(void *))ndis_reset_nic, sc, NDIS_TASKQUEUE);
2363 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2369 * Stop the adapter and free any mbufs allocated to the
2374 struct ndis_softc *sc;
2378 ifp = &sc->arpcom.ac_if;
2379 untimeout(ndis_tick, sc, sc->ndis_stat_ch);
2386 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2393 * Stop all chip I/O so that the kernel's probe routines don't
2394 * get confused by errant DMAs when rebooting.
2400 struct ndis_softc *sc;
2402 sc = device_get_softc(dev);
2403 ndis_shutdown_nic(sc);