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 added by Arvind Srinivasan <arvind@celar.us>
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/queue.h>
48 #include <sys/module.h>
50 #if __FreeBSD_version < 502113
51 #include <sys/sysctl.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/route.h>
63 #include <machine/bus_memio.h>
64 #include <machine/bus_pio.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
70 #include <net80211/ieee80211_var.h>
71 #include <net80211/ieee80211_ioctl.h>
73 #include <dev/wi/if_wavelan_ieee.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
78 #include <compat/ndis/pe_var.h>
79 #include <compat/ndis/cfg_var.h>
80 #include <compat/ndis/resource_var.h>
81 #include <compat/ndis/ntoskrnl_var.h>
82 #include <compat/ndis/hal_var.h>
83 #include <compat/ndis/ndis_var.h>
84 #include <dev/if_ndis/if_ndisvar.h>
86 MODULE_DEPEND(ndis, ether, 1, 1, 1);
87 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
88 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
90 MODULE_VERSION(ndis, 1);
92 int ndis_attach (device_t);
93 int ndis_detach (device_t);
94 int ndis_suspend (device_t);
95 int ndis_resume (device_t);
96 void ndis_shutdown (device_t);
98 int ndisdrv_modevent (module_t, int, void *);
100 static void ndis_txeof (ndis_handle, ndis_packet *, ndis_status);
101 static void ndis_rxeof (ndis_handle, ndis_packet **, uint32_t);
102 static void ndis_linksts (ndis_handle, ndis_status, void *, uint32_t);
103 static void ndis_linksts_done (ndis_handle);
105 /* We need to wrap these functions for amd64. */
107 static funcptr ndis_txeof_wrap;
108 static funcptr ndis_rxeof_wrap;
109 static funcptr ndis_linksts_wrap;
110 static funcptr ndis_linksts_done_wrap;
112 static void ndis_intr (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 int ndis_add_key (struct ndis_softc *,
133 struct ieee80211req_key *, int16_t);
134 static void ndis_media_status (struct ifnet *, struct ifmediareq *);
136 static void ndis_setmulti (struct ndis_softc *);
137 static void ndis_map_sclist (void *, bus_dma_segment_t *,
138 int, bus_size_t, int);
140 static int ndisdrv_loaded = 0;
143 * This routine should call windrv_load() once for each driver
144 * image. This will do the relocation and dynalinking for the
145 * image, and create a Windows driver object which will be
146 * saved in our driver database.
150 ndisdrv_modevent(mod, cmd, arg)
160 if (ndisdrv_loaded > 1)
162 windrv_wrap((funcptr)ndis_rxeof, &ndis_rxeof_wrap,
163 3, WINDRV_WRAP_STDCALL);
164 windrv_wrap((funcptr)ndis_txeof, &ndis_txeof_wrap,
165 3, WINDRV_WRAP_STDCALL);
166 windrv_wrap((funcptr)ndis_linksts, &ndis_linksts_wrap,
167 4, WINDRV_WRAP_STDCALL);
168 windrv_wrap((funcptr)ndis_linksts_done,
169 &ndis_linksts_done_wrap, 1, WINDRV_WRAP_STDCALL);
173 if (ndisdrv_loaded > 0)
175 windrv_unwrap(ndis_rxeof_wrap);
176 windrv_unwrap(ndis_txeof_wrap);
177 windrv_unwrap(ndis_linksts_wrap);
178 windrv_unwrap(ndis_linksts_done_wrap);
181 windrv_unwrap(ndis_rxeof_wrap);
182 windrv_unwrap(ndis_txeof_wrap);
183 windrv_unwrap(ndis_linksts_wrap);
184 windrv_unwrap(ndis_linksts_done_wrap);
195 * Program the 64-bit multicast hash filter.
199 struct ndis_softc *sc;
202 struct ifmultiaddr *ifma;
203 int len, mclistsz, error;
206 ifp = &sc->arpcom.ac_if;
208 if (!NDIS_INITIALIZED(sc))
211 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
212 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
213 len = sizeof(sc->ndis_filter);
214 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
215 &sc->ndis_filter, &len);
217 device_printf (sc->ndis_dev,
218 "set filter failed: %d\n", error);
222 if (TAILQ_EMPTY(&ifp->if_multiaddrs))
225 len = sizeof(mclistsz);
226 ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
228 mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
230 if (mclist == NULL) {
231 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
235 sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
238 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
239 if (ifma->ifma_addr->sa_family != AF_LINK)
241 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
242 mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
244 if (len > mclistsz) {
245 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
246 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
251 len = len * ETHER_ADDR_LEN;
252 error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
254 device_printf (sc->ndis_dev, "set mclist failed: %d\n", error);
255 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
256 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
260 free(mclist, M_TEMP);
262 len = sizeof(sc->ndis_filter);
263 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
264 &sc->ndis_filter, &len);
266 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
273 struct ndis_softc *sc;
275 ndis_task_offload *nto;
276 ndis_task_offload_hdr *ntoh;
277 ndis_task_tcpip_csum *nttc;
281 ifp = &sc->arpcom.ac_if;
283 if (!NDIS_INITIALIZED(sc))
286 /* See if there's anything to set. */
288 error = ndis_probe_offload(sc);
292 if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
295 len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
296 sizeof(ndis_task_tcpip_csum);
298 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
303 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
304 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
305 ntoh->ntoh_offset_firsttask = sizeof(ndis_task_offload_hdr);
306 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
307 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
308 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
310 nto = (ndis_task_offload *)((char *)ntoh +
311 ntoh->ntoh_offset_firsttask);
313 nto->nto_vers = NDIS_TASK_OFFLOAD_VERSION;
314 nto->nto_len = sizeof(ndis_task_offload);
315 nto->nto_task = NDIS_TASK_TCPIP_CSUM;
316 nto->nto_offset_nexttask = 0;
317 nto->nto_taskbuflen = sizeof(ndis_task_tcpip_csum);
319 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
321 if (ifp->if_capenable & IFCAP_TXCSUM)
322 nttc->nttc_v4tx = sc->ndis_v4tx;
324 if (ifp->if_capenable & IFCAP_RXCSUM)
325 nttc->nttc_v4rx = sc->ndis_v4rx;
327 error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
334 ndis_probe_offload(sc)
335 struct ndis_softc *sc;
337 ndis_task_offload *nto;
338 ndis_task_offload_hdr *ntoh;
339 ndis_task_tcpip_csum *nttc = NULL;
341 int len, error, dummy;
343 ifp = &sc->arpcom.ac_if;
346 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
351 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
356 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
357 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
358 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
359 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
360 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
362 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
369 if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
374 nto = (ndis_task_offload *)((char *)ntoh +
375 ntoh->ntoh_offset_firsttask);
378 switch (nto->nto_task) {
379 case NDIS_TASK_TCPIP_CSUM:
380 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
382 /* Don't handle these yet. */
383 case NDIS_TASK_IPSEC:
384 case NDIS_TASK_TCP_LARGESEND:
388 if (nto->nto_offset_nexttask == 0)
390 nto = (ndis_task_offload *)((char *)nto +
391 nto->nto_offset_nexttask);
399 sc->ndis_v4tx = nttc->nttc_v4tx;
400 sc->ndis_v4rx = nttc->nttc_v4rx;
402 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_IP_CSUM)
403 sc->ndis_hwassist |= CSUM_IP;
404 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
405 sc->ndis_hwassist |= CSUM_TCP;
406 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
407 sc->ndis_hwassist |= CSUM_UDP;
409 if (sc->ndis_hwassist)
410 ifp->if_capabilities |= IFCAP_TXCSUM;
412 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_IP_CSUM)
413 ifp->if_capabilities |= IFCAP_RXCSUM;
414 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
415 ifp->if_capabilities |= IFCAP_RXCSUM;
416 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
417 ifp->if_capabilities |= IFCAP_RXCSUM;
424 * Attach the interface. Allocate softc structures, do ifmedia
425 * setup and ethernet/BPF attach.
431 u_char eaddr[ETHER_ADDR_LEN];
432 struct ndis_softc *sc;
435 struct ifnet *ifp = NULL;
439 sc = device_get_softc(dev);
441 mtx_init(&sc->ndis_mtx, "ndis softc lock",
442 MTX_NETWORK_LOCK, MTX_DEF);
445 * Hook interrupt early, since calling the driver's
446 * init routine may trigger an interrupt. Note that
447 * we don't need to do any explicit interrupt setup
451 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
452 error = bus_setup_intr(dev, sc->ndis_irq,
453 INTR_TYPE_NET | INTR_MPSAFE,
454 ndis_intr, sc, &sc->ndis_intrhand);
457 device_printf(dev, "couldn't set up irq\n");
462 if (sc->ndis_iftype == PCMCIABus) {
463 error = ndis_alloc_amem(sc);
465 device_printf(dev, "failed to allocate "
466 "attribute memory\n");
471 #if __FreeBSD_version < 502113
472 sysctl_ctx_init(&sc->ndis_ctx);
475 /* Create sysctl registry nodes */
476 ndis_create_sysctls(sc);
478 /* Find the PDO for this device instance. */
480 if (sc->ndis_iftype == PCIBus)
481 pdrv = windrv_lookup(0, "PCI Bus");
482 else if (sc->ndis_iftype == PCMCIABus)
483 pdrv = windrv_lookup(0, "PCCARD Bus");
485 pdrv = windrv_lookup(0, "USB Bus");
486 pdo = windrv_find_pdo(pdrv, dev);
489 * Create a new functional device object for this
490 * device. This is what creates the miniport block
491 * for this device instance.
494 if (NdisAddDevice(sc->ndis_dobj, pdo) != STATUS_SUCCESS) {
495 device_printf(dev, "failed to create FDO!\n");
500 /* Tell the user what version of the API the driver is using. */
501 device_printf(dev, "NDIS API version: %d.%d\n",
502 sc->ndis_chars->nmc_version_major,
503 sc->ndis_chars->nmc_version_minor);
505 /* Do resource conversion. */
506 if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus)
507 ndis_convert_res(sc);
509 sc->ndis_block->nmb_rlist = NULL;
511 /* Install our RX and TX interrupt handlers. */
512 sc->ndis_block->nmb_senddone_func = ndis_txeof_wrap;
513 sc->ndis_block->nmb_pktind_func = ndis_rxeof_wrap;
515 /* Call driver's init routine. */
516 if (ndis_init_nic(sc)) {
517 device_printf (dev, "init handler failed\n");
523 * Get station address from the driver.
526 ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
528 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
531 * Figure out if we're allowed to use multipacket sends
532 * with this driver, and if so, how many.
535 if (sc->ndis_chars->nmc_sendsingle_func &&
536 sc->ndis_chars->nmc_sendmulti_func == NULL) {
537 sc->ndis_maxpkts = 1;
539 len = sizeof(sc->ndis_maxpkts);
540 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
541 &sc->ndis_maxpkts, &len);
544 sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
545 sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
547 /* Allocate a pool of ndis_packets for TX encapsulation. */
549 NdisAllocatePacketPool(&i, &sc->ndis_txpool,
550 sc->ndis_maxpkts, PROTOCOL_RESERVED_SIZE_IN_PACKET);
552 if (i != NDIS_STATUS_SUCCESS) {
553 sc->ndis_txpool = NULL;
554 device_printf(dev, "failed to allocate TX packet pool");
559 sc->ndis_txpending = sc->ndis_maxpkts;
562 /* Get supported oid list. */
563 ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
565 /* If the NDIS module requested scatter/gather, init maps. */
570 * See if the OID_802_11_CONFIGURATION OID is
571 * supported by this driver. If it is, then this an 802.11
572 * wireless driver, and we should set up media for wireless.
574 for (i = 0; i < sc->ndis_oidcnt; i++) {
575 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
581 /* Check for task offload support. */
582 ndis_probe_offload(sc);
584 ifp = &sc->arpcom.ac_if;
586 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
587 ifp->if_mtu = ETHERMTU;
588 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
589 ifp->if_ioctl = ndis_ioctl;
590 ifp->if_start = ndis_start;
591 ifp->if_watchdog = ndis_watchdog;
592 ifp->if_init = ndis_init;
593 ifp->if_baudrate = 10000000;
594 #if __FreeBSD_version < 502114
595 ifp->if_snd.ifq_maxlen = 50;
597 IFQ_SET_MAXLEN(&ifp->if_snd, 50);
598 ifp->if_snd.ifq_drv_maxlen = 25;
599 IFQ_SET_READY(&ifp->if_snd);
601 ifp->if_capenable = ifp->if_capabilities;
602 ifp->if_hwassist = sc->ndis_hwassist;
605 if (sc->ndis_80211) {
606 struct ieee80211com *ic = (void *)&sc->ic;
607 ndis_80211_rates_ex rates;
608 struct ndis_80211_nettype_list *ntl;
613 ic->ic_phytype = IEEE80211_T_DS;
614 ic->ic_opmode = IEEE80211_M_STA;
615 ic->ic_caps = IEEE80211_C_IBSS;
616 ic->ic_state = IEEE80211_S_ASSOC;
617 ic->ic_modecaps = (1<<IEEE80211_MODE_AUTO);
619 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
623 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
624 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
631 for (i = 0; i < ntl->ntl_items; i++) {
632 switch (ntl->ntl_type[i]) {
633 case NDIS_80211_NETTYPE_11FH:
634 case NDIS_80211_NETTYPE_11DS:
635 ic->ic_modecaps |= (1<<IEEE80211_MODE_11B);
637 case NDIS_80211_NETTYPE_11OFDM5:
638 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
640 case NDIS_80211_NETTYPE_11OFDM24:
641 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
650 bzero((char *)&rates, len);
651 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
652 (void *)rates, &len);
654 device_printf (dev, "get rates failed: 0x%x\n", r);
656 * Since the supported rates only up to 8 can be supported,
657 * if this is not 802.11b we're just going to be faking it
661 #define TESTSETRATE(x, y) \
664 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) { \
665 if (ic->ic_sup_rates[x].rs_rates[i] == (y)) \
668 if (i == ic->ic_sup_rates[x].rs_nrates) { \
669 ic->ic_sup_rates[x].rs_rates[i] = (y); \
670 ic->ic_sup_rates[x].rs_nrates++; \
674 #define SETRATE(x, y) \
675 ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
677 ic->ic_sup_rates[x].rs_nrates++
679 ic->ic_curmode = IEEE80211_MODE_AUTO;
680 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A))
681 ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
682 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B))
683 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
684 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G))
685 ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
686 for (i = 0; i < len; i++) {
687 switch (rates[i] & IEEE80211_RATE_VAL) {
693 if (!(ic->ic_modecaps &
694 (1<<IEEE80211_MODE_11B))) {
695 /* Lazy-init 802.11b. */
697 (1<<IEEE80211_MODE_11B);
698 ic->ic_sup_rates[IEEE80211_MODE_11B].
701 SETRATE(IEEE80211_MODE_11B, rates[i]);
702 INCRATE(IEEE80211_MODE_11B);
705 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
706 SETRATE(IEEE80211_MODE_11A, rates[i]);
707 INCRATE(IEEE80211_MODE_11A);
709 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
710 SETRATE(IEEE80211_MODE_11G, rates[i]);
711 INCRATE(IEEE80211_MODE_11G);
718 * If the hardware supports 802.11g, it most
719 * likely supports 802.11b and all of the
720 * 802.11b and 802.11g speeds, so maybe we can
721 * just cheat here. Just how in the heck do
722 * we detect turbo modes, though?
724 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
725 TESTSETRATE(IEEE80211_MODE_11B,
726 IEEE80211_RATE_BASIC|2);
727 TESTSETRATE(IEEE80211_MODE_11B,
728 IEEE80211_RATE_BASIC|4);
729 TESTSETRATE(IEEE80211_MODE_11B,
730 IEEE80211_RATE_BASIC|11);
731 TESTSETRATE(IEEE80211_MODE_11B,
732 IEEE80211_RATE_BASIC|22);
734 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
735 TESTSETRATE(IEEE80211_MODE_11G, 47);
736 TESTSETRATE(IEEE80211_MODE_11G, 72);
737 TESTSETRATE(IEEE80211_MODE_11G, 96);
738 TESTSETRATE(IEEE80211_MODE_11G, 108);
740 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
741 TESTSETRATE(IEEE80211_MODE_11A, 47);
742 TESTSETRATE(IEEE80211_MODE_11A, 72);
743 TESTSETRATE(IEEE80211_MODE_11A, 96);
744 TESTSETRATE(IEEE80211_MODE_11A, 108);
749 * Taking yet more guesses here.
751 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
754 if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
755 chanflag |= IEEE80211_CHAN_G;
757 chanflag |= IEEE80211_CHAN_B;
758 if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
760 chanflag = IEEE80211_CHAN_A;
763 ic->ic_channels[i].ic_freq =
764 ieee80211_ieee2mhz(i, chanflag);
765 ic->ic_channels[i].ic_flags = chanflag;
769 r = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &i);
770 if (arg != NDIS_80211_WEPSTAT_NOTSUPPORTED)
771 ic->ic_caps |= IEEE80211_C_WEP;
773 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
775 ic->ic_caps |= IEEE80211_C_PMGT;
776 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
777 ieee80211_ifattach(ic);
778 ieee80211_media_init(ic, ieee80211_media_change,
780 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
781 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
783 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
785 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
786 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
787 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
788 ifmedia_add(&sc->ifmedia,
789 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
790 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
791 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
792 ether_ifattach(ifp, eaddr);
795 /* Override the status handler so we can detect link changes. */
796 sc->ndis_block->nmb_status_func = ndis_linksts_wrap;
797 sc->ndis_block->nmb_statusdone_func = ndis_linksts_done_wrap;
802 /* We're done talking to the NIC for now; halt it. */
809 * Shutdown hardware and free up resources. This can be called any
810 * time after the mutex has been initialized. It is called in both
811 * the error case in attach and the normal detach case so it needs
812 * to be careful about only freeing resources that have actually been
819 struct ndis_softc *sc;
823 sc = device_get_softc(dev);
824 KASSERT(mtx_initialized(&sc->ndis_mtx),
825 ("ndis mutex not initialized"));
827 ifp = &sc->arpcom.ac_if;
828 ifp->if_flags &= ~IFF_UP;
830 if (device_is_attached(dev)) {
834 ieee80211_ifdetach(&sc->ic);
840 bus_generic_detach(dev);
842 if (sc->ndis_intrhand)
843 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
845 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
847 bus_release_resource(dev, SYS_RES_IOPORT,
848 sc->ndis_io_rid, sc->ndis_res_io);
849 if (sc->ndis_res_mem)
850 bus_release_resource(dev, SYS_RES_MEMORY,
851 sc->ndis_mem_rid, sc->ndis_res_mem);
852 if (sc->ndis_res_altmem)
853 bus_release_resource(dev, SYS_RES_MEMORY,
854 sc->ndis_altmem_rid, sc->ndis_res_altmem);
856 if (sc->ndis_iftype == PCMCIABus)
860 ndis_destroy_dma(sc);
862 if (sc->ndis_txarray)
863 free(sc->ndis_txarray, M_DEVBUF);
866 ifmedia_removeall(&sc->ifmedia);
868 if (sc->ndis_txpool != NULL)
869 NdisFreePacketPool(sc->ndis_txpool);
871 ndis_unload_driver(sc);
873 /* Destroy the PDO for this device. */
875 if (sc->ndis_iftype == PCIBus)
876 drv = windrv_lookup(0, "PCI Bus");
877 else if (sc->ndis_iftype == PCMCIABus)
878 drv = windrv_lookup(0, "PCCARD Bus");
880 drv = windrv_lookup(0, "USB Bus");
882 panic("couldn't find driver object");
883 windrv_destroy_pdo(drv, dev);
885 if (sc->ndis_iftype == PCIBus)
886 bus_dma_tag_destroy(sc->ndis_parent_tag);
888 #if __FreeBSD_version < 502113
889 sysctl_ctx_free(&sc->ndis_ctx);
892 mtx_destroy(&sc->ndis_mtx);
901 struct ndis_softc *sc;
904 sc = device_get_softc(dev);
905 ifp = &sc->arpcom.ac_if;
908 if (NDIS_INITIALIZED(sc))
919 struct ndis_softc *sc;
922 sc = device_get_softc(dev);
923 ifp = &sc->arpcom.ac_if;
925 if (NDIS_INITIALIZED(sc))
932 * A frame has been uploaded: pass the resulting mbuf chain up to
933 * the higher level protocols.
935 * When handling received NDIS packets, the 'status' field in the
936 * out-of-band portion of the ndis_packet has special meaning. In the
937 * most common case, the underlying NDIS driver will set this field
938 * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
939 * take posession of it. We then change the status field to
940 * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
941 * and that we will return it at some point in the future via the
942 * return packet handler.
944 * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
945 * this means the driver is running out of packet/buffer resources and
946 * wants to maintain ownership of the packet. In this case, we have to
947 * copy the packet data into local storage and let the driver keep the
951 ndis_rxeof(adapter, packets, pktcnt)
953 ndis_packet **packets;
956 struct ndis_softc *sc;
957 ndis_miniport_block *block;
960 ndis_tcpip_csum *csum;
965 block = (ndis_miniport_block *)adapter;
966 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
967 ifp = &sc->arpcom.ac_if;
969 for (i = 0; i < pktcnt; i++) {
971 /* Stash the softc here so ptom can use it. */
973 if (ndis_ptom(&m0, p)) {
974 device_printf (sc->ndis_dev, "ptom failed\n");
975 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
976 ndis_return_packet(sc, p);
978 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
979 m = m_dup(m0, M_DONTWAIT);
981 * NOTE: we want to destroy the mbuf here, but
982 * we don't actually want to return it to the
983 * driver via the return packet handler. By
984 * bumping np_refcnt, we can prevent the
985 * ndis_return_packet() routine from actually
995 p->np_oob.npo_status = NDIS_STATUS_PENDING;
996 m0->m_pkthdr.rcvif = ifp;
999 /* Deal with checksum offload. */
1001 if (ifp->if_capenable & IFCAP_RXCSUM &&
1002 p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
1004 p->np_ext.npe_info[ndis_tcpipcsum_info];
1005 csum = (ndis_tcpip_csum *)&s;
1006 if (csum->u.ntc_rxflags &
1007 NDIS_RXCSUM_IP_PASSED)
1008 m0->m_pkthdr.csum_flags |=
1009 CSUM_IP_CHECKED|CSUM_IP_VALID;
1010 if (csum->u.ntc_rxflags &
1011 (NDIS_RXCSUM_TCP_PASSED |
1012 NDIS_RXCSUM_UDP_PASSED)) {
1013 m0->m_pkthdr.csum_flags |=
1014 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1015 m0->m_pkthdr.csum_data = 0xFFFF;
1019 (*ifp->if_input)(ifp, m0);
1027 * A frame was downloaded to the chip. It's safe for us to clean up
1031 ndis_txeof(adapter, packet, status)
1032 ndis_handle adapter;
1033 ndis_packet *packet;
1037 struct ndis_softc *sc;
1038 ndis_miniport_block *block;
1043 block = (ndis_miniport_block *)adapter;
1044 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1045 ifp = &sc->arpcom.ac_if;
1048 idx = packet->np_txidx;
1050 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
1052 ndis_free_packet(packet);
1056 sc->ndis_txarray[idx] = NULL;
1057 sc->ndis_txpending++;
1059 if (status == NDIS_STATUS_SUCCESS)
1064 ifp->if_flags &= ~IFF_OACTIVE;
1067 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
1073 ndis_linksts(adapter, status, sbuf, slen)
1074 ndis_handle adapter;
1079 ndis_miniport_block *block;
1080 struct ndis_softc *sc;
1083 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1085 block->nmb_getstat = status;
1091 ndis_linksts_done(adapter)
1092 ndis_handle adapter;
1094 ndis_miniport_block *block;
1095 struct ndis_softc *sc;
1099 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1100 ifp = &sc->arpcom.ac_if;
1102 if (!NDIS_INITIALIZED(sc))
1105 switch (block->nmb_getstat) {
1106 case NDIS_STATUS_MEDIA_CONNECT:
1107 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1108 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
1110 case NDIS_STATUS_MEDIA_DISCONNECT:
1112 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1125 struct ndis_softc *sc;
1127 int is_our_intr = 0;
1130 ndis_miniport_interrupt *intr;
1133 ifp = &sc->arpcom.ac_if;
1134 intr = sc->ndis_block->nmb_interrupt;
1136 if (sc->ndis_block->nmb_miniportadapterctx == NULL)
1139 KeAcquireSpinLock(&intr->ni_dpccountlock, &irql);
1140 if (sc->ndis_block->nmb_interrupt->ni_isrreq == TRUE)
1141 ndis_isr(sc, &is_our_intr, &call_isr);
1143 ndis_disable_intr(sc);
1146 KeReleaseSpinLock(&intr->ni_dpccountlock, irql);
1148 if ((is_our_intr || call_isr))
1149 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
1158 struct ndis_softc *sc;
1164 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1165 sc->ndis_stat_ch = timeout(ndis_tick, sc, hz *
1166 sc->ndis_block->nmb_checkforhangsecs);
1177 struct ndis_softc *sc;
1178 ndis_checkforhang_handler hangfunc;
1180 ndis_media_state linkstate;
1185 hangfunc = sc->ndis_chars->nmc_checkhang_func;
1187 if (hangfunc != NULL) {
1188 rval = MSCALL1(hangfunc,
1189 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);
2077 if (!NDIS_INITIALIZED(sc))
2080 error = ndis_80211_ioctl_get(ifp, command, data);
2085 if (!NDIS_INITIALIZED(sc))
2088 error = ndis_80211_ioctl_set(ifp, command, data);
2092 case SIOCGIFGENERIC:
2093 case SIOCSIFGENERIC:
2094 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
2095 if (command == SIOCGIFGENERIC)
2096 error = ndis_wi_ioctl_get(ifp, command, data);
2098 error = ndis_wi_ioctl_set(ifp, command, data);
2101 if (error != ENOTTY)
2106 if (sc->ndis_80211) {
2107 error = ieee80211_ioctl(&sc->ic, command, data);
2108 if (error == ENETRESET) {
2109 ndis_setstate_80211(sc);
2113 error = ether_ioctl(ifp, command, data);
2118 /*NDIS_UNLOCK(sc);*/
2124 ndis_wi_ioctl_get(ifp, command, data)
2131 struct ndis_softc *sc;
2132 ndis_80211_bssid_list_ex *bl;
2133 ndis_wlan_bssid_ex *wb;
2134 struct wi_apinfo *api;
2135 int error, i, j, len, maxaps;
2138 ifr = (struct ifreq *)data;
2139 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2143 switch (wreq.wi_type) {
2144 case WI_RID_READ_APS:
2146 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2149 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2151 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2152 if (error != ENOSPC)
2154 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
2155 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2160 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
2161 maxaps = MIN(maxaps, bl->nblx_items);
2162 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
2163 *(int *)&wreq.wi_val = maxaps;
2164 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
2165 wb = bl->nblx_bssid;
2167 bzero(api, sizeof(*api));
2168 bcopy(&wb->nwbx_macaddr, &api->bssid,
2169 sizeof(api->bssid));
2170 api->namelen = wb->nwbx_ssid.ns_ssidlen;
2171 bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
2172 if (wb->nwbx_privacy)
2173 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
2174 /* XXX Where can we get noise information? */
2175 api->signal = wb->nwbx_rssi + 149; /* XXX */
2176 api->quality = api->signal;
2178 ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2180 /* In "auto" infrastructure mode, this is useless. */
2181 if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
2182 api->capinfo |= IEEE80211_CAPINFO_IBSS;
2183 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
2184 j = sizeof(ndis_80211_rates_ex);
2185 /* handle other extended things */
2187 j = sizeof(ndis_80211_rates);
2188 for (i = api->rate = 0; i < j; i++)
2189 api->rate = MAX(api->rate, 5 *
2190 (wb->nwbx_supportedrates[i] & 0x7f));
2192 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2195 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2205 ndis_wi_ioctl_set(ifp, command, data)
2212 struct ndis_softc *sc;
2216 error = suser(curthread);
2221 ifr = (struct ifreq *)data;
2222 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2226 switch (wreq.wi_type) {
2227 case WI_RID_SCAN_APS:
2228 case WI_RID_SCAN_REQ: /* arguments ignored */
2231 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2242 ndis_80211_ioctl_get(struct ifnet *ifp, u_long command, caddr_t data)
2244 struct ndis_softc *sc;
2245 struct ieee80211req *ireq;
2246 ndis_80211_bssid_list_ex *bl;
2247 ndis_80211_ssid ssid;
2248 ndis_80211_macaddr bssid;
2249 ndis_wlan_bssid_ex *wb;
2250 struct ieee80211req_scan_result *sr, *bsr;
2251 int error, len, i, j;
2253 uint8_t nodename[IEEE80211_NWID_LEN];
2254 uint16_t nodename_u[IEEE80211_NWID_LEN + 1];
2258 ireq = (struct ieee80211req *) data;
2260 switch (ireq->i_type) {
2261 case IEEE80211_IOC_MLME:
2264 case IEEE80211_IOC_BSSID:
2265 len = sizeof(bssid);
2266 bzero((char*)&bssid, len);
2267 error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
2269 device_printf(sc->ndis_dev, "failed to get bssid\n");
2273 error = copyout(&bssid, ireq->i_data, len);
2275 case IEEE80211_IOC_SSID:
2277 bzero((char*)&ssid, len);
2278 error = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
2280 device_printf(sc->ndis_dev, "failed to get ssid: %d\n", error);
2283 ireq->i_len = ssid.ns_ssidlen;
2284 error = copyout(&ssid.ns_ssid, ireq->i_data, ssid.ns_ssidlen);
2286 case IEEE80211_IOC_SCAN_RESULTS:
2288 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2289 if (error != ENOSPC)
2291 bl = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
2292 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2297 sr = bsr = malloc(ireq->i_len, M_DEVBUF, M_WAITOK | M_ZERO);
2298 wb = bl->nblx_bssid;
2300 for (i = 0; i < bl->nblx_items; i++) {
2302 * Check if we have enough space left for this ap
2304 j = roundup(sizeof(*sr) + wb->nwbx_ssid.ns_ssidlen
2306 sizeof(struct ndis_80211_fixed_ies),
2308 if (len + j > ireq->i_len)
2310 bcopy(&wb->nwbx_macaddr, &sr->isr_bssid,
2311 sizeof(sr->isr_bssid));
2312 if (wb->nwbx_privacy)
2313 sr->isr_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2314 sr->isr_rssi = wb->nwbx_rssi + 200;
2315 sr->isr_freq = wb->nwbx_config.nc_dsconfig / 1000;
2316 sr->isr_intval = wb->nwbx_config.nc_beaconperiod;
2317 switch (wb->nwbx_netinfra) {
2318 case NDIS_80211_NET_INFRA_IBSS:
2319 sr->isr_capinfo |= IEEE80211_CAPINFO_IBSS;
2321 case NDIS_80211_NET_INFRA_BSS:
2322 sr->isr_capinfo |= IEEE80211_CAPINFO_ESS;
2325 for (j = 0; j < sizeof(sr->isr_rates); j++) {
2326 /* XXX - check units */
2327 if (wb->nwbx_supportedrates[j] == 0)
2330 wb->nwbx_supportedrates[j] & 0x7f;
2333 sr->isr_ssid_len = wb->nwbx_ssid.ns_ssidlen;
2334 cp = (char *)sr + sizeof(*sr);
2335 bcopy(&wb->nwbx_ssid.ns_ssid, cp, sr->isr_ssid_len);
2336 cp += sr->isr_ssid_len;
2337 sr->isr_ie_len = wb->nwbx_ielen
2338 - sizeof(struct ndis_80211_fixed_ies);
2339 bcopy((char *)wb->nwbx_ies +
2340 sizeof(struct ndis_80211_fixed_ies),
2341 cp, sr->isr_ie_len);
2342 sr->isr_len = roundup(sizeof(*sr) + sr->isr_ssid_len
2343 + sr->isr_ie_len, sizeof(uint32_t));
2345 sr = (struct ieee80211req_scan_result *)((char *)sr +
2347 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2350 error = copyout(bsr, ireq->i_data, len);
2352 free(bsr, M_DEVBUF);
2354 case IEEE80211_IOC_STATIONNAME:
2355 error = ndis_get_info(sc, OID_GEN_MACHINE_NAME,
2360 bzero((char *)nodename, IEEE80211_NWID_LEN);
2361 ndis_unicode_to_ascii(nodename_u, len, &acode);
2362 ireq->i_len = len / 2 + 1;
2363 error = copyout(acode, ireq->i_data, ireq->i_len);
2366 error = ieee80211_ioctl(&sc->ic, command, data);
2373 ndis_add_key(sc, wk, i_len)
2374 struct ndis_softc *sc;
2375 struct ieee80211req_key *wk;
2378 ndis_80211_key *rkey;
2379 ndis_80211_wep *wep;
2383 /* infrastructure mode only supported for now */
2385 arg = NDIS_80211_NET_INFRA_BSS;
2386 error = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
2388 device_printf(sc->ndis_dev,
2389 "setting infrastructure mode failed\n");
2393 switch(wk->ik_type) {
2394 case IEEE80211_CIPHER_WEP:
2395 len = 12 + wk->ik_keylen;
2396 wep = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2399 wep->nw_length = len;
2400 wep->nw_keyidx = wk->ik_keyix;
2401 wep->nw_keylen = wk->ik_keylen;
2402 if(wk->ik_flags & IEEE80211_KEY_XMIT)
2403 wep->nw_keyidx |= 1 << 31;
2404 device_printf(sc->ndis_dev, "setting wep key\n");
2405 error = copyin(wk->ik_keydata, wep->nw_keydata, wk->ik_keylen);
2407 device_printf(sc->ndis_dev,
2408 "copyin of wep key to kernel space failed\n");
2412 error = ndis_set_info(sc, OID_802_11_ADD_WEP, wep, &len);
2414 device_printf(sc->ndis_dev,
2415 "setting wep key failed\n");
2420 /* set the authentication mode */
2422 arg = NDIS_80211_AUTHMODE_OPEN;
2423 error = ndis_set_info(sc,
2424 OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2426 device_printf(sc->ndis_dev,
2427 "setting authentication mode failed\n");
2430 /* set the encryption */
2433 arg = NDIS_80211_WEPSTAT_ENABLED;
2434 error = ndis_set_info(sc,
2435 OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2437 device_printf(sc->ndis_dev,
2438 "setting encryption status failed\n");
2442 case IEEE80211_CIPHER_TKIP:
2443 len = 12 + 6 + 6 + 8 + wk->ik_keylen;
2444 rkey = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2448 error = copyin(wk->ik_macaddr,
2449 rkey->nk_bssid, IEEE80211_ADDR_LEN);
2451 device_printf(sc->ndis_dev,
2452 "copyin of bssid to kernel space failed\n");
2457 /* keyrsc needs to be fixed: need to do some shifting */
2458 error = copyin(&(wk->ik_keyrsc),
2459 &(rkey->nk_keyrsc), sizeof(rkey->nk_keyrsc));
2461 device_printf(sc->ndis_dev,
2462 "copyin of keyrsc to kernel space failed\n");
2467 /* key index - gets weird in NDIS */
2469 rkey->nk_keyidx = wk->ik_keyix;
2470 if(wk->ik_flags & IEEE80211_KEY_XMIT)
2471 rkey->nk_keyidx |= 1 << 31;
2472 if((bcmp(rkey->nk_bssid, "\xff\xff\xff\xff\xff\xff",
2473 IEEE80211_ADDR_LEN) == 0) ||
2474 (bcmp(rkey->nk_bssid, "\x0\x0\x0\x0\x0\x0",
2475 IEEE80211_ADDR_LEN) == 0)) {
2476 /* group key - nothing to do in ndis */
2479 rkey->nk_keyidx |= 1 << 30;
2482 /* need to set bit 29 based on keyrsc */
2484 rkey->nk_keylen = wk->ik_keylen;
2485 if (wk->ik_type == IEEE80211_CIPHER_TKIP &&
2486 wk->ik_keylen == 32) {
2488 * key data needs to be offset by 4 due
2489 * to mismatch between NDIS spec and BSD??
2491 error = copyin(wk->ik_keydata,
2492 rkey->nk_keydata + 4, 16);
2494 device_printf(sc->ndis_dev, "copyin of "
2495 "keydata(0) to kernel space failed\n");
2499 error = copyin(wk->ik_keydata + 24,
2500 rkey->nk_keydata + 20, 8);
2502 device_printf(sc->ndis_dev, "copyin of "
2503 "keydata(1) to kernel space failed\n");
2507 error = copyin(wk->ik_keydata + 16,
2508 rkey->nk_keydata + 28, 8);
2510 device_printf(sc->ndis_dev, "copyin of "
2511 "keydata(2) to kernel space failed\n");
2516 error = copyin(wk->ik_keydata,
2517 rkey->nk_keydata + 4, wk->ik_keylen);
2519 device_printf(sc->ndis_dev, "copyin of "
2520 "keydata(CCMP) to kernel space failed\n");
2525 error = ndis_set_info(sc, OID_802_11_ADD_KEY, rkey, &len);
2527 case IEEE80211_CIPHER_AES_CCM:
2536 ndis_80211_ioctl_set(struct ifnet *ifp, u_long command, caddr_t data)
2538 struct ndis_softc *sc;
2539 struct ieee80211req *ireq;
2540 int error, len, arg, ucnt;
2541 uint8_t nodename[IEEE80211_NWID_LEN];
2542 uint16_t nodename_u[IEEE80211_NWID_LEN + 1];
2544 struct ieee80211req_del_key *rk;
2545 struct ieee80211req_key *wk;
2546 unsigned char *wpa_ie;
2547 ndis_80211_ssid ssid;
2548 ndis_80211_remove_key rkey;
2551 ireq = (struct ieee80211req *) data;
2553 switch (ireq->i_type) {
2554 case IEEE80211_IOC_MLME:
2555 case IEEE80211_IOC_ROAMING:
2556 case IEEE80211_IOC_COUNTERMEASURES:
2557 case IEEE80211_IOC_DROPUNENCRYPTED:
2560 case IEEE80211_IOC_PRIVACY:
2562 arg = NDIS_80211_PRIVFILT_8021XWEP;
2563 error = ndis_set_info(sc,
2564 OID_802_11_PRIVACY_FILTER, &arg, &len);
2566 device_printf(sc->ndis_dev,
2567 "setting wep privacy filter failed\n");
2571 case IEEE80211_IOC_WPA:
2575 case IEEE80211_IOC_OPTIE:
2576 wpa_ie = (char*)ireq->i_data;
2577 if (ireq->i_len < 14 || !wpa_ie) {
2578 /* cannot figure out anything */
2579 arg = NDIS_80211_AUTHMODE_OPEN;
2580 error = ndis_set_info(sc,
2581 OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2584 if (wpa_ie[0] == IEEE80211_ELEMID_RSN) {
2587 } else if (wpa_ie[0] == IEEE80211_ELEMID_VENDOR) {
2589 /* set the encryption based on multicast cipher */
2591 if (!memcmp(wpa_ie + 8, "\x00\x50\xf2\x02", 4)) {
2593 arg = NDIS_80211_WEPSTAT_ENC2ENABLED;
2594 error = ndis_set_info(sc,
2595 OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2597 device_printf(sc->ndis_dev, "setting "
2598 "encryption status to "
2600 /* continue anyway */
2605 /* set the authentication mode */
2607 ucnt = wpa_ie[12] + 256* wpa_ie[13];
2609 /* 4 bytes per unicast cipher */
2611 ucnt = 14 + 4*ucnt + 2; /* account for number of authsels */
2613 if (ireq->i_len < ucnt) {
2614 arg = NDIS_80211_AUTHMODE_WPANONE;
2616 if (!memcmp((void*)(&wpa_ie[ucnt]),
2617 "\x00\x50\xf2\x02", 4)) {
2618 arg = NDIS_80211_AUTHMODE_WPAPSK;
2619 } else if (!memcmp((void*)(&wpa_ie[ucnt]),
2620 "\x00\x50\xf2\x01", 4)) {
2621 arg = NDIS_80211_AUTHMODE_WPA;
2623 arg = NDIS_80211_AUTHMODE_WPANONE;
2627 error = ndis_set_info(sc,
2628 OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2630 device_printf(sc->ndis_dev,
2631 "setting authentication mode to WPA-PSK failed\n");
2635 case IEEE80211_IOC_SSID:
2637 bzero((char*)&ssid, len);
2638 ssid.ns_ssidlen = ireq->i_len;
2639 error = copyin(ireq->i_data, &(ssid.ns_ssid), ireq->i_len);
2642 device_printf(sc->ndis_dev,
2643 "setting SSID to %s\n", ssid.ns_ssid);
2644 error = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
2646 device_printf(sc->ndis_dev,
2647 "setting SSID to %s\n", ssid.ns_ssid);
2650 case IEEE80211_IOC_DELKEY:
2652 bzero((char*)&rkey, len);
2653 rk = (struct ieee80211req_del_key*)ireq->i_data;
2655 rkey.nk_keyidx = rk->idk_keyix;
2656 error = copyin(rk->idk_macaddr,
2657 &(rkey.nk_bssid), sizeof(ndis_80211_macaddr));
2660 error = ndis_set_info(sc, OID_802_11_REMOVE_KEY, &rkey, &len);
2662 device_printf(sc->ndis_dev, "deleting key\n");
2664 case IEEE80211_IOC_WPAKEY:
2665 wk = (struct ieee80211req_key*)ireq->i_data;
2666 error = ndis_add_key(sc, wk, ireq->i_len);
2668 case IEEE80211_IOC_SCAN_REQ:
2670 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2672 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2673 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
2675 case IEEE80211_IOC_STATIONNAME:
2676 error = suser(curthread);
2679 if (ireq->i_val != 0 ||
2680 ireq->i_len > IEEE80211_NWID_LEN) {
2684 bzero((char *)nodename, IEEE80211_NWID_LEN);
2685 error = copyin(ireq->i_data, nodename, ireq->i_len);
2689 ndis_ascii_to_unicode((char *)nodename, &ucode);
2690 len = ireq->i_len * 2;
2691 error = ndis_set_info(sc, OID_GEN_MACHINE_NAME,
2695 error = ieee80211_ioctl(&sc->ic, command, data);
2696 if (error == ENETRESET) {
2697 ndis_setstate_80211(sc);
2709 struct ndis_softc *sc;
2715 device_printf(sc->ndis_dev, "watchdog timeout\n");
2718 ndis_sched((void(*)(void *))ndis_reset_nic, sc, NDIS_TASKQUEUE);
2719 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2725 * Stop the adapter and free any mbufs allocated to the
2730 struct ndis_softc *sc;
2734 ifp = &sc->arpcom.ac_if;
2735 untimeout(ndis_tick, sc, sc->ndis_stat_ch);
2742 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2749 * Stop all chip I/O so that the kernel's probe routines don't
2750 * get confused by errant DMAs when rebooting.
2756 struct ndis_softc *sc;
2758 sc = device_get_softc(dev);
2759 ndis_shutdown_nic(sc);