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.
32 * WPA support originally contributed by Arvind Srinivasan <arvind@celar.us>
33 * then hacked upon mercilessly by my.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
43 #include <sys/malloc.h>
44 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/queue.h>
49 #include <sys/module.h>
51 #include <sys/sysctl.h>
52 #include <sys/kthread.h>
53 #include <sys/taskqueue.h>
57 #include <net/if_arp.h>
58 #include <net/ethernet.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
71 #include <net80211/ieee80211_var.h>
72 #include <net80211/ieee80211_ioctl.h>
73 #include <net80211/ieee80211_regdomain.h>
75 #if __FreeBSD_version < 700046
76 #include <dev/wi/if_wavelan_ieee.h>
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
82 #include <compat/ndis/pe_var.h>
83 #include <compat/ndis/cfg_var.h>
84 #include <compat/ndis/resource_var.h>
85 #include <compat/ndis/ntoskrnl_var.h>
86 #include <compat/ndis/hal_var.h>
87 #include <compat/ndis/ndis_var.h>
88 #include <dev/if_ndis/if_ndisvar.h>
92 #define DPRINTF(x) do { if (ndis_debug > 0) printf x; } while (0)
94 SYSCTL_INT(_debug, OID_AUTO, ndis, CTLFLAG_RW, &ndis_debug, 0,
95 "if_ndis debug level");
100 MODULE_DEPEND(ndis, ether, 1, 1, 1);
101 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
102 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
104 MODULE_VERSION(ndis, 1);
106 int ndis_attach (device_t);
107 int ndis_detach (device_t);
108 int ndis_suspend (device_t);
109 int ndis_resume (device_t);
110 void ndis_shutdown (device_t);
112 int ndisdrv_modevent (module_t, int, void *);
114 static void ndis_txeof (ndis_handle, ndis_packet *, ndis_status);
115 static void ndis_rxeof (ndis_handle, ndis_packet **, uint32_t);
116 static void ndis_rxeof_eth (ndis_handle, ndis_handle, char *, void *,
117 uint32_t, void *, uint32_t, uint32_t);
118 static void ndis_rxeof_done (ndis_handle);
119 static void ndis_rxeof_xfr (kdpc *, ndis_handle, void *, void *);
120 static void ndis_rxeof_xfr_done (ndis_handle, ndis_packet *,
122 static void ndis_linksts (ndis_handle, ndis_status, void *, uint32_t);
123 static void ndis_linksts_done (ndis_handle);
125 /* We need to wrap these functions for amd64. */
127 static funcptr ndis_txeof_wrap;
128 static funcptr ndis_rxeof_wrap;
129 static funcptr ndis_rxeof_eth_wrap;
130 static funcptr ndis_rxeof_done_wrap;
131 static funcptr ndis_rxeof_xfr_wrap;
132 static funcptr ndis_rxeof_xfr_done_wrap;
133 static funcptr ndis_linksts_wrap;
134 static funcptr ndis_linksts_done_wrap;
135 static funcptr ndis_ticktask_wrap;
136 static funcptr ndis_starttask_wrap;
137 static funcptr ndis_resettask_wrap;
138 static funcptr ndis_inputtask_wrap;
140 static void ndis_tick (void *);
141 static void ndis_ticktask (device_object *, void *);
142 static void ndis_start (struct ifnet *);
143 static void ndis_starttask (device_object *, void *);
144 static void ndis_resettask (device_object *, void *);
145 static void ndis_inputtask (device_object *, void *);
146 static int ndis_ioctl (struct ifnet *, u_long, caddr_t);
147 #if __FreeBSD_version < 700046
148 static int ndis_wi_ioctl_get (struct ifnet *, u_long, caddr_t);
149 static int ndis_wi_ioctl_set (struct ifnet *, u_long, caddr_t);
151 static int ndis_80211_ioctl_get (struct ifnet *, u_long, caddr_t);
152 static int ndis_80211_ioctl_set (struct ifnet *, u_long, caddr_t);
153 static int ndis_newstate (struct ieee80211com *, enum ieee80211_state,
155 static int ndis_nettype_chan (uint32_t);
156 static int ndis_nettype_mode (uint32_t);
157 static void ndis_scan (void *, int);
158 static void ndis_scan_results (struct ndis_softc *);
159 static void ndis_scan_start (struct ieee80211com *);
160 static void ndis_scan_end (struct ieee80211com *);
161 static void ndis_set_channel (struct ieee80211com *);
162 static void ndis_scan_curchan (struct ieee80211com *, unsigned long);
163 static void ndis_scan_mindwell (struct ieee80211com *);
164 static void ndis_init (void *);
165 static void ndis_stop (struct ndis_softc *);
166 static void ndis_watchdog (struct ifnet *);
167 static int ndis_ifmedia_upd (struct ifnet *);
168 static void ndis_ifmedia_sts (struct ifnet *, struct ifmediareq *);
169 static int ndis_get_assoc (struct ndis_softc *, ndis_wlan_bssid_ex **);
170 static int ndis_probe_offload (struct ndis_softc *);
171 static int ndis_set_offload (struct ndis_softc *);
172 static void ndis_getstate_80211 (struct ndis_softc *);
173 static void ndis_setstate_80211 (struct ndis_softc *);
174 #ifdef IEEE80211_F_WPA
175 static int ndis_set_cipher (struct ndis_softc *, int);
176 static int ndis_set_wpa (struct ndis_softc *);
177 static int ndis_add_key (struct ieee80211com *,
178 const struct ieee80211_key *, const u_int8_t []);
179 static int ndis_del_key (struct ieee80211com *,
180 const struct ieee80211_key *);
182 static void ndis_media_status (struct ifnet *, struct ifmediareq *);
184 static void ndis_setmulti (struct ndis_softc *);
185 static void ndis_map_sclist (void *, bus_dma_segment_t *,
186 int, bus_size_t, int);
188 static int ndisdrv_loaded = 0;
191 * This routine should call windrv_load() once for each driver
192 * image. This will do the relocation and dynalinking for the
193 * image, and create a Windows driver object which will be
194 * saved in our driver database.
198 ndisdrv_modevent(mod, cmd, arg)
208 if (ndisdrv_loaded > 1)
210 windrv_wrap((funcptr)ndis_rxeof, &ndis_rxeof_wrap,
211 3, WINDRV_WRAP_STDCALL);
212 windrv_wrap((funcptr)ndis_rxeof_eth, &ndis_rxeof_eth_wrap,
213 8, WINDRV_WRAP_STDCALL);
214 windrv_wrap((funcptr)ndis_rxeof_done, &ndis_rxeof_done_wrap,
215 1, WINDRV_WRAP_STDCALL);
216 windrv_wrap((funcptr)ndis_rxeof_xfr, &ndis_rxeof_xfr_wrap,
217 4, WINDRV_WRAP_STDCALL);
218 windrv_wrap((funcptr)ndis_rxeof_xfr_done,
219 &ndis_rxeof_xfr_done_wrap, 4, WINDRV_WRAP_STDCALL);
220 windrv_wrap((funcptr)ndis_txeof, &ndis_txeof_wrap,
221 3, WINDRV_WRAP_STDCALL);
222 windrv_wrap((funcptr)ndis_linksts, &ndis_linksts_wrap,
223 4, WINDRV_WRAP_STDCALL);
224 windrv_wrap((funcptr)ndis_linksts_done,
225 &ndis_linksts_done_wrap, 1, WINDRV_WRAP_STDCALL);
226 windrv_wrap((funcptr)ndis_ticktask, &ndis_ticktask_wrap,
227 2, WINDRV_WRAP_STDCALL);
228 windrv_wrap((funcptr)ndis_starttask, &ndis_starttask_wrap,
229 2, WINDRV_WRAP_STDCALL);
230 windrv_wrap((funcptr)ndis_resettask, &ndis_resettask_wrap,
231 2, WINDRV_WRAP_STDCALL);
232 windrv_wrap((funcptr)ndis_inputtask, &ndis_inputtask_wrap,
233 2, WINDRV_WRAP_STDCALL);
237 if (ndisdrv_loaded > 0)
241 windrv_unwrap(ndis_rxeof_wrap);
242 windrv_unwrap(ndis_rxeof_eth_wrap);
243 windrv_unwrap(ndis_rxeof_done_wrap);
244 windrv_unwrap(ndis_rxeof_xfr_wrap);
245 windrv_unwrap(ndis_rxeof_xfr_done_wrap);
246 windrv_unwrap(ndis_txeof_wrap);
247 windrv_unwrap(ndis_linksts_wrap);
248 windrv_unwrap(ndis_linksts_done_wrap);
249 windrv_unwrap(ndis_ticktask_wrap);
250 windrv_unwrap(ndis_starttask_wrap);
251 windrv_unwrap(ndis_resettask_wrap);
252 windrv_unwrap(ndis_inputtask_wrap);
263 * Program the 64-bit multicast hash filter.
267 struct ndis_softc *sc;
270 struct ifmultiaddr *ifma;
271 int len, mclistsz, error;
276 if (!NDIS_INITIALIZED(sc))
279 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
280 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
281 len = sizeof(sc->ndis_filter);
282 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
283 &sc->ndis_filter, &len);
285 device_printf (sc->ndis_dev,
286 "set allmulti failed: %d\n", error);
290 if (TAILQ_EMPTY(&ifp->if_multiaddrs))
293 len = sizeof(mclistsz);
294 ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
296 mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
298 if (mclist == NULL) {
299 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
303 sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
307 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
308 if (ifma->ifma_addr->sa_family != AF_LINK)
310 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
311 mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
313 if (len > mclistsz) {
315 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
316 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
322 len = len * ETHER_ADDR_LEN;
323 error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
325 device_printf (sc->ndis_dev, "set mclist failed: %d\n", error);
326 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
327 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
331 free(mclist, M_TEMP);
333 len = sizeof(sc->ndis_filter);
334 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
335 &sc->ndis_filter, &len);
337 device_printf (sc->ndis_dev, "set multi failed: %d\n", error);
344 struct ndis_softc *sc;
346 ndis_task_offload *nto;
347 ndis_task_offload_hdr *ntoh;
348 ndis_task_tcpip_csum *nttc;
354 if (!NDIS_INITIALIZED(sc))
357 /* See if there's anything to set. */
359 error = ndis_probe_offload(sc);
363 if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
366 len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
367 sizeof(ndis_task_tcpip_csum);
369 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
374 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
375 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
376 ntoh->ntoh_offset_firsttask = sizeof(ndis_task_offload_hdr);
377 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
378 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
379 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
381 nto = (ndis_task_offload *)((char *)ntoh +
382 ntoh->ntoh_offset_firsttask);
384 nto->nto_vers = NDIS_TASK_OFFLOAD_VERSION;
385 nto->nto_len = sizeof(ndis_task_offload);
386 nto->nto_task = NDIS_TASK_TCPIP_CSUM;
387 nto->nto_offset_nexttask = 0;
388 nto->nto_taskbuflen = sizeof(ndis_task_tcpip_csum);
390 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
392 if (ifp->if_capenable & IFCAP_TXCSUM)
393 nttc->nttc_v4tx = sc->ndis_v4tx;
395 if (ifp->if_capenable & IFCAP_RXCSUM)
396 nttc->nttc_v4rx = sc->ndis_v4rx;
398 error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
405 ndis_probe_offload(sc)
406 struct ndis_softc *sc;
408 ndis_task_offload *nto;
409 ndis_task_offload_hdr *ntoh;
410 ndis_task_tcpip_csum *nttc = NULL;
412 int len, error, dummy;
417 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
422 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
427 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
428 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
429 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
430 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
431 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
433 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
440 if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
445 nto = (ndis_task_offload *)((char *)ntoh +
446 ntoh->ntoh_offset_firsttask);
449 switch (nto->nto_task) {
450 case NDIS_TASK_TCPIP_CSUM:
451 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
453 /* Don't handle these yet. */
454 case NDIS_TASK_IPSEC:
455 case NDIS_TASK_TCP_LARGESEND:
459 if (nto->nto_offset_nexttask == 0)
461 nto = (ndis_task_offload *)((char *)nto +
462 nto->nto_offset_nexttask);
470 sc->ndis_v4tx = nttc->nttc_v4tx;
471 sc->ndis_v4rx = nttc->nttc_v4rx;
473 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_IP_CSUM)
474 sc->ndis_hwassist |= CSUM_IP;
475 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
476 sc->ndis_hwassist |= CSUM_TCP;
477 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
478 sc->ndis_hwassist |= CSUM_UDP;
480 if (sc->ndis_hwassist)
481 ifp->if_capabilities |= IFCAP_TXCSUM;
483 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_IP_CSUM)
484 ifp->if_capabilities |= IFCAP_RXCSUM;
485 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
486 ifp->if_capabilities |= IFCAP_RXCSUM;
487 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
488 ifp->if_capabilities |= IFCAP_RXCSUM;
495 ndis_nettype_chan(uint32_t type)
498 case NDIS_80211_NETTYPE_11FH: return (IEEE80211_CHAN_FHSS);
499 case NDIS_80211_NETTYPE_11DS: return (IEEE80211_CHAN_B);
500 case NDIS_80211_NETTYPE_11OFDM5: return (IEEE80211_CHAN_A);
501 case NDIS_80211_NETTYPE_11OFDM24: return (IEEE80211_CHAN_G);
503 DPRINTF(("unknown channel nettype %d\n", type));
504 return (IEEE80211_CHAN_B); /* Default to 11B chan */
508 ndis_nettype_mode(uint32_t type)
511 case NDIS_80211_NETTYPE_11FH: return (IEEE80211_MODE_FH);
512 case NDIS_80211_NETTYPE_11DS: return (IEEE80211_MODE_11B);
513 case NDIS_80211_NETTYPE_11OFDM5: return (IEEE80211_MODE_11A);
514 case NDIS_80211_NETTYPE_11OFDM24: return (IEEE80211_MODE_11G);
516 DPRINTF(("unknown mode nettype %d\n", type));
517 return (IEEE80211_MODE_AUTO);
521 * Attach the interface. Allocate softc structures, do ifmedia
522 * setup and ethernet/BPF attach.
528 u_char eaddr[ETHER_ADDR_LEN];
529 struct ndis_softc *sc;
532 struct ifnet *ifp = NULL;
533 int error = 0, len, mode, bands = 0;
536 sc = device_get_softc(dev);
537 #if __FreeBSD_version < 600031
538 sc->ifp = &sc->arpcom.ac_if;
541 ifp = sc->ifp = if_alloc(IFT_ETHER);
549 KeInitializeSpinLock(&sc->ndis_spinlock);
550 KeInitializeSpinLock(&sc->ndis_rxlock);
551 InitializeListHead(&sc->ndis_shlist);
553 if (sc->ndis_iftype == PCMCIABus) {
554 error = ndis_alloc_amem(sc);
556 device_printf(dev, "failed to allocate "
557 "attribute memory\n");
562 #if __FreeBSD_version < 502113
563 sysctl_ctx_init(&sc->ndis_ctx);
566 /* Create sysctl registry nodes */
567 ndis_create_sysctls(sc);
569 /* Find the PDO for this device instance. */
571 if (sc->ndis_iftype == PCIBus)
572 pdrv = windrv_lookup(0, "PCI Bus");
573 else if (sc->ndis_iftype == PCMCIABus)
574 pdrv = windrv_lookup(0, "PCCARD Bus");
576 pdrv = windrv_lookup(0, "USB Bus");
577 pdo = windrv_find_pdo(pdrv, dev);
580 * Create a new functional device object for this
581 * device. This is what creates the miniport block
582 * for this device instance.
585 if (NdisAddDevice(sc->ndis_dobj, pdo) != STATUS_SUCCESS) {
586 device_printf(dev, "failed to create FDO!\n");
591 /* Tell the user what version of the API the driver is using. */
592 device_printf(dev, "NDIS API version: %d.%d\n",
593 sc->ndis_chars->nmc_version_major,
594 sc->ndis_chars->nmc_version_minor);
596 /* Do resource conversion. */
597 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus)
598 ndis_convert_res(sc);
600 sc->ndis_block->nmb_rlist = NULL;
602 /* Install our RX and TX interrupt handlers. */
603 sc->ndis_block->nmb_senddone_func = ndis_txeof_wrap;
604 sc->ndis_block->nmb_pktind_func = ndis_rxeof_wrap;
605 sc->ndis_block->nmb_ethrxindicate_func = ndis_rxeof_eth_wrap;
606 sc->ndis_block->nmb_ethrxdone_func = ndis_rxeof_done_wrap;
607 sc->ndis_block->nmb_tdcond_func = ndis_rxeof_xfr_done_wrap;
609 /* Override the status handler so we can detect link changes. */
610 sc->ndis_block->nmb_status_func = ndis_linksts_wrap;
611 sc->ndis_block->nmb_statusdone_func = ndis_linksts_done_wrap;
613 /* Set up work item handlers. */
614 sc->ndis_tickitem = IoAllocateWorkItem(sc->ndis_block->nmb_deviceobj);
615 sc->ndis_startitem = IoAllocateWorkItem(sc->ndis_block->nmb_deviceobj);
616 sc->ndis_resetitem = IoAllocateWorkItem(sc->ndis_block->nmb_deviceobj);
617 sc->ndis_inputitem = IoAllocateWorkItem(sc->ndis_block->nmb_deviceobj);
618 KeInitializeDpc(&sc->ndis_rxdpc, ndis_rxeof_xfr_wrap, sc->ndis_block);
620 /* make sure drv flags are all cleared before initing the NIC. */
622 ifp->if_drv_flags = 0;
624 /* Call driver's init routine. */
625 if (ndis_init_nic(sc)) {
626 device_printf (dev, "init handler failed\n");
632 * Get station address from the driver.
635 ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
638 * Figure out how big to make the TX buffer pool.
641 len = sizeof(sc->ndis_maxpkts);
642 if (ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
643 &sc->ndis_maxpkts, &len)) {
644 device_printf (dev, "failed to get max TX packets\n");
650 * If this is a deserialized miniport, we don't have
651 * to honor the OID_GEN_MAXIMUM_SEND_PACKETS result.
654 if (!NDIS_SERIALIZED(sc->ndis_block))
655 sc->ndis_maxpkts = NDIS_TXPKTS;
657 /* Enforce some sanity, just in case. */
659 if (sc->ndis_maxpkts == 0)
660 sc->ndis_maxpkts = 10;
662 sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
663 sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
665 /* Allocate a pool of ndis_packets for TX encapsulation. */
667 NdisAllocatePacketPool(&i, &sc->ndis_txpool,
668 sc->ndis_maxpkts, PROTOCOL_RESERVED_SIZE_IN_PACKET);
670 if (i != NDIS_STATUS_SUCCESS) {
671 sc->ndis_txpool = NULL;
672 device_printf(dev, "failed to allocate TX packet pool");
677 sc->ndis_txpending = sc->ndis_maxpkts;
680 /* Get supported oid list. */
681 ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
683 /* If the NDIS module requested scatter/gather, init maps. */
688 * See if the OID_802_11_CONFIGURATION OID is
689 * supported by this driver. If it is, then this an 802.11
690 * wireless driver, and we should set up media for wireless.
692 for (i = 0; i < sc->ndis_oidcnt; i++) {
693 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
699 /* Check for task offload support. */
700 ndis_probe_offload(sc);
702 #if __FreeBSD_version < 502109
704 * An NDIS device was detected. Inform the world.
706 device_printf(dev, "%s address: %6D\n",
707 sc->ndis_80211 ? "802.11" : "Ethernet", eaddr, ":");
710 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
711 ifp->if_mtu = ETHERMTU;
712 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
713 ifp->if_ioctl = ndis_ioctl;
714 ifp->if_start = ndis_start;
715 ifp->if_watchdog = ndis_watchdog;
716 ifp->if_init = ndis_init;
717 ifp->if_baudrate = 10000000;
718 #if __FreeBSD_version < 502114
719 ifp->if_snd.ifq_maxlen = 50;
721 IFQ_SET_MAXLEN(&ifp->if_snd, 50);
722 ifp->if_snd.ifq_drv_maxlen = 25;
723 IFQ_SET_READY(&ifp->if_snd);
725 ifp->if_capenable = ifp->if_capabilities;
726 ifp->if_hwassist = sc->ndis_hwassist;
729 if (sc->ndis_80211) {
730 struct ieee80211com *ic = (void *)&sc->ic;
731 ndis_80211_rates_ex rates;
732 struct ndis_80211_nettype_list *ntl;
736 #if __FreeBSD_version >= 700000
737 sc->ndis_tq = taskqueue_create("nids_taskq", M_NOWAIT | M_ZERO,
738 taskqueue_thread_enqueue, &sc->ndis_tq);
739 taskqueue_start_threads(&sc->ndis_tq, 1, PI_NET, "%s taskq",
740 device_get_nameunit(dev));
742 sc->ndis_tq = taskqueue_create("ndis_taskq", M_NOWAIT | M_ZERO,
743 taskqueue_thread_enqueue, &sc->ndis_tq, &sc->sc_tqproc);
744 kthread_create(taskqueue_thread_loop, &sc->ndis_tq, &sc->sc_tqproc,
745 0, 0, "%s taskq", device_get_nameunit(dev));
747 TASK_INIT(&sc->ndis_scantask, 0, ndis_scan, sc);
749 #if __FreeBSD_version >= 600007
752 ic->ic_phytype = IEEE80211_T_DS;
753 ic->ic_opmode = IEEE80211_M_STA;
754 ic->ic_caps = IEEE80211_C_IBSS;
755 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
757 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
761 ntl = malloc(len, M_DEVBUF, M_NOWAIT|M_ZERO);
762 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
769 for (i = 0; i < ntl->ntl_items; i++) {
770 mode = ndis_nettype_mode(ntl->ntl_type[i]);
772 setbit(ic->ic_modecaps, mode);
773 setbit(&bands, mode);
775 device_printf(dev, "Unknown nettype %d\n",
780 /* Default to 11b channels if the card did not supply any */
782 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
783 setbit(&bands, IEEE80211_MODE_11B);
786 bzero((char *)&rates, len);
787 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
788 (void *)rates, &len);
790 device_printf (dev, "get rates failed: 0x%x\n", r);
792 * Since the supported rates only up to 8 can be supported,
793 * if this is not 802.11b we're just going to be faking it
797 #define TESTSETRATE(x, y) \
800 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) { \
801 if (ic->ic_sup_rates[x].rs_rates[i] == (y)) \
804 if (i == ic->ic_sup_rates[x].rs_nrates) { \
805 ic->ic_sup_rates[x].rs_rates[i] = (y); \
806 ic->ic_sup_rates[x].rs_nrates++; \
810 #define SETRATE(x, y) \
811 ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
813 ic->ic_sup_rates[x].rs_nrates++
815 ic->ic_curmode = IEEE80211_MODE_AUTO;
816 if (isset(ic->ic_modecaps, IEEE80211_MODE_11A))
817 ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
818 if (isset(ic->ic_modecaps, IEEE80211_MODE_11B))
819 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
820 if (isset(ic->ic_modecaps, IEEE80211_MODE_11G))
821 ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
822 for (i = 0; i < len; i++) {
823 switch (rates[i] & IEEE80211_RATE_VAL) {
829 if (isclr(ic->ic_modecaps, IEEE80211_MODE_11B)) {
830 /* Lazy-init 802.11b. */
831 setbit(ic->ic_modecaps,
833 ic->ic_sup_rates[IEEE80211_MODE_11B].
836 SETRATE(IEEE80211_MODE_11B, rates[i]);
837 INCRATE(IEEE80211_MODE_11B);
840 if (isset(ic->ic_modecaps, IEEE80211_MODE_11A)) {
841 SETRATE(IEEE80211_MODE_11A, rates[i]);
842 INCRATE(IEEE80211_MODE_11A);
844 if (isset(ic->ic_modecaps, IEEE80211_MODE_11G)) {
845 SETRATE(IEEE80211_MODE_11G, rates[i]);
846 INCRATE(IEEE80211_MODE_11G);
853 * If the hardware supports 802.11g, it most
854 * likely supports 802.11b and all of the
855 * 802.11b and 802.11g speeds, so maybe we can
856 * just cheat here. Just how in the heck do
857 * we detect turbo modes, though?
859 if (isset(ic->ic_modecaps, IEEE80211_MODE_11B)) {
860 TESTSETRATE(IEEE80211_MODE_11B,
861 IEEE80211_RATE_BASIC|2);
862 TESTSETRATE(IEEE80211_MODE_11B,
863 IEEE80211_RATE_BASIC|4);
864 TESTSETRATE(IEEE80211_MODE_11B,
865 IEEE80211_RATE_BASIC|11);
866 TESTSETRATE(IEEE80211_MODE_11B,
867 IEEE80211_RATE_BASIC|22);
869 if (isset(ic->ic_modecaps, IEEE80211_MODE_11G)) {
870 TESTSETRATE(IEEE80211_MODE_11G, 48);
871 TESTSETRATE(IEEE80211_MODE_11G, 72);
872 TESTSETRATE(IEEE80211_MODE_11G, 96);
873 TESTSETRATE(IEEE80211_MODE_11G, 108);
875 if (isset(ic->ic_modecaps, IEEE80211_MODE_11A)) {
876 TESTSETRATE(IEEE80211_MODE_11A, 48);
877 TESTSETRATE(IEEE80211_MODE_11A, 72);
878 TESTSETRATE(IEEE80211_MODE_11A, 96);
879 TESTSETRATE(IEEE80211_MODE_11A, 108);
883 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
886 * To test for WPA support, we need to see if we can
887 * set AUTHENTICATION_MODE to WPA and read it back
890 #ifdef IEEE80211_F_WPA
892 arg = NDIS_80211_AUTHMODE_WPA;
893 r = ndis_set_info(sc,
894 OID_802_11_AUTHENTICATION_MODE, &arg, &i);
896 r = ndis_get_info(sc,
897 OID_802_11_AUTHENTICATION_MODE, &arg, &i);
898 if (r == 0 && arg == NDIS_80211_AUTHMODE_WPA)
899 ic->ic_caps |= IEEE80211_C_WPA;
903 * To test for supported ciphers, we set each
904 * available encryption type in descending order.
905 * If ENC3 works, then we have WEP, TKIP and AES.
906 * If only ENC2 works, then we have WEP and TKIP.
907 * If only ENC1 works, then we have just WEP.
910 arg = NDIS_80211_WEPSTAT_ENC3ENABLED;
911 r = ndis_set_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &i);
913 ic->ic_caps |= IEEE80211_C_WEP|IEEE80211_C_TKIP|
917 arg = NDIS_80211_WEPSTAT_ENC2ENABLED;
918 r = ndis_set_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &i);
920 ic->ic_caps |= IEEE80211_C_WEP|IEEE80211_C_TKIP;
924 arg = NDIS_80211_WEPSTAT_ENC1ENABLED;
925 r = ndis_set_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &i);
927 ic->ic_caps |= IEEE80211_C_WEP;
928 #ifdef IEEE80211_F_WPA
932 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
934 ic->ic_caps |= IEEE80211_C_PMGT;
935 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
936 #if __FreeBSD_version < 600007
937 ieee80211_ifattach(ifp);
938 ieee80211_media_init(ifp, ieee80211_media_change,
941 ieee80211_ifattach(ic);
942 ieee80211_media_init(ic, ieee80211_media_change,
945 ic->ic_scan_start = ndis_scan_start;
946 ic->ic_scan_end = ndis_scan_end;
947 ic->ic_set_channel = ndis_set_channel;
948 ic->ic_scan_curchan = ndis_scan_curchan;
949 ic->ic_scan_mindwell = ndis_scan_mindwell;
950 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
951 ic->ic_bss->ni_chan = ic->ic_bsschan;
952 /* override state transition machine */
953 sc->ndis_newstate = ic->ic_newstate;
954 ic->ic_newstate = ndis_newstate;
955 #ifdef IEEE80211_F_WPA
956 /* install key handing routines */
957 ic->ic_crypto.cs_key_set = ndis_add_key;
958 ic->ic_crypto.cs_key_delete = ndis_del_key;
961 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
963 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
964 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
965 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
966 ifmedia_add(&sc->ifmedia,
967 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
968 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
969 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
970 ether_ifattach(ifp, eaddr);
977 /* We're done talking to the NIC for now; halt it. */
984 * Shutdown hardware and free up resources. This can be called any
985 * time after the mutex has been initialized. It is called in both
986 * the error case in attach and the normal detach case so it needs
987 * to be careful about only freeing resources that have actually been
994 struct ndis_softc *sc;
998 sc = device_get_softc(dev);
1001 ifp->if_flags &= ~IFF_UP;
1003 if (device_is_attached(dev)) {
1007 #if __FreeBSD_version < 600007
1008 ieee80211_ifdetach(ifp);
1010 ieee80211_ifdetach(&sc->ic);
1013 ether_ifdetach(ifp);
1017 taskqueue_drain(sc->ndis_tq, &sc->ndis_scantask);
1019 if (sc->ndis_tickitem != NULL)
1020 IoFreeWorkItem(sc->ndis_tickitem);
1021 if (sc->ndis_startitem != NULL)
1022 IoFreeWorkItem(sc->ndis_startitem);
1023 if (sc->ndis_resetitem != NULL)
1024 IoFreeWorkItem(sc->ndis_resetitem);
1025 if (sc->ndis_inputitem != NULL)
1026 IoFreeWorkItem(sc->ndis_inputitem);
1028 bus_generic_detach(dev);
1029 ndis_unload_driver(sc);
1032 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
1033 if (sc->ndis_res_io)
1034 bus_release_resource(dev, SYS_RES_IOPORT,
1035 sc->ndis_io_rid, sc->ndis_res_io);
1036 if (sc->ndis_res_mem)
1037 bus_release_resource(dev, SYS_RES_MEMORY,
1038 sc->ndis_mem_rid, sc->ndis_res_mem);
1039 if (sc->ndis_res_altmem)
1040 bus_release_resource(dev, SYS_RES_MEMORY,
1041 sc->ndis_altmem_rid, sc->ndis_res_altmem);
1043 #if __FreeBSD_version >= 600031
1048 if (sc->ndis_iftype == PCMCIABus)
1052 ndis_destroy_dma(sc);
1054 if (sc->ndis_txarray)
1055 free(sc->ndis_txarray, M_DEVBUF);
1057 if (!sc->ndis_80211)
1058 ifmedia_removeall(&sc->ifmedia);
1060 if (sc->ndis_txpool != NULL)
1061 NdisFreePacketPool(sc->ndis_txpool);
1063 /* Destroy the PDO for this device. */
1065 if (sc->ndis_iftype == PCIBus)
1066 drv = windrv_lookup(0, "PCI Bus");
1067 else if (sc->ndis_iftype == PCMCIABus)
1068 drv = windrv_lookup(0, "PCCARD Bus");
1070 drv = windrv_lookup(0, "USB Bus");
1072 panic("couldn't find driver object");
1073 windrv_destroy_pdo(drv, dev);
1075 if (sc->ndis_iftype == PCIBus)
1076 bus_dma_tag_destroy(sc->ndis_parent_tag);
1078 taskqueue_free(sc->ndis_tq);
1079 #if __FreeBSD_version < 502113
1080 sysctl_ctx_free(&sc->ndis_ctx);
1090 struct ndis_softc *sc;
1093 sc = device_get_softc(dev);
1097 if (NDIS_INITIALIZED(sc))
1108 struct ndis_softc *sc;
1111 sc = device_get_softc(dev);
1114 if (NDIS_INITIALIZED(sc))
1121 * The following bunch of routines are here to support drivers that
1122 * use the NdisMEthIndicateReceive()/MiniportTransferData() mechanism.
1123 * The NdisMEthIndicateReceive() handler runs at DISPATCH_LEVEL for
1124 * serialized miniports, or IRQL <= DISPATCH_LEVEL for deserialized
1129 ndis_rxeof_eth(adapter, ctx, addr, hdr, hdrlen, lookahead, lookaheadlen, pktlen)
1130 ndis_handle adapter;
1136 uint32_t lookaheadlen;
1139 ndis_miniport_block *block;
1149 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1156 /* Save the data provided to us so far. */
1158 m->m_len = lookaheadlen + hdrlen;
1159 m->m_pkthdr.len = pktlen + hdrlen;
1161 m_copyback(m, 0, hdrlen, hdr);
1162 m_copyback(m, hdrlen, lookaheadlen, lookahead);
1164 /* Now create a fake NDIS_PACKET to hold the data */
1166 NdisAllocatePacket(&status, &p, block->nmb_rxpool);
1168 if (status != NDIS_STATUS_SUCCESS) {
1175 b = IoAllocateMdl(m->m_data, m->m_pkthdr.len, FALSE, FALSE, NULL);
1183 p->np_private.npp_head = p->np_private.npp_tail = b;
1184 p->np_private.npp_totlen = m->m_pkthdr.len;
1186 /* Save the packet RX context somewhere. */
1187 priv = (ndis_ethpriv *)&p->np_protocolreserved;
1188 priv->nep_ctx = ctx;
1190 if (!NDIS_SERIALIZED(block))
1191 KeAcquireSpinLock(&block->nmb_lock, &irql);
1193 InsertTailList((&block->nmb_packetlist), (&p->np_list));
1195 if (!NDIS_SERIALIZED(block))
1196 KeReleaseSpinLock(&block->nmb_lock, irql);
1202 * NdisMEthIndicateReceiveComplete() handler, runs at DISPATCH_LEVEL
1203 * for serialized miniports, or IRQL <= DISPATCH_LEVEL for deserialized
1208 ndis_rxeof_done(adapter)
1209 ndis_handle adapter;
1211 struct ndis_softc *sc;
1212 ndis_miniport_block *block;
1216 /* Schedule transfer/RX of queued packets. */
1218 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1220 KeInsertQueueDpc(&sc->ndis_rxdpc, NULL, NULL);
1226 * MiniportTransferData() handler, runs at DISPATCH_LEVEL.
1229 ndis_rxeof_xfr(dpc, adapter, sysarg1, sysarg2)
1231 ndis_handle adapter;
1235 ndis_miniport_block *block;
1236 struct ndis_softc *sc;
1245 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1248 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
1250 l = block->nmb_packetlist.nle_flink;
1251 while(!IsListEmpty(&block->nmb_packetlist)) {
1252 l = RemoveHeadList((&block->nmb_packetlist));
1253 p = CONTAINING_RECORD(l, ndis_packet, np_list);
1254 InitializeListHead((&p->np_list));
1256 priv = (ndis_ethpriv *)&p->np_protocolreserved;
1261 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
1263 status = MSCALL6(sc->ndis_chars->nmc_transferdata_func,
1264 p, &p->np_private.npp_totlen, block, priv->nep_ctx,
1265 m->m_len, m->m_pkthdr.len - m->m_len);
1267 KeAcquireSpinLockAtDpcLevel(&block->nmb_lock);
1270 * If status is NDIS_STATUS_PENDING, do nothing and
1271 * wait for a callback to the ndis_rxeof_xfr_done()
1275 m->m_len = m->m_pkthdr.len;
1276 m->m_pkthdr.rcvif = ifp;
1278 if (status == NDIS_STATUS_SUCCESS) {
1279 IoFreeMdl(p->np_private.npp_head);
1281 KeAcquireSpinLockAtDpcLevel(&sc->ndis_rxlock);
1282 _IF_ENQUEUE(&sc->ndis_rxqueue, m);
1283 KeReleaseSpinLockFromDpcLevel(&sc->ndis_rxlock);
1284 IoQueueWorkItem(sc->ndis_inputitem,
1285 (io_workitem_func)ndis_inputtask_wrap,
1286 WORKQUEUE_CRITICAL, ifp);
1289 if (status == NDIS_STATUS_FAILURE)
1292 /* Advance to next packet */
1293 l = block->nmb_packetlist.nle_flink;
1296 KeReleaseSpinLockFromDpcLevel(&block->nmb_lock);
1302 * NdisMTransferDataComplete() handler, runs at DISPATCH_LEVEL.
1305 ndis_rxeof_xfr_done(adapter, packet, status, len)
1306 ndis_handle adapter;
1307 ndis_packet *packet;
1311 ndis_miniport_block *block;
1312 struct ndis_softc *sc;
1317 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1321 IoFreeMdl(packet->np_private.npp_head);
1322 NdisFreePacket(packet);
1324 if (status != NDIS_STATUS_SUCCESS) {
1329 m->m_len = m->m_pkthdr.len;
1330 m->m_pkthdr.rcvif = ifp;
1331 KeAcquireSpinLockAtDpcLevel(&sc->ndis_rxlock);
1332 _IF_ENQUEUE(&sc->ndis_rxqueue, m);
1333 KeReleaseSpinLockFromDpcLevel(&sc->ndis_rxlock);
1334 IoQueueWorkItem(sc->ndis_inputitem,
1335 (io_workitem_func)ndis_inputtask_wrap,
1336 WORKQUEUE_CRITICAL, ifp);
1341 * A frame has been uploaded: pass the resulting mbuf chain up to
1342 * the higher level protocols.
1344 * When handling received NDIS packets, the 'status' field in the
1345 * out-of-band portion of the ndis_packet has special meaning. In the
1346 * most common case, the underlying NDIS driver will set this field
1347 * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
1348 * take posession of it. We then change the status field to
1349 * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
1350 * and that we will return it at some point in the future via the
1351 * return packet handler.
1353 * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
1354 * this means the driver is running out of packet/buffer resources and
1355 * wants to maintain ownership of the packet. In this case, we have to
1356 * copy the packet data into local storage and let the driver keep the
1360 ndis_rxeof(adapter, packets, pktcnt)
1361 ndis_handle adapter;
1362 ndis_packet **packets;
1365 struct ndis_softc *sc;
1366 ndis_miniport_block *block;
1369 ndis_tcpip_csum *csum;
1371 struct mbuf *m0, *m;
1374 block = (ndis_miniport_block *)adapter;
1375 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1379 * There's a slim chance the driver may indicate some packets
1380 * before we're completely ready to handle them. If we detect this,
1381 * we need to return them to the miniport and ignore them.
1384 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1385 for (i = 0; i < pktcnt; i++) {
1387 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS) {
1389 ndis_return_packet(p, block);
1395 for (i = 0; i < pktcnt; i++) {
1397 /* Stash the softc here so ptom can use it. */
1399 if (ndis_ptom(&m0, p)) {
1400 device_printf (sc->ndis_dev, "ptom failed\n");
1401 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
1402 ndis_return_packet(p, block);
1405 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
1406 m = m_dup(m0, M_DONTWAIT);
1408 * NOTE: we want to destroy the mbuf here, but
1409 * we don't actually want to return it to the
1410 * driver via the return packet handler. By
1411 * bumping np_refcnt, we can prevent the
1412 * ndis_return_packet() routine from actually
1422 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1424 m = m_dup(m0, M_DONTWAIT);
1425 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES)
1428 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1435 m0->m_pkthdr.rcvif = ifp;
1437 /* Deal with checksum offload. */
1439 if (ifp->if_capenable & IFCAP_RXCSUM &&
1440 p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
1442 p->np_ext.npe_info[ndis_tcpipcsum_info];
1443 csum = (ndis_tcpip_csum *)&s;
1444 if (csum->u.ntc_rxflags &
1445 NDIS_RXCSUM_IP_PASSED)
1446 m0->m_pkthdr.csum_flags |=
1447 CSUM_IP_CHECKED|CSUM_IP_VALID;
1448 if (csum->u.ntc_rxflags &
1449 (NDIS_RXCSUM_TCP_PASSED |
1450 NDIS_RXCSUM_UDP_PASSED)) {
1451 m0->m_pkthdr.csum_flags |=
1452 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1453 m0->m_pkthdr.csum_data = 0xFFFF;
1457 KeAcquireSpinLockAtDpcLevel(&sc->ndis_rxlock);
1458 _IF_ENQUEUE(&sc->ndis_rxqueue, m0);
1459 KeReleaseSpinLockFromDpcLevel(&sc->ndis_rxlock);
1460 IoQueueWorkItem(sc->ndis_inputitem,
1461 (io_workitem_func)ndis_inputtask_wrap,
1462 WORKQUEUE_CRITICAL, ifp);
1470 * This routine is run at PASSIVE_LEVEL. We use this routine to pass
1471 * packets into the stack in order to avoid calling (*ifp->if_input)()
1472 * with any locks held (at DISPATCH_LEVEL, we'll be holding the
1473 * 'dispatch level' per-cpu sleep lock).
1477 ndis_inputtask(dobj, arg)
1478 device_object *dobj;
1481 ndis_miniport_block *block;
1483 struct ndis_softc *sc;
1489 block = dobj->do_devext;
1491 KeAcquireSpinLock(&sc->ndis_rxlock, &irql);
1493 _IF_DEQUEUE(&sc->ndis_rxqueue, m);
1496 KeReleaseSpinLock(&sc->ndis_rxlock, irql);
1498 (*ifp->if_input)(ifp, m);
1499 KeAcquireSpinLock(&sc->ndis_rxlock, &irql);
1501 KeReleaseSpinLock(&sc->ndis_rxlock, irql);
1507 * A frame was downloaded to the chip. It's safe for us to clean up
1511 ndis_txeof(adapter, packet, status)
1512 ndis_handle adapter;
1513 ndis_packet *packet;
1517 struct ndis_softc *sc;
1518 ndis_miniport_block *block;
1523 block = (ndis_miniport_block *)adapter;
1524 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1528 idx = packet->np_txidx;
1530 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
1532 ndis_free_packet(packet);
1536 sc->ndis_txarray[idx] = NULL;
1537 sc->ndis_txpending++;
1539 if (status == NDIS_STATUS_SUCCESS)
1545 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1549 IoQueueWorkItem(sc->ndis_startitem,
1550 (io_workitem_func)ndis_starttask_wrap,
1551 WORKQUEUE_CRITICAL, ifp);
1557 ndis_linksts(adapter, status, sbuf, slen)
1558 ndis_handle adapter;
1563 ndis_miniport_block *block;
1564 struct ndis_softc *sc;
1567 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1568 sc->ndis_sts = status;
1570 /* Event list is all full up, drop this one. */
1573 if (sc->ndis_evt[sc->ndis_evtpidx].ne_sts) {
1578 /* Cache the event. */
1581 sc->ndis_evt[sc->ndis_evtpidx].ne_buf = malloc(slen,
1583 if (sc->ndis_evt[sc->ndis_evtpidx].ne_buf == NULL) {
1588 sc->ndis_evt[sc->ndis_evtpidx].ne_buf, slen);
1590 sc->ndis_evt[sc->ndis_evtpidx].ne_sts = status;
1591 sc->ndis_evt[sc->ndis_evtpidx].ne_len = slen;
1592 NDIS_EVTINC(sc->ndis_evtpidx);
1599 ndis_linksts_done(adapter)
1600 ndis_handle adapter;
1602 ndis_miniport_block *block;
1603 struct ndis_softc *sc;
1607 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1610 if (!NDIS_INITIALIZED(sc))
1613 switch (sc->ndis_sts) {
1614 case NDIS_STATUS_MEDIA_CONNECT:
1615 IoQueueWorkItem(sc->ndis_tickitem,
1616 (io_workitem_func)ndis_ticktask_wrap,
1617 WORKQUEUE_CRITICAL, sc);
1618 IoQueueWorkItem(sc->ndis_startitem,
1619 (io_workitem_func)ndis_starttask_wrap,
1620 WORKQUEUE_CRITICAL, ifp);
1622 case NDIS_STATUS_MEDIA_DISCONNECT:
1624 IoQueueWorkItem(sc->ndis_tickitem,
1625 (io_workitem_func)ndis_ticktask_wrap,
1626 WORKQUEUE_CRITICAL, sc);
1632 /* Notify possible listners of interface change. */
1643 struct ndis_softc *sc;
1649 IoQueueWorkItem(sc->ndis_tickitem,
1650 (io_workitem_func)ndis_ticktask_wrap,
1651 WORKQUEUE_CRITICAL, sc);
1652 sc->ndis_stat_ch = timeout(ndis_tick, sc, hz *
1653 sc->ndis_block->nmb_checkforhangsecs);
1661 ndis_ticktask(d, xsc)
1665 struct ndis_softc *sc;
1666 struct ieee80211com *ic;
1667 ndis_checkforhang_handler hangfunc;
1674 if (!NDIS_INITIALIZED(sc)) {
1680 hangfunc = sc->ndis_chars->nmc_checkhang_func;
1682 if (hangfunc != NULL) {
1683 rval = MSCALL1(hangfunc,
1684 sc->ndis_block->nmb_miniportadapterctx);
1692 if (sc->ndis_link == 0 &&
1693 sc->ndis_sts == NDIS_STATUS_MEDIA_CONNECT) {
1696 if (sc->ndis_80211) {
1697 ndis_getstate_80211(sc);
1698 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1701 #ifdef LINK_STATE_UP
1702 #if __FreeBSD_version > 600006
1703 if_link_state_change(sc->ifp, LINK_STATE_UP);
1705 sc->ifp->if_link_state = LINK_STATE_UP;
1709 device_printf(sc->ndis_dev, "link state changed to UP\n");
1710 #endif /* LINK_STATE_UP */
1713 if (sc->ndis_link == 1 &&
1714 sc->ndis_sts == NDIS_STATUS_MEDIA_DISCONNECT) {
1717 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
1718 #ifdef LINK_STATE_DOWN
1719 #if __FreeBSD_version > 600006
1720 if_link_state_change(sc->ifp, LINK_STATE_DOWN);
1722 sc->ifp->if_link_state = LINK_STATE_DOWN;
1726 device_printf(sc->ndis_dev, "link state changed to DOWN\n");
1727 #endif /* LINK_STATE_DOWN */
1736 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1738 bus_dma_segment_t *segs;
1744 struct ndis_sc_list *sclist;
1747 if (error || arg == NULL)
1752 sclist->nsl_frags = nseg;
1754 for (i = 0; i < nseg; i++) {
1755 sclist->nsl_elements[i].nse_addr.np_quad = segs[i].ds_addr;
1756 sclist->nsl_elements[i].nse_len = segs[i].ds_len;
1763 ndis_starttask(d, arg)
1771 #if __FreeBSD_version < 502114
1772 if (ifp->if_snd.ifq_head != NULL)
1774 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1781 * Main transmit routine. To make NDIS drivers happy, we need to
1782 * transform mbuf chains into NDIS packets and feed them to the
1783 * send packet routines. Most drivers allow you to send several
1784 * packets at once (up to the maxpkts limit). Unfortunately, rather
1785 * that accepting them in the form of a linked list, they expect
1786 * a contiguous array of pointers to packets.
1788 * For those drivers which use the NDIS scatter/gather DMA mechanism,
1789 * we need to perform busdma work here. Those that use map registers
1790 * will do the mapping themselves on a buffer by buffer basis.
1797 struct ndis_softc *sc;
1798 struct mbuf *m = NULL;
1799 ndis_packet **p0 = NULL, *p = NULL;
1800 ndis_tcpip_csum *csum;
1801 int pcnt = 0, status;
1807 if (!sc->ndis_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1812 p0 = &sc->ndis_txarray[sc->ndis_txidx];
1814 while(sc->ndis_txpending) {
1815 #if __FreeBSD_version < 502114
1816 IF_DEQUEUE(&ifp->if_snd, m);
1818 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1823 NdisAllocatePacket(&status,
1824 &sc->ndis_txarray[sc->ndis_txidx], sc->ndis_txpool);
1826 if (status != NDIS_STATUS_SUCCESS)
1829 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1830 #if __FreeBSD_version >= 502114
1831 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1834 #if __FreeBSD_version < 502114
1835 IF_PREPEND(&ifp->if_snd, m);
1841 * Save pointer to original mbuf
1842 * so we can free it later.
1845 p = sc->ndis_txarray[sc->ndis_txidx];
1846 p->np_txidx = sc->ndis_txidx;
1848 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1851 * Do scatter/gather processing, if driver requested it.
1854 bus_dmamap_load_mbuf(sc->ndis_ttag,
1855 sc->ndis_tmaps[sc->ndis_txidx], m,
1856 ndis_map_sclist, &p->np_sclist, BUS_DMA_NOWAIT);
1857 bus_dmamap_sync(sc->ndis_ttag,
1858 sc->ndis_tmaps[sc->ndis_txidx],
1859 BUS_DMASYNC_PREREAD);
1860 p->np_ext.npe_info[ndis_sclist_info] = &p->np_sclist;
1863 /* Handle checksum offload. */
1865 if (ifp->if_capenable & IFCAP_TXCSUM &&
1866 m->m_pkthdr.csum_flags) {
1867 csum = (ndis_tcpip_csum *)
1868 &p->np_ext.npe_info[ndis_tcpipcsum_info];
1869 csum->u.ntc_txflags = NDIS_TXCSUM_DO_IPV4;
1870 if (m->m_pkthdr.csum_flags & CSUM_IP)
1871 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_IP;
1872 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1873 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_TCP;
1874 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1875 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_UDP;
1876 p->np_private.npp_flags = NDIS_PROTOCOL_ID_TCP_IP;
1880 sc->ndis_txpending--;
1885 * If there's a BPF listener, bounce a copy of this frame
1892 * The array that p0 points to must appear contiguous,
1893 * so we must not wrap past the end of sc->ndis_txarray[].
1894 * If it looks like we're about to wrap, break out here
1895 * so the this batch of packets can be transmitted, then
1896 * wait for txeof to ask us to send the rest.
1899 if (sc->ndis_txidx == 0)
1908 if (sc->ndis_txpending == 0)
1909 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1912 * Set a timeout in case the chip goes out to lunch.
1919 * According to NDIS documentation, if a driver exports
1920 * a MiniportSendPackets() routine, we prefer that over
1921 * a MiniportSend() routine (which sends just a single
1925 if (sc->ndis_chars->nmc_sendmulti_func != NULL)
1926 ndis_send_packets(sc, p0, pcnt);
1928 ndis_send_packet(sc, p);
1937 struct ndis_softc *sc = xsc;
1938 struct ieee80211com *ic = (void *)&sc->ic;
1939 struct ifnet *ifp = sc->ifp;
1943 * Avoid reintializing the link unnecessarily.
1944 * This should be dealt with in a better way by
1945 * fixing the upper layer modules so they don't
1946 * call ifp->if_init() quite as often.
1948 if (sc->ndis_link && sc->ndis_skip)
1952 * Cancel pending I/O and free all RX/TX buffers.
1956 if (ndis_init_nic(sc))
1959 /* Init our MAC address */
1961 /* Program the packet filter */
1963 sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1965 if (ifp->if_flags & IFF_BROADCAST)
1966 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1968 if (ifp->if_flags & IFF_PROMISC)
1969 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1971 len = sizeof(sc->ndis_filter);
1973 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1974 &sc->ndis_filter, &len);
1977 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1985 ndis_set_info(sc, OID_GEN_CURRENT_LOOKAHEAD, &i, &len);
1988 * Program the multicast filter, if necessary.
1992 /* Setup task offload. */
1993 ndis_set_offload(sc);
1996 ndis_setstate_80211(sc);
2001 sc->ndis_txpending = sc->ndis_maxpkts;
2004 #ifdef LINK_STATE_UNKNOWN
2005 #if __FreeBSD_version > 600006
2006 if_link_state_change(sc->ifp, LINK_STATE_UNKNOWN);
2008 sc->ifp->if_link_state = LINK_STATE_DOWN;
2011 #endif /* LINK_STATE_UNKNOWN */
2013 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2015 * NB: When restarting the adapter clock the state
2016 * machine regardless of the roaming mode; otherwise
2017 * we need to notify user apps so they can manually
2018 * get us going again.
2020 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2021 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
2023 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2025 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2026 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2031 * Some drivers don't set this value. The NDIS spec says
2032 * the default checkforhang timeout is "approximately 2
2033 * seconds." We use 3 seconds, because it seems for some
2034 * drivers, exactly 2 seconds is too fast.
2037 if (sc->ndis_block->nmb_checkforhangsecs == 0)
2038 sc->ndis_block->nmb_checkforhangsecs = 3;
2040 sc->ndis_stat_ch = timeout(ndis_tick, sc,
2041 hz * sc->ndis_block->nmb_checkforhangsecs);
2047 * Set media options.
2050 ndis_ifmedia_upd(ifp)
2053 struct ndis_softc *sc;
2057 if (NDIS_INITIALIZED(sc))
2064 * Report current media status.
2067 ndis_ifmedia_sts(ifp, ifmr)
2069 struct ifmediareq *ifmr;
2071 struct ndis_softc *sc;
2072 uint32_t media_info;
2073 ndis_media_state linkstate;
2076 ifmr->ifm_status = IFM_AVALID;
2077 ifmr->ifm_active = IFM_ETHER;
2080 if (!NDIS_INITIALIZED(sc))
2083 len = sizeof(linkstate);
2084 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
2085 (void *)&linkstate, &len);
2087 len = sizeof(media_info);
2088 error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
2089 (void *)&media_info, &len);
2091 if (linkstate == nmc_connected)
2092 ifmr->ifm_status |= IFM_ACTIVE;
2094 switch(media_info) {
2096 ifmr->ifm_active |= IFM_10_T;
2099 ifmr->ifm_active |= IFM_100_TX;
2102 ifmr->ifm_active |= IFM_1000_T;
2105 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
2112 #ifdef IEEE80211_F_WPA
2114 ndis_set_cipher(sc, cipher)
2115 struct ndis_softc *sc;
2118 struct ieee80211com *ic;
2126 if (cipher == WPA_CSE_WEP40 || WPA_CSE_WEP104) {
2127 if (!(ic->ic_caps & IEEE80211_C_WEP))
2129 arg = NDIS_80211_WEPSTAT_ENC1ENABLED;
2132 if (cipher == WPA_CSE_TKIP) {
2133 if (!(ic->ic_caps & IEEE80211_C_TKIP))
2135 arg = NDIS_80211_WEPSTAT_ENC2ENABLED;
2138 if (cipher == WPA_CSE_CCMP) {
2139 if (!(ic->ic_caps & IEEE80211_C_AES_CCM))
2141 arg = NDIS_80211_WEPSTAT_ENC3ENABLED;
2144 DPRINTF(("Setting cipher to %d\n", arg));
2146 rval = ndis_set_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2151 /* Check that the cipher was set correctly. */
2154 rval = ndis_get_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2156 if (rval != 0 || arg != save)
2163 * WPA is hairy to set up. Do the work in a separate routine
2164 * so we don't clutter the setstate function too much.
2165 * Important yet undocumented fact: first we have to set the
2166 * authentication mode, _then_ we enable the ciphers. If one
2167 * of the WPA authentication modes isn't enabled, the driver
2168 * might not permit the TKIP or AES ciphers to be selected.
2173 struct ndis_softc *sc;
2175 struct ieee80211com *ic;
2176 struct ieee80211_ie_wpa *w;
2185 * Apparently, the only way for us to know what ciphers
2186 * and key management/authentication mode to use is for
2187 * us to inspect the optional information element (IE)
2188 * stored in the 802.11 state machine. This IE should be
2189 * supplied by the WPA supplicant.
2192 w = (struct ieee80211_ie_wpa *)ic->ic_opt_ie;
2194 /* Check for the right kind of IE. */
2195 if (w->wpa_id != IEEE80211_ELEMID_VENDOR) {
2196 DPRINTF(("Incorrect IE type %d\n", w->wpa_id));
2200 /* Skip over the ucast cipher OIDs. */
2201 pos = (char *)&w->wpa_uciphers[0];
2202 pos += w->wpa_uciphercnt * sizeof(struct ndis_ie);
2204 /* Skip over the authmode count. */
2205 pos += sizeof(u_int16_t);
2208 * Check for the authentication modes. I'm
2209 * pretty sure there's only supposed to be one.
2212 n = (struct ndis_ie *)pos;
2213 if (n->ni_val == WPA_ASE_NONE)
2214 arg = NDIS_80211_AUTHMODE_WPANONE;
2216 if (n->ni_val == WPA_ASE_8021X_UNSPEC)
2217 arg = NDIS_80211_AUTHMODE_WPA;
2219 if (n->ni_val == WPA_ASE_8021X_PSK)
2220 arg = NDIS_80211_AUTHMODE_WPAPSK;
2222 DPRINTF(("Setting WPA auth mode to %d\n", arg));
2224 if (ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &i))
2227 ndis_get_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &i);
2229 /* Now configure the desired ciphers. */
2231 /* First, set up the multicast group cipher. */
2232 n = (struct ndis_ie *)&w->wpa_mcipher[0];
2234 if (ndis_set_cipher(sc, n->ni_val))
2237 /* Now start looking around for the unicast ciphers. */
2238 pos = (char *)&w->wpa_uciphers[0];
2239 n = (struct ndis_ie *)pos;
2241 for (i = 0; i < w->wpa_uciphercnt; i++) {
2242 if (ndis_set_cipher(sc, n->ni_val))
2252 ndis_setstate_80211(sc)
2253 struct ndis_softc *sc;
2255 struct ieee80211com *ic;
2256 struct ieee80211_node *ni;
2257 ndis_80211_ssid ssid;
2258 ndis_80211_macaddr bssid;
2259 ndis_80211_config config;
2261 int i, rval = 0, len;
2269 if (!NDIS_INITIALIZED(sc)) {
2270 DPRINTF(("%s: NDIS not initialized\n", __func__));
2274 /* Disassociate and turn off radio. */
2278 ndis_set_info(sc, OID_802_11_DISASSOCIATE, &arg, &len);
2280 /* Set network infrastructure mode. */
2283 if (ic->ic_opmode == IEEE80211_M_IBSS)
2284 arg = NDIS_80211_NET_INFRA_IBSS;
2286 arg = NDIS_80211_NET_INFRA_BSS;
2288 rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
2291 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
2293 /* Set RTS threshold */
2296 arg = ic->ic_rtsthreshold;
2297 ndis_set_info(sc, OID_802_11_RTS_THRESHOLD, &arg, &len);
2299 /* Set fragmentation threshold */
2302 arg = ic->ic_fragthreshold;
2303 ndis_set_info(sc, OID_802_11_FRAGMENTATION_THRESHOLD, &arg, &len);
2305 /* Set power management */
2308 if (ic->ic_flags & IEEE80211_F_PMGTON)
2309 arg = NDIS_80211_POWERMODE_FAST_PSP;
2311 arg = NDIS_80211_POWERMODE_CAM;
2312 ndis_set_info(sc, OID_802_11_POWER_MODE, &arg, &len);
2315 * Default encryption mode to off, authentication
2316 * to open and privacy to 'accept everything.'
2320 arg = NDIS_80211_WEPSTAT_DISABLED;
2321 ndis_set_info(sc, OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2324 arg = NDIS_80211_AUTHMODE_OPEN;
2325 ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2328 * Note that OID_80211_PRIVACY_FILTER is optional:
2329 * not all drivers implement it.
2332 arg = NDIS_80211_PRIVFILT_8021XWEP;
2333 ndis_set_info(sc, OID_802_11_PRIVACY_FILTER, &arg, &len);
2337 #if __FreeBSD_version < 600007
2338 if (ic->ic_flags & IEEE80211_F_WEPON) {
2340 if (ic->ic_flags & IEEE80211_F_PRIVACY &&
2341 !(ic->ic_flags & IEEE80211_F_WPA)) {
2345 #if __FreeBSD_version >= 600007
2346 if (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) {
2348 arg = NDIS_80211_AUTHMODE_SHARED;
2349 DPRINTF(("Setting shared auth\n"));
2350 ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE,
2354 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2355 if (ic->ic_nw_keys[i].wk_keylen) {
2356 #if __FreeBSD_version >= 600007
2357 if (ic->ic_nw_keys[i].wk_cipher->ic_cipher !=
2358 IEEE80211_CIPHER_WEP)
2361 bzero((char *)&wep, sizeof(wep));
2362 wep.nw_keylen = ic->ic_nw_keys[i].wk_keylen;
2365 * 5, 13 and 16 are the only valid
2366 * only valid key lengths. Anything
2367 * in between will be zero padded out to
2368 * the next highest boundary.
2370 if (ic->ic_nw_keys[i].wk_keylen < 5)
2372 else if (ic->ic_nw_keys[i].wk_keylen > 5 &&
2373 ic->ic_nw_keys[i].wk_keylen < 13)
2375 else if (ic->ic_nw_keys[i].wk_keylen > 13 &&
2376 ic->ic_nw_keys[i].wk_keylen < 16)
2380 wep.nw_length = (sizeof(uint32_t) * 3)
2382 if (i == ic->ic_def_txkey)
2383 wep.nw_keyidx |= NDIS_80211_WEPKEY_TX;
2384 bcopy(ic->ic_nw_keys[i].wk_key,
2385 wep.nw_keydata, wep.nw_length);
2387 DPRINTF(("Setting WEP key %d\n", i));
2388 rval = ndis_set_info(sc,
2389 OID_802_11_ADD_WEP, &wep, &len);
2391 device_printf(sc->ndis_dev,
2392 "set wepkey failed: %d\n", rval);
2397 DPRINTF(("Setting WEP on\n"));
2398 arg = NDIS_80211_WEPSTAT_ENABLED;
2400 rval = ndis_set_info(sc,
2401 OID_802_11_WEP_STATUS, &arg, &len);
2403 device_printf(sc->ndis_dev,
2404 "enable WEP failed: %d\n", rval);
2405 #ifdef IEEE80211_F_DROPUNENC
2406 if (ic->ic_flags & IEEE80211_F_DROPUNENC)
2407 arg = NDIS_80211_PRIVFILT_8021XWEP;
2409 arg = NDIS_80211_PRIVFILT_ACCEPTALL;
2413 OID_802_11_PRIVACY_FILTER, &arg, &len);
2418 #ifdef IEEE80211_F_WPA
2421 if (ic->ic_flags & IEEE80211_F_WPA1 && ic->ic_opt_ie_len &&
2422 ic->ic_caps & IEEE80211_C_WPA)
2423 if (ndis_set_wpa(sc))
2424 device_printf(sc->ndis_dev, "WPA setup failed\n");
2428 /* Set network type. */
2432 switch (ic->ic_curmode) {
2433 case IEEE80211_MODE_11A:
2434 arg = NDIS_80211_NETTYPE_11OFDM5;
2436 case IEEE80211_MODE_11B:
2437 arg = NDIS_80211_NETTYPE_11DS;
2439 case IEEE80211_MODE_11G:
2440 arg = NDIS_80211_NETTYPE_11OFDM24;
2443 device_printf(sc->ndis_dev, "unknown mode: %d\n",
2448 DPRINTF(("Setting network type to %d\n", arg));
2450 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
2453 device_printf (sc->ndis_dev,
2454 "set nettype failed: %d\n", rval);
2458 len = sizeof(config);
2459 bzero((char *)&config, len);
2460 config.nc_length = len;
2461 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
2462 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
2465 * Some drivers expect us to initialize these values, so
2466 * provide some defaults.
2469 if (config.nc_beaconperiod == 0)
2470 config.nc_beaconperiod = 100;
2471 if (config.nc_atimwin == 0)
2472 config.nc_atimwin = 100;
2473 if (config.nc_fhconfig.ncf_dwelltime == 0)
2474 config.nc_fhconfig.ncf_dwelltime = 200;
2475 if (rval == 0 && ic->ic_bsschan != IEEE80211_CHAN_ANYC) {
2478 chan = ieee80211_chan2ieee(ic, ic->ic_bsschan);
2479 chanflag = config.nc_dsconfig > 2500000 ? IEEE80211_CHAN_2GHZ :
2480 IEEE80211_CHAN_5GHZ;
2481 if (chan != ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0)) {
2482 config.nc_dsconfig =
2483 ic->ic_bsschan->ic_freq * 1000;
2484 ic->ic_bss->ni_chan = ic->ic_bsschan;
2485 len = sizeof(config);
2486 config.nc_length = len;
2487 config.nc_fhconfig.ncf_length =
2488 sizeof(ndis_80211_config_fh);
2489 DPRINTF(("Setting channel to %ukHz\n", config.nc_dsconfig));
2490 rval = ndis_set_info(sc, OID_802_11_CONFIGURATION,
2493 device_printf(sc->ndis_dev, "couldn't change "
2494 "DS config to %ukHz: %d\n",
2495 config.nc_dsconfig, rval);
2498 device_printf(sc->ndis_dev, "couldn't retrieve "
2499 "channel info: %d\n", rval);
2502 * If the user selected a specific BSSID, try
2503 * to use that one. This is useful in the case where
2504 * there are several APs in range with the same network
2505 * name. To delete the BSSID, we use the broadcast
2506 * address as the BSSID.
2507 * Note that some drivers seem to allow setting a BSSID
2508 * in ad-hoc mode, which has the effect of forcing the
2509 * NIC to create an ad-hoc cell with a specific BSSID,
2510 * instead of a randomly chosen one. However, the net80211
2511 * code makes the assumtion that the BSSID setting is invalid
2512 * when you're in ad-hoc mode, so we don't allow that here.
2515 len = IEEE80211_ADDR_LEN;
2516 if (ic->ic_flags & IEEE80211_F_DESBSSID &&
2517 ic->ic_opmode != IEEE80211_M_IBSS)
2518 bcopy(ni->ni_bssid, bssid, len);
2520 bcopy(ifp->if_broadcastaddr, bssid, len);
2522 DPRINTF(("Setting BSSID to %6D\n", &bssid, ":"));
2523 rval = ndis_set_info(sc, OID_802_11_BSSID, &bssid, &len);
2525 device_printf(sc->ndis_dev,
2526 "setting BSSID failed: %d\n", rval);
2528 /* Set SSID -- always do this last. */
2531 if (ndis_debug > 0) {
2532 printf("Setting ESSID to ");
2533 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2539 bzero((char *)&ssid, len);
2540 ssid.ns_ssidlen = ni->ni_esslen;
2541 if (ssid.ns_ssidlen == 0) {
2542 ssid.ns_ssidlen = 1;
2544 bcopy(ni->ni_essid, ssid.ns_ssid, ssid.ns_ssidlen);
2546 rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
2549 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
2551 if (ic->ic_state == IEEE80211_S_AUTH)
2552 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 0);
2558 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2560 struct ieee80211com *ic = &((struct ndis_softc *)ifp->if_softc)->ic;
2561 struct ieee80211_node *ni = NULL;
2563 imr->ifm_status = IFM_AVALID;
2564 imr->ifm_active = IFM_IEEE80211;
2565 if (ic->ic_state == IEEE80211_S_RUN)
2566 imr->ifm_status |= IFM_ACTIVE;
2567 imr->ifm_active |= IFM_AUTO;
2568 switch (ic->ic_opmode) {
2569 case IEEE80211_M_STA:
2571 /* calculate rate subtype */
2572 imr->ifm_active |= ieee80211_rate2media(ic,
2573 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
2575 case IEEE80211_M_IBSS:
2577 /* calculate rate subtype */
2578 imr->ifm_active |= ieee80211_rate2media(ic,
2579 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
2580 imr->ifm_active |= IFM_IEEE80211_ADHOC;
2582 case IEEE80211_M_AHDEMO:
2583 /* should not come here */
2585 case IEEE80211_M_HOSTAP:
2586 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
2588 case IEEE80211_M_MONITOR:
2589 imr->ifm_active |= IFM_IEEE80211_MONITOR;
2591 case IEEE80211_M_WDS:
2592 printf("WARNING: WDS operation mode not supported by NDIS\n");
2595 switch (ic->ic_curmode) {
2596 case IEEE80211_MODE_11A:
2597 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
2599 case IEEE80211_MODE_11B:
2600 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
2602 case IEEE80211_MODE_11G:
2603 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
2605 #ifdef IEEE80211_MODE_TURBO_A
2606 case IEEE80211_MODE_TURBO_A:
2607 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
2608 | IFM_IEEE80211_TURBO;
2615 ndis_get_assoc(sc, assoc)
2616 struct ndis_softc *sc;
2617 ndis_wlan_bssid_ex **assoc;
2619 ndis_80211_bssid_list_ex *bl;
2620 ndis_wlan_bssid_ex *bs;
2621 ndis_80211_macaddr bssid;
2627 len = sizeof(bssid);
2628 error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
2630 device_printf(sc->ndis_dev, "failed to get bssid\n");
2635 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2636 if (error != ENOSPC)
2639 bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
2640 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2643 device_printf(sc->ndis_dev, "bssid_list failed\n");
2647 bs = (ndis_wlan_bssid_ex *)&bl->nblx_bssid[0];
2648 for (i = 0; i < bl->nblx_items; i++) {
2649 if (bcmp(bs->nwbx_macaddr, bssid, sizeof(bssid)) == 0) {
2650 *assoc = malloc(bs->nwbx_len, M_TEMP, M_NOWAIT);
2651 if (*assoc == NULL) {
2655 bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
2659 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
2667 ndis_getstate_80211(sc)
2668 struct ndis_softc *sc;
2670 struct ieee80211com *ic;
2671 ndis_wlan_bssid_ex *bs;
2672 int rval, len, i = 0;
2680 if (!NDIS_INITIALIZED(sc))
2683 if ((rval = ndis_get_assoc(sc, &bs)) != 0)
2686 /* We're associated, retrieve info on the current bssid. */
2687 ic->ic_curmode = ndis_nettype_mode(bs->nwbx_nettype);
2688 chanflag = ndis_nettype_chan(bs->nwbx_nettype);
2689 IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid, bs->nwbx_macaddr);
2691 /* Get SSID from current association info. */
2692 bcopy(bs->nwbx_ssid.ns_ssid, ic->ic_bss->ni_essid,
2693 bs->nwbx_ssid.ns_ssidlen);
2694 ic->ic_bss->ni_esslen = bs->nwbx_ssid.ns_ssidlen;
2697 rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
2699 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
2702 if (isset(ic->ic_modecaps, IEEE80211_MODE_11B)) {
2703 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2704 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
2705 if ((ic->ic_bss->ni_rates.rs_rates[i] &
2706 IEEE80211_RATE_VAL) == arg / 5000)
2711 if (i == ic->ic_bss->ni_rates.rs_nrates &&
2712 isset(ic->ic_modecaps, IEEE80211_MODE_11G)) {
2713 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11G];
2714 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
2715 if ((ic->ic_bss->ni_rates.rs_rates[i] &
2716 IEEE80211_RATE_VAL) == arg / 5000)
2721 if (i == ic->ic_bss->ni_rates.rs_nrates)
2722 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
2725 ic->ic_bss->ni_txrate = i;
2727 if (ic->ic_caps & IEEE80211_C_PMGT) {
2729 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
2732 device_printf(sc->ndis_dev,
2733 "get power mode failed: %d\n", rval);
2734 if (arg == NDIS_80211_POWERMODE_CAM)
2735 ic->ic_flags &= ~IEEE80211_F_PMGTON;
2737 ic->ic_flags |= IEEE80211_F_PMGTON;
2741 * Use the current association information to reflect
2742 * what channel we're on.
2744 ic->ic_curchan = ieee80211_find_channel(ic,
2745 bs->nwbx_config.nc_dsconfig / 1000, chanflag);
2746 if (ic->ic_curchan == NULL)
2747 ic->ic_curchan = &ic->ic_channels[0];
2748 ic->ic_bss->ni_chan = ic->ic_curchan;
2749 ic->ic_bsschan = ic->ic_curchan;
2754 * Determine current authentication mode. Note: authmode
2755 * reporting isn't supported prior to FreeBSD 6.x.
2758 #if __FreeBSD_version >= 600007
2760 rval = ndis_get_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2762 device_printf (sc->ndis_dev,
2763 "get authmode status failed: %d\n", rval);
2765 ic->ic_flags &= ~IEEE80211_F_WPA;
2767 case NDIS_80211_AUTHMODE_OPEN:
2768 ic->ic_bss->ni_authmode = IEEE80211_AUTH_OPEN;
2770 case NDIS_80211_AUTHMODE_SHARED:
2771 ic->ic_bss->ni_authmode = IEEE80211_AUTH_SHARED;
2773 case NDIS_80211_AUTHMODE_AUTO:
2774 ic->ic_bss->ni_authmode = IEEE80211_AUTH_AUTO;
2776 case NDIS_80211_AUTHMODE_WPA:
2777 case NDIS_80211_AUTHMODE_WPAPSK:
2778 case NDIS_80211_AUTHMODE_WPANONE:
2779 ic->ic_bss->ni_authmode = IEEE80211_AUTH_WPA;
2780 ic->ic_flags |= IEEE80211_F_WPA1;
2782 case NDIS_80211_AUTHMODE_WPA2:
2783 case NDIS_80211_AUTHMODE_WPA2PSK:
2784 ic->ic_bss->ni_authmode = IEEE80211_AUTH_WPA;
2785 ic->ic_flags |= IEEE80211_F_WPA2;
2788 ic->ic_bss->ni_authmode = IEEE80211_AUTH_NONE;
2795 rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
2798 device_printf (sc->ndis_dev,
2799 "get wep status failed: %d\n", rval);
2801 #if __FreeBSD_version < 600007
2802 if (arg == NDIS_80211_WEPSTAT_ENABLED)
2803 ic->ic_flags |= IEEE80211_F_WEPON;
2805 ic->ic_flags &= ~IEEE80211_F_WEPON;
2807 if (arg == NDIS_80211_WEPSTAT_ENABLED)
2808 ic->ic_flags |= IEEE80211_F_PRIVACY|IEEE80211_F_DROPUNENC;
2810 ic->ic_flags &= ~(IEEE80211_F_PRIVACY|IEEE80211_F_DROPUNENC);
2818 ndis_ioctl(ifp, command, data)
2823 struct ndis_softc *sc = ifp->if_softc;
2824 struct ifreq *ifr = (struct ifreq *) data;
2825 struct ndis_oid_data oid;
2826 struct ndis_evt evt;
2834 if (ifp->if_flags & IFF_UP) {
2835 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
2836 ifp->if_flags & IFF_PROMISC &&
2837 !(sc->ndis_if_flags & IFF_PROMISC)) {
2839 NDIS_PACKET_TYPE_PROMISCUOUS;
2840 i = sizeof(sc->ndis_filter);
2841 error = ndis_set_info(sc,
2842 OID_GEN_CURRENT_PACKET_FILTER,
2843 &sc->ndis_filter, &i);
2844 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
2845 !(ifp->if_flags & IFF_PROMISC) &&
2846 sc->ndis_if_flags & IFF_PROMISC) {
2848 ~NDIS_PACKET_TYPE_PROMISCUOUS;
2849 i = sizeof(sc->ndis_filter);
2850 error = ndis_set_info(sc,
2851 OID_GEN_CURRENT_PACKET_FILTER,
2852 &sc->ndis_filter, &i);
2856 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2859 sc->ndis_if_flags = ifp->if_flags;
2869 if (sc->ndis_80211) {
2870 #if __FreeBSD_version < 600007
2871 error = ieee80211_ioctl(ifp, command, data);
2873 error = ieee80211_ioctl(&sc->ic, command, data);
2875 if (error == ENETRESET) {
2876 ndis_setstate_80211(sc);
2881 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
2884 ifp->if_capenable = ifr->ifr_reqcap;
2885 if (ifp->if_capenable & IFCAP_TXCSUM)
2886 ifp->if_hwassist = sc->ndis_hwassist;
2888 ifp->if_hwassist = 0;
2889 ndis_set_offload(sc);
2892 if (!NDIS_INITIALIZED(sc))
2895 error = ndis_80211_ioctl_get(ifp, command, data);
2900 if (!NDIS_INITIALIZED(sc))
2903 error = ndis_80211_ioctl_set(ifp, command, data);
2908 if ((error = priv_check(curthread, PRIV_DRIVER)))
2910 error = copyin(ifr->ifr_data, &oid, sizeof(oid));
2913 oidbuf = malloc(oid.len, M_TEMP, M_NOWAIT|M_ZERO);
2914 if (oidbuf == NULL) {
2918 error = copyin(ifr->ifr_data + sizeof(oid), oidbuf, oid.len);
2920 free(oidbuf, M_TEMP);
2923 error = ndis_get_info(sc, oid.oid, oidbuf, &oid.len);
2925 free(oidbuf, M_TEMP);
2928 error = copyout(&oid, ifr->ifr_data, sizeof(oid));
2930 free(oidbuf, M_TEMP);
2933 error = copyout(oidbuf, ifr->ifr_data + sizeof(oid), oid.len);
2934 free(oidbuf, M_TEMP);
2937 if ((error = priv_check(curthread, PRIV_DRIVER)))
2939 error = copyin(ifr->ifr_data, &oid, sizeof(oid));
2942 oidbuf = malloc(oid.len, M_TEMP, M_NOWAIT|M_ZERO);
2943 if (oidbuf == NULL) {
2947 error = copyin(ifr->ifr_data + sizeof(oid), oidbuf, oid.len);
2949 free(oidbuf, M_TEMP);
2952 error = ndis_set_info(sc, oid.oid, oidbuf, &oid.len);
2954 free(oidbuf, M_TEMP);
2957 error = copyout(&oid, ifr->ifr_data, sizeof(oid));
2959 free(oidbuf, M_TEMP);
2962 error = copyout(oidbuf, ifr->ifr_data + sizeof(oid), oid.len);
2963 free(oidbuf, M_TEMP);
2965 case SIOCGPRIVATE_0:
2966 if ((error = priv_check(curthread, PRIV_DRIVER)))
2969 if (sc->ndis_evt[sc->ndis_evtcidx].ne_sts == 0) {
2974 error = copyin(ifr->ifr_data, &evt, sizeof(evt));
2979 if (evt.ne_len < sc->ndis_evt[sc->ndis_evtcidx].ne_len) {
2984 error = copyout(&sc->ndis_evt[sc->ndis_evtcidx],
2985 ifr->ifr_data, sizeof(uint32_t) * 2);
2990 if (sc->ndis_evt[sc->ndis_evtcidx].ne_len) {
2991 error = copyout(sc->ndis_evt[sc->ndis_evtcidx].ne_buf,
2992 ifr->ifr_data + (sizeof(uint32_t) * 2),
2993 sc->ndis_evt[sc->ndis_evtcidx].ne_len);
2998 free(sc->ndis_evt[sc->ndis_evtcidx].ne_buf, M_TEMP);
2999 sc->ndis_evt[sc->ndis_evtcidx].ne_buf = NULL;
3001 sc->ndis_evt[sc->ndis_evtcidx].ne_len = 0;
3002 sc->ndis_evt[sc->ndis_evtcidx].ne_sts = 0;
3003 NDIS_EVTINC(sc->ndis_evtcidx);
3006 #if __FreeBSD_version < 700046
3007 case SIOCGIFGENERIC:
3008 case SIOCSIFGENERIC:
3009 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
3010 if (command == SIOCGIFGENERIC)
3011 error = ndis_wi_ioctl_get(ifp, command, data);
3013 error = ndis_wi_ioctl_set(ifp, command, data);
3016 if (error != ENOTTY)
3022 if (sc->ndis_80211) {
3023 #if __FreeBSD_version < 600007
3024 error = ieee80211_ioctl(ifp, command, data);
3026 error = ieee80211_ioctl(&sc->ic, command, data);
3028 if (error == ENETRESET) {
3029 ndis_setstate_80211(sc);
3033 error = ether_ioctl(ifp, command, data);
3038 /*NDIS_UNLOCK(sc);*/
3043 #if __FreeBSD_version < 700046
3045 ndis_wi_ioctl_get(ifp, command, data)
3052 struct ndis_softc *sc;
3053 ndis_80211_bssid_list_ex *bl;
3054 ndis_wlan_bssid_ex *wb;
3055 struct wi_apinfo *api;
3056 int error, i, j, len, maxaps;
3059 ifr = (struct ifreq *)data;
3060 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
3064 switch (wreq.wi_type) {
3065 case WI_RID_READ_APS:
3067 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
3070 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
3072 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
3073 if (error != ENOSPC)
3075 bl = malloc(len, M_DEVBUF, M_NOWAIT|M_ZERO);
3076 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
3081 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
3082 maxaps = MIN(maxaps, bl->nblx_items);
3083 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
3084 *(int *)&wreq.wi_val = maxaps;
3085 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
3086 wb = bl->nblx_bssid;
3088 bzero(api, sizeof(*api));
3089 bcopy(&wb->nwbx_macaddr, &api->bssid,
3090 sizeof(api->bssid));
3091 api->namelen = wb->nwbx_ssid.ns_ssidlen;
3092 bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
3093 if (wb->nwbx_privacy)
3094 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
3095 /* XXX Where can we get noise information? */
3096 api->signal = wb->nwbx_rssi + 149; /* XXX */
3097 api->quality = api->signal;
3099 ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
3101 /* In "auto" infrastructure mode, this is useless. */
3102 if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
3103 api->capinfo |= IEEE80211_CAPINFO_IBSS;
3104 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
3105 j = sizeof(ndis_80211_rates_ex);
3106 /* handle other extended things */
3108 j = sizeof(ndis_80211_rates);
3109 for (i = api->rate = 0; i < j; i++)
3110 api->rate = MAX(api->rate, 5 *
3111 (wb->nwbx_supportedrates[i] & 0x7f));
3113 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
3116 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
3126 ndis_wi_ioctl_set(ifp, command, data)
3133 struct ndis_softc *sc;
3137 error = priv_check(curthread, PRIV_DRIVER);
3142 ifr = (struct ifreq *)data;
3143 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
3147 switch (wreq.wi_type) {
3148 case WI_RID_SCAN_APS:
3149 case WI_RID_SCAN_REQ: /* arguments ignored */
3152 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
3164 ndis_80211_ioctl_get(struct ifnet *ifp, u_long command, caddr_t data)
3166 struct ndis_softc *sc;
3167 struct ieee80211req *ireq;
3169 uint16_t nodename_u[IEEE80211_NWID_LEN + 1];
3174 ireq = (struct ieee80211req *) data;
3176 switch (ireq->i_type) {
3177 #ifdef IEEE80211_IOC_MLME
3178 case IEEE80211_IOC_MLME:
3182 case IEEE80211_IOC_STATIONNAME:
3183 error = ndis_get_info(sc, OID_GEN_MACHINE_NAME,
3187 us.us_len = us.us_maxlen = len;
3188 us.us_buf = nodename_u;
3189 if (RtlUnicodeStringToAnsiString(&as, &us, TRUE)) {
3193 ireq->i_len = as.as_len;
3194 error = copyout(as.as_buf, ireq->i_data, ireq->i_len);
3195 RtlFreeAnsiString(&as);
3198 #if __FreeBSD_version < 600007
3199 error = ieee80211_ioctl(ifp, command, data);
3201 error = ieee80211_ioctl(&sc->ic, command, data);
3209 #ifdef IEEE80211_F_WPA
3211 ndis_del_key(ic, key)
3212 struct ieee80211com *ic;
3213 const struct ieee80211_key *key;
3215 struct ndis_softc *sc;
3216 ndis_80211_key rkey;
3219 sc = ic->ic_ifp->if_softc;
3221 bzero((char *)&rkey, sizeof(rkey));
3225 rkey.nk_keyidx = key->wk_keyix;
3227 bcopy(ic->ic_ifp->if_broadcastaddr,
3228 rkey.nk_bssid, IEEE80211_ADDR_LEN);
3230 error = ndis_set_info(sc, OID_802_11_REMOVE_KEY, &rkey, &len);
3239 * In theory this could be called for any key, but we'll
3240 * only use it for WPA TKIP or AES keys. These need to be
3241 * set after initial authentication with the AP.
3245 ndis_add_key(ic, key, mac)
3246 struct ieee80211com *ic;
3247 const struct ieee80211_key *key;
3248 const uint8_t mac[IEEE80211_ADDR_LEN];
3250 struct ndis_softc *sc;
3251 ndis_80211_key rkey;
3254 sc = ic->ic_ifp->if_softc;
3256 switch (key->wk_cipher->ic_cipher) {
3257 case IEEE80211_CIPHER_TKIP:
3259 len = sizeof(ndis_80211_key);
3260 bzero((char *)&rkey, sizeof(rkey));
3263 rkey.nk_keylen = key->wk_keylen;
3265 if (key->wk_flags & IEEE80211_KEY_SWMIC)
3266 rkey.nk_keylen += 16;
3268 /* key index - gets weird in NDIS */
3270 if (key->wk_keyix != IEEE80211_KEYIX_NONE)
3271 rkey.nk_keyidx = key->wk_keyix;
3275 if (key->wk_flags & IEEE80211_KEY_XMIT)
3276 rkey.nk_keyidx |= 1 << 31;
3278 if (key->wk_flags & IEEE80211_KEY_GROUP) {
3279 bcopy(ic->ic_ifp->if_broadcastaddr,
3280 rkey.nk_bssid, IEEE80211_ADDR_LEN);
3282 bcopy(ic->ic_bss->ni_bssid,
3283 rkey.nk_bssid, IEEE80211_ADDR_LEN);
3285 rkey.nk_keyidx |= 1 << 30;
3288 /* need to set bit 29 based on keyrsc */
3289 rkey.nk_keyrsc = key->wk_keyrsc;
3292 rkey.nk_keyidx |= 1 << 29;
3294 if (key->wk_flags & IEEE80211_KEY_SWMIC) {
3295 bcopy(key->wk_key, rkey.nk_keydata, 16);
3296 bcopy(key->wk_key + 24, rkey.nk_keydata + 16, 8);
3297 bcopy(key->wk_key + 16, rkey.nk_keydata + 24, 8);
3299 bcopy(key->wk_key, rkey.nk_keydata, key->wk_keylen);
3301 error = ndis_set_info(sc, OID_802_11_ADD_KEY, &rkey, &len);
3303 case IEEE80211_CIPHER_WEP:
3307 * I don't know how to set up keys for the AES
3308 * cipher yet. Is it the same as TKIP?
3310 case IEEE80211_CIPHER_AES_CCM:
3316 /* We need to return 1 for success, 0 for failure. */
3326 ndis_80211_ioctl_set(struct ifnet *ifp, u_long command, caddr_t data)
3328 struct ndis_softc *sc;
3329 struct ieee80211req *ireq;
3330 int error = EINVAL, len;
3335 ireq = (struct ieee80211req *) data;
3337 switch (ireq->i_type) {
3338 #ifdef IEEE80211_IOC_COUNTERMEASURES
3339 case IEEE80211_IOC_COUNTERMEASURES:
3340 case IEEE80211_IOC_DROPUNENCRYPTED:
3344 case IEEE80211_IOC_STATIONNAME:
3345 error = priv_check(curthread, PRIV_NET80211_MANAGE);
3348 if (ireq->i_val != 0 ||
3349 ireq->i_len > IEEE80211_NWID_LEN) {
3353 as.as_len = as.as_maxlen = ireq->i_len;
3354 as.as_buf = ireq->i_data;
3355 if (RtlAnsiStringToUnicodeString(&us, &as, TRUE)) {
3360 error = ndis_set_info(sc, OID_GEN_MACHINE_NAME,
3362 RtlFreeUnicodeString(&us);
3365 #if __FreeBSD_version < 600007
3366 error = ieee80211_ioctl(ifp, command, data);
3368 error = ieee80211_ioctl(&sc->ic, command, data);
3370 if (error == ENETRESET) {
3371 ndis_setstate_80211(sc);
3380 ndis_resettask(d, arg)
3384 struct ndis_softc *sc;
3395 struct ndis_softc *sc;
3401 device_printf(sc->ndis_dev, "watchdog timeout\n");
3404 IoQueueWorkItem(sc->ndis_resetitem,
3405 (io_workitem_func)ndis_resettask_wrap,
3406 WORKQUEUE_CRITICAL, sc);
3407 IoQueueWorkItem(sc->ndis_startitem,
3408 (io_workitem_func)ndis_starttask_wrap,
3409 WORKQUEUE_CRITICAL, ifp);
3415 * Stop the adapter and free any mbufs allocated to the
3420 struct ndis_softc *sc;
3423 struct ieee80211com *ic;
3428 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3431 untimeout(ndis_tick, sc, sc->ndis_stat_ch);
3436 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3442 for (i = 0; i < NDIS_EVENTS; i++) {
3443 if (sc->ndis_evt[i].ne_sts && sc->ndis_evt[i].ne_buf != NULL)
3444 free(sc->ndis_evt[i].ne_buf, M_TEMP);
3445 sc->ndis_evt[i].ne_sts = 0;
3446 sc->ndis_evt[i].ne_len = 0;
3448 sc->ndis_evtcidx = 0;
3449 sc->ndis_evtpidx = 0;
3456 * Stop all chip I/O so that the kernel's probe routines don't
3457 * get confused by errant DMAs when rebooting.
3463 struct ndis_softc *sc;
3465 sc = device_get_softc(dev);
3472 ndis_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
3474 struct ifnet *ifp = ic->ic_ifp;
3475 struct ndis_softc *sc = ifp->if_softc;
3476 enum ieee80211_state ostate;
3478 DPRINTF(("%s: %s -> %s\n", __func__,
3479 ieee80211_state_name[ic->ic_state],
3480 ieee80211_state_name[nstate]));
3482 ostate = ic->ic_state;
3483 ic->ic_state = nstate;
3486 /* pass on to net80211 */
3487 case IEEE80211_S_INIT:
3488 case IEEE80211_S_SCAN:
3489 return (sc->ndis_newstate(ic, nstate, arg));
3491 case IEEE80211_S_ASSOC:
3492 if (ostate != IEEE80211_S_AUTH)
3493 ndis_setstate_80211(sc);
3496 case IEEE80211_S_AUTH:
3497 ndis_setstate_80211(sc);
3507 ndis_scan(void *arg, int npending)
3509 struct ndis_softc *sc = arg;
3510 struct ieee80211com *ic = (void *)&sc->ic;
3511 struct ieee80211_scan_state *ss = ic->ic_scan;
3512 ndis_80211_ssid ssid;
3515 if (!NDIS_INITIALIZED(sc)) {
3516 DPRINTF(("%s: scan aborted\n", __func__));
3517 ieee80211_cancel_scan(ic);
3521 if (ss->ss_nssid != 0) {
3522 /* Perform a directed scan */
3524 bzero((char *)&ssid, len);
3525 ssid.ns_ssidlen = ss->ss_ssid[0].len;
3526 bcopy(ss->ss_ssid[0].ssid, ssid.ns_ssid, ssid.ns_ssidlen);
3528 error = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
3530 DPRINTF(("%s: set ESSID failed\n", __func__));
3534 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
3537 DPRINTF(("%s: scan command failed\n", __func__));
3538 ieee80211_cancel_scan(ic);
3542 pause("ssidscan", hz * 3);
3543 if (!NDIS_INITIALIZED(sc))
3544 /* The interface was downed while we were sleeping */
3547 ndis_scan_results(sc);
3548 ieee80211_scan_done(ic);
3552 ndis_scan_results(struct ndis_softc *sc)
3554 struct ieee80211com *ic = (void *)&sc->ic;
3555 ndis_80211_bssid_list_ex *bl;
3556 ndis_wlan_bssid_ex *wb;
3557 struct ieee80211_scanparams sp;
3558 struct ieee80211_frame wh;
3560 int error, len, rssi, noise, freq, chanflag;
3562 uint8_t ssid[2+IEEE80211_NWID_LEN];
3563 uint8_t rates[2+IEEE80211_RATE_MAXSIZE];
3564 uint8_t *frm, *efrm;
3568 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
3569 if (error != ENOSPC)
3571 bl = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
3572 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
3574 DPRINTF(("%s: failed to read\n", __func__));
3579 DPRINTF(("%s: %d results\n", __func__, bl->nblx_items));
3581 wb = &bl->nblx_bssid[0];
3582 for (i = 0; i < bl->nblx_items; i++) {
3583 memset(&sp, 0, sizeof(sp));
3585 memcpy(wh.i_addr2, wb->nwbx_macaddr, sizeof(wh.i_addr2));
3586 memcpy(wh.i_addr3, wb->nwbx_macaddr, sizeof(wh.i_addr3));
3587 rssi = 100 * (wb->nwbx_rssi - noise) / (-32 - noise);
3588 rssi = max(0, min(rssi, 100)); /* limit 0 <= rssi <= 100 */
3589 if (wb->nwbx_privacy)
3590 sp.capinfo |= IEEE80211_CAPINFO_PRIVACY;
3591 sp.bintval = wb->nwbx_config.nc_beaconperiod;
3592 switch (wb->nwbx_netinfra) {
3593 case NDIS_80211_NET_INFRA_IBSS:
3594 sp.capinfo |= IEEE80211_CAPINFO_IBSS;
3596 case NDIS_80211_NET_INFRA_BSS:
3597 sp.capinfo |= IEEE80211_CAPINFO_ESS;
3600 sp.rates = &rates[0];
3601 for (j = 0; j < IEEE80211_RATE_MAXSIZE; j++) {
3602 /* XXX - check units */
3603 if (wb->nwbx_supportedrates[j] == 0)
3606 wb->nwbx_supportedrates[j] & 0x7f;
3609 sp.ssid = (uint8_t *)&ssid[0];
3610 memcpy(sp.ssid + 2, &wb->nwbx_ssid.ns_ssid,
3611 wb->nwbx_ssid.ns_ssidlen);
3612 sp.ssid[1] = wb->nwbx_ssid.ns_ssidlen;
3614 chanflag = ndis_nettype_chan(wb->nwbx_nettype);
3615 freq = wb->nwbx_config.nc_dsconfig / 1000;
3616 sp.bchan = ieee80211_mhz2ieee(freq, chanflag);
3617 sp.curchan = ieee80211_find_channel(ic, freq, chanflag);
3618 if (sp.curchan == NULL)
3619 sp.curchan = &ic->ic_channels[0];
3621 /* Process extended info from AP */
3622 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
3623 frm = (uint8_t *)&wb->nwbx_ies;
3624 efrm = frm + wb->nwbx_ielen;
3625 if (efrm - frm < 12)
3629 sp.bintval = le16toh(*(uint16_t *)frm);
3631 sp.capinfo = le16toh(*(uint16_t *)frm);
3634 /* Grab variable length ies */
3635 while (efrm - frm > 1) {
3636 if (efrm - frm < frm[1] + 2)
3639 case IEEE80211_ELEMID_RSN:
3647 DPRINTF(("scan: bssid %s chan %dMHz (%d/%d) rssi %d\n",
3648 ether_sprintf(wb->nwbx_macaddr), freq, sp.bchan, chanflag,
3650 ieee80211_add_scan(ic, &sp, &wh, 0, rssi, noise, rstamp);
3651 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
3657 ndis_scan_start(struct ieee80211com *ic)
3659 struct ifnet *ifp = ic->ic_ifp;
3660 struct ndis_softc *sc = ifp->if_softc;
3662 taskqueue_enqueue(sc->ndis_tq, &sc->ndis_scantask);
3666 ndis_set_channel(struct ieee80211com *ic)
3672 ndis_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell)
3678 ndis_scan_mindwell(struct ieee80211com *ic)
3680 /* NB: don't try to abort scan; wait for firmware to finish */
3684 ndis_scan_end(struct ieee80211com *ic)