1 /* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.166 2004/04/01 00:38:45 sam Exp $ 35 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.24 2005/06/06 23:13:26 okumoto Exp $ 36 */ 37 38 /* 39 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver. 40 * 41 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 42 * Electrical Engineering Department 43 * Columbia University, New York City 44 */ 45 46 /* 47 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 48 * from Lucent. Unlike the older cards, the new ones are programmed 49 * entirely via a firmware-driven controller called the Hermes. 50 * Unfortunately, Lucent will not release the Hermes programming manual 51 * without an NDA (if at all). What they do release is an API library 52 * called the HCF (Hardware Control Functions) which is supposed to 53 * do the device-specific operations of a device driver for you. The 54 * publically available version of the HCF library (the 'HCF Light') is 55 * a) extremely gross, b) lacks certain features, particularly support 56 * for 802.11 frames, and c) is contaminated by the GNU Public License. 57 * 58 * This driver does not use the HCF or HCF Light at all. Instead, it 59 * programs the Hermes controller directly, using information gleaned 60 * from the HCF Light code and corresponding documentation. 61 * 62 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent 63 * WaveLan cards (based on the Hermes chipset), as well as the newer 64 * Prism 2 chipsets with firmware from Intersil and Symbol. 65 */ 66 67 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 68 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 69 70 #include <sys/param.h> 71 #include <sys/endian.h> 72 #include <sys/systm.h> 73 #include <sys/sockio.h> 74 #include <sys/mbuf.h> 75 #include <sys/proc.h> 76 #include <sys/kernel.h> 77 #include <sys/socket.h> 78 #include <sys/module.h> 79 #include <sys/bus.h> 80 #include <sys/random.h> 81 #include <sys/syslog.h> 82 #include <sys/sysctl.h> 83 #include <sys/thread2.h> 84 85 #include <machine/bus.h> 86 #include <machine/resource.h> 87 #include <machine/clock.h> 88 #include <machine/atomic.h> 89 #include <sys/rman.h> 90 91 #include <net/if.h> 92 #include <net/if_arp.h> 93 #include <net/ethernet.h> 94 #include <net/if_dl.h> 95 #include <net/if_media.h> 96 #include <net/if_types.h> 97 #include <net/ifq_var.h> 98 99 #include <netproto/802_11/ieee80211_var.h> 100 #include <netproto/802_11/ieee80211_ioctl.h> 101 #include <netproto/802_11/ieee80211_radiotap.h> 102 #include <netproto/802_11/if_wavelan_ieee.h> 103 104 #include <netinet/in.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/in_var.h> 107 #include <netinet/ip.h> 108 #include <netinet/if_ether.h> 109 110 #include <net/bpf.h> 111 112 #include <dev/netif/wi/if_wireg.h> 113 #include <dev/netif/wi/if_wivar.h> 114 115 static void wi_start(struct ifnet *); 116 static int wi_reset(struct wi_softc *); 117 static void wi_watchdog(struct ifnet *); 118 static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 119 static int wi_media_change(struct ifnet *); 120 static void wi_media_status(struct ifnet *, struct ifmediareq *); 121 122 static void wi_rx_intr(struct wi_softc *); 123 static void wi_tx_intr(struct wi_softc *); 124 static void wi_tx_ex_intr(struct wi_softc *); 125 static void wi_info_intr(struct wi_softc *); 126 127 static int wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *); 128 static int wi_set_cfg(struct ifnet *, u_long, caddr_t); 129 static int wi_write_txrate(struct wi_softc *); 130 static int wi_write_wep(struct wi_softc *); 131 static int wi_write_multi(struct wi_softc *); 132 static int wi_alloc_fid(struct wi_softc *, int, int *); 133 static void wi_read_nicid(struct wi_softc *); 134 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 135 136 static int wi_cmd(struct wi_softc *, int, int, int, int); 137 static int wi_seek_bap(struct wi_softc *, int, int); 138 static int wi_read_bap(struct wi_softc *, int, int, void *, int); 139 static int wi_write_bap(struct wi_softc *, int, int, void *, int); 140 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 141 static int wi_read_rid(struct wi_softc *, int, void *, int *); 142 static int wi_write_rid(struct wi_softc *, int, void *, int); 143 144 static int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 145 146 static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 147 static void wi_scan_result(struct wi_softc *, int, int); 148 149 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 150 151 static int wi_get_debug(struct wi_softc *, struct wi_req *); 152 static int wi_set_debug(struct wi_softc *, struct wi_req *); 153 154 /* support to download firmware for symbol CF card */ 155 static int wi_symbol_write_firm(struct wi_softc *, const void *, int, 156 const void *, int); 157 static int wi_symbol_set_hcr(struct wi_softc *, int); 158 #ifdef DEVICE_POLLING 159 static void wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count); 160 #endif 161 162 static __inline int 163 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 164 { 165 166 val = htole16(val); 167 return wi_write_rid(sc, rid, &val, sizeof(val)); 168 } 169 170 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 171 172 static struct timeval lasttxerror; /* time of last tx error msg */ 173 static int curtxeps; /* current tx error msgs/sec */ 174 static int wi_txerate = 0; /* tx error rate: max msgs/sec */ 175 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 176 0, "max tx error msgs/sec; 0 to disable msgs"); 177 178 #define WI_DEBUG 179 #ifdef WI_DEBUG 180 static int wi_debug = 0; 181 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 182 0, "control debugging printfs"); 183 184 #define DPRINTF(X) if (wi_debug) printf X 185 #define DPRINTF2(X) if (wi_debug > 1) printf X 186 #define IFF_DUMPPKTS(_ifp) \ 187 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 188 #else 189 #define DPRINTF(X) 190 #define DPRINTF2(X) 191 #define IFF_DUMPPKTS(_ifp) 0 192 #endif 193 194 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 195 196 struct wi_card_ident wi_card_ident[] = { 197 /* CARD_ID CARD_NAME FIRM_TYPE */ 198 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 199 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 200 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 201 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 202 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 203 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 204 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 205 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 206 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 207 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 208 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 209 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 210 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 211 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 212 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 213 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 214 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 215 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 216 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 217 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 218 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 219 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 220 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 221 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 222 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 223 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 224 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 225 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 226 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 227 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 228 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 229 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 230 { 0, NULL, 0 }, 231 }; 232 233 devclass_t wi_devclass; 234 235 int 236 wi_attach(device_t dev) 237 { 238 struct wi_softc *sc = device_get_softc(dev); 239 struct ieee80211com *ic = &sc->sc_ic; 240 struct ifnet *ifp = &ic->ic_if; 241 int i, nrates, buflen; 242 u_int16_t val; 243 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 244 struct ieee80211_rateset *rs; 245 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 247 }; 248 int error; 249 250 /* 251 * NB: no locking is needed here; don't put it here 252 * unless you can prove it! 253 */ 254 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 255 wi_intr, sc, &sc->wi_intrhand, NULL); 256 257 if (error) { 258 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 259 wi_free(dev); 260 return (error); 261 } 262 263 sc->wi_cmd_count = 500; 264 /* Reset the NIC. */ 265 if (wi_reset(sc) != 0) 266 return ENXIO; /* XXX */ 267 268 /* 269 * Read the station address. 270 * And do it twice. I've seen PRISM-based cards that return 271 * an error when trying to read it the first time, which causes 272 * the probe to fail. 273 */ 274 buflen = IEEE80211_ADDR_LEN; 275 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 276 if (error != 0) { 277 buflen = IEEE80211_ADDR_LEN; 278 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 279 } 280 if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 281 if (error != 0) 282 device_printf(dev, "mac read failed %d\n", error); 283 else 284 device_printf(dev, "mac read failed (all zeros)\n"); 285 wi_free(dev); 286 return (error); 287 } 288 289 /* Read NIC identification */ 290 wi_read_nicid(sc); 291 292 ifp->if_softc = sc; 293 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 294 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 295 ifp->if_ioctl = wi_ioctl; 296 ifp->if_start = wi_start; 297 ifp->if_watchdog = wi_watchdog; 298 ifp->if_init = wi_init; 299 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN); 300 ifq_set_ready(&ifp->if_snd); 301 #ifdef DEVICE_POLLING 302 ifp->if_poll = wi_poll; 303 #endif 304 ifp->if_capenable = ifp->if_capabilities; 305 306 ic->ic_phytype = IEEE80211_T_DS; 307 ic->ic_opmode = IEEE80211_M_STA; 308 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO; 309 ic->ic_state = IEEE80211_S_INIT; 310 311 /* 312 * Query the card for available channels and setup the 313 * channel table. We assume these are all 11b channels. 314 */ 315 buflen = sizeof(val); 316 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 317 val = htole16(0x1fff); /* assume 1-11 */ 318 KASSERT(val != 0, ("wi_attach: no available channels listed!")); 319 320 val <<= 1; /* shift for base 1 indices */ 321 for (i = 1; i < 16; i++) { 322 if (isset((u_int8_t*)&val, i)) { 323 ic->ic_channels[i].ic_freq = 324 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 325 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B; 326 } 327 } 328 329 /* 330 * Read the default channel from the NIC. This may vary 331 * depending on the country where the NIC was purchased, so 332 * we can't hard-code a default and expect it to work for 333 * everyone. 334 * 335 * If no channel is specified, let the 802.11 code select. 336 */ 337 buflen = sizeof(val); 338 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) { 339 val = le16toh(val); 340 KASSERT(val < IEEE80211_CHAN_MAX && 341 ic->ic_channels[val].ic_flags != 0, 342 ("wi_attach: invalid own channel %u!", val)); 343 ic->ic_ibss_chan = &ic->ic_channels[val]; 344 } else { 345 device_printf(dev, 346 "WI_RID_OWN_CHNL failed, using first channel!\n"); 347 ic->ic_ibss_chan = &ic->ic_channels[0]; 348 } 349 350 /* 351 * Set flags based on firmware version. 352 */ 353 switch (sc->sc_firmware_type) { 354 case WI_LUCENT: 355 sc->sc_ntxbuf = 1; 356 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 357 #ifdef WI_HERMES_AUTOINC_WAR 358 /* XXX: not confirmed, but never seen for recent firmware */ 359 if (sc->sc_sta_firmware_ver < 40000) { 360 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 361 } 362 #endif 363 if (sc->sc_sta_firmware_ver >= 60000) 364 sc->sc_flags |= WI_FLAGS_HAS_MOR; 365 if (sc->sc_sta_firmware_ver >= 60006) { 366 ic->ic_caps |= IEEE80211_C_IBSS; 367 ic->ic_caps |= IEEE80211_C_MONITOR; 368 } 369 sc->sc_ibss_port = htole16(1); 370 371 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 372 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 373 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 374 break; 375 376 case WI_INTERSIL: 377 sc->sc_ntxbuf = WI_NTXBUF; 378 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 379 sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 380 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 381 /* 382 * Old firmware are slow, so give peace a chance. 383 */ 384 if (sc->sc_sta_firmware_ver < 10000) 385 sc->wi_cmd_count = 5000; 386 if (sc->sc_sta_firmware_ver > 10101) 387 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 388 if (sc->sc_sta_firmware_ver >= 800) { 389 ic->ic_caps |= IEEE80211_C_IBSS; 390 ic->ic_caps |= IEEE80211_C_MONITOR; 391 } 392 /* 393 * version 0.8.3 and newer are the only ones that are known 394 * to currently work. Earlier versions can be made to work, 395 * at least according to the Linux driver. 396 */ 397 if (sc->sc_sta_firmware_ver >= 803) 398 ic->ic_caps |= IEEE80211_C_HOSTAP; 399 sc->sc_ibss_port = htole16(0); 400 401 sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 402 sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 403 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 404 break; 405 406 case WI_SYMBOL: 407 sc->sc_ntxbuf = 1; 408 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 409 if (sc->sc_sta_firmware_ver >= 25000) 410 ic->ic_caps |= IEEE80211_C_IBSS; 411 sc->sc_ibss_port = htole16(4); 412 413 sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 414 sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 415 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 416 break; 417 } 418 419 /* 420 * Find out if we support WEP on this card. 421 */ 422 buflen = sizeof(val); 423 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 424 val != htole16(0)) 425 ic->ic_caps |= IEEE80211_C_WEP; 426 427 /* Find supported rates. */ 428 buflen = sizeof(ratebuf); 429 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 430 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 431 nrates = le16toh(*(u_int16_t *)ratebuf); 432 if (nrates > IEEE80211_RATE_MAXSIZE) 433 nrates = IEEE80211_RATE_MAXSIZE; 434 rs->rs_nrates = 0; 435 for (i = 0; i < nrates; i++) 436 if (ratebuf[2+i]) 437 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 438 } else { 439 /* XXX fallback on error? */ 440 rs->rs_nrates = 0; 441 } 442 443 buflen = sizeof(val); 444 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 445 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 446 sc->sc_dbm_offset = le16toh(val); 447 } 448 449 sc->sc_max_datalen = 2304; 450 sc->sc_system_scale = 1; 451 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 452 sc->sc_roaming_mode = 1; 453 454 sc->sc_portnum = WI_DEFAULT_PORT; 455 sc->sc_authtype = WI_DEFAULT_AUTHTYPE; 456 457 bzero(sc->sc_nodename, sizeof(sc->sc_nodename)); 458 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1; 459 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen); 460 461 bzero(sc->sc_net_name, sizeof(sc->sc_net_name)); 462 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name, 463 sizeof(WI_DEFAULT_NETNAME) - 1); 464 465 /* 466 * Call MI attach routine. 467 */ 468 ieee80211_ifattach(ifp); 469 /* override state transition method */ 470 sc->sc_newstate = ic->ic_newstate; 471 ic->ic_newstate = wi_newstate; 472 ieee80211_media_init(ifp, wi_media_change, wi_media_status); 473 474 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO, 475 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 476 &sc->sc_drvbpf); 477 /* 478 * Initialize constant fields. 479 * XXX make header lengths a multiple of 32-bits so subsequent 480 * headers are properly aligned; this is a kludge to keep 481 * certain applications happy. 482 * 483 * NB: the channel is setup each time we transition to the 484 * RUN state to avoid filling it in for each frame. 485 */ 486 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 487 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 488 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 489 490 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 491 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 492 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 493 494 return (0); 495 } 496 497 int 498 wi_detach(device_t dev) 499 { 500 struct wi_softc *sc = device_get_softc(dev); 501 struct ifnet *ifp = &sc->sc_ic.ic_if; 502 WI_LOCK_DECL(); 503 504 WI_LOCK(sc); 505 506 /* check if device was removed */ 507 sc->wi_gone |= !bus_child_present(dev); 508 509 wi_stop(ifp, 0); 510 511 ieee80211_ifdetach(ifp); 512 WI_UNLOCK(sc); 513 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 514 wi_free(dev); 515 return (0); 516 } 517 518 void 519 wi_shutdown(device_t dev) 520 { 521 struct wi_softc *sc = device_get_softc(dev); 522 523 wi_stop(&sc->sc_if, 1); 524 } 525 526 #ifdef DEVICE_POLLING 527 528 static void 529 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 530 { 531 struct wi_softc *sc = ifp->if_softc; 532 uint16_t status; 533 534 switch(cmd) { 535 case POLL_REGISTER: 536 /* disable interruptds */ 537 CSR_WRITE_2(sc, WI_INT_EN, 0); 538 break; 539 case POLL_DEREGISTER: 540 /* enable interrupts */ 541 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 542 break; 543 default: 544 status = CSR_READ_2(sc, WI_EVENT_STAT); 545 546 if (status & WI_EV_RX) 547 wi_rx_intr(sc); 548 if (status & WI_EV_ALLOC) 549 wi_tx_intr(sc); 550 if (status & WI_EV_INFO) 551 wi_info_intr(sc); 552 553 if (cmd == POLL_AND_CHECK_STATUS) { 554 if (status & WI_EV_INFO) 555 wi_info_intr(sc); 556 } 557 558 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 559 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && !ifq_is_empty(&ifp->if_snd)) { 560 wi_start(ifp); 561 } 562 break; 563 } 564 } 565 #endif /* DEVICE_POLLING */ 566 567 void 568 wi_intr(void *arg) 569 { 570 struct wi_softc *sc = arg; 571 struct ifnet *ifp = &sc->sc_ic.ic_if; 572 u_int16_t status; 573 WI_LOCK_DECL(); 574 575 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 576 CSR_WRITE_2(sc, WI_INT_EN, 0); 577 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 578 return; 579 } 580 581 WI_LOCK(sc); 582 583 /* Disable interrupts. */ 584 CSR_WRITE_2(sc, WI_INT_EN, 0); 585 586 status = CSR_READ_2(sc, WI_EVENT_STAT); 587 if (status & WI_EV_RX) 588 wi_rx_intr(sc); 589 if (status & WI_EV_ALLOC) 590 wi_tx_intr(sc); 591 if (status & WI_EV_TX_EXC) 592 wi_tx_ex_intr(sc); 593 if (status & WI_EV_INFO) 594 wi_info_intr(sc); 595 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 596 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 597 !ifq_is_empty(&ifp->if_snd)) 598 wi_start(ifp); 599 600 /* Re-enable interrupts. */ 601 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 602 603 WI_UNLOCK(sc); 604 605 return; 606 } 607 608 void 609 wi_init(void *arg) 610 { 611 struct wi_softc *sc = arg; 612 struct ifnet *ifp = &sc->sc_if; 613 struct ieee80211com *ic = &sc->sc_ic; 614 struct wi_joinreq join; 615 int i; 616 int error = 0, wasenabled; 617 WI_LOCK_DECL(); 618 619 WI_LOCK(sc); 620 621 if (sc->wi_gone) { 622 WI_UNLOCK(sc); 623 return; 624 } 625 626 if ((wasenabled = sc->sc_enabled)) 627 wi_stop(ifp, 1); 628 wi_reset(sc); 629 630 /* common 802.11 configuration */ 631 ic->ic_flags &= ~IEEE80211_F_IBSSON; 632 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 633 switch (ic->ic_opmode) { 634 case IEEE80211_M_STA: 635 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 636 break; 637 case IEEE80211_M_IBSS: 638 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 639 ic->ic_flags |= IEEE80211_F_IBSSON; 640 break; 641 case IEEE80211_M_AHDEMO: 642 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 643 break; 644 case IEEE80211_M_HOSTAP: 645 /* 646 * For PRISM cards, override the empty SSID, because in 647 * HostAP mode the controller will lock up otherwise. 648 */ 649 if (sc->sc_firmware_type == WI_INTERSIL && 650 ic->ic_des_esslen == 0) { 651 ic->ic_des_essid[0] = ' '; 652 ic->ic_des_esslen = 1; 653 } 654 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 655 break; 656 case IEEE80211_M_MONITOR: 657 if (sc->sc_firmware_type == WI_LUCENT) 658 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 659 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0); 660 break; 661 } 662 663 /* Intersil interprets this RID as joining ESS even in IBSS mode */ 664 if (sc->sc_firmware_type == WI_LUCENT && 665 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 666 wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 667 else 668 wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 669 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 670 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 671 ic->ic_des_esslen); 672 wi_write_val(sc, WI_RID_OWN_CHNL, 673 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 674 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 675 676 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 677 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 678 679 wi_write_val(sc, WI_RID_PM_ENABLED, 680 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 681 682 /* not yet common 802.11 configuration */ 683 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 684 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold); 685 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 686 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold); 687 688 /* driver specific 802.11 configuration */ 689 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 690 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 691 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 692 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 693 if (sc->sc_flags & WI_FLAGS_HAS_MOR) 694 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 695 wi_write_txrate(sc); 696 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 697 698 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 699 sc->sc_firmware_type == WI_INTERSIL) { 700 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 701 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 702 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 703 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 704 } 705 706 /* 707 * Initialize promisc mode. 708 * Being in the Host-AP mode causes a great 709 * deal of pain if primisc mode is set. 710 * Therefore we avoid confusing the firmware 711 * and always reset promisc mode in Host-AP 712 * mode. Host-AP sees all the packets anyway. 713 */ 714 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 715 (ifp->if_flags & IFF_PROMISC) != 0) { 716 wi_write_val(sc, WI_RID_PROMISC, 1); 717 } else { 718 wi_write_val(sc, WI_RID_PROMISC, 0); 719 } 720 721 /* Configure WEP. */ 722 if (ic->ic_caps & IEEE80211_C_WEP) 723 wi_write_wep(sc); 724 725 /* Set multicast filter. */ 726 wi_write_multi(sc); 727 728 /* Allocate fids for the card */ 729 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 730 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 731 if (sc->sc_firmware_type == WI_SYMBOL) 732 sc->sc_buflen = 1585; /* XXX */ 733 for (i = 0; i < sc->sc_ntxbuf; i++) { 734 error = wi_alloc_fid(sc, sc->sc_buflen, 735 &sc->sc_txd[i].d_fid); 736 if (error) { 737 device_printf(sc->sc_dev, 738 "tx buffer allocation failed (error %u)\n", 739 error); 740 goto out; 741 } 742 sc->sc_txd[i].d_len = 0; 743 } 744 } 745 sc->sc_txcur = sc->sc_txnext = 0; 746 747 /* Enable desired port */ 748 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 749 750 sc->sc_enabled = 1; 751 ifp->if_flags |= IFF_RUNNING; 752 ifp->if_flags &= ~IFF_OACTIVE; 753 if (ic->ic_opmode == IEEE80211_M_AHDEMO || 754 ic->ic_opmode == IEEE80211_M_MONITOR || 755 ic->ic_opmode == IEEE80211_M_HOSTAP) 756 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 757 758 /* Enable interrupts if not polling */ 759 #ifdef DEVICE_POLLING 760 if ((ifp->if_flags & IFF_POLLING) == 0) 761 #endif 762 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 763 764 if (!wasenabled && 765 ic->ic_opmode == IEEE80211_M_HOSTAP && 766 sc->sc_firmware_type == WI_INTERSIL) { 767 /* XXX: some card need to be re-enabled for hostap */ 768 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 769 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 770 } 771 772 if (ic->ic_opmode == IEEE80211_M_STA && 773 ((ic->ic_flags & IEEE80211_F_DESBSSID) || 774 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 775 memset(&join, 0, sizeof(join)); 776 if (ic->ic_flags & IEEE80211_F_DESBSSID) 777 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 778 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 779 join.wi_chan = htole16( 780 ieee80211_chan2ieee(ic, ic->ic_des_chan)); 781 /* Lucent firmware does not support the JOIN RID. */ 782 if (sc->sc_firmware_type != WI_LUCENT) 783 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 784 } 785 786 WI_UNLOCK(sc); 787 return; 788 out: 789 if (error) { 790 if_printf(ifp, "interface not running\n"); 791 wi_stop(ifp, 1); 792 } 793 WI_UNLOCK(sc); 794 DPRINTF(("wi_init: return %d\n", error)); 795 return; 796 } 797 798 void 799 wi_stop(struct ifnet *ifp, int disable) 800 { 801 struct ieee80211com *ic = (struct ieee80211com *) ifp; 802 struct wi_softc *sc = ifp->if_softc; 803 WI_LOCK_DECL(); 804 805 WI_LOCK(sc); 806 807 DELAY(100000); 808 809 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 810 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 811 if (sc->sc_enabled && !sc->wi_gone) { 812 CSR_WRITE_2(sc, WI_INT_EN, 0); 813 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 814 if (disable) { 815 #ifdef __NetBSD__ 816 if (sc->sc_disable) 817 (*sc->sc_disable)(sc); 818 #endif 819 sc->sc_enabled = 0; 820 } 821 } else if (sc->wi_gone && disable) /* gone --> not enabled */ 822 sc->sc_enabled = 0; 823 824 sc->sc_tx_timer = 0; 825 sc->sc_scan_timer = 0; 826 sc->sc_syn_timer = 0; 827 sc->sc_false_syns = 0; 828 sc->sc_naps = 0; 829 ifp->if_timer = 0; 830 831 WI_UNLOCK(sc); 832 } 833 834 static void 835 wi_start(struct ifnet *ifp) 836 { 837 struct wi_softc *sc = ifp->if_softc; 838 struct ieee80211com *ic = &sc->sc_ic; 839 struct ieee80211_node *ni; 840 struct ieee80211_frame *wh; 841 struct mbuf *m0; 842 struct wi_frame frmhdr; 843 int cur, fid, off, error; 844 WI_LOCK_DECL(); 845 846 WI_LOCK(sc); 847 848 if (sc->wi_gone) { 849 WI_UNLOCK(sc); 850 return; 851 } 852 if (sc->sc_flags & WI_FLAGS_OUTRANGE) { 853 WI_UNLOCK(sc); 854 return; 855 } 856 857 memset(&frmhdr, 0, sizeof(frmhdr)); 858 cur = sc->sc_txnext; 859 for (;;) { 860 IF_POLL(&ic->ic_mgtq, m0); 861 if (m0 != NULL) { 862 if (sc->sc_txd[cur].d_len != 0) { 863 ifp->if_flags |= IFF_OACTIVE; 864 break; 865 } 866 IF_DEQUEUE(&ic->ic_mgtq, m0); 867 /* 868 * Hack! The referenced node pointer is in the 869 * rcvif field of the packet header. This is 870 * placed there by ieee80211_mgmt_output because 871 * we need to hold the reference with the frame 872 * and there's no other way (other than packet 873 * tags which we consider too expensive to use) 874 * to pass it along. 875 */ 876 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 877 m0->m_pkthdr.rcvif = NULL; 878 879 m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 880 (caddr_t)&frmhdr.wi_ehdr); 881 frmhdr.wi_ehdr.ether_type = 0; 882 wh = mtod(m0, struct ieee80211_frame *); 883 } else { 884 if (ic->ic_state != IEEE80211_S_RUN) 885 break; 886 m0 = ifq_poll(&ifp->if_snd); 887 if (m0 == NULL) 888 break; 889 if (sc->sc_txd[cur].d_len != 0) { 890 ifp->if_flags |= IFF_OACTIVE; 891 break; 892 } 893 m0 = ifq_dequeue(&ifp->if_snd); 894 ifp->if_opackets++; 895 m_copydata(m0, 0, ETHER_HDR_LEN, 896 (caddr_t)&frmhdr.wi_ehdr); 897 BPF_MTAP(ifp, m0); 898 899 m0 = ieee80211_encap(ifp, m0, &ni); 900 if (m0 == NULL) { 901 ifp->if_oerrors++; 902 continue; 903 } 904 wh = mtod(m0, struct ieee80211_frame *); 905 if (ic->ic_flags & IEEE80211_F_WEPON) 906 wh->i_fc[1] |= IEEE80211_FC1_WEP; 907 908 } 909 910 if (ic->ic_rawbpf != NULL) 911 bpf_mtap(ic->ic_rawbpf, m0); 912 913 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 914 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 915 (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 916 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) { 917 ifp->if_oerrors++; 918 if (ni && ni != ic->ic_bss) 919 ieee80211_free_node(ic, ni); 920 continue; 921 } 922 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 923 } 924 925 if (sc->sc_drvbpf) { 926 sc->sc_tx_th.wt_rate = 927 ni->ni_rates.rs_rates[ni->ni_txrate]; 928 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th, 929 sc->sc_tx_th_len); 930 } 931 932 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 933 (caddr_t)&frmhdr.wi_whdr); 934 m_adj(m0, sizeof(struct ieee80211_frame)); 935 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 936 if (IFF_DUMPPKTS(ifp)) 937 wi_dump_pkt(&frmhdr, NULL, -1); 938 fid = sc->sc_txd[cur].d_fid; 939 off = sizeof(frmhdr); 940 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 941 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 942 m_freem(m0); 943 if (ni && ni != ic->ic_bss) 944 ieee80211_free_node(ic, ni); 945 if (error) { 946 ifp->if_oerrors++; 947 continue; 948 } 949 sc->sc_txd[cur].d_len = off; 950 if (sc->sc_txcur == cur) { 951 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 952 if_printf(ifp, "xmit failed\n"); 953 sc->sc_txd[cur].d_len = 0; 954 continue; 955 } 956 sc->sc_tx_timer = 5; 957 ifp->if_timer = 1; 958 } 959 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 960 } 961 962 WI_UNLOCK(sc); 963 } 964 965 static int 966 wi_reset(struct wi_softc *sc) 967 { 968 struct ieee80211com *ic = &sc->sc_ic; 969 struct ifnet *ifp = &ic->ic_if; 970 #define WI_INIT_TRIES 3 971 int i; 972 int error = 0; 973 int tries; 974 975 /* Symbol firmware cannot be initialized more than once */ 976 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset) 977 return (0); 978 if (sc->sc_firmware_type == WI_SYMBOL) 979 tries = 1; 980 else 981 tries = WI_INIT_TRIES; 982 983 for (i = 0; i < tries; i++) { 984 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 985 break; 986 DELAY(WI_DELAY * 1000); 987 } 988 sc->sc_reset = 1; 989 990 if (i == tries) { 991 if_printf(ifp, "init failed\n"); 992 return (error); 993 } 994 995 CSR_WRITE_2(sc, WI_INT_EN, 0); 996 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 997 998 /* Calibrate timer. */ 999 wi_write_val(sc, WI_RID_TICK_TIME, 8); 1000 1001 return (0); 1002 #undef WI_INIT_TRIES 1003 } 1004 1005 static void 1006 wi_watchdog(struct ifnet *ifp) 1007 { 1008 struct wi_softc *sc = ifp->if_softc; 1009 1010 ifp->if_timer = 0; 1011 if (!sc->sc_enabled) 1012 return; 1013 1014 if (sc->sc_tx_timer) { 1015 if (--sc->sc_tx_timer == 0) { 1016 if_printf(ifp, "device timeout\n"); 1017 ifp->if_oerrors++; 1018 wi_init(ifp->if_softc); 1019 return; 1020 } 1021 ifp->if_timer = 1; 1022 } 1023 1024 if (sc->sc_scan_timer) { 1025 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1026 sc->sc_firmware_type == WI_INTERSIL) { 1027 DPRINTF(("wi_watchdog: inquire scan\n")); 1028 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 1029 } 1030 if (sc->sc_scan_timer) 1031 ifp->if_timer = 1; 1032 } 1033 1034 if (sc->sc_syn_timer) { 1035 if (--sc->sc_syn_timer == 0) { 1036 struct ieee80211com *ic = (struct ieee80211com *) ifp; 1037 DPRINTF2(("wi_watchdog: %d false syns\n", 1038 sc->sc_false_syns)); 1039 sc->sc_false_syns = 0; 1040 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1041 sc->sc_syn_timer = 5; 1042 } 1043 ifp->if_timer = 1; 1044 } 1045 1046 /* TODO: rate control */ 1047 ieee80211_watchdog(ifp); 1048 } 1049 1050 static int 1051 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 1052 { 1053 struct wi_softc *sc = ifp->if_softc; 1054 struct ieee80211com *ic = &sc->sc_ic; 1055 struct ifreq *ifr = (struct ifreq *)data; 1056 struct ieee80211req *ireq; 1057 u_int8_t nodename[IEEE80211_NWID_LEN]; 1058 int error = 0; 1059 struct wi_req wreq; 1060 WI_LOCK_DECL(); 1061 1062 WI_LOCK(sc); 1063 1064 if (sc->wi_gone) { 1065 error = ENODEV; 1066 goto out; 1067 } 1068 1069 switch (cmd) { 1070 case SIOCSIFFLAGS: 1071 /* 1072 * Can't do promisc and hostap at the same time. If all that's 1073 * changing is the promisc flag, try to short-circuit a call to 1074 * wi_init() by just setting PROMISC in the hardware. 1075 */ 1076 if (ifp->if_flags & IFF_UP) { 1077 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1078 ifp->if_flags & IFF_RUNNING) { 1079 if (ifp->if_flags & IFF_PROMISC && 1080 !(sc->sc_if_flags & IFF_PROMISC)) { 1081 wi_write_val(sc, WI_RID_PROMISC, 1); 1082 } else if (!(ifp->if_flags & IFF_PROMISC) && 1083 sc->sc_if_flags & IFF_PROMISC) { 1084 wi_write_val(sc, WI_RID_PROMISC, 0); 1085 } else { 1086 wi_init(sc); 1087 } 1088 } else { 1089 wi_init(sc); 1090 } 1091 } else { 1092 if (ifp->if_flags & IFF_RUNNING) { 1093 wi_stop(ifp, 1); 1094 } 1095 sc->wi_gone = 0; 1096 } 1097 sc->sc_if_flags = ifp->if_flags; 1098 error = 0; 1099 break; 1100 case SIOCADDMULTI: 1101 case SIOCDELMULTI: 1102 error = wi_write_multi(sc); 1103 break; 1104 case SIOCGIFGENERIC: 1105 error = wi_get_cfg(ifp, cmd, data, cr); 1106 break; 1107 case SIOCSIFGENERIC: 1108 error = suser_cred(cr, NULL_CRED_OKAY); 1109 if (error) 1110 break; 1111 error = wi_set_cfg(ifp, cmd, data); 1112 break; 1113 case SIOCGPRISM2DEBUG: 1114 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1115 if (error) 1116 break; 1117 if (!(ifp->if_flags & IFF_RUNNING) || 1118 sc->sc_firmware_type == WI_LUCENT) { 1119 error = EIO; 1120 break; 1121 } 1122 error = wi_get_debug(sc, &wreq); 1123 if (error == 0) 1124 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 1125 break; 1126 case SIOCSPRISM2DEBUG: 1127 if ((error = suser_cred(cr, NULL_CRED_OKAY))) 1128 goto out; 1129 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1130 if (error) 1131 break; 1132 error = wi_set_debug(sc, &wreq); 1133 break; 1134 case SIOCG80211: 1135 ireq = (struct ieee80211req *) data; 1136 switch (ireq->i_type) { 1137 case IEEE80211_IOC_STATIONNAME: 1138 ireq->i_len = sc->sc_nodelen + 1; 1139 error = copyout(sc->sc_nodename, ireq->i_data, 1140 ireq->i_len); 1141 break; 1142 default: 1143 error = ieee80211_ioctl(ifp, cmd, data, cr); 1144 break; 1145 } 1146 break; 1147 case SIOCS80211: 1148 error = suser_cred(cr, NULL_CRED_OKAY); 1149 if (error) 1150 break; 1151 ireq = (struct ieee80211req *) data; 1152 switch (ireq->i_type) { 1153 case IEEE80211_IOC_STATIONNAME: 1154 if (ireq->i_val != 0 || 1155 ireq->i_len > IEEE80211_NWID_LEN) { 1156 error = EINVAL; 1157 break; 1158 } 1159 memset(nodename, 0, IEEE80211_NWID_LEN); 1160 error = copyin(ireq->i_data, nodename, ireq->i_len); 1161 if (error) 1162 break; 1163 if (sc->sc_enabled) { 1164 error = wi_write_ssid(sc, WI_RID_NODENAME, 1165 nodename, ireq->i_len); 1166 if (error) 1167 break; 1168 } 1169 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN); 1170 sc->sc_nodelen = ireq->i_len; 1171 break; 1172 default: 1173 error = ieee80211_ioctl(ifp, cmd, data, cr); 1174 break; 1175 } 1176 break; 1177 case SIOCSIFCAP: 1178 if (ifp->if_flags & IFF_RUNNING) 1179 wi_init(sc); 1180 break; 1181 default: 1182 error = ieee80211_ioctl(ifp, cmd, data, cr); 1183 break; 1184 } 1185 if (error == ENETRESET) { 1186 if (sc->sc_enabled) 1187 wi_init(sc); /* XXX no error return */ 1188 error = 0; 1189 } 1190 out: 1191 WI_UNLOCK(sc); 1192 1193 return (error); 1194 } 1195 1196 static int 1197 wi_media_change(struct ifnet *ifp) 1198 { 1199 struct wi_softc *sc = ifp->if_softc; 1200 int error; 1201 1202 error = ieee80211_media_change(ifp); 1203 if (error == ENETRESET) { 1204 if (sc->sc_enabled) 1205 wi_init(sc); /* XXX no error return */ 1206 error = 0; 1207 } 1208 return error; 1209 } 1210 1211 static void 1212 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1213 { 1214 struct wi_softc *sc = ifp->if_softc; 1215 struct ieee80211com *ic = &sc->sc_ic; 1216 u_int16_t val; 1217 int rate, len; 1218 1219 if (sc->wi_gone || !sc->sc_enabled) { 1220 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1221 imr->ifm_status = 0; 1222 return; 1223 } 1224 1225 imr->ifm_status = IFM_AVALID; 1226 imr->ifm_active = IFM_IEEE80211; 1227 if (ic->ic_state == IEEE80211_S_RUN && 1228 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1229 imr->ifm_status |= IFM_ACTIVE; 1230 len = sizeof(val); 1231 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0) 1232 rate = 0; 1233 else { 1234 /* convert to 802.11 rate */ 1235 rate = val * 2; 1236 if (sc->sc_firmware_type == WI_LUCENT) { 1237 if (rate == 4 * 2) 1238 rate = 11; /* 5.5Mbps */ 1239 else if (rate == 5 * 2) 1240 rate = 22; /* 11Mbps */ 1241 } else { 1242 if (rate == 4*2) 1243 rate = 11; /* 5.5Mbps */ 1244 else if (rate == 8*2) 1245 rate = 22; /* 11Mbps */ 1246 } 1247 } 1248 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1249 switch (ic->ic_opmode) { 1250 case IEEE80211_M_STA: 1251 break; 1252 case IEEE80211_M_IBSS: 1253 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1254 break; 1255 case IEEE80211_M_AHDEMO: 1256 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1257 break; 1258 case IEEE80211_M_HOSTAP: 1259 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1260 break; 1261 case IEEE80211_M_MONITOR: 1262 imr->ifm_active |= IFM_IEEE80211_MONITOR; 1263 break; 1264 } 1265 } 1266 1267 static void 1268 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1269 { 1270 struct ieee80211com *ic = &sc->sc_ic; 1271 struct ieee80211_node *ni = ic->ic_bss; 1272 struct ifnet *ifp = &ic->ic_if; 1273 1274 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1275 return; 1276 1277 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":")); 1278 DPRINTF(("%6D ?\n", new_bssid, ":")); 1279 1280 /* In promiscuous mode, the BSSID field is not a reliable 1281 * indicator of the firmware's BSSID. Damp spurious 1282 * change-of-BSSID indications. 1283 */ 1284 if ((ifp->if_flags & IFF_PROMISC) != 0 && 1285 sc->sc_false_syns >= WI_MAX_FALSE_SYNS) 1286 return; 1287 1288 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1289 } 1290 1291 static void 1292 wi_rx_monitor(struct wi_softc *sc, int fid) 1293 { 1294 struct ieee80211com *ic = &sc->sc_ic; 1295 struct ifnet *ifp = &ic->ic_if; 1296 struct wi_frame *rx_frame; 1297 struct mbuf *m; 1298 int datlen, hdrlen; 1299 1300 /* first allocate mbuf for packet storage */ 1301 m = m_getcl(MB_DONTWAIT, MT_DATA, 0); 1302 if (m == NULL) { 1303 ifp->if_ierrors++; 1304 return; 1305 } 1306 1307 m->m_pkthdr.rcvif = ifp; 1308 1309 /* now read wi_frame first so we know how much data to read */ 1310 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) { 1311 ifp->if_ierrors++; 1312 goto done; 1313 } 1314 1315 rx_frame = mtod(m, struct wi_frame *); 1316 1317 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) { 1318 case 7: 1319 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1320 case IEEE80211_FC0_TYPE_DATA: 1321 hdrlen = WI_DATA_HDRLEN; 1322 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1323 break; 1324 case IEEE80211_FC0_TYPE_MGT: 1325 hdrlen = WI_MGMT_HDRLEN; 1326 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1327 break; 1328 case IEEE80211_FC0_TYPE_CTL: 1329 /* 1330 * prism2 cards don't pass control packets 1331 * down properly or consistently, so we'll only 1332 * pass down the header. 1333 */ 1334 hdrlen = WI_CTL_HDRLEN; 1335 datlen = 0; 1336 break; 1337 default: 1338 if_printf(ifp, "received packet of unknown type " 1339 "on port 7\n"); 1340 ifp->if_ierrors++; 1341 goto done; 1342 } 1343 break; 1344 case 0: 1345 hdrlen = WI_DATA_HDRLEN; 1346 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1347 break; 1348 default: 1349 if_printf(ifp, "received packet on invalid " 1350 "port (wi_status=0x%x)\n", rx_frame->wi_status); 1351 ifp->if_ierrors++; 1352 goto done; 1353 } 1354 1355 if (hdrlen + datlen + 2 > MCLBYTES) { 1356 if_printf(ifp, "oversized packet received " 1357 "(wi_dat_len=%d, wi_status=0x%x)\n", 1358 datlen, rx_frame->wi_status); 1359 ifp->if_ierrors++; 1360 goto done; 1361 } 1362 1363 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen, 1364 datlen + 2) == 0) { 1365 m->m_pkthdr.len = m->m_len = hdrlen + datlen; 1366 ifp->if_ipackets++; 1367 BPF_MTAP(ifp, m); /* Handle BPF listeners. */ 1368 } else 1369 ifp->if_ierrors++; 1370 done: 1371 m_freem(m); 1372 } 1373 1374 static void 1375 wi_rx_intr(struct wi_softc *sc) 1376 { 1377 struct ieee80211com *ic = &sc->sc_ic; 1378 struct ifnet *ifp = &ic->ic_if; 1379 struct wi_frame frmhdr; 1380 struct mbuf *m; 1381 struct ieee80211_frame *wh; 1382 struct ieee80211_node *ni; 1383 int fid, len, off, rssi; 1384 u_int8_t dir; 1385 u_int16_t status; 1386 u_int32_t rstamp; 1387 1388 fid = CSR_READ_2(sc, WI_RX_FID); 1389 1390 if (sc->wi_debug.wi_monitor) { 1391 /* 1392 * If we are in monitor mode just 1393 * read the data from the device. 1394 */ 1395 wi_rx_monitor(sc, fid); 1396 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1397 return; 1398 } 1399 1400 /* First read in the frame header */ 1401 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1402 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1403 ifp->if_ierrors++; 1404 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1405 return; 1406 } 1407 1408 if (IFF_DUMPPKTS(ifp)) 1409 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 1410 1411 /* 1412 * Drop undecryptable or packets with receive errors here 1413 */ 1414 status = le16toh(frmhdr.wi_status); 1415 if (status & WI_STAT_ERRSTAT) { 1416 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1417 ifp->if_ierrors++; 1418 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1419 return; 1420 } 1421 rssi = frmhdr.wi_rx_signal; 1422 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1423 le16toh(frmhdr.wi_rx_tstamp1); 1424 1425 len = le16toh(frmhdr.wi_dat_len); 1426 off = ALIGN(sizeof(struct ieee80211_frame)); 1427 1428 /* 1429 * Sometimes the PRISM2.x returns bogusly large frames. Except 1430 * in monitor mode, just throw them away. 1431 */ 1432 if (off + len > MCLBYTES) { 1433 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1434 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1435 ifp->if_ierrors++; 1436 DPRINTF(("wi_rx_intr: oversized packet\n")); 1437 return; 1438 } else 1439 len = 0; 1440 } 1441 1442 MGETHDR(m, MB_DONTWAIT, MT_DATA); 1443 if (m == NULL) { 1444 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1445 ifp->if_ierrors++; 1446 DPRINTF(("wi_rx_intr: MGET failed\n")); 1447 return; 1448 } 1449 if (off + len > MHLEN) { 1450 MCLGET(m, MB_DONTWAIT); 1451 if ((m->m_flags & M_EXT) == 0) { 1452 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1453 m_freem(m); 1454 ifp->if_ierrors++; 1455 DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1456 return; 1457 } 1458 } 1459 1460 m->m_data += off - sizeof(struct ieee80211_frame); 1461 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1462 wi_read_bap(sc, fid, sizeof(frmhdr), 1463 m->m_data + sizeof(struct ieee80211_frame), len); 1464 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1465 m->m_pkthdr.rcvif = ifp; 1466 1467 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1468 1469 if (sc->sc_drvbpf) { 1470 /* XXX replace divide by table */ 1471 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5; 1472 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal; 1473 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence; 1474 sc->sc_rx_th.wr_flags = 0; 1475 if (frmhdr.wi_status & WI_STAT_PCF) 1476 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1477 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len); 1478 } 1479 1480 wh = mtod(m, struct ieee80211_frame *); 1481 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1482 /* 1483 * WEP is decrypted by hardware. Clear WEP bit 1484 * header for ieee80211_input(). 1485 */ 1486 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1487 } 1488 1489 /* synchronize driver's BSSID with firmware's BSSID */ 1490 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1491 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1492 wi_sync_bssid(sc, wh->i_addr3); 1493 1494 /* 1495 * Locate the node for sender, track state, and 1496 * then pass this node (referenced) up to the 802.11 1497 * layer for its use. We are required to pass 1498 * something so we fallback to ic_bss when this frame 1499 * is from an unknown sender. 1500 */ 1501 if (ic->ic_opmode != IEEE80211_M_STA) { 1502 ni = ieee80211_find_node(ic, wh->i_addr2); 1503 if (ni == NULL) 1504 ni = ieee80211_ref_node(ic->ic_bss); 1505 } else 1506 ni = ieee80211_ref_node(ic->ic_bss); 1507 /* 1508 * Send frame up for processing. 1509 */ 1510 ieee80211_input(ifp, m, ni, rssi, rstamp); 1511 /* 1512 * The frame may have caused the node to be marked for 1513 * reclamation (e.g. in response to a DEAUTH message) 1514 * so use free_node here instead of unref_node. 1515 */ 1516 if (ni == ic->ic_bss) 1517 ieee80211_unref_node(&ni); 1518 else 1519 ieee80211_free_node(ic, ni); 1520 } 1521 1522 static void 1523 wi_tx_ex_intr(struct wi_softc *sc) 1524 { 1525 struct ieee80211com *ic = &sc->sc_ic; 1526 struct ifnet *ifp = &ic->ic_if; 1527 struct wi_frame frmhdr; 1528 int fid; 1529 1530 fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1531 /* Read in the frame header */ 1532 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1533 u_int16_t status = le16toh(frmhdr.wi_status); 1534 1535 /* 1536 * Spontaneous station disconnects appear as xmit 1537 * errors. Don't announce them and/or count them 1538 * as an output error. 1539 */ 1540 if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1541 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1542 if_printf(ifp, "tx failed"); 1543 if (status & WI_TXSTAT_RET_ERR) 1544 printf(", retry limit exceeded"); 1545 if (status & WI_TXSTAT_AGED_ERR) 1546 printf(", max transmit lifetime exceeded"); 1547 if (status & WI_TXSTAT_DISCONNECT) 1548 printf(", port disconnected"); 1549 if (status & WI_TXSTAT_FORM_ERR) 1550 printf(", invalid format (data len %u src %6D)", 1551 le16toh(frmhdr.wi_dat_len), 1552 frmhdr.wi_ehdr.ether_shost, ":"); 1553 if (status & ~0xf) 1554 printf(", status=0x%x", status); 1555 printf("\n"); 1556 } 1557 ifp->if_oerrors++; 1558 } else { 1559 DPRINTF(("port disconnected\n")); 1560 ifp->if_collisions++; /* XXX */ 1561 } 1562 } else 1563 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1564 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1565 } 1566 1567 static void 1568 wi_tx_intr(struct wi_softc *sc) 1569 { 1570 struct ieee80211com *ic = &sc->sc_ic; 1571 struct ifnet *ifp = &ic->ic_if; 1572 int fid, cur; 1573 1574 if (sc->wi_gone) 1575 return; 1576 1577 fid = CSR_READ_2(sc, WI_ALLOC_FID); 1578 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1579 1580 cur = sc->sc_txcur; 1581 if (sc->sc_txd[cur].d_fid != fid) { 1582 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1583 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1584 return; 1585 } 1586 sc->sc_tx_timer = 0; 1587 sc->sc_txd[cur].d_len = 0; 1588 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1589 if (sc->sc_txd[cur].d_len == 0) 1590 ifp->if_flags &= ~IFF_OACTIVE; 1591 else { 1592 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1593 0, 0)) { 1594 if_printf(ifp, "xmit failed\n"); 1595 sc->sc_txd[cur].d_len = 0; 1596 } else { 1597 sc->sc_tx_timer = 5; 1598 ifp->if_timer = 1; 1599 } 1600 } 1601 } 1602 1603 static void 1604 wi_info_intr(struct wi_softc *sc) 1605 { 1606 struct ieee80211com *ic = &sc->sc_ic; 1607 struct ifnet *ifp = &ic->ic_if; 1608 int i, fid, len, off; 1609 u_int16_t ltbuf[2]; 1610 u_int16_t stat; 1611 u_int32_t *ptr; 1612 1613 fid = CSR_READ_2(sc, WI_INFO_FID); 1614 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 1615 1616 switch (le16toh(ltbuf[1])) { 1617 1618 case WI_INFO_LINK_STAT: 1619 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1620 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1621 switch (le16toh(stat)) { 1622 case WI_INFO_LINK_STAT_CONNECTED: 1623 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1624 if (ic->ic_state == IEEE80211_S_RUN && 1625 ic->ic_opmode != IEEE80211_M_IBSS) 1626 break; 1627 /* FALLTHROUGH */ 1628 case WI_INFO_LINK_STAT_AP_CHG: 1629 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1630 break; 1631 case WI_INFO_LINK_STAT_AP_INR: 1632 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1633 break; 1634 case WI_INFO_LINK_STAT_AP_OOR: 1635 if (sc->sc_firmware_type == WI_SYMBOL && 1636 sc->sc_scan_timer > 0) { 1637 if (wi_cmd(sc, WI_CMD_INQUIRE, 1638 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1639 sc->sc_scan_timer = 0; 1640 break; 1641 } 1642 if (ic->ic_opmode == IEEE80211_M_STA) 1643 sc->sc_flags |= WI_FLAGS_OUTRANGE; 1644 break; 1645 case WI_INFO_LINK_STAT_DISCONNECTED: 1646 case WI_INFO_LINK_STAT_ASSOC_FAILED: 1647 if (ic->ic_opmode == IEEE80211_M_STA) 1648 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1649 break; 1650 } 1651 break; 1652 1653 case WI_INFO_COUNTERS: 1654 /* some card versions have a larger stats structure */ 1655 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1656 ptr = (u_int32_t *)&sc->sc_stats; 1657 off = sizeof(ltbuf); 1658 for (i = 0; i < len; i++, off += 2, ptr++) { 1659 wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1660 #ifdef WI_HERMES_STATS_WAR 1661 if (stat & 0xf000) 1662 stat = ~stat; 1663 #endif 1664 *ptr += stat; 1665 } 1666 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1667 sc->sc_stats.wi_tx_multi_retries + 1668 sc->sc_stats.wi_tx_retry_limit; 1669 break; 1670 1671 case WI_INFO_SCAN_RESULTS: 1672 case WI_INFO_HOST_SCAN_RESULTS: 1673 wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1674 break; 1675 1676 default: 1677 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1678 le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1679 break; 1680 } 1681 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1682 } 1683 1684 static int 1685 wi_write_multi(struct wi_softc *sc) 1686 { 1687 struct ifnet *ifp = &sc->sc_ic.ic_if; 1688 int n; 1689 struct ifmultiaddr *ifma; 1690 struct wi_mcast mlist; 1691 1692 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1693 allmulti: 1694 memset(&mlist, 0, sizeof(mlist)); 1695 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1696 sizeof(mlist)); 1697 } 1698 1699 n = 0; 1700 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1701 if (ifma->ifma_addr->sa_family != AF_LINK) 1702 continue; 1703 if (n >= 16) 1704 goto allmulti; 1705 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1706 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1707 n++; 1708 } 1709 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1710 IEEE80211_ADDR_LEN * n); 1711 } 1712 1713 static void 1714 wi_read_nicid(struct wi_softc *sc) 1715 { 1716 struct wi_card_ident *id; 1717 char *p; 1718 int len; 1719 u_int16_t ver[4]; 1720 1721 /* getting chip identity */ 1722 memset(ver, 0, sizeof(ver)); 1723 len = sizeof(ver); 1724 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1725 device_printf(sc->sc_dev, "using "); 1726 1727 sc->sc_firmware_type = WI_NOTYPE; 1728 for (id = wi_card_ident; id->card_name != NULL; id++) { 1729 if (le16toh(ver[0]) == id->card_id) { 1730 printf("%s", id->card_name); 1731 sc->sc_firmware_type = id->firm_type; 1732 break; 1733 } 1734 } 1735 if (sc->sc_firmware_type == WI_NOTYPE) { 1736 if (le16toh(ver[0]) & 0x8000) { 1737 printf("Unknown PRISM2 chip"); 1738 sc->sc_firmware_type = WI_INTERSIL; 1739 } else { 1740 printf("Unknown Lucent chip"); 1741 sc->sc_firmware_type = WI_LUCENT; 1742 } 1743 } 1744 1745 /* get primary firmware version (Only Prism chips) */ 1746 if (sc->sc_firmware_type != WI_LUCENT) { 1747 memset(ver, 0, sizeof(ver)); 1748 len = sizeof(ver); 1749 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1750 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1751 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1752 } 1753 1754 /* get station firmware version */ 1755 memset(ver, 0, sizeof(ver)); 1756 len = sizeof(ver); 1757 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1758 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1759 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1760 if (sc->sc_firmware_type == WI_INTERSIL && 1761 (sc->sc_sta_firmware_ver == 10102 || 1762 sc->sc_sta_firmware_ver == 20102)) { 1763 char ident[12]; 1764 memset(ident, 0, sizeof(ident)); 1765 len = sizeof(ident); 1766 /* value should be the format like "V2.00-11" */ 1767 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1768 *(p = (char *)ident) >= 'A' && 1769 p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1770 sc->sc_firmware_type = WI_SYMBOL; 1771 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1772 (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1773 (p[6] - '0') * 10 + (p[7] - '0'); 1774 } 1775 } 1776 printf("\n"); 1777 device_printf(sc->sc_dev, "%s Firmware: ", 1778 sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1779 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1780 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1781 printf("Primary (%u.%u.%u), ", 1782 sc->sc_pri_firmware_ver / 10000, 1783 (sc->sc_pri_firmware_ver % 10000) / 100, 1784 sc->sc_pri_firmware_ver % 100); 1785 printf("Station (%u.%u.%u)\n", 1786 sc->sc_sta_firmware_ver / 10000, 1787 (sc->sc_sta_firmware_ver % 10000) / 100, 1788 sc->sc_sta_firmware_ver % 100); 1789 } 1790 1791 static int 1792 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1793 { 1794 struct wi_ssid ssid; 1795 1796 if (buflen > IEEE80211_NWID_LEN) 1797 return ENOBUFS; 1798 memset(&ssid, 0, sizeof(ssid)); 1799 ssid.wi_len = htole16(buflen); 1800 memcpy(ssid.wi_ssid, buf, buflen); 1801 return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1802 } 1803 1804 static int 1805 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 1806 { 1807 struct wi_softc *sc = ifp->if_softc; 1808 struct ieee80211com *ic = &sc->sc_ic; 1809 struct ifreq *ifr = (struct ifreq *)data; 1810 struct wi_req wreq; 1811 struct wi_scan_res *res; 1812 size_t reslen; 1813 int len, n, error, mif, val, off, i; 1814 1815 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1816 if (error) 1817 return error; 1818 len = (wreq.wi_len - 1) * 2; 1819 if (len < sizeof(u_int16_t)) 1820 return ENOSPC; 1821 if (len > sizeof(wreq.wi_val)) 1822 len = sizeof(wreq.wi_val); 1823 1824 switch (wreq.wi_type) { 1825 1826 case WI_RID_IFACE_STATS: 1827 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1828 if (len < sizeof(sc->sc_stats)) 1829 error = ENOSPC; 1830 else 1831 len = sizeof(sc->sc_stats); 1832 break; 1833 1834 case WI_RID_ENCRYPTION: 1835 case WI_RID_TX_CRYPT_KEY: 1836 case WI_RID_DEFLT_CRYPT_KEYS: 1837 case WI_RID_TX_RATE: 1838 return ieee80211_cfgget(ifp, cmd, data, cr); 1839 1840 case WI_RID_MICROWAVE_OVEN: 1841 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1842 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1843 &len); 1844 break; 1845 } 1846 wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 1847 len = sizeof(u_int16_t); 1848 break; 1849 1850 case WI_RID_DBM_ADJUST: 1851 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 1852 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1853 &len); 1854 break; 1855 } 1856 wreq.wi_val[0] = htole16(sc->sc_dbm_offset); 1857 len = sizeof(u_int16_t); 1858 break; 1859 1860 case WI_RID_ROAMING_MODE: 1861 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 1862 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1863 &len); 1864 break; 1865 } 1866 wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 1867 len = sizeof(u_int16_t); 1868 break; 1869 1870 case WI_RID_SYSTEM_SCALE: 1871 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 1872 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1873 &len); 1874 break; 1875 } 1876 wreq.wi_val[0] = htole16(sc->sc_system_scale); 1877 len = sizeof(u_int16_t); 1878 break; 1879 1880 case WI_RID_FRAG_THRESH: 1881 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 1882 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1883 &len); 1884 break; 1885 } 1886 wreq.wi_val[0] = htole16(ic->ic_fragthreshold); 1887 len = sizeof(u_int16_t); 1888 break; 1889 1890 case WI_RID_READ_APS: 1891 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1892 return ieee80211_cfgget(ifp, cmd, data, cr); 1893 if (sc->sc_scan_timer > 0) { 1894 error = EINPROGRESS; 1895 break; 1896 } 1897 n = sc->sc_naps; 1898 if (len < sizeof(n)) { 1899 error = ENOSPC; 1900 break; 1901 } 1902 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 1903 n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 1904 len = sizeof(n) + sizeof(struct wi_apinfo) * n; 1905 memcpy(wreq.wi_val, &n, sizeof(n)); 1906 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 1907 sizeof(struct wi_apinfo) * n); 1908 break; 1909 1910 case WI_RID_PRISM2: 1911 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT; 1912 len = sizeof(u_int16_t); 1913 break; 1914 1915 case WI_RID_MIF: 1916 mif = wreq.wi_val[0]; 1917 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0); 1918 val = CSR_READ_2(sc, WI_RESP0); 1919 wreq.wi_val[0] = val; 1920 len = sizeof(u_int16_t); 1921 break; 1922 1923 case WI_RID_ZERO_CACHE: 1924 case WI_RID_PROCFRAME: /* ignore for compatibility */ 1925 /* XXX ??? */ 1926 break; 1927 1928 case WI_RID_READ_CACHE: 1929 return ieee80211_cfgget(ifp, cmd, data, cr); 1930 1931 case WI_RID_SCAN_RES: /* compatibility interface */ 1932 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1933 return ieee80211_cfgget(ifp, cmd, data, cr); 1934 if (sc->sc_scan_timer > 0) { 1935 error = EINPROGRESS; 1936 break; 1937 } 1938 n = sc->sc_naps; 1939 if (sc->sc_firmware_type == WI_LUCENT) { 1940 off = 0; 1941 reslen = WI_WAVELAN_RES_SIZE; 1942 } else { 1943 off = sizeof(struct wi_scan_p2_hdr); 1944 reslen = WI_PRISM2_RES_SIZE; 1945 } 1946 if (len < off + reslen * n) 1947 n = (len - off) / reslen; 1948 len = off + reslen * n; 1949 if (off != 0) { 1950 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1951 /* 1952 * Prepend Prism-specific header. 1953 */ 1954 if (len < sizeof(struct wi_scan_p2_hdr)) { 1955 error = ENOSPC; 1956 break; 1957 } 1958 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1959 p2->wi_rsvd = 0; 1960 p2->wi_reason = n; /* XXX */ 1961 } 1962 for (i = 0; i < n; i++, off += reslen) { 1963 const struct wi_apinfo *ap = &sc->sc_aps[i]; 1964 1965 res = (struct wi_scan_res *)((char *)wreq.wi_val + off); 1966 res->wi_chan = ap->channel; 1967 res->wi_noise = ap->noise; 1968 res->wi_signal = ap->signal; 1969 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid); 1970 res->wi_interval = ap->interval; 1971 res->wi_capinfo = ap->capinfo; 1972 res->wi_ssid_len = ap->namelen; 1973 memcpy(res->wi_ssid, ap->name, 1974 IEEE80211_NWID_LEN); 1975 if (sc->sc_firmware_type != WI_LUCENT) { 1976 /* XXX not saved from Prism cards */ 1977 memset(res->wi_srates, 0, 1978 sizeof(res->wi_srates)); 1979 res->wi_rate = ap->rate; 1980 res->wi_rsvd = 0; 1981 } 1982 } 1983 break; 1984 1985 default: 1986 if (sc->sc_enabled) { 1987 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1988 &len); 1989 break; 1990 } 1991 switch (wreq.wi_type) { 1992 case WI_RID_MAX_DATALEN: 1993 wreq.wi_val[0] = htole16(sc->sc_max_datalen); 1994 len = sizeof(u_int16_t); 1995 break; 1996 case WI_RID_RTS_THRESH: 1997 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold); 1998 len = sizeof(u_int16_t); 1999 break; 2000 case WI_RID_CNFAUTHMODE: 2001 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 2002 len = sizeof(u_int16_t); 2003 break; 2004 case WI_RID_NODENAME: 2005 if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 2006 error = ENOSPC; 2007 break; 2008 } 2009 len = sc->sc_nodelen + sizeof(u_int16_t); 2010 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 2011 memcpy(&wreq.wi_val[1], sc->sc_nodename, 2012 sc->sc_nodelen); 2013 break; 2014 default: 2015 return ieee80211_cfgget(ifp, cmd, data, cr); 2016 } 2017 break; 2018 } 2019 if (error) 2020 return error; 2021 wreq.wi_len = (len + 1) / 2 + 1; 2022 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 2023 } 2024 2025 static int 2026 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 2027 { 2028 struct wi_softc *sc = ifp->if_softc; 2029 struct ieee80211com *ic = &sc->sc_ic; 2030 struct ifreq *ifr = (struct ifreq *)data; 2031 struct wi_req wreq; 2032 struct mbuf *m; 2033 int i, len, error, mif, val; 2034 struct ieee80211_rateset *rs; 2035 2036 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2037 if (error) 2038 return error; 2039 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0; 2040 switch (wreq.wi_type) { 2041 case WI_RID_DBM_ADJUST: 2042 return ENODEV; 2043 2044 case WI_RID_NODENAME: 2045 if (le16toh(wreq.wi_val[0]) * 2 > len || 2046 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 2047 error = ENOSPC; 2048 break; 2049 } 2050 if (sc->sc_enabled) { 2051 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2052 len); 2053 if (error) 2054 break; 2055 } 2056 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2057 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen); 2058 break; 2059 2060 case WI_RID_MICROWAVE_OVEN: 2061 case WI_RID_ROAMING_MODE: 2062 case WI_RID_SYSTEM_SCALE: 2063 case WI_RID_FRAG_THRESH: 2064 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2065 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2066 break; 2067 if (wreq.wi_type == WI_RID_ROAMING_MODE && 2068 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2069 break; 2070 if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2071 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2072 break; 2073 if (wreq.wi_type == WI_RID_FRAG_THRESH && 2074 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2075 break; 2076 /* FALLTHROUGH */ 2077 case WI_RID_RTS_THRESH: 2078 case WI_RID_CNFAUTHMODE: 2079 case WI_RID_MAX_DATALEN: 2080 if (sc->sc_enabled) { 2081 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2082 sizeof(u_int16_t)); 2083 if (error) 2084 break; 2085 } 2086 switch (wreq.wi_type) { 2087 case WI_RID_FRAG_THRESH: 2088 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]); 2089 break; 2090 case WI_RID_RTS_THRESH: 2091 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]); 2092 break; 2093 case WI_RID_MICROWAVE_OVEN: 2094 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2095 break; 2096 case WI_RID_ROAMING_MODE: 2097 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2098 break; 2099 case WI_RID_SYSTEM_SCALE: 2100 sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2101 break; 2102 case WI_RID_CNFAUTHMODE: 2103 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2104 break; 2105 case WI_RID_MAX_DATALEN: 2106 sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2107 break; 2108 } 2109 break; 2110 2111 case WI_RID_TX_RATE: 2112 switch (le16toh(wreq.wi_val[0])) { 2113 case 3: 2114 ic->ic_fixed_rate = -1; 2115 break; 2116 default: 2117 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2118 for (i = 0; i < rs->rs_nrates; i++) { 2119 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2120 / 2 == le16toh(wreq.wi_val[0])) 2121 break; 2122 } 2123 if (i == rs->rs_nrates) 2124 return EINVAL; 2125 ic->ic_fixed_rate = i; 2126 } 2127 if (sc->sc_enabled) 2128 error = wi_write_txrate(sc); 2129 break; 2130 2131 case WI_RID_SCAN_APS: 2132 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2133 error = wi_scan_ap(sc, 0x3fff, 0x000f); 2134 break; 2135 2136 case WI_RID_SCAN_REQ: /* compatibility interface */ 2137 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2138 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]); 2139 break; 2140 2141 case WI_RID_MGMT_XMIT: 2142 if (!sc->sc_enabled) { 2143 error = ENETDOWN; 2144 break; 2145 } 2146 if (ic->ic_mgtq.ifq_len > 5) { 2147 error = EAGAIN; 2148 break; 2149 } 2150 /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2151 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL); 2152 if (m == NULL) { 2153 error = ENOMEM; 2154 break; 2155 } 2156 IF_ENQUEUE(&ic->ic_mgtq, m); 2157 break; 2158 2159 case WI_RID_MIF: 2160 mif = wreq.wi_val[0]; 2161 val = wreq.wi_val[1]; 2162 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0); 2163 break; 2164 2165 case WI_RID_PROCFRAME: /* ignore for compatibility */ 2166 break; 2167 2168 case WI_RID_OWN_SSID: 2169 if (le16toh(wreq.wi_val[0]) * 2 > len || 2170 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) { 2171 error = ENOSPC; 2172 break; 2173 } 2174 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 2175 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2; 2176 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen); 2177 error = ENETRESET; 2178 break; 2179 2180 default: 2181 if (sc->sc_enabled) { 2182 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2183 len); 2184 if (error) 2185 break; 2186 } 2187 error = ieee80211_cfgset(ifp, cmd, data); 2188 break; 2189 } 2190 return error; 2191 } 2192 2193 static int 2194 wi_write_txrate(struct wi_softc *sc) 2195 { 2196 struct ieee80211com *ic = &sc->sc_ic; 2197 int i; 2198 u_int16_t rate; 2199 2200 if (ic->ic_fixed_rate < 0) 2201 rate = 0; /* auto */ 2202 else 2203 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] & 2204 IEEE80211_RATE_VAL) / 2; 2205 2206 /* rate: 0, 1, 2, 5, 11 */ 2207 2208 switch (sc->sc_firmware_type) { 2209 case WI_LUCENT: 2210 switch (rate) { 2211 case 0: /* auto == 11mbps auto */ 2212 rate = 3; 2213 break; 2214 /* case 1, 2 map to 1, 2*/ 2215 case 5: /* 5.5Mbps -> 4 */ 2216 rate = 4; 2217 break; 2218 case 11: /* 11mbps -> 5 */ 2219 rate = 5; 2220 break; 2221 default: 2222 break; 2223 } 2224 break; 2225 default: 2226 /* Choose a bit according to this table. 2227 * 2228 * bit | data rate 2229 * ----+------------------- 2230 * 0 | 1Mbps 2231 * 1 | 2Mbps 2232 * 2 | 5.5Mbps 2233 * 3 | 11Mbps 2234 */ 2235 for (i = 8; i > 0; i >>= 1) { 2236 if (rate >= i) 2237 break; 2238 } 2239 if (i == 0) 2240 rate = 0xf; /* auto */ 2241 else 2242 rate = i; 2243 break; 2244 } 2245 return wi_write_val(sc, WI_RID_TX_RATE, rate); 2246 } 2247 2248 static int 2249 wi_write_wep(struct wi_softc *sc) 2250 { 2251 struct ieee80211com *ic = &sc->sc_ic; 2252 int error = 0; 2253 int i, keylen; 2254 u_int16_t val; 2255 struct wi_key wkey[IEEE80211_WEP_NKID]; 2256 2257 switch (sc->sc_firmware_type) { 2258 case WI_LUCENT: 2259 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0; 2260 error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2261 if (error) 2262 break; 2263 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey); 2264 if (error) 2265 break; 2266 memset(wkey, 0, sizeof(wkey)); 2267 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2268 keylen = ic->ic_nw_keys[i].wk_len; 2269 wkey[i].wi_keylen = htole16(keylen); 2270 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2271 keylen); 2272 } 2273 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2274 wkey, sizeof(wkey)); 2275 break; 2276 2277 case WI_INTERSIL: 2278 case WI_SYMBOL: 2279 if (ic->ic_flags & IEEE80211_F_WEPON) { 2280 /* 2281 * ONLY HWB3163 EVAL-CARD Firmware version 2282 * less than 0.8 variant2 2283 * 2284 * If promiscuous mode disable, Prism2 chip 2285 * does not work with WEP . 2286 * It is under investigation for details. 2287 * (ichiro@netbsd.org) 2288 */ 2289 if (sc->sc_firmware_type == WI_INTERSIL && 2290 sc->sc_sta_firmware_ver < 802 ) { 2291 /* firm ver < 0.8 variant 2 */ 2292 wi_write_val(sc, WI_RID_PROMISC, 1); 2293 } 2294 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2295 sc->sc_cnfauthmode); 2296 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 2297 /* 2298 * Encryption firmware has a bug for HostAP mode. 2299 */ 2300 if (sc->sc_firmware_type == WI_INTERSIL && 2301 ic->ic_opmode == IEEE80211_M_HOSTAP) 2302 val |= HOST_ENCRYPT; 2303 } else { 2304 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2305 IEEE80211_AUTH_OPEN); 2306 val = HOST_ENCRYPT | HOST_DECRYPT; 2307 } 2308 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2309 if (error) 2310 break; 2311 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2312 ic->ic_wep_txkey); 2313 if (error) 2314 break; 2315 /* 2316 * It seems that the firmware accept 104bit key only if 2317 * all the keys have 104bit length. We get the length of 2318 * the transmit key and use it for all other keys. 2319 * Perhaps we should use software WEP for such situation. 2320 */ 2321 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len; 2322 if (keylen > IEEE80211_WEP_KEYLEN) 2323 keylen = 13; /* 104bit keys */ 2324 else 2325 keylen = IEEE80211_WEP_KEYLEN; 2326 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2327 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2328 ic->ic_nw_keys[i].wk_key, keylen); 2329 if (error) 2330 break; 2331 } 2332 break; 2333 } 2334 return error; 2335 } 2336 2337 static int 2338 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 2339 { 2340 int i, s = 0; 2341 static volatile int count = 0; 2342 2343 if (sc->wi_gone) 2344 return (ENODEV); 2345 2346 if (count > 0) 2347 panic("Hey partner, hold on there!"); 2348 count++; 2349 2350 /* wait for the busy bit to clear */ 2351 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 2352 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 2353 break; 2354 DELAY(1*1000); /* 1ms */ 2355 } 2356 if (i == 0) { 2357 device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" ); 2358 sc->wi_gone = 1; 2359 count--; 2360 return(ETIMEDOUT); 2361 } 2362 2363 CSR_WRITE_2(sc, WI_PARAM0, val0); 2364 CSR_WRITE_2(sc, WI_PARAM1, val1); 2365 CSR_WRITE_2(sc, WI_PARAM2, val2); 2366 CSR_WRITE_2(sc, WI_COMMAND, cmd); 2367 2368 if (cmd == WI_CMD_INI) { 2369 /* XXX: should sleep here. */ 2370 DELAY(100*1000); /* 100ms delay for init */ 2371 } 2372 for (i = 0; i < WI_TIMEOUT; i++) { 2373 /* 2374 * Wait for 'command complete' bit to be 2375 * set in the event status register. 2376 */ 2377 s = CSR_READ_2(sc, WI_EVENT_STAT); 2378 if (s & WI_EV_CMD) { 2379 /* Ack the event and read result code. */ 2380 s = CSR_READ_2(sc, WI_STATUS); 2381 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2382 if (s & WI_STAT_CMD_RESULT) { 2383 count--; 2384 return(EIO); 2385 } 2386 break; 2387 } 2388 DELAY(WI_DELAY); 2389 } 2390 2391 count--; 2392 if (i == WI_TIMEOUT) { 2393 device_printf(sc->sc_dev, 2394 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s); 2395 if (s == 0xffff) 2396 sc->wi_gone = 1; 2397 return(ETIMEDOUT); 2398 } 2399 return (0); 2400 } 2401 2402 static int 2403 wi_seek_bap(struct wi_softc *sc, int id, int off) 2404 { 2405 int i, status; 2406 2407 CSR_WRITE_2(sc, WI_SEL0, id); 2408 CSR_WRITE_2(sc, WI_OFF0, off); 2409 2410 for (i = 0; ; i++) { 2411 status = CSR_READ_2(sc, WI_OFF0); 2412 if ((status & WI_OFF_BUSY) == 0) 2413 break; 2414 if (i == WI_TIMEOUT) { 2415 device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n", 2416 id, off); 2417 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2418 if (status == 0xffff) 2419 sc->wi_gone = 1; 2420 return ETIMEDOUT; 2421 } 2422 DELAY(1); 2423 } 2424 if (status & WI_OFF_ERR) { 2425 device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off); 2426 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2427 return EIO; 2428 } 2429 sc->sc_bap_id = id; 2430 sc->sc_bap_off = off; 2431 return 0; 2432 } 2433 2434 static int 2435 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2436 { 2437 u_int16_t *ptr; 2438 int i, error, cnt; 2439 2440 if (buflen == 0) 2441 return 0; 2442 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2443 if ((error = wi_seek_bap(sc, id, off)) != 0) 2444 return error; 2445 } 2446 cnt = (buflen + 1) / 2; 2447 ptr = (u_int16_t *)buf; 2448 for (i = 0; i < cnt; i++) 2449 *ptr++ = CSR_READ_2(sc, WI_DATA0); 2450 sc->sc_bap_off += cnt * 2; 2451 return 0; 2452 } 2453 2454 static int 2455 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2456 { 2457 u_int16_t *ptr; 2458 int i, error, cnt; 2459 2460 if (buflen == 0) 2461 return 0; 2462 2463 #ifdef WI_HERMES_AUTOINC_WAR 2464 again: 2465 #endif 2466 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2467 if ((error = wi_seek_bap(sc, id, off)) != 0) 2468 return error; 2469 } 2470 cnt = (buflen + 1) / 2; 2471 ptr = (u_int16_t *)buf; 2472 for (i = 0; i < cnt; i++) 2473 CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 2474 sc->sc_bap_off += cnt * 2; 2475 2476 #ifdef WI_HERMES_AUTOINC_WAR 2477 /* 2478 * According to the comments in the HCF Light code, there is a bug 2479 * in the Hermes (or possibly in certain Hermes firmware revisions) 2480 * where the chip's internal autoincrement counter gets thrown off 2481 * during data writes: the autoincrement is missed, causing one 2482 * data word to be overwritten and subsequent words to be written to 2483 * the wrong memory locations. The end result is that we could end 2484 * up transmitting bogus frames without realizing it. The workaround 2485 * for this is to write a couple of extra guard words after the end 2486 * of the transfer, then attempt to read then back. If we fail to 2487 * locate the guard words where we expect them, we preform the 2488 * transfer over again. 2489 */ 2490 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2491 CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2492 CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2493 wi_seek_bap(sc, id, sc->sc_bap_off); 2494 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2495 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2496 CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2497 device_printf(sc->sc_dev, 2498 "detect auto increment bug, try again\n"); 2499 goto again; 2500 } 2501 } 2502 #endif 2503 return 0; 2504 } 2505 2506 static int 2507 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2508 { 2509 int error, len; 2510 struct mbuf *m; 2511 2512 for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2513 if (m->m_len == 0) 2514 continue; 2515 2516 len = min(m->m_len, totlen); 2517 2518 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2519 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 2520 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 2521 totlen); 2522 } 2523 2524 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2525 return error; 2526 2527 off += m->m_len; 2528 totlen -= len; 2529 } 2530 return 0; 2531 } 2532 2533 static int 2534 wi_alloc_fid(struct wi_softc *sc, int len, int *idp) 2535 { 2536 int i; 2537 2538 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2539 device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 2540 len); 2541 return ENOMEM; 2542 } 2543 2544 for (i = 0; i < WI_TIMEOUT; i++) { 2545 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2546 break; 2547 if (i == WI_TIMEOUT) { 2548 device_printf(sc->sc_dev, "timeout in alloc\n"); 2549 return ETIMEDOUT; 2550 } 2551 DELAY(1); 2552 } 2553 *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2554 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2555 return 0; 2556 } 2557 2558 static int 2559 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2560 { 2561 int error, len; 2562 u_int16_t ltbuf[2]; 2563 2564 /* Tell the NIC to enter record read mode. */ 2565 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2566 if (error) 2567 return error; 2568 2569 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2570 if (error) 2571 return error; 2572 2573 if (le16toh(ltbuf[1]) != rid) { 2574 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2575 rid, le16toh(ltbuf[1])); 2576 return EIO; 2577 } 2578 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2579 if (*buflenp < len) { 2580 device_printf(sc->sc_dev, "record buffer is too small, " 2581 "rid=%x, size=%d, len=%d\n", 2582 rid, *buflenp, len); 2583 return ENOSPC; 2584 } 2585 *buflenp = len; 2586 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2587 } 2588 2589 static int 2590 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2591 { 2592 int error; 2593 u_int16_t ltbuf[2]; 2594 2595 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2596 ltbuf[1] = htole16(rid); 2597 2598 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2599 if (error) 2600 return error; 2601 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2602 if (error) 2603 return error; 2604 2605 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 2606 } 2607 2608 static int 2609 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 2610 { 2611 struct ifnet *ifp = &ic->ic_if; 2612 struct wi_softc *sc = ifp->if_softc; 2613 struct ieee80211_node *ni = ic->ic_bss; 2614 int buflen; 2615 u_int16_t val; 2616 struct wi_ssid ssid; 2617 u_int8_t old_bssid[IEEE80211_ADDR_LEN]; 2618 2619 DPRINTF(("%s: %s -> %s\n", __func__, 2620 ieee80211_state_name[ic->ic_state], 2621 ieee80211_state_name[nstate])); 2622 2623 switch (nstate) { 2624 case IEEE80211_S_INIT: 2625 ic->ic_flags &= ~IEEE80211_F_SIBSS; 2626 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2627 return (*sc->sc_newstate)(ic, nstate, arg); 2628 2629 case IEEE80211_S_RUN: 2630 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2631 buflen = IEEE80211_ADDR_LEN; 2632 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2633 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2634 buflen = sizeof(val); 2635 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2636 /* XXX validate channel */ 2637 ni->ni_chan = &ic->ic_channels[le16toh(val)]; 2638 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 2639 htole16(ni->ni_chan->ic_freq); 2640 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 2641 htole16(ni->ni_chan->ic_flags); 2642 2643 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid)) 2644 sc->sc_false_syns++; 2645 else 2646 sc->sc_false_syns = 0; 2647 2648 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2649 ni->ni_esslen = ic->ic_des_esslen; 2650 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 2651 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; 2652 ni->ni_intval = ic->ic_lintval; 2653 ni->ni_capinfo = IEEE80211_CAPINFO_ESS; 2654 if (ic->ic_flags & IEEE80211_F_WEPON) 2655 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 2656 } else { 2657 /* XXX check return value */ 2658 buflen = sizeof(ssid); 2659 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2660 ni->ni_esslen = le16toh(ssid.wi_len); 2661 if (ni->ni_esslen > IEEE80211_NWID_LEN) 2662 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2663 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 2664 } 2665 break; 2666 2667 case IEEE80211_S_SCAN: 2668 case IEEE80211_S_AUTH: 2669 case IEEE80211_S_ASSOC: 2670 break; 2671 } 2672 2673 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 2674 return 0; 2675 } 2676 2677 static int 2678 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 2679 { 2680 int error = 0; 2681 u_int16_t val[2]; 2682 2683 if (!sc->sc_enabled) 2684 return ENXIO; 2685 switch (sc->sc_firmware_type) { 2686 case WI_LUCENT: 2687 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 2688 break; 2689 case WI_INTERSIL: 2690 val[0] = chanmask; /* channel */ 2691 val[1] = txrate; /* tx rate */ 2692 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 2693 break; 2694 case WI_SYMBOL: 2695 /* 2696 * XXX only supported on 3.x ? 2697 */ 2698 val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2699 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2700 val, sizeof(val[0])); 2701 break; 2702 } 2703 if (error == 0) { 2704 sc->sc_scan_timer = WI_SCAN_WAIT; 2705 sc->sc_ic.ic_if.if_timer = 1; 2706 DPRINTF(("wi_scan_ap: start scanning, " 2707 "chamask 0x%x txrate 0x%x\n", chanmask, txrate)); 2708 } 2709 return error; 2710 } 2711 2712 static void 2713 wi_scan_result(struct wi_softc *sc, int fid, int cnt) 2714 { 2715 #define N(a) (sizeof (a) / sizeof (a[0])) 2716 int i, naps, off, szbuf; 2717 struct wi_scan_header ws_hdr; /* Prism2 header */ 2718 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2719 struct wi_apinfo *ap; 2720 2721 off = sizeof(u_int16_t) * 2; 2722 memset(&ws_hdr, 0, sizeof(ws_hdr)); 2723 switch (sc->sc_firmware_type) { 2724 case WI_INTERSIL: 2725 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2726 off += sizeof(ws_hdr); 2727 szbuf = sizeof(struct wi_scan_data_p2); 2728 break; 2729 case WI_SYMBOL: 2730 szbuf = sizeof(struct wi_scan_data_p2) + 6; 2731 break; 2732 case WI_LUCENT: 2733 szbuf = sizeof(struct wi_scan_data); 2734 break; 2735 default: 2736 device_printf(sc->sc_dev, 2737 "wi_scan_result: unknown firmware type %u\n", 2738 sc->sc_firmware_type); 2739 naps = 0; 2740 goto done; 2741 } 2742 naps = (cnt * 2 + 2 - off) / szbuf; 2743 if (naps > N(sc->sc_aps)) 2744 naps = N(sc->sc_aps); 2745 sc->sc_naps = naps; 2746 /* Read Data */ 2747 ap = sc->sc_aps; 2748 memset(&ws_dat, 0, sizeof(ws_dat)); 2749 for (i = 0; i < naps; i++, ap++) { 2750 wi_read_bap(sc, fid, off, &ws_dat, 2751 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2752 DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off, 2753 ws_dat.wi_bssid, ":")); 2754 off += szbuf; 2755 ap->scanreason = le16toh(ws_hdr.wi_reason); 2756 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2757 ap->channel = le16toh(ws_dat.wi_chid); 2758 ap->signal = le16toh(ws_dat.wi_signal); 2759 ap->noise = le16toh(ws_dat.wi_noise); 2760 ap->quality = ap->signal - ap->noise; 2761 ap->capinfo = le16toh(ws_dat.wi_capinfo); 2762 ap->interval = le16toh(ws_dat.wi_interval); 2763 ap->rate = le16toh(ws_dat.wi_rate); 2764 ap->namelen = le16toh(ws_dat.wi_namelen); 2765 if (ap->namelen > sizeof(ap->name)) 2766 ap->namelen = sizeof(ap->name); 2767 memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2768 } 2769 done: 2770 /* Done scanning */ 2771 sc->sc_scan_timer = 0; 2772 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2773 #undef N 2774 } 2775 2776 static void 2777 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 2778 { 2779 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 2780 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi); 2781 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 2782 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 2783 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 2784 printf(" rx_signal %u rx_rate %u rx_flow %u\n", 2785 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 2786 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 2787 wh->wi_tx_rtry, wh->wi_tx_rate, 2788 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 2789 printf(" ehdr dst %6D src %6D type 0x%x\n", 2790 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":", 2791 wh->wi_ehdr.ether_type); 2792 } 2793 2794 int 2795 wi_alloc(device_t dev, int rid) 2796 { 2797 struct wi_softc *sc = device_get_softc(dev); 2798 2799 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2800 sc->iobase_rid = rid; 2801 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2802 &sc->iobase_rid, 0, ~0, (1 << 6), 2803 rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2804 if (!sc->iobase) { 2805 device_printf(dev, "No I/O space?!\n"); 2806 return (ENXIO); 2807 } 2808 2809 sc->wi_io_addr = rman_get_start(sc->iobase); 2810 sc->wi_btag = rman_get_bustag(sc->iobase); 2811 sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2812 } else { 2813 sc->mem_rid = rid; 2814 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2815 &sc->mem_rid, RF_ACTIVE); 2816 2817 if (!sc->mem) { 2818 device_printf(dev, "No Mem space on prism2.5?\n"); 2819 return (ENXIO); 2820 } 2821 2822 sc->wi_btag = rman_get_bustag(sc->mem); 2823 sc->wi_bhandle = rman_get_bushandle(sc->mem); 2824 } 2825 2826 2827 sc->irq_rid = 0; 2828 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2829 RF_ACTIVE | 2830 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2831 2832 if (!sc->irq) { 2833 wi_free(dev); 2834 device_printf(dev, "No irq?!\n"); 2835 return (ENXIO); 2836 } 2837 2838 sc->sc_dev = dev; 2839 sc->sc_unit = device_get_unit(dev); 2840 2841 return (0); 2842 } 2843 2844 void 2845 wi_free(device_t dev) 2846 { 2847 struct wi_softc *sc = device_get_softc(dev); 2848 2849 if (sc->iobase != NULL) { 2850 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2851 sc->iobase = NULL; 2852 } 2853 if (sc->irq != NULL) { 2854 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2855 sc->irq = NULL; 2856 } 2857 if (sc->mem != NULL) { 2858 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2859 sc->mem = NULL; 2860 } 2861 2862 return; 2863 } 2864 2865 static int 2866 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq) 2867 { 2868 int error = 0; 2869 2870 wreq->wi_len = 1; 2871 2872 switch (wreq->wi_type) { 2873 case WI_DEBUG_SLEEP: 2874 wreq->wi_len++; 2875 wreq->wi_val[0] = sc->wi_debug.wi_sleep; 2876 break; 2877 case WI_DEBUG_DELAYSUPP: 2878 wreq->wi_len++; 2879 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp; 2880 break; 2881 case WI_DEBUG_TXSUPP: 2882 wreq->wi_len++; 2883 wreq->wi_val[0] = sc->wi_debug.wi_txsupp; 2884 break; 2885 case WI_DEBUG_MONITOR: 2886 wreq->wi_len++; 2887 wreq->wi_val[0] = sc->wi_debug.wi_monitor; 2888 break; 2889 case WI_DEBUG_LEDTEST: 2890 wreq->wi_len += 3; 2891 wreq->wi_val[0] = sc->wi_debug.wi_ledtest; 2892 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0; 2893 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1; 2894 break; 2895 case WI_DEBUG_CONTTX: 2896 wreq->wi_len += 2; 2897 wreq->wi_val[0] = sc->wi_debug.wi_conttx; 2898 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0; 2899 break; 2900 case WI_DEBUG_CONTRX: 2901 wreq->wi_len++; 2902 wreq->wi_val[0] = sc->wi_debug.wi_contrx; 2903 break; 2904 case WI_DEBUG_SIGSTATE: 2905 wreq->wi_len += 2; 2906 wreq->wi_val[0] = sc->wi_debug.wi_sigstate; 2907 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0; 2908 break; 2909 case WI_DEBUG_CONFBITS: 2910 wreq->wi_len += 2; 2911 wreq->wi_val[0] = sc->wi_debug.wi_confbits; 2912 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0; 2913 break; 2914 default: 2915 error = EIO; 2916 break; 2917 } 2918 2919 return (error); 2920 } 2921 2922 static int 2923 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq) 2924 { 2925 int error = 0; 2926 u_int16_t cmd, param0 = 0, param1 = 0; 2927 2928 switch (wreq->wi_type) { 2929 case WI_DEBUG_RESET: 2930 case WI_DEBUG_INIT: 2931 case WI_DEBUG_CALENABLE: 2932 break; 2933 case WI_DEBUG_SLEEP: 2934 sc->wi_debug.wi_sleep = 1; 2935 break; 2936 case WI_DEBUG_WAKE: 2937 sc->wi_debug.wi_sleep = 0; 2938 break; 2939 case WI_DEBUG_CHAN: 2940 param0 = wreq->wi_val[0]; 2941 break; 2942 case WI_DEBUG_DELAYSUPP: 2943 sc->wi_debug.wi_delaysupp = 1; 2944 break; 2945 case WI_DEBUG_TXSUPP: 2946 sc->wi_debug.wi_txsupp = 1; 2947 break; 2948 case WI_DEBUG_MONITOR: 2949 sc->wi_debug.wi_monitor = 1; 2950 break; 2951 case WI_DEBUG_LEDTEST: 2952 param0 = wreq->wi_val[0]; 2953 param1 = wreq->wi_val[1]; 2954 sc->wi_debug.wi_ledtest = 1; 2955 sc->wi_debug.wi_ledtest_param0 = param0; 2956 sc->wi_debug.wi_ledtest_param1 = param1; 2957 break; 2958 case WI_DEBUG_CONTTX: 2959 param0 = wreq->wi_val[0]; 2960 sc->wi_debug.wi_conttx = 1; 2961 sc->wi_debug.wi_conttx_param0 = param0; 2962 break; 2963 case WI_DEBUG_STOPTEST: 2964 sc->wi_debug.wi_delaysupp = 0; 2965 sc->wi_debug.wi_txsupp = 0; 2966 sc->wi_debug.wi_monitor = 0; 2967 sc->wi_debug.wi_ledtest = 0; 2968 sc->wi_debug.wi_ledtest_param0 = 0; 2969 sc->wi_debug.wi_ledtest_param1 = 0; 2970 sc->wi_debug.wi_conttx = 0; 2971 sc->wi_debug.wi_conttx_param0 = 0; 2972 sc->wi_debug.wi_contrx = 0; 2973 sc->wi_debug.wi_sigstate = 0; 2974 sc->wi_debug.wi_sigstate_param0 = 0; 2975 break; 2976 case WI_DEBUG_CONTRX: 2977 sc->wi_debug.wi_contrx = 1; 2978 break; 2979 case WI_DEBUG_SIGSTATE: 2980 param0 = wreq->wi_val[0]; 2981 sc->wi_debug.wi_sigstate = 1; 2982 sc->wi_debug.wi_sigstate_param0 = param0; 2983 break; 2984 case WI_DEBUG_CONFBITS: 2985 param0 = wreq->wi_val[0]; 2986 param1 = wreq->wi_val[1]; 2987 sc->wi_debug.wi_confbits = param0; 2988 sc->wi_debug.wi_confbits_param0 = param1; 2989 break; 2990 default: 2991 error = EIO; 2992 break; 2993 } 2994 2995 if (error) 2996 return (error); 2997 2998 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8); 2999 error = wi_cmd(sc, cmd, param0, param1, 0); 3000 3001 return (error); 3002 } 3003 3004 /* 3005 * Special routines to download firmware for Symbol CF card. 3006 * XXX: This should be modified generic into any PRISM-2 based card. 3007 */ 3008 3009 #define WI_SBCF_PDIADDR 0x3100 3010 3011 /* unaligned load little endian */ 3012 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24)) 3013 #define GETLE16(p) ((p)[0] | ((p)[1]<<8)) 3014 3015 int 3016 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen, 3017 const void *secsym, int seclen) 3018 { 3019 uint8_t ebuf[256]; 3020 int i; 3021 3022 /* load primary code and run it */ 3023 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD); 3024 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0)) 3025 return EIO; 3026 wi_symbol_set_hcr(sc, WI_HCR_RUN); 3027 for (i = 0; ; i++) { 3028 if (i == 10) 3029 return ETIMEDOUT; 3030 tsleep(sc, 0, "wiinit", 1); 3031 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT) 3032 break; 3033 /* write the magic key value to unlock aux port */ 3034 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0); 3035 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1); 3036 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2); 3037 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL); 3038 } 3039 3040 /* issue read EEPROM command: XXX copied from wi_cmd() */ 3041 CSR_WRITE_2(sc, WI_PARAM0, 0); 3042 CSR_WRITE_2(sc, WI_PARAM1, 0); 3043 CSR_WRITE_2(sc, WI_PARAM2, 0); 3044 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE); 3045 for (i = 0; i < WI_TIMEOUT; i++) { 3046 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 3047 break; 3048 DELAY(1); 3049 } 3050 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 3051 3052 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ); 3053 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ); 3054 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA, 3055 (uint16_t *)ebuf, sizeof(ebuf) / 2); 3056 if (GETLE16(ebuf) > sizeof(ebuf)) 3057 return EIO; 3058 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf))) 3059 return EIO; 3060 return 0; 3061 } 3062 3063 static int 3064 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen, 3065 const void *ebuf, int ebuflen) 3066 { 3067 const uint8_t *p, *ep, *q, *eq; 3068 char *tp; 3069 uint32_t addr, id, eid; 3070 int i, len, elen, nblk, pdrlen; 3071 3072 /* 3073 * Parse the header of the firmware image. 3074 */ 3075 p = buf; 3076 ep = p + buflen; 3077 while (p < ep && *p++ != ' '); /* FILE: */ 3078 while (p < ep && *p++ != ' '); /* filename */ 3079 while (p < ep && *p++ != ' '); /* type of the firmware */ 3080 nblk = strtoul(p, &tp, 10); 3081 p = tp; 3082 pdrlen = strtoul(p + 1, &tp, 10); 3083 p = tp; 3084 while (p < ep && *p++ != 0x1a); /* skip rest of header */ 3085 3086 /* 3087 * Block records: address[4], length[2], data[length]; 3088 */ 3089 for (i = 0; i < nblk; i++) { 3090 addr = GETLE32(p); p += 4; 3091 len = GETLE16(p); p += 2; 3092 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3093 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3094 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3095 (const uint16_t *)p, len / 2); 3096 p += len; 3097 } 3098 3099 /* 3100 * PDR: id[4], address[4], length[4]; 3101 */ 3102 for (i = 0; i < pdrlen; ) { 3103 id = GETLE32(p); p += 4; i += 4; 3104 addr = GETLE32(p); p += 4; i += 4; 3105 len = GETLE32(p); p += 4; i += 4; 3106 /* replace PDR entry with the values from EEPROM, if any */ 3107 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) { 3108 elen = GETLE16(q); q += 2; 3109 eid = GETLE16(q); q += 2; 3110 elen--; /* elen includes eid */ 3111 if (eid == 0) 3112 break; 3113 if (eid != id) 3114 continue; 3115 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3116 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3117 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3118 (const uint16_t *)q, len / 2); 3119 break; 3120 } 3121 } 3122 return 0; 3123 } 3124 3125 static int 3126 wi_symbol_set_hcr(struct wi_softc *sc, int mode) 3127 { 3128 uint16_t hcr; 3129 3130 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET); 3131 tsleep(sc, 0, "wiinit", 1); 3132 hcr = CSR_READ_2(sc, WI_HCR); 3133 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE); 3134 CSR_WRITE_2(sc, WI_HCR, hcr); 3135 tsleep(sc, 0, "wiinit", 1); 3136 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE); 3137 tsleep(sc, 0, "wiinit", 1); 3138 return 0; 3139 } 3140