]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_ndis/if_ndis.c
This commit was generated by cvs2svn to compensate for changes in r145673,
[FreeBSD/FreeBSD.git] / sys / dev / if_ndis / if_ndis.c
1 /*-
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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.
31  *
32  * WPA support added by Arvind Srinivasan <arvind@celar.us>
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_bdg.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.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>
49 #include <sys/proc.h>
50 #if __FreeBSD_version < 502113
51 #include <sys/sysctl.h>
52 #endif
53
54 #include <net/if.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>
60
61 #include <net/bpf.h>
62
63 #include <machine/bus_memio.h>
64 #include <machine/bus_pio.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
67 #include <sys/bus.h>
68 #include <sys/rman.h>
69
70 #include <net80211/ieee80211_var.h>
71 #include <net80211/ieee80211_ioctl.h>
72
73 #include <dev/wi/if_wavelan_ieee.h>
74
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
77
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>
85
86 MODULE_DEPEND(ndis, ether, 1, 1, 1);
87 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
88 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
89
90 MODULE_VERSION(ndis, 1);
91
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);
97
98 int ndisdrv_modevent            (module_t, int, void *);
99
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);
104
105 /* We need to wrap these functions for amd64. */
106
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;
111
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 *);
135
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);
139
140 static int ndisdrv_loaded = 0;
141
142 /*
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.
147  */
148
149 int
150 ndisdrv_modevent(mod, cmd, arg)
151         module_t                mod;
152         int                     cmd;
153         void                    *arg;
154 {
155         int                     error = 0;
156
157         switch (cmd) {
158         case MOD_LOAD:
159                 ndisdrv_loaded++;
160                 if (ndisdrv_loaded > 1)
161                         break;
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);
170                 break;
171         case MOD_UNLOAD:
172                 ndisdrv_loaded--;
173                 if (ndisdrv_loaded > 0)
174                         break;
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);
179                 break;
180         case MOD_SHUTDOWN:
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);
185                 break;
186         default:
187                 error = EINVAL;
188                 break;
189         }
190
191         return (error);
192 }
193
194 /*
195  * Program the 64-bit multicast hash filter.
196  */
197 static void
198 ndis_setmulti(sc)
199         struct ndis_softc       *sc;
200 {
201         struct ifnet            *ifp;
202         struct ifmultiaddr      *ifma;
203         int                     len, mclistsz, error;
204         uint8_t                 *mclist;
205
206         ifp = &sc->arpcom.ac_if;
207
208         if (!NDIS_INITIALIZED(sc))
209                 return;
210
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);
216                 if (error)
217                         device_printf (sc->ndis_dev,
218                             "set filter failed: %d\n", error);
219                 return;
220         }
221
222         if (TAILQ_EMPTY(&ifp->if_multiaddrs))
223                 return;
224
225         len = sizeof(mclistsz);
226         ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
227
228         mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
229
230         if (mclist == NULL) {
231                 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
232                 goto out;
233         }
234
235         sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
236
237         len = 0;
238         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
239                 if (ifma->ifma_addr->sa_family != AF_LINK)
240                         continue;
241                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
242                     mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
243                 len++;
244                 if (len > mclistsz) {
245                         sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
246                         sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
247                         goto out;
248                 }
249         }
250
251         len = len * ETHER_ADDR_LEN;
252         error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
253         if (error) {
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;
257         }
258
259 out:
260         free(mclist, M_TEMP);
261
262         len = sizeof(sc->ndis_filter);
263         error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
264             &sc->ndis_filter, &len);
265         if (error)
266                 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
267
268         return;
269 }
270
271 static int
272 ndis_set_offload(sc)
273         struct ndis_softc       *sc;
274 {
275         ndis_task_offload       *nto;
276         ndis_task_offload_hdr   *ntoh;
277         ndis_task_tcpip_csum    *nttc;
278         struct ifnet            *ifp;
279         int                     len, error;
280
281         ifp = &sc->arpcom.ac_if;
282
283         if (!NDIS_INITIALIZED(sc))
284                 return(EINVAL);
285
286         /* See if there's anything to set. */
287
288         error = ndis_probe_offload(sc);
289         if (error)
290                 return(error);
291                 
292         if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
293                 return(0);
294
295         len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
296             sizeof(ndis_task_tcpip_csum);
297
298         ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
299
300         if (ntoh == NULL)
301                 return(ENOMEM);
302
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;
309
310         nto = (ndis_task_offload *)((char *)ntoh +
311             ntoh->ntoh_offset_firsttask);
312
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);
318
319         nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
320
321         if (ifp->if_capenable & IFCAP_TXCSUM)
322                 nttc->nttc_v4tx = sc->ndis_v4tx;
323
324         if (ifp->if_capenable & IFCAP_RXCSUM)
325                 nttc->nttc_v4rx = sc->ndis_v4rx;
326
327         error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
328         free(ntoh, M_TEMP);
329
330         return(error);
331 }
332
333 static int
334 ndis_probe_offload(sc)
335         struct ndis_softc       *sc;
336 {
337         ndis_task_offload       *nto;
338         ndis_task_offload_hdr   *ntoh;
339         ndis_task_tcpip_csum    *nttc = NULL;
340         struct ifnet            *ifp;
341         int                     len, error, dummy;
342
343         ifp = &sc->arpcom.ac_if;
344
345         len = sizeof(dummy);
346         error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
347
348         if (error != ENOSPC)
349                 return(error);
350
351         ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
352
353         if (ntoh == NULL)
354                 return(ENOMEM);
355
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;
361
362         error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
363
364         if (error) {
365                 free(ntoh, M_TEMP);
366                 return(error);
367         }
368
369         if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
370                 free(ntoh, M_TEMP);
371                 return(EINVAL);
372         }
373
374         nto = (ndis_task_offload *)((char *)ntoh +
375             ntoh->ntoh_offset_firsttask);
376
377         while (1) {
378                 switch (nto->nto_task) {
379                 case NDIS_TASK_TCPIP_CSUM:
380                         nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
381                         break;
382                 /* Don't handle these yet. */
383                 case NDIS_TASK_IPSEC:
384                 case NDIS_TASK_TCP_LARGESEND:
385                 default:
386                         break;
387                 }
388                 if (nto->nto_offset_nexttask == 0)
389                         break;
390                 nto = (ndis_task_offload *)((char *)nto +
391                     nto->nto_offset_nexttask);
392         }
393
394         if (nttc == NULL) {
395                 free(ntoh, M_TEMP);
396                 return(ENOENT);
397         }
398
399         sc->ndis_v4tx = nttc->nttc_v4tx;
400         sc->ndis_v4rx = nttc->nttc_v4rx;
401
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;
408
409         if (sc->ndis_hwassist)
410                 ifp->if_capabilities |= IFCAP_TXCSUM;
411
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;
418
419         free(ntoh, M_TEMP);
420         return(0);
421 }
422
423 /*
424  * Attach the interface. Allocate softc structures, do ifmedia
425  * setup and ethernet/BPF attach.
426  */
427 int
428 ndis_attach(dev)
429         device_t                dev;
430 {
431         u_char                  eaddr[ETHER_ADDR_LEN];
432         struct ndis_softc       *sc;
433         driver_object           *pdrv;
434         device_object           *pdo;
435         struct ifnet            *ifp = NULL;
436         int                     error = 0, len;
437         int                     i;
438
439         sc = device_get_softc(dev);
440
441         mtx_init(&sc->ndis_mtx, "ndis softc lock",
442             MTX_NETWORK_LOCK, MTX_DEF);
443
444         /*
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
448          * for USB.
449          */
450
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);
455
456                 if (error) {
457                         device_printf(dev, "couldn't set up irq\n");
458                         goto fail;
459                 }
460         }
461
462         if (sc->ndis_iftype == PCMCIABus) {
463                 error = ndis_alloc_amem(sc);
464                 if (error) {
465                         device_printf(dev, "failed to allocate "
466                             "attribute memory\n");
467                         goto fail;
468                 }
469         }
470
471 #if __FreeBSD_version < 502113
472         sysctl_ctx_init(&sc->ndis_ctx);
473 #endif
474
475         /* Create sysctl registry nodes */
476         ndis_create_sysctls(sc);
477
478         /* Find the PDO for this device instance. */
479
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");
484         else
485                 pdrv = windrv_lookup(0, "USB Bus");
486         pdo = windrv_find_pdo(pdrv, dev);
487
488         /*
489          * Create a new functional device object for this
490          * device. This is what creates the miniport block
491          * for this device instance.
492          */
493
494         if (NdisAddDevice(sc->ndis_dobj, pdo) != STATUS_SUCCESS) {
495                 device_printf(dev, "failed to create FDO!\n");
496                 error = ENXIO;
497                 goto fail;
498         }
499
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);
504
505         /* Do resource conversion. */
506         if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus)
507                 ndis_convert_res(sc);
508         else
509                 sc->ndis_block->nmb_rlist = NULL;
510
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;
514
515         /* Call driver's init routine. */
516         if (ndis_init_nic(sc)) {
517                 device_printf (dev, "init handler failed\n");
518                 error = ENXIO;
519                 goto fail;
520         }
521
522         /*
523          * Get station address from the driver.
524          */
525         len = sizeof(eaddr);
526         ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
527
528         bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
529
530         /*
531          * Figure out if we're allowed to use multipacket sends
532          * with this driver, and if so, how many.
533          */
534
535         if (sc->ndis_chars->nmc_sendsingle_func &&
536             sc->ndis_chars->nmc_sendmulti_func == NULL) {
537                 sc->ndis_maxpkts = 1;
538         } else {
539                 len = sizeof(sc->ndis_maxpkts);
540                 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
541                     &sc->ndis_maxpkts, &len);
542         }
543
544         sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
545             sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
546
547         /* Allocate a pool of ndis_packets for TX encapsulation. */
548
549         NdisAllocatePacketPool(&i, &sc->ndis_txpool,
550            sc->ndis_maxpkts, PROTOCOL_RESERVED_SIZE_IN_PACKET);
551
552         if (i != NDIS_STATUS_SUCCESS) {
553                 sc->ndis_txpool = NULL;
554                 device_printf(dev, "failed to allocate TX packet pool");
555                 error = ENOMEM;
556                 goto fail;
557         }
558
559         sc->ndis_txpending = sc->ndis_maxpkts;
560
561         sc->ndis_oidcnt = 0;
562         /* Get supported oid list. */
563         ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
564
565         /* If the NDIS module requested scatter/gather, init maps. */
566         if (sc->ndis_sc)
567                 ndis_init_dma(sc);
568
569         /*
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.
573          */
574         for (i = 0; i < sc->ndis_oidcnt; i++) {
575                 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
576                         sc->ndis_80211++;
577                         break;
578                 }
579         }
580
581         /* Check for task offload support. */
582         ndis_probe_offload(sc);
583
584         ifp = &sc->arpcom.ac_if;
585         ifp->if_softc = sc;
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;
596 #else
597         IFQ_SET_MAXLEN(&ifp->if_snd, 50);
598         ifp->if_snd.ifq_drv_maxlen = 25;
599         IFQ_SET_READY(&ifp->if_snd);
600 #endif
601         ifp->if_capenable = ifp->if_capabilities;
602         ifp->if_hwassist = sc->ndis_hwassist;
603
604         /* Do media setup */
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;
609                 uint32_t                arg;
610                 int                     r;
611
612                 ic->ic_ifp = ifp;
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);
618                 len = 0;
619                 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
620                     NULL, &len);
621                 if (r != ENOSPC)
622                         goto nonettypes;
623                 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
624                 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
625                     ntl, &len);
626                 if (r != 0) {
627                         free(ntl, M_DEVBUF);
628                         goto nonettypes;
629                 }
630
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);
636                                 break;
637                         case NDIS_80211_NETTYPE_11OFDM5:
638                                 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
639                                 break;
640                         case NDIS_80211_NETTYPE_11OFDM24:
641                                 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
642                                 break;
643                         default:
644                                 break;
645                         }
646                 }
647                 free(ntl, M_DEVBUF);
648 nonettypes:
649                 len = sizeof(rates);
650                 bzero((char *)&rates, len);
651                 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
652                     (void *)rates, &len);
653                 if (r)
654                         device_printf (dev, "get rates failed: 0x%x\n", r);
655                 /*
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
658                  * all up to heck.
659                  */
660
661 #define TESTSETRATE(x, y)                                               \
662         do {                                                            \
663                 int                     i;                              \
664                 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) {   \
665                         if (ic->ic_sup_rates[x].rs_rates[i] == (y))     \
666                                 break;                                  \
667                 }                                                       \
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++;                \
671                 }                                                       \
672         } while (0)
673
674 #define SETRATE(x, y)   \
675         ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
676 #define INCRATE(x)      \
677         ic->ic_sup_rates[x].rs_nrates++
678
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) {
688                         case 2:
689                         case 4:
690                         case 11:
691                         case 10:
692                         case 22:
693                                 if (!(ic->ic_modecaps &
694                                     (1<<IEEE80211_MODE_11B))) {
695                                         /* Lazy-init 802.11b. */
696                                         ic->ic_modecaps |=
697                                             (1<<IEEE80211_MODE_11B);
698                                         ic->ic_sup_rates[IEEE80211_MODE_11B].
699                                             rs_nrates = 0;
700                                 }
701                                 SETRATE(IEEE80211_MODE_11B, rates[i]);
702                                 INCRATE(IEEE80211_MODE_11B);
703                                 break;
704                         default:
705                                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
706                                         SETRATE(IEEE80211_MODE_11A, rates[i]);
707                                         INCRATE(IEEE80211_MODE_11A);
708                                 }
709                                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
710                                         SETRATE(IEEE80211_MODE_11G, rates[i]);
711                                         INCRATE(IEEE80211_MODE_11G);
712                                 }
713                                 break;
714                         }
715                 }
716
717                 /*
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?
723                  */
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);
733                 }
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);
739                 }
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);
745                 }
746 #undef SETRATE
747 #undef INCRATE
748                 /*
749                  * Taking yet more guesses here.
750                  */
751                 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
752                         int chanflag = 0;
753
754                         if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
755                                 chanflag |= IEEE80211_CHAN_G;
756                         if (i <= 14)
757                                 chanflag |= IEEE80211_CHAN_B;
758                         if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
759                             i > 14)
760                                 chanflag = IEEE80211_CHAN_A;
761                         if (chanflag == 0)
762                                 break;
763                         ic->ic_channels[i].ic_freq =
764                             ieee80211_ieee2mhz(i, chanflag);
765                         ic->ic_channels[i].ic_flags = chanflag;
766                 }
767
768                 i = sizeof(arg);
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;
772                 i = sizeof(arg);
773                 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
774                 if (r == 0)
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,
779                     ndis_media_status);
780                 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
781                 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
782         } else {
783                 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
784                     ndis_ifmedia_sts);
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);
793         }
794
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;
798 fail:
799         if (error)
800                 ndis_detach(dev);
801         else
802                 /* We're done talking to the NIC for now; halt it. */
803                 ndis_halt_nic(sc);
804
805         return(error);
806 }
807
808 /*
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
813  * allocated.
814  */
815 int
816 ndis_detach(dev)
817         device_t                dev;
818 {
819         struct ndis_softc       *sc;
820         struct ifnet            *ifp;
821         driver_object           *drv;
822
823         sc = device_get_softc(dev);
824         KASSERT(mtx_initialized(&sc->ndis_mtx),
825             ("ndis mutex not initialized"));
826         NDIS_LOCK(sc);
827         ifp = &sc->arpcom.ac_if;
828         ifp->if_flags &= ~IFF_UP;
829
830         if (device_is_attached(dev)) {
831                 NDIS_UNLOCK(sc);
832                 ndis_stop(sc);
833                 if (sc->ndis_80211)
834                         ieee80211_ifdetach(&sc->ic);
835                 else
836                         ether_ifdetach(ifp);
837         } else
838                 NDIS_UNLOCK(sc);
839
840         bus_generic_detach(dev);
841
842         if (sc->ndis_intrhand)
843                 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
844         if (sc->ndis_irq)
845                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
846         if (sc->ndis_res_io)
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);
855
856         if (sc->ndis_iftype == PCMCIABus)
857                 ndis_free_amem(sc);
858
859         if (sc->ndis_sc)
860                 ndis_destroy_dma(sc);
861
862         if (sc->ndis_txarray)
863                 free(sc->ndis_txarray, M_DEVBUF);
864
865         if (!sc->ndis_80211)
866                 ifmedia_removeall(&sc->ifmedia);
867
868         if (sc->ndis_txpool != NULL)
869                 NdisFreePacketPool(sc->ndis_txpool);
870
871         ndis_unload_driver(sc);
872
873         /* Destroy the PDO for this device. */
874         
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");
879         else
880                 drv = windrv_lookup(0, "USB Bus");
881         if (drv == NULL)
882                 panic("couldn't find driver object");
883         windrv_destroy_pdo(drv, dev);
884
885         if (sc->ndis_iftype == PCIBus)
886                 bus_dma_tag_destroy(sc->ndis_parent_tag);
887
888 #if __FreeBSD_version < 502113
889         sysctl_ctx_free(&sc->ndis_ctx);
890 #endif
891
892         mtx_destroy(&sc->ndis_mtx);
893
894         return(0);
895 }
896
897 int
898 ndis_suspend(dev)
899         device_t                dev;
900 {
901         struct ndis_softc       *sc;
902         struct ifnet            *ifp;
903
904         sc = device_get_softc(dev);
905         ifp = &sc->arpcom.ac_if;
906
907 #ifdef notdef
908         if (NDIS_INITIALIZED(sc))
909                 ndis_stop(sc);
910 #endif
911
912         return(0);
913 }
914
915 int
916 ndis_resume(dev)
917         device_t                dev;
918 {
919         struct ndis_softc       *sc;
920         struct ifnet            *ifp;
921
922         sc = device_get_softc(dev);
923         ifp = &sc->arpcom.ac_if;
924
925         if (NDIS_INITIALIZED(sc))
926                 ndis_init(sc);
927
928         return(0);
929 }
930
931 /*
932  * A frame has been uploaded: pass the resulting mbuf chain up to
933  * the higher level protocols.
934  *
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.
943  *
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
948  * packet.
949  */
950 static void
951 ndis_rxeof(adapter, packets, pktcnt)
952         ndis_handle             adapter;
953         ndis_packet             **packets;
954         uint32_t                pktcnt;
955 {
956         struct ndis_softc       *sc;
957         ndis_miniport_block     *block;
958         ndis_packet             *p;
959         uint32_t                s;
960         ndis_tcpip_csum         *csum;
961         struct ifnet            *ifp;
962         struct mbuf             *m0, *m;
963         int                     i;
964
965         block = (ndis_miniport_block *)adapter;
966         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
967         ifp = &sc->arpcom.ac_if;
968
969         for (i = 0; i < pktcnt; i++) {
970                 p = packets[i];
971                 /* Stash the softc here so ptom can use it. */
972                 p->np_softc = sc;
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);
977                 } else {
978                         if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
979                                 m = m_dup(m0, M_DONTWAIT);
980                                 /*
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
986                                  * doing anything.
987                                  */
988                                 p->np_refcnt++;
989                                 m_freem(m0);
990                                 if (m == NULL)
991                                         ifp->if_ierrors++;
992                                 else
993                                         m0 = m;
994                         } else
995                                 p->np_oob.npo_status = NDIS_STATUS_PENDING;
996                         m0->m_pkthdr.rcvif = ifp;
997                         ifp->if_ipackets++;
998
999                         /* Deal with checksum offload. */
1000
1001                         if (ifp->if_capenable & IFCAP_RXCSUM &&
1002                             p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
1003                                 s = (uintptr_t)
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;
1016                                 }
1017                         }
1018
1019                         (*ifp->if_input)(ifp, m0);
1020                 }
1021         }
1022
1023         return;
1024 }
1025
1026 /*
1027  * A frame was downloaded to the chip. It's safe for us to clean up
1028  * the list buffers.
1029  */
1030 static void
1031 ndis_txeof(adapter, packet, status)
1032         ndis_handle             adapter;
1033         ndis_packet             *packet;
1034         ndis_status             status;
1035
1036 {
1037         struct ndis_softc       *sc;
1038         ndis_miniport_block     *block;
1039         struct ifnet            *ifp;
1040         int                     idx;
1041         struct mbuf             *m;
1042
1043         block = (ndis_miniport_block *)adapter;
1044         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1045         ifp = &sc->arpcom.ac_if;
1046
1047         m = packet->np_m0;
1048         idx = packet->np_txidx;
1049         if (sc->ndis_sc)
1050                 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
1051
1052         ndis_free_packet(packet);
1053         m_freem(m);
1054
1055         NDIS_LOCK(sc);
1056         sc->ndis_txarray[idx] = NULL;
1057         sc->ndis_txpending++;
1058
1059         if (status == NDIS_STATUS_SUCCESS)
1060                 ifp->if_opackets++;
1061         else
1062                 ifp->if_oerrors++;
1063         ifp->if_timer = 0;
1064         ifp->if_flags &= ~IFF_OACTIVE;
1065         NDIS_UNLOCK(sc);
1066
1067         ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
1068
1069         return;
1070 }
1071
1072 static void
1073 ndis_linksts(adapter, status, sbuf, slen)
1074         ndis_handle             adapter;
1075         ndis_status             status;
1076         void                    *sbuf;
1077         uint32_t                slen;
1078 {
1079         ndis_miniport_block     *block;
1080         struct ndis_softc       *sc;
1081
1082         block = adapter;
1083         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1084
1085         block->nmb_getstat = status;
1086
1087         return;
1088 }
1089
1090 static void
1091 ndis_linksts_done(adapter)
1092         ndis_handle             adapter;
1093 {
1094         ndis_miniport_block     *block;
1095         struct ndis_softc       *sc;
1096         struct ifnet            *ifp;
1097
1098         block = adapter;
1099         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
1100         ifp = &sc->arpcom.ac_if;
1101
1102         if (!NDIS_INITIALIZED(sc))
1103                 return;
1104
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);
1109                 break;
1110         case NDIS_STATUS_MEDIA_DISCONNECT:
1111                 if (sc->ndis_link)
1112                         ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1113                 break;
1114         default:
1115                 break;
1116         }
1117
1118         return;
1119 }
1120
1121 static void
1122 ndis_intr(arg)
1123         void                    *arg;
1124 {
1125         struct ndis_softc       *sc;
1126         struct ifnet            *ifp;
1127         int                     is_our_intr = 0;
1128         int                     call_isr = 0;
1129         uint8_t                 irql;
1130         ndis_miniport_interrupt *intr;
1131
1132         sc = arg;
1133         ifp = &sc->arpcom.ac_if;
1134         intr = sc->ndis_block->nmb_interrupt;
1135
1136         if (sc->ndis_block->nmb_miniportadapterctx == NULL)
1137                 return;
1138
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);
1142         else {
1143                 ndis_disable_intr(sc);
1144                 call_isr = 1;
1145         }
1146         KeReleaseSpinLock(&intr->ni_dpccountlock, irql);
1147
1148         if ((is_our_intr || call_isr))
1149                 IoRequestDpc(sc->ndis_block->nmb_deviceobj, NULL, sc);
1150
1151         return;
1152 }
1153
1154 static void
1155 ndis_tick(xsc)
1156         void                    *xsc;
1157 {
1158         struct ndis_softc       *sc;
1159
1160         mtx_unlock(&Giant);
1161
1162         sc = xsc;
1163
1164         ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1165         sc->ndis_stat_ch = timeout(ndis_tick, sc, hz *
1166             sc->ndis_block->nmb_checkforhangsecs);
1167
1168         mtx_lock(&Giant);
1169
1170         return;
1171 }
1172
1173 static void
1174 ndis_ticktask(xsc)
1175         void                    *xsc;
1176 {
1177         struct ndis_softc       *sc;
1178         ndis_checkforhang_handler hangfunc;
1179         uint8_t                 rval;
1180         ndis_media_state        linkstate;
1181         int                     error, len;
1182
1183         sc = xsc;
1184
1185         hangfunc = sc->ndis_chars->nmc_checkhang_func;
1186
1187         if (hangfunc != NULL) {
1188                 rval = MSCALL1(hangfunc,
1189                     sc->ndis_block->nmb_miniportadapterctx);
1190                 if (rval == TRUE) {
1191                         ndis_reset_nic(sc);
1192                         return;
1193                 }
1194         }
1195
1196         len = sizeof(linkstate);
1197         error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1198             (void *)&linkstate, &len);
1199
1200         NDIS_LOCK(sc);
1201
1202         if (sc->ndis_link == 0 && linkstate == nmc_connected) {
1203                 device_printf(sc->ndis_dev, "link up\n");
1204                 sc->ndis_link = 1;
1205                 NDIS_UNLOCK(sc);
1206                 if (sc->ndis_80211)
1207                         ndis_getstate_80211(sc);
1208                 NDIS_LOCK(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 */
1213         }
1214
1215         if (sc->ndis_link == 1 && linkstate == nmc_disconnected) {
1216                 device_printf(sc->ndis_dev, "link down\n");
1217                 sc->ndis_link = 0;
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 */
1222         }
1223
1224         NDIS_UNLOCK(sc);
1225
1226         return;
1227 }
1228
1229 static void
1230 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1231         void                    *arg;
1232         bus_dma_segment_t       *segs;
1233         int                     nseg;
1234         bus_size_t              mapsize;
1235         int                     error;
1236
1237 {
1238         struct ndis_sc_list     *sclist;
1239         int                     i;
1240
1241         if (error || arg == NULL)
1242                 return;
1243
1244         sclist = arg;
1245
1246         sclist->nsl_frags = nseg;
1247
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;
1251         }
1252
1253         return;
1254 }
1255
1256 static void
1257 ndis_starttask(arg)
1258         void                    *arg;
1259 {
1260         struct ifnet            *ifp;
1261
1262         ifp = arg;
1263 #if __FreeBSD_version < 502114
1264         if (ifp->if_snd.ifq_head != NULL)
1265 #else
1266         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1267 #endif
1268                 ndis_start(ifp);
1269         return;
1270 }
1271
1272 /*
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.
1279  *
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.
1283  */
1284
1285 static void
1286 ndis_start(ifp)
1287         struct ifnet            *ifp;
1288 {
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;
1294
1295         sc = ifp->if_softc;
1296
1297         NDIS_LOCK(sc);
1298
1299         if (!sc->ndis_link || ifp->if_flags & IFF_OACTIVE) {
1300                 NDIS_UNLOCK(sc);
1301                 return;
1302         }
1303
1304         p0 = &sc->ndis_txarray[sc->ndis_txidx];
1305
1306         while(sc->ndis_txpending) {
1307 #if __FreeBSD_version < 502114
1308                 IF_DEQUEUE(&ifp->if_snd, m);
1309 #else
1310                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1311 #endif
1312                 if (m == NULL)
1313                         break;
1314
1315                 NdisAllocatePacket(&status,
1316                     &sc->ndis_txarray[sc->ndis_txidx], sc->ndis_txpool);
1317
1318                 if (status != NDIS_STATUS_SUCCESS)
1319                         break;
1320
1321                 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1322 #if __FreeBSD_version >= 502114
1323                         IFQ_DRV_PREPEND(&ifp->if_snd, m);
1324 #endif
1325                         NDIS_UNLOCK(sc);
1326 #if __FreeBSD_version < 502114
1327                         IF_PREPEND(&ifp->if_snd, m);
1328 #endif
1329                         return;
1330                 }
1331
1332                 /*
1333                  * Save pointer to original mbuf
1334                  * so we can free it later.
1335                  */
1336
1337                 p = sc->ndis_txarray[sc->ndis_txidx];
1338                 p->np_txidx = sc->ndis_txidx;
1339                 p->np_m0 = m;
1340                 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1341
1342                 /*
1343                  * Do scatter/gather processing, if driver requested it.
1344                  */
1345                 if (sc->ndis_sc) {
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;
1353                 }
1354
1355                 /* Handle checksum offload. */
1356
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;
1369                 }
1370
1371                 NDIS_INC(sc);
1372                 sc->ndis_txpending--;
1373
1374                 pcnt++;
1375
1376                 /*
1377                  * If there's a BPF listener, bounce a copy of this frame
1378                  * to him.
1379                  */
1380
1381                 BPF_MTAP(ifp, m);
1382
1383                 /*
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.
1389                  */
1390
1391                 if (sc->ndis_txidx == 0)
1392                         break;
1393         }
1394
1395         if (pcnt == 0) {
1396                 NDIS_UNLOCK(sc);
1397                 return;
1398         }
1399
1400         if (sc->ndis_txpending == 0)
1401                 ifp->if_flags |= IFF_OACTIVE;
1402
1403         /*
1404          * Set a timeout in case the chip goes out to lunch.
1405          */
1406         ifp->if_timer = 5;
1407
1408         NDIS_UNLOCK(sc);
1409
1410         if (sc->ndis_maxpkts == 1)
1411                 ndis_send_packet(sc, p);
1412         else
1413                 ndis_send_packets(sc, p0, pcnt);
1414
1415         return;
1416 }
1417
1418 static void
1419 ndis_init(xsc)
1420         void                    *xsc;
1421 {
1422         struct ndis_softc       *sc = xsc;
1423         struct ifnet            *ifp = &sc->arpcom.ac_if;
1424         int                     i, error;
1425
1426         /*
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.
1431          */
1432         if (sc->ndis_link && sc->ndis_skip)
1433                 return;
1434
1435         /*
1436          * Cancel pending I/O and free all RX/TX buffers.
1437          */
1438         ndis_stop(sc);
1439         if (ndis_init_nic(sc))
1440                 return;
1441
1442         /* Init our MAC address */
1443
1444         /* Program the packet filter */
1445
1446         sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1447
1448         if (ifp->if_flags & IFF_BROADCAST)
1449                 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1450
1451         if (ifp->if_flags & IFF_PROMISC)
1452                 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1453
1454         i = sizeof(sc->ndis_filter);
1455
1456         error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1457             &sc->ndis_filter, &i);
1458
1459         if (error)
1460                 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1461
1462         /*
1463          * Program the multicast filter, if necessary.
1464          */
1465         ndis_setmulti(sc);
1466
1467         /* Setup task offload. */
1468         ndis_set_offload(sc);
1469
1470         /* Enable interrupts. */
1471         ndis_enable_intr(sc);
1472
1473         if (sc->ndis_80211)
1474                 ndis_setstate_80211(sc);
1475
1476         NDIS_LOCK(sc);
1477
1478         sc->ndis_txidx = 0;
1479         sc->ndis_txpending = sc->ndis_maxpkts;
1480         sc->ndis_link = 0;
1481
1482         ifp->if_flags |= IFF_RUNNING;
1483         ifp->if_flags &= ~IFF_OACTIVE;
1484
1485         NDIS_UNLOCK(sc);
1486
1487         /*
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.
1492          */
1493
1494         if (sc->ndis_block->nmb_checkforhangsecs == 0)
1495                 sc->ndis_block->nmb_checkforhangsecs = 3;
1496
1497         sc->ndis_stat_ch = timeout(ndis_tick, sc,
1498             hz * sc->ndis_block->nmb_checkforhangsecs);
1499
1500         return;
1501 }
1502
1503 /*
1504  * Set media options.
1505  */
1506 static int
1507 ndis_ifmedia_upd(ifp)
1508         struct ifnet            *ifp;
1509 {
1510         struct ndis_softc               *sc;
1511
1512         sc = ifp->if_softc;
1513
1514         if (NDIS_INITIALIZED(sc))
1515                 ndis_init(sc);
1516
1517         return(0);
1518 }
1519
1520 /*
1521  * Report current media status.
1522  */
1523 static void
1524 ndis_ifmedia_sts(ifp, ifmr)
1525         struct ifnet            *ifp;
1526         struct ifmediareq       *ifmr;
1527 {
1528         struct ndis_softc       *sc;
1529         uint32_t                media_info;
1530         ndis_media_state        linkstate;
1531         int                     error, len;
1532
1533         ifmr->ifm_status = IFM_AVALID;
1534         ifmr->ifm_active = IFM_ETHER;
1535         sc = ifp->if_softc;
1536
1537         if (!NDIS_INITIALIZED(sc))
1538                 return;
1539
1540         len = sizeof(linkstate);
1541         error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1542             (void *)&linkstate, &len);
1543
1544         len = sizeof(media_info);
1545         error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
1546             (void *)&media_info, &len);
1547
1548         if (linkstate == nmc_connected)
1549                 ifmr->ifm_status |= IFM_ACTIVE;
1550
1551         switch(media_info) {
1552         case 100000:
1553                 ifmr->ifm_active |= IFM_10_T;
1554                 break;
1555         case 1000000:
1556                 ifmr->ifm_active |= IFM_100_TX;
1557                 break;
1558         case 10000000:
1559                 ifmr->ifm_active |= IFM_1000_T;
1560                 break;
1561         default:
1562                 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
1563                 break;
1564         }
1565
1566         return;
1567 }
1568
1569 static void
1570 ndis_setstate_80211(sc)
1571         struct ndis_softc       *sc;
1572 {
1573         struct ieee80211com     *ic;
1574         ndis_80211_ssid         ssid;
1575         ndis_80211_config       config;
1576         ndis_80211_wep          wep;
1577         int                     i, rval = 0, len;
1578         uint32_t                arg;
1579         struct ifnet            *ifp;
1580
1581         ic = &sc->ic;
1582         ifp = &sc->arpcom.ac_if;
1583
1584         if (!NDIS_INITIALIZED(sc))
1585                 return;
1586
1587         /* Set network infrastructure mode. */
1588
1589         len = sizeof(arg);
1590         if (ic->ic_opmode == IEEE80211_M_IBSS)
1591                 arg = NDIS_80211_NET_INFRA_IBSS;
1592         else
1593                 arg = NDIS_80211_NET_INFRA_BSS;
1594
1595         rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
1596
1597         if (rval)
1598                 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
1599
1600         /* Set WEP */
1601
1602 #ifdef IEEE80211_F_PRIVACY
1603         if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1604 #else
1605         if (ic->ic_wep_mode >= IEEE80211_WEP_ON) {
1606 #endif
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;
1611 #ifdef notdef
1612                                 /* 5 and 13 are the only valid key lengths */
1613                                 if (ic->ic_nw_keys[i].wk_keylen < 5)
1614                                         wep.nw_keylen = 5;
1615                                 else if (ic->ic_nw_keys[i].wk_keylen > 5 &&
1616                                      ic->ic_nw_keys[i].wk_keylen < 13)
1617                                         wep.nw_keylen = 13;
1618 #endif
1619                                 wep.nw_keyidx = i;
1620                                 wep.nw_length = (sizeof(uint32_t) * 3)
1621                                     + wep.nw_keylen;
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);
1626                                 len = sizeof(wep);
1627                                 rval = ndis_set_info(sc,
1628                                     OID_802_11_ADD_WEP, &wep, &len);
1629                                 if (rval)
1630                                         device_printf(sc->ndis_dev,
1631                                             "set wepkey failed: %d\n", rval);
1632                         }
1633                 }
1634                 arg = NDIS_80211_WEPSTAT_ENABLED;
1635                 len = sizeof(arg);
1636                 rval = ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1637                 if (rval)
1638                         device_printf(sc->ndis_dev,
1639                             "enable WEP failed: %d\n", rval);
1640 #ifndef IEEE80211_F_WEPON
1641 #if 0
1642                 if (ic->ic_wep_mode != IEEE80211_WEP_8021X &&
1643                     ic->ic_wep_mode != IEEE80211_WEP_ON)
1644                         arg = NDIS_80211_PRIVFILT_ACCEPTALL;
1645                 else
1646 #endif
1647 #endif
1648                         arg = NDIS_80211_PRIVFILT_8021XWEP;
1649                 len = sizeof(arg);
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. */
1653                 if (rval == 0) {
1654                         if (arg == NDIS_80211_PRIVFILT_ACCEPTALL)
1655                                 ic->ic_wep_mode = IEEE80211_WEP_MIXED;
1656                         else
1657                                 ic->ic_wep_mode = IEEE80211_WEP_8021X;
1658                 }
1659 #endif
1660                 arg = NDIS_80211_AUTHMODE_OPEN;
1661         } else {
1662                 arg = NDIS_80211_WEPSTAT_DISABLED;
1663                 len = sizeof(arg);
1664                 ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1665                 arg = NDIS_80211_AUTHMODE_OPEN;
1666         }
1667
1668         len = sizeof(arg);
1669         rval = ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
1670
1671 #ifdef notyet
1672         if (rval)
1673                 device_printf (sc->ndis_dev, "set auth failed: %d\n", rval);
1674 #endif
1675
1676 #ifdef notyet
1677         /* Set network type. */
1678
1679         arg = 0;
1680
1681         switch (ic->ic_curmode) {
1682         case IEEE80211_MODE_11A:
1683                 arg = NDIS_80211_NETTYPE_11OFDM5;
1684                 break;
1685         case IEEE80211_MODE_11B:
1686                 arg = NDIS_80211_NETTYPE_11DS;
1687                 break;
1688         case IEEE80211_MODE_11G:
1689                 arg = NDIS_80211_NETTYPE_11OFDM24;
1690                 break;
1691         default:
1692                 device_printf(sc->ndis_dev, "unknown mode: %d\n",
1693                     ic->ic_curmode);
1694         }
1695
1696         if (arg) {
1697                 len = sizeof(arg);
1698                 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
1699                     &arg, &len);
1700                 if (rval)
1701                         device_printf (sc->ndis_dev,
1702                             "set nettype failed: %d\n", rval);
1703         }
1704 #endif
1705
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); 
1711
1712         /*
1713          * Some drivers expect us to initialize these values, so
1714          * provide some defaults.
1715          */
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;
1722
1723         if (rval == 0 && ic->ic_ibss_chan != IEEE80211_CHAN_ANYC) { 
1724                 int chan, chanflag;
1725
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,
1738                             &config, &len);
1739                         if (rval)
1740                                 device_printf(sc->ndis_dev, "couldn't change "
1741                                     "DS config to %ukHz: %d\n",
1742                                     config.nc_dsconfig, rval);
1743                 }
1744         } else if (rval)
1745                 device_printf(sc->ndis_dev, "couldn't retrieve "
1746                     "channel info: %d\n", rval);
1747
1748         /* Set SSID -- always do this last. */
1749
1750         len = sizeof(ssid);
1751         bzero((char *)&ssid, len);
1752         ssid.ns_ssidlen = ic->ic_des_esslen;
1753         if (ssid.ns_ssidlen == 0) {
1754                 ssid.ns_ssidlen = 1;
1755         } else
1756                 bcopy(ic->ic_des_essid, ssid.ns_ssid, ssid.ns_ssidlen);
1757         rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
1758
1759         if (rval)
1760                 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
1761
1762         return;
1763 }
1764
1765 static void
1766 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1767 {
1768         struct ieee80211com *ic = &((struct ndis_softc *)ifp->if_softc)->ic;
1769         struct ieee80211_node *ni = NULL;
1770
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:
1778                 ni = ic->ic_bss;
1779                 /* calculate rate subtype */
1780                 imr->ifm_active |= ieee80211_rate2media(ic,
1781                         ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1782                 break;
1783         case IEEE80211_M_IBSS:
1784                 ni = ic->ic_bss;
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;
1789                 break;
1790         case IEEE80211_M_AHDEMO:
1791                 /* should not come here */
1792                 break;
1793         case IEEE80211_M_HOSTAP:
1794                 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1795                 break;
1796         case IEEE80211_M_MONITOR:
1797                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1798                 break;
1799         }
1800         switch (ic->ic_curmode) {
1801         case IEEE80211_MODE_11A:
1802                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
1803                 break;
1804         case IEEE80211_MODE_11B:
1805                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
1806                 break;
1807         case IEEE80211_MODE_11G:
1808                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
1809                 break;
1810         case IEEE80211_MODE_TURBO_A:
1811                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
1812                                 |  IFM_IEEE80211_TURBO;
1813                 break;
1814         }
1815 }
1816
1817 static int
1818 ndis_get_assoc(sc, assoc)
1819         struct ndis_softc       *sc;
1820         ndis_wlan_bssid_ex      **assoc;
1821 {
1822         ndis_80211_bssid_list_ex        *bl;
1823         ndis_wlan_bssid_ex      *bs;
1824         ndis_80211_macaddr      bssid;
1825         int                     i, len, error;
1826
1827         if (!sc->ndis_link)
1828                 return(ENOENT);
1829
1830         len = sizeof(bssid);
1831         error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
1832         if (error) {
1833                 device_printf(sc->ndis_dev, "failed to get bssid\n");
1834                 return(ENOENT);
1835         }
1836         len = 0;
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");
1840                 return (error);
1841         }
1842
1843         bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
1844         error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1845         if (error) {
1846                 free(bl, M_TEMP);
1847                 device_printf(sc->ndis_dev, "bssid_list failed\n");
1848                 return (error);
1849         }
1850
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) {
1856                                 free(bl, M_TEMP);
1857                                 return(ENOMEM);
1858                         }
1859                         bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
1860                         free(bl, M_TEMP);
1861                         return(0);
1862                 }       
1863                 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
1864         }
1865
1866         free(bl, M_TEMP);
1867         return(ENOENT);
1868 }
1869
1870 static void
1871 ndis_getstate_80211(sc)
1872         struct ndis_softc       *sc;
1873 {
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;
1879         uint32_t                arg;
1880         struct ifnet            *ifp;
1881
1882         ic = &sc->ic;
1883         ifp = &sc->arpcom.ac_if;
1884
1885         if (!NDIS_INITIALIZED(sc))
1886                 return;
1887
1888         if (sc->ndis_link)
1889                 ic->ic_state = IEEE80211_S_RUN;
1890         else
1891                 ic->ic_state = IEEE80211_S_ASSOC;
1892
1893
1894         /*
1895          * If we're associated, retrieve info on the current bssid.
1896          */
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;
1902                         break;
1903                 case NDIS_80211_NETTYPE_11OFDM5:
1904                         ic->ic_curmode = IEEE80211_MODE_11A;
1905                         break;
1906                 case NDIS_80211_NETTYPE_11OFDM24:
1907                         ic->ic_curmode = IEEE80211_MODE_11G;
1908                         break;
1909                 default:
1910                         device_printf(sc->ndis_dev,
1911                             "unknown nettype %d\n", arg);
1912                         break;
1913                 }
1914                 IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid, bs->nwbx_macaddr);
1915                 free(bs, M_TEMP);
1916         } else
1917                 return;
1918
1919         len = sizeof(ssid);
1920         bzero((char *)&ssid, len);
1921         rval = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
1922
1923         if (rval)
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;
1927
1928         len = sizeof(arg);
1929         rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
1930         if (rval)
1931                 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
1932                     rval);
1933
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)
1939                                 break;
1940                 }
1941         }
1942
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)
1949                                 break;
1950                 }
1951         }
1952
1953         if (i == ic->ic_bss->ni_rates.rs_nrates)
1954                 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
1955                     arg / 5000);
1956         else
1957                 ic->ic_bss->ni_txrate = i;
1958
1959         if (ic->ic_caps & IEEE80211_C_PMGT) {
1960                 len = sizeof(arg);
1961                 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
1962
1963                 if (rval)
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;
1968                 else
1969                         ic->ic_flags |= IEEE80211_F_PMGTON;
1970         }
1971
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);   
1977         if (rval == 0) { 
1978                 int chan;
1979
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];
1987                 } else
1988                         ic->ic_bss->ni_chan = &ic->ic_channels[chan];
1989         } else
1990                 device_printf(sc->ndis_dev, "couldn't retrieve "
1991                     "channel info: %d\n", rval);
1992
1993 /*
1994         len = sizeof(arg);
1995         rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1996
1997         if (rval)
1998                 device_printf (sc->ndis_dev,
1999                     "get wep status failed: %d\n", rval);
2000
2001         if (arg == NDIS_80211_WEPSTAT_ENABLED)
2002                 ic->ic_flags |= IEEE80211_F_WEPON;
2003         else
2004                 ic->ic_flags &= ~IEEE80211_F_WEPON;
2005 */
2006         return;
2007 }
2008
2009 static int
2010 ndis_ioctl(ifp, command, data)
2011         struct ifnet            *ifp;
2012         u_long                  command;
2013         caddr_t                 data;
2014 {
2015         struct ndis_softc       *sc = ifp->if_softc;
2016         struct ifreq            *ifr = (struct ifreq *) data;
2017         int                     i, error = 0;
2018
2019         /*NDIS_LOCK(sc);*/
2020
2021         switch(command) {
2022         case SIOCSIFFLAGS:
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)) {
2027                                 sc->ndis_filter |=
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) {
2036                                 sc->ndis_filter &=
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);
2042                         } else
2043                                 ndis_init(sc);
2044                 } else {
2045                         if (ifp->if_flags & IFF_RUNNING)
2046                                 ndis_stop(sc);
2047                 }
2048                 sc->ndis_if_flags = ifp->if_flags;
2049                 error = 0;
2050                 break;
2051         case SIOCADDMULTI:
2052         case SIOCDELMULTI:
2053                 ndis_setmulti(sc);
2054                 error = 0;
2055                 break;
2056         case SIOCGIFMEDIA:
2057         case SIOCSIFMEDIA:
2058                 if (sc->ndis_80211) {
2059                         error = ieee80211_ioctl(&sc->ic, command, data);
2060                         if (error == ENETRESET) {
2061                                 ndis_setstate_80211(sc);
2062                                 /*ndis_init(sc);*/
2063                                 error = 0;
2064                         }
2065                 } else
2066                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
2067                 break;
2068         case SIOCSIFCAP:
2069                 ifp->if_capenable = ifr->ifr_reqcap;
2070                 if (ifp->if_capenable & IFCAP_TXCSUM)
2071                         ifp->if_hwassist = sc->ndis_hwassist;
2072                 else
2073                         ifp->if_hwassist = 0;
2074                 ndis_set_offload(sc);
2075                 break;
2076         case SIOCG80211:
2077                 if (!NDIS_INITIALIZED(sc))
2078                         goto do_80211;
2079                 if (sc->ndis_80211)
2080                         error = ndis_80211_ioctl_get(ifp, command, data);
2081                 else
2082                         error = ENOTTY;
2083                 break;
2084         case SIOCS80211:
2085                 if (!NDIS_INITIALIZED(sc))
2086                         goto do_80211;
2087                 if (sc->ndis_80211)
2088                         error = ndis_80211_ioctl_set(ifp, command, data);
2089                 else
2090                         error = ENOTTY;
2091                 break;
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);
2097                         else
2098                                 error = ndis_wi_ioctl_set(ifp, command, data);
2099                 } else
2100                         error = ENOTTY;
2101                 if (error != ENOTTY)
2102                         break;
2103         default:
2104 do_80211:
2105                 sc->ndis_skip = 1;
2106                 if (sc->ndis_80211) {
2107                         error = ieee80211_ioctl(&sc->ic, command, data);
2108                         if (error == ENETRESET) {
2109                                 ndis_setstate_80211(sc);
2110                                 error = 0;
2111                         }
2112                 } else
2113                         error = ether_ioctl(ifp, command, data);
2114                 sc->ndis_skip = 0;
2115                 break;
2116         }
2117
2118         /*NDIS_UNLOCK(sc);*/
2119
2120         return(error);
2121 }
2122
2123 static int
2124 ndis_wi_ioctl_get(ifp, command, data)
2125         struct ifnet            *ifp;
2126         u_long                  command;
2127         caddr_t                 data;
2128 {
2129         struct wi_req           wreq;
2130         struct ifreq            *ifr;
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;
2136
2137         sc = ifp->if_softc;
2138         ifr = (struct ifreq *)data;
2139         error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2140         if (error)
2141                 return (error);
2142
2143         switch (wreq.wi_type) {
2144         case WI_RID_READ_APS:
2145                 len = 0;
2146                 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2147                     NULL, &len);
2148                 if (error == 0)
2149                         tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2150                 len = 0;
2151                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2152                 if (error != ENOSPC)
2153                         break;
2154                 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
2155                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2156                 if (error) {
2157                         free(bl, M_DEVBUF);
2158                         break;
2159                 }
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;
2166                 while (maxaps--) {
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;
2177                         api->channel =
2178                             ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2179                             1000, 0);
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 */
2186                         } else
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));
2191                         api++;
2192                         wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2193                 }
2194                 free(bl, M_DEVBUF);
2195                 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2196                 break;
2197         default:
2198                 error = ENOTTY;
2199                 break;
2200         }
2201         return (error);
2202 }
2203
2204 static int
2205 ndis_wi_ioctl_set(ifp, command, data)
2206         struct ifnet            *ifp;
2207         u_long                  command;
2208         caddr_t                 data;
2209 {
2210         struct wi_req           wreq;
2211         struct ifreq            *ifr;
2212         struct ndis_softc       *sc;
2213         uint32_t                foo;
2214         int                     error, len;
2215
2216         error = suser(curthread);
2217         if (error)
2218                 return (error);
2219
2220         sc = ifp->if_softc;
2221         ifr = (struct ifreq *)data;
2222         error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2223         if (error)
2224                 return (error);
2225
2226         switch (wreq.wi_type) {
2227         case WI_RID_SCAN_APS:
2228         case WI_RID_SCAN_REQ:                   /* arguments ignored */
2229                 len = sizeof(foo);
2230                 foo = 0;
2231                 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2232                     &len);
2233                 break;
2234         default:
2235                 error = ENOTTY;
2236                 break;
2237         }
2238         return (error);
2239 }
2240
2241 static int
2242 ndis_80211_ioctl_get(struct ifnet *ifp, u_long command, caddr_t data)
2243 {
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;
2252         char                    *cp;
2253         uint8_t                 nodename[IEEE80211_NWID_LEN];
2254         uint16_t                nodename_u[IEEE80211_NWID_LEN + 1];
2255         char                    *acode;
2256
2257         sc = ifp->if_softc;
2258         ireq = (struct ieee80211req *) data;
2259                 
2260         switch (ireq->i_type) {
2261         case IEEE80211_IOC_MLME:
2262                 error = 0;
2263                 break;
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);
2268                 if (error) {
2269                         device_printf(sc->ndis_dev, "failed to get bssid\n");
2270                         return(error);
2271                 }
2272                 ireq->i_len = len;
2273                 error = copyout(&bssid, ireq->i_data, len);
2274                 break;
2275         case IEEE80211_IOC_SSID:
2276                 len = sizeof(ssid);
2277                 bzero((char*)&ssid, len);
2278                 error = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
2279                 if (error) {
2280                         device_printf(sc->ndis_dev, "failed to get ssid: %d\n", error);
2281                         return(error);
2282                 }
2283                 ireq->i_len = ssid.ns_ssidlen;
2284                 error = copyout(&ssid.ns_ssid, ireq->i_data, ssid.ns_ssidlen);
2285                 break;
2286         case IEEE80211_IOC_SCAN_RESULTS:
2287                 len = 0;
2288                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2289                 if (error != ENOSPC)
2290                         break;
2291                 bl = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
2292                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2293                 if (error) {
2294                         free(bl, M_DEVBUF);
2295                         break;
2296                 }
2297                 sr = bsr = malloc(ireq->i_len, M_DEVBUF, M_WAITOK | M_ZERO);
2298                 wb = bl->nblx_bssid;
2299                 len = 0;
2300                 for (i = 0; i < bl->nblx_items; i++) {
2301                         /*
2302                          * Check if we have enough space left for this ap
2303                          */
2304                         j = roundup(sizeof(*sr) + wb->nwbx_ssid.ns_ssidlen
2305                             + wb->nwbx_ielen -
2306                             sizeof(struct ndis_80211_fixed_ies),
2307                             sizeof(uint32_t));
2308                         if (len + j > ireq->i_len)
2309                                 break;
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;
2320                                 break;
2321                         case NDIS_80211_NET_INFRA_BSS:
2322                                 sr->isr_capinfo |= IEEE80211_CAPINFO_ESS;
2323                                 break;
2324                         }
2325                         for (j = 0; j < sizeof(sr->isr_rates); j++) {
2326                                 /* XXX - check units */
2327                                 if (wb->nwbx_supportedrates[j] == 0)
2328                                         break;
2329                                 sr->isr_rates[j] =
2330                                     wb->nwbx_supportedrates[j] & 0x7f;
2331                         }
2332                         sr->isr_nrates = j;
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));
2344                         len += sr->isr_len;
2345                         sr = (struct ieee80211req_scan_result *)((char *)sr +
2346                             sr->isr_len);
2347                         wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2348                 }
2349                 ireq->i_len = len;
2350                 error = copyout(bsr, ireq->i_data, len);
2351                 free(bl, M_DEVBUF);
2352                 free(bsr, M_DEVBUF);
2353                 break;
2354         case IEEE80211_IOC_STATIONNAME:
2355                 error = ndis_get_info(sc, OID_GEN_MACHINE_NAME,
2356                     &nodename_u, &len);
2357                 if (error)
2358                         break;
2359                 acode = nodename;
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);
2364                 break;
2365         default:
2366                 error = ieee80211_ioctl(&sc->ic, command, data);
2367         }
2368         
2369         return(error);
2370 }
2371
2372 static int
2373 ndis_add_key(sc, wk, i_len)
2374         struct ndis_softc       *sc;
2375         struct ieee80211req_key *wk;
2376         int16_t                 i_len;
2377 {
2378         ndis_80211_key          *rkey;
2379         ndis_80211_wep          *wep;
2380         int                     len, error;
2381         uint32_t                arg;
2382
2383         /* infrastructure mode only supported for now */
2384         len = sizeof(arg);
2385         arg = NDIS_80211_NET_INFRA_BSS;
2386         error = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
2387         if (error) {
2388                 device_printf(sc->ndis_dev,
2389                     "setting infrastructure mode failed\n");
2390                 return(error);
2391         }
2392
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);
2397                 if(!wep)
2398                         return(ENOSPC);
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);
2406                 if(error) {
2407                         device_printf(sc->ndis_dev,
2408                             "copyin of wep key to kernel space failed\n");
2409                         free(wep, M_TEMP);
2410                         break;
2411                 }
2412                 error = ndis_set_info(sc, OID_802_11_ADD_WEP, wep, &len);
2413                 if(error) {
2414                         device_printf(sc->ndis_dev,
2415                             "setting wep key failed\n");
2416                         break;
2417                 }
2418                 free(wep, M_TEMP);
2419
2420                 /* set the authentication mode */
2421
2422                 arg = NDIS_80211_AUTHMODE_OPEN;
2423                 error = ndis_set_info(sc,
2424                     OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2425                 if(error) {
2426                         device_printf(sc->ndis_dev,
2427                             "setting authentication mode failed\n");
2428                 }
2429
2430                 /* set the encryption */
2431
2432                 len = sizeof(arg);
2433                 arg = NDIS_80211_WEPSTAT_ENABLED;
2434                 error = ndis_set_info(sc,
2435                     OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2436                 if(error) {
2437                         device_printf(sc->ndis_dev,
2438                             "setting encryption status failed\n");
2439                         return(error);
2440                 }
2441                 break;
2442         case IEEE80211_CIPHER_TKIP:
2443                 len = 12 + 6 + 6 + 8 + wk->ik_keylen;
2444                 rkey = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2445                 if(!rkey)
2446                         return(ENOSPC);
2447                 rkey->nk_len = len;
2448                 error = copyin(wk->ik_macaddr,
2449                     rkey->nk_bssid, IEEE80211_ADDR_LEN);
2450                 if(error) {
2451                         device_printf(sc->ndis_dev,
2452                             "copyin of bssid to kernel space failed\n");
2453                         free(rkey, M_TEMP);
2454                         break;
2455                 }
2456
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));
2460                 if(error) {
2461                         device_printf(sc->ndis_dev,
2462                             "copyin of keyrsc to kernel space failed\n");
2463                         free(rkey, M_TEMP);
2464                         break;
2465                 }
2466
2467                 /* key index - gets weird in NDIS */
2468
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 */
2477                 } else {
2478                         /* pairwise key */
2479                         rkey->nk_keyidx |= 1 << 30;
2480                 }
2481
2482                 /* need to set bit 29 based on keyrsc */
2483
2484                 rkey->nk_keylen = wk->ik_keylen;
2485                 if (wk->ik_type == IEEE80211_CIPHER_TKIP &&
2486                     wk->ik_keylen == 32) {
2487                         /*
2488                          * key data needs to be offset by 4 due
2489                          * to mismatch between NDIS spec and BSD??
2490                          */
2491                         error = copyin(wk->ik_keydata,
2492                             rkey->nk_keydata + 4, 16);
2493                         if(error) {
2494                                 device_printf(sc->ndis_dev, "copyin of "
2495                                     "keydata(0) to kernel space failed\n");
2496                                 free(rkey, M_TEMP);
2497                                 break;
2498                         }
2499                         error = copyin(wk->ik_keydata + 24,
2500                             rkey->nk_keydata + 20, 8);
2501                         if(error) {
2502                                 device_printf(sc->ndis_dev, "copyin of "
2503                                     "keydata(1) to kernel space failed\n");
2504                                 free(rkey, M_TEMP);
2505                                 break;
2506                         }
2507                         error = copyin(wk->ik_keydata + 16,
2508                             rkey->nk_keydata + 28, 8);
2509                         if(error) {
2510                                 device_printf(sc->ndis_dev, "copyin of "
2511                                     "keydata(2) to kernel space failed\n");
2512                                 free(rkey, M_TEMP);
2513                                 break;
2514                         }
2515                 } else {
2516                         error = copyin(wk->ik_keydata,
2517                             rkey->nk_keydata + 4, wk->ik_keylen);
2518                         if(error) {
2519                                 device_printf(sc->ndis_dev, "copyin of "
2520                                     "keydata(CCMP) to kernel space failed\n");
2521                                 free(rkey, M_TEMP);
2522                                 break;
2523                         }
2524                 }
2525                 error = ndis_set_info(sc, OID_802_11_ADD_KEY, rkey, &len);
2526                 break;
2527         case IEEE80211_CIPHER_AES_CCM:
2528                 return(ENOTTY);
2529         default:
2530                 return(ENOTTY);
2531         }
2532         return(error);
2533 }
2534
2535 static int
2536 ndis_80211_ioctl_set(struct ifnet *ifp, u_long command, caddr_t data)
2537 {
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];
2543         uint16_t                *ucode;
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;
2549
2550         sc = ifp->if_softc;
2551         ireq = (struct ieee80211req *) data;
2552                 
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:
2558                 error = 0;
2559                 break;
2560         case IEEE80211_IOC_PRIVACY:
2561                 len = sizeof(arg);
2562                 arg = NDIS_80211_PRIVFILT_8021XWEP;
2563                 error = ndis_set_info(sc,
2564                     OID_802_11_PRIVACY_FILTER, &arg, &len);
2565                 if (error) {
2566                         device_printf(sc->ndis_dev,
2567                             "setting wep privacy filter failed\n");
2568                         error = 0;
2569                 }
2570                 break;
2571         case IEEE80211_IOC_WPA:
2572                 /* nothing to do */
2573                 error = 0;
2574                 break;
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);
2582                         return(error);
2583                 }
2584                 if (wpa_ie[0] == IEEE80211_ELEMID_RSN) {
2585                         error = ENOTTY;
2586                         break;
2587                 } else if (wpa_ie[0] == IEEE80211_ELEMID_VENDOR) {
2588
2589                         /* set the encryption based on multicast cipher */
2590
2591                         if (!memcmp(wpa_ie + 8, "\x00\x50\xf2\x02", 4)) {
2592                                 len = sizeof(arg);
2593                                 arg = NDIS_80211_WEPSTAT_ENC2ENABLED;
2594                                 error = ndis_set_info(sc,
2595                                     OID_802_11_ENCRYPTION_STATUS, &arg, &len);
2596                                 if (error) {
2597                                         device_printf(sc->ndis_dev, "setting "
2598                                             "encryption status to "
2599                                             "ENC2 failed\n");
2600                                         /* continue anyway */
2601                                 }
2602                         }
2603                 }
2604
2605                 /* set the authentication mode */
2606
2607                 ucnt = wpa_ie[12] + 256* wpa_ie[13];
2608
2609                 /* 4 bytes per unicast cipher */
2610
2611                 ucnt = 14 + 4*ucnt + 2; /* account for number of authsels */
2612
2613                 if (ireq->i_len < ucnt) {
2614                         arg = NDIS_80211_AUTHMODE_WPANONE;
2615                 } else {
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;
2622                         } else {
2623                                 arg = NDIS_80211_AUTHMODE_WPANONE;
2624                         }
2625                 }
2626                 len = sizeof(arg);
2627                 error = ndis_set_info(sc,
2628                     OID_802_11_AUTHENTICATION_MODE, &arg, &len);
2629                 if (error) {
2630                         device_printf(sc->ndis_dev,
2631                             "setting authentication mode to WPA-PSK failed\n");
2632                         break;
2633                 }
2634                 break;
2635         case IEEE80211_IOC_SSID:
2636                 len = sizeof(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);
2640                 if (error)
2641                         break;
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);
2645                 if (error) {
2646                         device_printf(sc->ndis_dev,
2647                             "setting SSID to %s\n", ssid.ns_ssid);
2648                 }
2649                 break;
2650         case IEEE80211_IOC_DELKEY:
2651                 len = sizeof(rkey);
2652                 bzero((char*)&rkey, len);
2653                 rk = (struct ieee80211req_del_key*)ireq->i_data;
2654                 rkey.nk_len = len;
2655                 rkey.nk_keyidx = rk->idk_keyix;
2656                 error = copyin(rk->idk_macaddr,
2657                     &(rkey.nk_bssid), sizeof(ndis_80211_macaddr));
2658                 if (error)
2659                         break;
2660                 error = ndis_set_info(sc, OID_802_11_REMOVE_KEY, &rkey, &len);
2661                 if (error)
2662                         device_printf(sc->ndis_dev, "deleting key\n");
2663                 break;
2664         case IEEE80211_IOC_WPAKEY:
2665                 wk = (struct ieee80211req_key*)ireq->i_data;
2666                 error = ndis_add_key(sc, wk, ireq->i_len);
2667                 break;
2668         case IEEE80211_IOC_SCAN_REQ:
2669                 len = 0;
2670                 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2671                     NULL, &len);
2672                 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2673                 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
2674                 break;
2675         case IEEE80211_IOC_STATIONNAME:
2676                 error = suser(curthread);
2677                 if (error)
2678                         break;
2679                 if (ireq->i_val != 0 ||
2680                     ireq->i_len > IEEE80211_NWID_LEN) {
2681                         error = EINVAL;
2682                         break;
2683                 }
2684                 bzero((char *)nodename, IEEE80211_NWID_LEN);
2685                 error = copyin(ireq->i_data, nodename, ireq->i_len);
2686                 if (error)
2687                         break;
2688                 ucode = nodename_u;
2689                 ndis_ascii_to_unicode((char *)nodename, &ucode);
2690                 len = ireq->i_len * 2;
2691                 error = ndis_set_info(sc, OID_GEN_MACHINE_NAME,
2692                     &nodename_u, &len);
2693                 break;
2694         default:
2695                 error = ieee80211_ioctl(&sc->ic, command, data);
2696                 if (error == ENETRESET) {
2697                         ndis_setstate_80211(sc);
2698                         error = 0;
2699                 }
2700         }
2701         
2702         return(error);
2703 }
2704
2705 static void
2706 ndis_watchdog(ifp)
2707         struct ifnet            *ifp;
2708 {
2709         struct ndis_softc               *sc;
2710
2711         sc = ifp->if_softc;
2712
2713         NDIS_LOCK(sc);
2714         ifp->if_oerrors++;
2715         device_printf(sc->ndis_dev, "watchdog timeout\n");
2716         NDIS_UNLOCK(sc);
2717
2718         ndis_sched((void(*)(void *))ndis_reset_nic, sc, NDIS_TASKQUEUE);
2719         ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2720
2721         return;
2722 }
2723
2724 /*
2725  * Stop the adapter and free any mbufs allocated to the
2726  * RX and TX lists.
2727  */
2728 static void
2729 ndis_stop(sc)
2730         struct ndis_softc               *sc;
2731 {
2732         struct ifnet            *ifp;
2733
2734         ifp = &sc->arpcom.ac_if;
2735         untimeout(ndis_tick, sc, sc->ndis_stat_ch);
2736
2737         ndis_halt_nic(sc);
2738
2739         NDIS_LOCK(sc);
2740         ifp->if_timer = 0;
2741         sc->ndis_link = 0;
2742         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2743         NDIS_UNLOCK(sc);
2744
2745         return;
2746 }
2747
2748 /*
2749  * Stop all chip I/O so that the kernel's probe routines don't
2750  * get confused by errant DMAs when rebooting.
2751  */
2752 void
2753 ndis_shutdown(dev)
2754         device_t                dev;
2755 {
2756         struct ndis_softc               *sc;
2757
2758         sc = device_get_softc(dev);
2759         ndis_shutdown_nic(sc);
2760
2761         return;
2762 }