1 /* $NetBSD: if_ural.c,v 1.5 2005/07/12 12:51:03 drochner Exp $ */ 2 /* $OpenBSD: if_ral.c,v 1.38 2005/07/07 08:33:22 jsg Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.9 2005/07/08 19:19:06 damien Exp $ */ 4 5 /*- 6 * Copyright (c) 2005 7 * Damien Bergamini <damien.bergamini@free.fr> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 /*- 23 * Ralink Technology RT2500USB chipset driver 24 * http://www.ralinktech.com/ 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: if_ural.c,v 1.5 2005/07/12 12:51:03 drochner Exp $"); 29 30 #include "bpfilter.h" 31 32 #include <sys/param.h> 33 #include <sys/sockio.h> 34 #include <sys/sysctl.h> 35 #include <sys/mbuf.h> 36 #include <sys/kernel.h> 37 #include <sys/socket.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/conf.h> 41 #include <sys/device.h> 42 43 #include <machine/bus.h> 44 #include <machine/endian.h> 45 #include <machine/intr.h> 46 47 #if NBPFILTER > 0 48 #include <net/bpf.h> 49 #endif 50 #include <net/if.h> 51 #include <net/if_arp.h> 52 #include <net/if_dl.h> 53 #include <net/if_ether.h> 54 #include <net/if_media.h> 55 #include <net/if_types.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip.h> 61 62 #include <net80211/ieee80211_netbsd.h> 63 #include <net80211/ieee80211_var.h> 64 #include <net80211/ieee80211_radiotap.h> 65 66 #include <dev/usb/usb.h> 67 #include <dev/usb/usbdi.h> 68 #include <dev/usb/usbdi_util.h> 69 #include <dev/usb/usbdevs.h> 70 71 #include <dev/usb/if_uralreg.h> 72 #include <dev/usb/if_uralvar.h> 73 74 #ifdef USB_DEBUG 75 #define URAL_DEBUG 76 #endif 77 78 #ifdef URAL_DEBUG 79 #define DPRINTF(x) do { if (ural_debug) logprintf x; } while (0) 80 #define DPRINTFN(n, x) do { if (ural_debug >= (n)) logprintf x; } while (0) 81 int ural_debug = 0; 82 #else 83 #define DPRINTF(x) 84 #define DPRINTFN(n, x) 85 #endif 86 87 /* various supported device vendors/products */ 88 static const struct usb_devno ural_devs[] = { 89 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G }, 90 { USB_VENDOR_ASUSTEK, USB_PRODUCT_RALINK_RT2570 }, 91 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 }, 92 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G }, 93 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP }, 94 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU }, 95 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122 }, 96 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG }, 97 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254 }, 98 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 }, 99 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI }, 100 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB }, 101 { USB_VENDOR_MSI, USB_PRODUCT_MSI_MS6861 }, 102 { USB_VENDOR_MSI, USB_PRODUCT_MSI_MS6865 }, 103 { USB_VENDOR_MSI, USB_PRODUCT_MSI_MS6869 }, 104 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570 }, 105 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2 }, 106 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_3 }, 107 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG }, 108 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_EP9001G }, 109 { USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570 }, 110 { USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_ZWXG261 }, 111 }; 112 113 Static int ural_alloc_tx_list(struct ural_softc *); 114 Static void ural_free_tx_list(struct ural_softc *); 115 Static int ural_alloc_rx_list(struct ural_softc *); 116 Static void ural_free_rx_list(struct ural_softc *); 117 Static int ural_key_alloc(struct ieee80211com *, 118 const struct ieee80211_key *); 119 Static int ural_media_change(struct ifnet *); 120 Static void ural_next_scan(void *); 121 Static void ural_task(void *); 122 Static int ural_newstate(struct ieee80211com *, 123 enum ieee80211_state, int); 124 Static void ural_txeof(usbd_xfer_handle, usbd_private_handle, 125 usbd_status); 126 Static void ural_rxeof(usbd_xfer_handle, usbd_private_handle, 127 usbd_status); 128 Static int ural_ack_rate(int); 129 Static uint16_t ural_txtime(int, int, uint32_t); 130 Static uint8_t ural_plcp_signal(int); 131 Static void ural_setup_tx_desc(struct ural_softc *, 132 struct ural_tx_desc *, uint32_t, int, int); 133 Static int ural_tx_bcn(struct ural_softc *, struct mbuf *, 134 struct ieee80211_node *); 135 Static int ural_tx_mgt(struct ural_softc *, struct mbuf *, 136 struct ieee80211_node *); 137 Static int ural_tx_data(struct ural_softc *, struct mbuf *, 138 struct ieee80211_node *); 139 Static void ural_start(struct ifnet *); 140 Static void ural_watchdog(struct ifnet *); 141 Static int ural_ioctl(struct ifnet *, u_long, caddr_t); 142 Static void ural_eeprom_read(struct ural_softc *, uint16_t, void *, 143 int); 144 Static uint16_t ural_read(struct ural_softc *, uint16_t); 145 Static void ural_read_multi(struct ural_softc *, uint16_t, void *, 146 int); 147 Static void ural_write(struct ural_softc *, uint16_t, uint16_t); 148 Static void ural_write_multi(struct ural_softc *, uint16_t, void *, 149 int); 150 Static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t); 151 Static uint8_t ural_bbp_read(struct ural_softc *, uint8_t); 152 Static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t); 153 Static void ural_set_chan(struct ural_softc *, 154 struct ieee80211_channel *); 155 Static void ural_disable_rf_tune(struct ural_softc *); 156 Static void ural_enable_tsf_sync(struct ural_softc *); 157 Static void ural_set_bssid(struct ural_softc *, uint8_t *); 158 Static void ural_set_macaddr(struct ural_softc *, uint8_t *); 159 Static void ural_update_promisc(struct ural_softc *); 160 Static const char *ural_get_rf(int); 161 Static void ural_read_eeprom(struct ural_softc *); 162 Static int ural_bbp_init(struct ural_softc *); 163 Static void ural_set_txantenna(struct ural_softc *, int); 164 Static void ural_set_rxantenna(struct ural_softc *, int); 165 Static int ural_init(struct ifnet *); 166 Static void ural_stop(struct ifnet *, int); 167 168 /* 169 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 170 */ 171 static const struct ieee80211_rateset ural_rateset_11a = 172 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 173 174 static const struct ieee80211_rateset ural_rateset_11b = 175 { 4, { 2, 4, 11, 22 } }; 176 177 static const struct ieee80211_rateset ural_rateset_11g = 178 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 179 180 /* 181 * Default values for MAC registers; values taken from the reference driver. 182 */ 183 static const struct { 184 uint16_t reg; 185 uint16_t val; 186 } ural_def_mac[] = { 187 { RAL_TXRX_CSR5, 0x8c8d }, 188 { RAL_TXRX_CSR6, 0x8b8a }, 189 { RAL_TXRX_CSR7, 0x8687 }, 190 { RAL_TXRX_CSR8, 0x0085 }, 191 { RAL_MAC_CSR13, 0x1111 }, 192 { RAL_MAC_CSR14, 0x1e11 }, 193 { RAL_TXRX_CSR21, 0xe78f }, 194 { RAL_MAC_CSR9, 0xff1d }, 195 { RAL_MAC_CSR11, 0x0002 }, 196 { RAL_MAC_CSR22, 0x0053 }, 197 { RAL_MAC_CSR15, 0x0000 }, 198 { RAL_MAC_CSR8, 0x0780 }, 199 { RAL_TXRX_CSR19, 0x0000 }, 200 { RAL_TXRX_CSR18, 0x005a }, 201 { RAL_PHY_CSR2, 0x0000 }, 202 { RAL_TXRX_CSR0, 0x1ec0 }, 203 { RAL_PHY_CSR4, 0x000f } 204 }; 205 206 /* 207 * Default values for BBP registers; values taken from the reference driver. 208 */ 209 static const struct { 210 uint8_t reg; 211 uint8_t val; 212 } ural_def_bbp[] = { 213 { 3, 0x02 }, 214 { 4, 0x19 }, 215 { 14, 0x1c }, 216 { 15, 0x30 }, 217 { 16, 0xac }, 218 { 17, 0x48 }, 219 { 18, 0x18 }, 220 { 19, 0xff }, 221 { 20, 0x1e }, 222 { 21, 0x08 }, 223 { 22, 0x08 }, 224 { 23, 0x08 }, 225 { 24, 0x80 }, 226 { 25, 0x50 }, 227 { 26, 0x08 }, 228 { 27, 0x23 }, 229 { 30, 0x10 }, 230 { 31, 0x2b }, 231 { 32, 0xb9 }, 232 { 34, 0x12 }, 233 { 35, 0x50 }, 234 { 39, 0xc4 }, 235 { 40, 0x02 }, 236 { 41, 0x60 }, 237 { 53, 0x10 }, 238 { 54, 0x18 }, 239 { 56, 0x08 }, 240 { 57, 0x10 }, 241 { 58, 0x08 }, 242 { 61, 0x60 }, 243 { 62, 0x10 }, 244 { 75, 0xff } 245 }; 246 247 /* 248 * Default values for RF register R2 indexed by channel numbers. 249 */ 250 static const uint32_t ural_rf2522_r2[] = { 251 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814, 252 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e 253 }; 254 255 static const uint32_t ural_rf2523_r2[] = { 256 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 257 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 258 }; 259 260 static const uint32_t ural_rf2524_r2[] = { 261 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 262 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 263 }; 264 265 static const uint32_t ural_rf2525_r2[] = { 266 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d, 267 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346 268 }; 269 270 static const uint32_t ural_rf2525_hi_r2[] = { 271 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345, 272 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e 273 }; 274 275 static const uint32_t ural_rf2525e_r2[] = { 276 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463, 277 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b 278 }; 279 280 static const uint32_t ural_rf2526_hi_r2[] = { 281 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d, 282 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241 283 }; 284 285 static const uint32_t ural_rf2526_r2[] = { 286 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229, 287 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d 288 }; 289 290 /* 291 * For dual-band RF, RF registers R1 and R4 also depend on channel number; 292 * values taken from the reference driver. 293 */ 294 static const struct { 295 uint8_t chan; 296 uint32_t r1; 297 uint32_t r2; 298 uint32_t r4; 299 } ural_rf5222[] = { 300 /* channels in the 2.4GHz band */ 301 { 1, 0x08808, 0x0044d, 0x00282 }, 302 { 2, 0x08808, 0x0044e, 0x00282 }, 303 { 3, 0x08808, 0x0044f, 0x00282 }, 304 { 4, 0x08808, 0x00460, 0x00282 }, 305 { 5, 0x08808, 0x00461, 0x00282 }, 306 { 6, 0x08808, 0x00462, 0x00282 }, 307 { 7, 0x08808, 0x00463, 0x00282 }, 308 { 8, 0x08808, 0x00464, 0x00282 }, 309 { 9, 0x08808, 0x00465, 0x00282 }, 310 { 10, 0x08808, 0x00466, 0x00282 }, 311 { 11, 0x08808, 0x00467, 0x00282 }, 312 { 12, 0x08808, 0x00468, 0x00282 }, 313 { 13, 0x08808, 0x00469, 0x00282 }, 314 { 14, 0x08808, 0x0046b, 0x00286 }, 315 316 /* channels in the 5.2GHz band */ 317 { 36, 0x08804, 0x06225, 0x00287 }, 318 { 40, 0x08804, 0x06226, 0x00287 }, 319 { 44, 0x08804, 0x06227, 0x00287 }, 320 { 48, 0x08804, 0x06228, 0x00287 }, 321 { 52, 0x08804, 0x06229, 0x00287 }, 322 { 56, 0x08804, 0x0622a, 0x00287 }, 323 { 60, 0x08804, 0x0622b, 0x00287 }, 324 { 64, 0x08804, 0x0622c, 0x00287 }, 325 326 { 100, 0x08804, 0x02200, 0x00283 }, 327 { 104, 0x08804, 0x02201, 0x00283 }, 328 { 108, 0x08804, 0x02202, 0x00283 }, 329 { 112, 0x08804, 0x02203, 0x00283 }, 330 { 116, 0x08804, 0x02204, 0x00283 }, 331 { 120, 0x08804, 0x02205, 0x00283 }, 332 { 124, 0x08804, 0x02206, 0x00283 }, 333 { 128, 0x08804, 0x02207, 0x00283 }, 334 { 132, 0x08804, 0x02208, 0x00283 }, 335 { 136, 0x08804, 0x02209, 0x00283 }, 336 { 140, 0x08804, 0x0220a, 0x00283 }, 337 338 { 149, 0x08808, 0x02429, 0x00281 }, 339 { 153, 0x08808, 0x0242b, 0x00281 }, 340 { 157, 0x08808, 0x0242d, 0x00281 }, 341 { 161, 0x08808, 0x0242f, 0x00281 } 342 }; 343 344 USB_DECLARE_DRIVER(ural); 345 346 USB_MATCH(ural) 347 { 348 USB_MATCH_START(ural, uaa); 349 350 if (uaa->iface != NULL) 351 return UMATCH_NONE; 352 353 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ? 354 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 355 } 356 357 USB_ATTACH(ural) 358 { 359 USB_ATTACH_START(ural, sc, uaa); 360 struct ieee80211com *ic = &sc->sc_ic; 361 struct ifnet *ifp = &sc->sc_if; 362 usb_interface_descriptor_t *id; 363 usb_endpoint_descriptor_t *ed; 364 usbd_status error; 365 char *devinfop; 366 int i; 367 368 sc->sc_udev = uaa->device; 369 370 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 371 USB_ATTACH_SETUP; 372 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); 373 usbd_devinfo_free(devinfop); 374 375 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) { 376 printf("%s: could not set configuration no\n", 377 USBDEVNAME(sc->sc_dev)); 378 USB_ATTACH_ERROR_RETURN; 379 } 380 381 /* get the first interface handle */ 382 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX, 383 &sc->sc_iface); 384 if (error != 0) { 385 printf("%s: could not get interface handle\n", 386 USBDEVNAME(sc->sc_dev)); 387 USB_ATTACH_ERROR_RETURN; 388 } 389 390 /* 391 * Find endpoints. 392 */ 393 id = usbd_get_interface_descriptor(sc->sc_iface); 394 395 sc->sc_rx_no = sc->sc_tx_no = -1; 396 for (i = 0; i < id->bNumEndpoints; i++) { 397 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 398 if (ed == NULL) { 399 printf("%s: no endpoint descriptor for iface %d\n", 400 USBDEVNAME(sc->sc_dev), i); 401 USB_ATTACH_ERROR_RETURN; 402 } 403 404 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 405 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 406 sc->sc_rx_no = ed->bEndpointAddress; 407 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 408 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 409 sc->sc_tx_no = ed->bEndpointAddress; 410 } 411 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) { 412 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev)); 413 USB_ATTACH_ERROR_RETURN; 414 } 415 416 usb_init_task(&sc->sc_task, ural_task, sc); 417 callout_init(&sc->scan_ch); 418 419 /* retrieve RT2570 rev. no */ 420 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0); 421 422 /* retrieve MAC address and various other things from EEPROM */ 423 ural_read_eeprom(sc); 424 425 printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s, address %s\n", 426 USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev), 427 ether_sprintf(ic->ic_myaddr)); 428 429 ic->ic_ifp = ifp; 430 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 431 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 432 ic->ic_state = IEEE80211_S_INIT; 433 434 /* set device capabilities */ 435 ic->ic_caps = IEEE80211_C_MONITOR | IEEE80211_C_IBSS | 436 IEEE80211_C_HOSTAP | IEEE80211_C_SHPREAMBLE | IEEE80211_C_SHSLOT | 437 IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | IEEE80211_C_WPA; 438 439 if (sc->rf_rev == RAL_RF_5222) { 440 /* set supported .11a rates */ 441 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a; 442 443 /* set supported .11a channels */ 444 for (i = 36; i <= 64; i += 4) { 445 ic->ic_channels[i].ic_freq = 446 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 447 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 448 } 449 for (i = 100; i <= 140; i += 4) { 450 ic->ic_channels[i].ic_freq = 451 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 452 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 453 } 454 for (i = 149; i <= 161; i += 4) { 455 ic->ic_channels[i].ic_freq = 456 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 457 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 458 } 459 } 460 461 /* set supported .11b and .11g rates */ 462 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b; 463 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g; 464 465 /* set supported .11b and .11g channels (1 through 14) */ 466 for (i = 1; i <= 14; i++) { 467 ic->ic_channels[i].ic_freq = 468 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 469 ic->ic_channels[i].ic_flags = 470 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 471 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 472 } 473 474 ifp->if_softc = sc; 475 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 476 ifp->if_init = ural_init; 477 ifp->if_stop = ural_stop; 478 ifp->if_ioctl = ural_ioctl; 479 ifp->if_start = ural_start; 480 ifp->if_watchdog = ural_watchdog; 481 IFQ_SET_READY(&ifp->if_snd); 482 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ); 483 484 if_attach(ifp); 485 ieee80211_ifattach(ic); 486 487 /* override state transition machine */ 488 sc->sc_newstate = ic->ic_newstate; 489 ic->ic_newstate = ural_newstate; 490 ic->ic_crypto.cs_key_alloc = ural_key_alloc; 491 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status); 492 493 #if NBPFILTER > 0 494 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 495 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf); 496 497 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 498 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 499 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT); 500 501 sc->sc_txtap_len = sizeof sc->sc_txtapu; 502 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 503 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT); 504 #endif 505 506 ieee80211_announce(ic); 507 508 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 509 USBDEV(sc->sc_dev)); 510 511 USB_ATTACH_SUCCESS_RETURN; 512 } 513 514 USB_DETACH(ural) 515 { 516 USB_DETACH_START(ural, sc); 517 struct ieee80211com *ic = &sc->sc_ic; 518 struct ifnet *ifp = &sc->sc_if; 519 int s; 520 521 s = splusb(); 522 523 usb_rem_task(sc->sc_udev, &sc->sc_task); 524 callout_stop(&sc->scan_ch); 525 526 if (sc->sc_rx_pipeh != NULL) { 527 usbd_abort_pipe(sc->sc_rx_pipeh); 528 usbd_close_pipe(sc->sc_rx_pipeh); 529 } 530 531 if (sc->sc_tx_pipeh != NULL) { 532 usbd_abort_pipe(sc->sc_tx_pipeh); 533 usbd_close_pipe(sc->sc_tx_pipeh); 534 } 535 536 ural_free_rx_list(sc); 537 ural_free_tx_list(sc); 538 539 #if NBPFILTER > 0 540 bpfdetach(ifp); 541 #endif 542 ieee80211_ifdetach(ic); 543 if_detach(ifp); 544 545 splx(s); 546 547 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 548 USBDEV(sc->sc_dev)); 549 550 return 0; 551 } 552 553 Static int 554 ural_alloc_tx_list(struct ural_softc *sc) 555 { 556 struct ural_tx_data *data; 557 int i, error; 558 559 sc->tx_queued = 0; 560 561 for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 562 data = &sc->tx_data[i]; 563 564 data->sc = sc; 565 566 data->xfer = usbd_alloc_xfer(sc->sc_udev); 567 if (data->xfer == NULL) { 568 printf("%s: could not allocate tx xfer\n", 569 USBDEVNAME(sc->sc_dev)); 570 error = ENOMEM; 571 goto fail; 572 } 573 574 data->buf = usbd_alloc_buffer(data->xfer, 575 RAL_TX_DESC_SIZE + MCLBYTES); 576 if (data->buf == NULL) { 577 printf("%s: could not allocate tx buffer\n", 578 USBDEVNAME(sc->sc_dev)); 579 error = ENOMEM; 580 goto fail; 581 } 582 } 583 584 return 0; 585 586 fail: ural_free_tx_list(sc); 587 return error; 588 } 589 590 Static void 591 ural_free_tx_list(struct ural_softc *sc) 592 { 593 struct ural_tx_data *data; 594 int i; 595 596 for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 597 data = &sc->tx_data[i]; 598 599 if (data->xfer != NULL) { 600 usbd_free_xfer(data->xfer); 601 data->xfer = NULL; 602 } 603 604 if (data->ni != NULL) { 605 ieee80211_free_node(data->ni); 606 data->ni = NULL; 607 } 608 } 609 } 610 611 Static int 612 ural_alloc_rx_list(struct ural_softc *sc) 613 { 614 struct ural_rx_data *data; 615 int i, error; 616 617 for (i = 0; i < RAL_RX_LIST_COUNT; i++) { 618 data = &sc->rx_data[i]; 619 620 data->sc = sc; 621 622 data->xfer = usbd_alloc_xfer(sc->sc_udev); 623 if (data->xfer == NULL) { 624 printf("%s: could not allocate rx xfer\n", 625 USBDEVNAME(sc->sc_dev)); 626 error = ENOMEM; 627 goto fail; 628 } 629 630 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) { 631 printf("%s: could not allocate rx buffer\n", 632 USBDEVNAME(sc->sc_dev)); 633 error = ENOMEM; 634 goto fail; 635 } 636 637 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 638 if (data->m == NULL) { 639 printf("%s: could not allocate rx mbuf\n", 640 USBDEVNAME(sc->sc_dev)); 641 error = ENOMEM; 642 goto fail; 643 } 644 645 MCLGET(data->m, M_DONTWAIT); 646 if (!(data->m->m_flags & M_EXT)) { 647 printf("%s: could not allocate rx mbuf cluster\n", 648 USBDEVNAME(sc->sc_dev)); 649 error = ENOMEM; 650 goto fail; 651 } 652 653 data->buf = mtod(data->m, uint8_t *); 654 } 655 656 return 0; 657 658 fail: ural_free_tx_list(sc); 659 return error; 660 } 661 662 Static void 663 ural_free_rx_list(struct ural_softc *sc) 664 { 665 struct ural_rx_data *data; 666 int i; 667 668 for (i = 0; i < RAL_RX_LIST_COUNT; i++) { 669 data = &sc->rx_data[i]; 670 671 if (data->xfer != NULL) { 672 usbd_free_xfer(data->xfer); 673 data->xfer = NULL; 674 } 675 676 if (data->m != NULL) { 677 m_freem(data->m); 678 data->m = NULL; 679 } 680 } 681 } 682 683 Static int 684 ural_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k) 685 { 686 if (k >= ic->ic_nw_keys && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID]) 687 return k - ic->ic_nw_keys; 688 689 return IEEE80211_KEYIX_NONE; 690 } 691 692 Static int 693 ural_media_change(struct ifnet *ifp) 694 { 695 int error; 696 697 error = ieee80211_media_change(ifp); 698 if (error != ENETRESET) 699 return error; 700 701 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 702 ural_init(ifp); 703 704 return 0; 705 } 706 707 /* 708 * This function is called periodically (every 200ms) during scanning to 709 * switch from one channel to another. 710 */ 711 Static void 712 ural_next_scan(void *arg) 713 { 714 struct ural_softc *sc = arg; 715 struct ieee80211com *ic = &sc->sc_ic; 716 717 if (ic->ic_state == IEEE80211_S_SCAN) 718 ieee80211_next_scan(ic); 719 } 720 721 Static void 722 ural_task(void *arg) 723 { 724 struct ural_softc *sc = arg; 725 struct ieee80211com *ic = &sc->sc_ic; 726 enum ieee80211_state ostate; 727 struct mbuf *m; 728 729 ostate = ic->ic_state; 730 731 switch (sc->sc_state) { 732 case IEEE80211_S_INIT: 733 if (ostate == IEEE80211_S_RUN) { 734 /* abort TSF synchronization */ 735 ural_write(sc, RAL_TXRX_CSR19, 0); 736 737 /* force tx led to stop blinking */ 738 ural_write(sc, RAL_MAC_CSR20, 0); 739 } 740 break; 741 742 case IEEE80211_S_SCAN: 743 ural_set_chan(sc, ic->ic_bss->ni_chan); 744 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc); 745 break; 746 747 case IEEE80211_S_AUTH: 748 ural_set_chan(sc, ic->ic_bss->ni_chan); 749 break; 750 751 case IEEE80211_S_ASSOC: 752 ural_set_chan(sc, ic->ic_bss->ni_chan); 753 break; 754 755 case IEEE80211_S_RUN: 756 ural_set_chan(sc, ic->ic_bss->ni_chan); 757 758 if (ic->ic_opmode != IEEE80211_M_MONITOR) 759 ural_set_bssid(sc, ic->ic_bss->ni_bssid); 760 761 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 762 ic->ic_opmode == IEEE80211_M_IBSS) { 763 m = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo); 764 if (m == NULL) { 765 printf("%s: could not allocate beacon\n", 766 USBDEVNAME(sc->sc_dev)); 767 return; 768 } 769 770 if (ural_tx_bcn(sc, m, ic->ic_bss) != 0) { 771 m_freem(m); 772 printf("%s: could not transmit beacon\n", 773 USBDEVNAME(sc->sc_dev)); 774 return; 775 } 776 777 /* beacon is no longer needed */ 778 m_freem(m); 779 } 780 781 /* make tx led blink on tx (controlled by ASIC) */ 782 ural_write(sc, RAL_MAC_CSR20, 1); 783 784 if (ic->ic_opmode != IEEE80211_M_MONITOR) 785 ural_enable_tsf_sync(sc); 786 break; 787 } 788 789 sc->sc_newstate(ic, sc->sc_state, -1); 790 } 791 792 Static int 793 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 794 { 795 struct ural_softc *sc = ic->ic_ifp->if_softc; 796 797 usb_rem_task(sc->sc_udev, &sc->sc_task); 798 callout_stop(&sc->scan_ch); 799 800 /* do it in a process context */ 801 sc->sc_state = nstate; 802 usb_add_task(sc->sc_udev, &sc->sc_task); 803 804 return 0; 805 } 806 807 /* quickly determine if a given rate is CCK or OFDM */ 808 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 809 810 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */ 811 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */ 812 #define RAL_SIFS 10 813 814 Static void 815 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 816 { 817 struct ural_tx_data *data = priv; 818 struct ural_softc *sc = data->sc; 819 struct ifnet *ifp = &sc->sc_if; 820 int s; 821 822 if (status != USBD_NORMAL_COMPLETION) { 823 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 824 return; 825 826 printf("%s: could not transmit buffer: %s\n", 827 USBDEVNAME(sc->sc_dev), usbd_errstr(status)); 828 829 if (status == USBD_STALLED) 830 usbd_clear_endpoint_stall(sc->sc_tx_pipeh); 831 832 ifp->if_oerrors++; 833 return; 834 } 835 836 s = splnet(); 837 838 m_freem(data->m); 839 data->m = NULL; 840 ieee80211_free_node(data->ni); 841 data->ni = NULL; 842 843 sc->tx_queued--; 844 ifp->if_opackets++; 845 846 DPRINTFN(10, ("tx done\n")); 847 848 sc->sc_tx_timer = 0; 849 ifp->if_flags &= ~IFF_OACTIVE; 850 ural_start(ifp); 851 852 splx(s); 853 } 854 855 Static void 856 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 857 { 858 struct ural_rx_data *data = priv; 859 struct ural_softc *sc = data->sc; 860 struct ieee80211com *ic = &sc->sc_ic; 861 struct ifnet *ifp = &sc->sc_if; 862 struct ural_rx_desc *desc; 863 struct ieee80211_frame_min *wh; 864 struct ieee80211_node *ni; 865 struct mbuf *m; 866 int s, len; 867 868 if (status != USBD_NORMAL_COMPLETION) { 869 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 870 return; 871 872 if (status == USBD_STALLED) 873 usbd_clear_endpoint_stall(sc->sc_rx_pipeh); 874 goto skip; 875 } 876 877 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 878 879 if (len < RAL_RX_DESC_SIZE) { 880 printf("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev), len); 881 ifp->if_ierrors++; 882 goto skip; 883 } 884 885 /* rx descriptor is located at the end */ 886 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE); 887 888 if (le32toh(desc->flags) & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) { 889 /* 890 * This should not happen since we did not request to receive 891 * those frames when we filled RAL_TXRX_CSR2. 892 */ 893 DPRINTFN(5, ("PHY or CRC error\n")); 894 ifp->if_ierrors++; 895 goto skip; 896 } 897 898 /* finalize mbuf */ 899 m = data->m; 900 m->m_pkthdr.rcvif = ifp; 901 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff; 902 m->m_flags |= M_HASFCS; /* hardware appends FCS */ 903 904 s = splnet(); 905 906 #if NBPFILTER > 0 907 if (sc->sc_drvbpf != NULL) { 908 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap; 909 910 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 911 tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 912 tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 913 tap->wr_antenna = sc->rx_ant; 914 tap->wr_antsignal = desc->rssi; 915 916 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 917 } 918 #endif 919 920 wh = mtod(m, struct ieee80211_frame_min *); 921 ni = ieee80211_find_rxnode(ic, wh); 922 923 /* send the frame to the 802.11 layer */ 924 ieee80211_input(ic, m, ni, desc->rssi, 0); 925 926 /* node is no longer needed */ 927 ieee80211_free_node(ni); 928 929 splx(s); 930 931 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 932 if (data->m == NULL) { 933 printf("%s: could not allocate rx mbuf\n", 934 USBDEVNAME(sc->sc_dev)); 935 return; 936 } 937 938 MCLGET(data->m, M_DONTWAIT); 939 if (!(data->m->m_flags & M_EXT)) { 940 printf("%s: could not allocate rx mbuf cluster\n", 941 USBDEVNAME(sc->sc_dev)); 942 m_freem(data->m); 943 data->m = NULL; 944 return; 945 } 946 947 data->buf = mtod(data->m, uint8_t *); 948 949 DPRINTFN(15, ("rx done\n")); 950 951 skip: /* setup a new transfer */ 952 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES, 953 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof); 954 usbd_transfer(xfer); 955 } 956 957 /* 958 * Return the expected ack rate for a frame transmitted at rate `rate'. 959 * XXX: this should depend on the destination node basic rate set. 960 */ 961 Static int 962 ural_ack_rate(int rate) 963 { 964 switch (rate) { 965 /* CCK rates */ 966 case 2: 967 return 2; 968 case 4: 969 case 11: 970 case 22: 971 return 4; 972 973 /* OFDM rates */ 974 case 12: 975 case 18: 976 return 12; 977 case 24: 978 case 36: 979 return 24; 980 case 48: 981 case 72: 982 case 96: 983 case 108: 984 return 48; 985 } 986 987 /* default to 1Mbps */ 988 return 2; 989 } 990 991 /* 992 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'. 993 * The function automatically determines the operating mode depending on the 994 * given rate. `flags' indicates whether short preamble is in use or not. 995 */ 996 Static uint16_t 997 ural_txtime(int len, int rate, uint32_t flags) 998 { 999 uint16_t txtime; 1000 int ceil, dbps; 1001 1002 if (RAL_RATE_IS_OFDM(rate)) { 1003 /* 1004 * OFDM TXTIME calculation. 1005 * From IEEE Std 802.11a-1999, pp. 37. 1006 */ 1007 dbps = rate * 2; /* data bits per OFDM symbol */ 1008 1009 ceil = (16 + 8 * len + 6) / dbps; 1010 if ((16 + 8 * len + 6) % dbps != 0) 1011 ceil++; 1012 1013 txtime = 16 + 4 + 4 * ceil + 6; 1014 } else { 1015 /* 1016 * High Rate TXTIME calculation. 1017 * From IEEE Std 802.11b-1999, pp. 28. 1018 */ 1019 ceil = (8 * len * 2) / rate; 1020 if ((8 * len * 2) % rate != 0) 1021 ceil++; 1022 1023 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) 1024 txtime = 72 + 24 + ceil; 1025 else 1026 txtime = 144 + 48 + ceil; 1027 } 1028 1029 return txtime; 1030 } 1031 1032 Static uint8_t 1033 ural_plcp_signal(int rate) 1034 { 1035 switch (rate) { 1036 /* CCK rates (returned values are device-dependent) */ 1037 case 2: return 0x0; 1038 case 4: return 0x1; 1039 case 11: return 0x2; 1040 case 22: return 0x3; 1041 1042 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1043 case 12: return 0xb; 1044 case 18: return 0xf; 1045 case 24: return 0xa; 1046 case 36: return 0xe; 1047 case 48: return 0x9; 1048 case 72: return 0xd; 1049 case 96: return 0x8; 1050 case 108: return 0xc; 1051 1052 /* unsupported rates (should not get there) */ 1053 default: return 0xff; 1054 } 1055 } 1056 1057 Static void 1058 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc, 1059 uint32_t flags, int len, int rate) 1060 { 1061 struct ieee80211com *ic = &sc->sc_ic; 1062 uint16_t plcp_length; 1063 int remainder; 1064 1065 desc->flags = htole32(flags); 1066 desc->flags |= htole32(RAL_TX_NEWSEQ); 1067 desc->flags |= htole32(len << 16); 1068 1069 if (RAL_RATE_IS_OFDM(rate)) 1070 desc->flags |= htole32(RAL_TX_OFDM); 1071 1072 desc->wme = htole16(RAL_LOGCWMAX(5) | RAL_LOGCWMIN(3) | RAL_AIFSN(2)); 1073 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame))); 1074 1075 /* 1076 * Fill PLCP fields. 1077 */ 1078 desc->plcp_service = 4; 1079 1080 len += 4; /* account for FCS */ 1081 if (RAL_RATE_IS_OFDM(rate)) { 1082 /* 1083 * PLCP length field (LENGTH). 1084 * From IEEE Std 802.11a-1999, pp. 14. 1085 */ 1086 plcp_length = len & 0xfff; 1087 desc->plcp_length = htole16((plcp_length >> 6) << 8 | 1088 (plcp_length & 0x3f)); 1089 } else { 1090 /* 1091 * Long PLCP LENGTH field. 1092 * From IEEE Std 802.11b-1999, pp. 16. 1093 */ 1094 plcp_length = (8 * len * 2) / rate; 1095 remainder = (8 * len * 2) % rate; 1096 if (remainder != 0) { 1097 if (rate == 22 && (rate - remainder) / 16 != 0) 1098 desc->plcp_service |= RAL_PLCP_LENGEXT; 1099 plcp_length++; 1100 } 1101 desc->plcp_length = htole16(plcp_length); 1102 } 1103 1104 desc->plcp_signal = ural_plcp_signal(rate); 1105 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1106 desc->plcp_signal |= 0x08; 1107 1108 desc->iv = 0; 1109 desc->eiv = 0; 1110 } 1111 1112 #define RAL_TX_TIMEOUT 5000 1113 1114 Static int 1115 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1116 { 1117 struct ural_tx_desc *desc; 1118 usbd_xfer_handle xfer; 1119 usbd_status error; 1120 uint8_t cmd = 0; 1121 uint8_t *buf; 1122 int xferlen, rate; 1123 1124 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4; 1125 1126 xfer = usbd_alloc_xfer(sc->sc_udev); 1127 if (xfer == NULL) 1128 return ENOMEM; 1129 1130 /* xfer length needs to be a multiple of two! */ 1131 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1; 1132 1133 buf = usbd_alloc_buffer(xfer, xferlen); 1134 if (buf == NULL) { 1135 usbd_free_xfer(xfer); 1136 return ENOMEM; 1137 } 1138 1139 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd, 1140 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL); 1141 1142 error = usbd_sync_transfer(xfer); 1143 if (error != 0) { 1144 usbd_free_xfer(xfer); 1145 return error; 1146 } 1147 1148 desc = (struct ural_tx_desc *)buf; 1149 1150 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE); 1151 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, 1152 m0->m_pkthdr.len, rate); 1153 1154 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n", 1155 m0->m_pkthdr.len, rate, xferlen)); 1156 1157 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen, 1158 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL); 1159 1160 error = usbd_sync_transfer(xfer); 1161 usbd_free_xfer(xfer); 1162 1163 return error; 1164 } 1165 1166 Static int 1167 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1168 { 1169 struct ieee80211com *ic = &sc->sc_ic; 1170 struct ural_tx_desc *desc; 1171 struct ural_tx_data *data; 1172 struct ieee80211_frame *wh; 1173 uint32_t flags = 0; 1174 uint16_t dur; 1175 usbd_status error; 1176 int xferlen, rate; 1177 1178 data = &sc->tx_data[0]; 1179 desc = (struct ural_tx_desc *)data->buf; 1180 1181 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4; 1182 1183 #if NBPFILTER > 0 1184 if (sc->sc_drvbpf != NULL) { 1185 struct ural_tx_radiotap_header *tap = &sc->sc_txtap; 1186 1187 tap->wt_flags = 0; 1188 tap->wt_rate = rate; 1189 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1190 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1191 tap->wt_antenna = sc->tx_ant; 1192 1193 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1194 } 1195 #endif 1196 1197 data->m = m0; 1198 data->ni = ni; 1199 1200 wh = mtod(m0, struct ieee80211_frame *); 1201 1202 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1203 flags |= RAL_TX_ACK; 1204 1205 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS; 1206 *(uint16_t *)wh->i_dur = htole16(dur); 1207 1208 /* tell hardware to add timestamp for probe responses */ 1209 if ((wh->i_fc[0] & 1210 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1211 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1212 flags |= RAL_TX_TIMESTAMP; 1213 } 1214 1215 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE); 1216 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate); 1217 1218 /* xfer length needs to be a multiple of two! */ 1219 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1; 1220 1221 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n", 1222 m0->m_pkthdr.len, rate, xferlen)); 1223 1224 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen, 1225 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof); 1226 1227 error = usbd_transfer(data->xfer); 1228 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) { 1229 m_freem(m0); 1230 return error; 1231 } 1232 1233 sc->tx_queued++; 1234 1235 return 0; 1236 } 1237 1238 Static int 1239 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1240 { 1241 struct ieee80211com *ic = &sc->sc_ic; 1242 struct ieee80211_rateset *rs; 1243 struct ural_tx_desc *desc; 1244 struct ural_tx_data *data; 1245 struct ieee80211_frame *wh; 1246 struct ieee80211_key *k; 1247 uint32_t flags = 0; 1248 uint16_t dur; 1249 usbd_status error; 1250 int xferlen, rate; 1251 1252 wh = mtod(m0, struct ieee80211_frame *); 1253 1254 /* XXX this should be reworked! */ 1255 if (ic->ic_fixed_rate != -1) { 1256 if (ic->ic_curmode != IEEE80211_MODE_AUTO) 1257 rs = &ic->ic_sup_rates[ic->ic_curmode]; 1258 else 1259 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 1260 1261 rate = rs->rs_rates[ic->ic_fixed_rate]; 1262 } else { 1263 rs = &ni->ni_rates; 1264 rate = rs->rs_rates[ni->ni_txrate]; 1265 } 1266 rate &= IEEE80211_RATE_VAL; 1267 1268 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1269 k = ieee80211_crypto_encap(ic, ni, m0); 1270 if (k == NULL) { 1271 m_freem(m0); 1272 return ENOBUFS; 1273 } 1274 1275 /* packet header may have moved, reset our local pointer */ 1276 wh = mtod(m0, struct ieee80211_frame *); 1277 } 1278 1279 #if NBPFILTER > 0 1280 if (sc->sc_drvbpf != NULL) { 1281 struct ural_tx_radiotap_header *tap = &sc->sc_txtap; 1282 1283 tap->wt_flags = 0; 1284 tap->wt_rate = rate; 1285 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1286 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1287 tap->wt_antenna = sc->tx_ant; 1288 1289 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1290 } 1291 #endif 1292 1293 data = &sc->tx_data[0]; 1294 desc = (struct ural_tx_desc *)data->buf; 1295 1296 data->m = m0; 1297 data->ni = ni; 1298 1299 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1300 flags |= RAL_TX_ACK; 1301 flags |= RAL_TX_RETRY(7); 1302 1303 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(rate), 1304 ic->ic_flags) + RAL_SIFS; 1305 *(uint16_t *)wh->i_dur = htole16(dur); 1306 } 1307 1308 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE); 1309 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate); 1310 1311 /* xfer length needs to be a multiple of two! */ 1312 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1; 1313 1314 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n", 1315 m0->m_pkthdr.len, rate, xferlen)); 1316 1317 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen, 1318 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof); 1319 1320 error = usbd_transfer(data->xfer); 1321 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) { 1322 m_freem(m0); 1323 return error; 1324 } 1325 1326 sc->tx_queued++; 1327 1328 return 0; 1329 } 1330 1331 Static void 1332 ural_start(struct ifnet *ifp) 1333 { 1334 struct ural_softc *sc = ifp->if_softc; 1335 struct ieee80211com *ic = &sc->sc_ic; 1336 struct ether_header *eh; 1337 struct ieee80211_node *ni; 1338 struct mbuf *m0; 1339 1340 for (;;) { 1341 IF_POLL(&ic->ic_mgtq, m0); 1342 if (m0 != NULL) { 1343 if (sc->tx_queued >= RAL_TX_LIST_COUNT) { 1344 ifp->if_flags |= IFF_OACTIVE; 1345 break; 1346 } 1347 IF_DEQUEUE(&ic->ic_mgtq, m0); 1348 1349 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1350 m0->m_pkthdr.rcvif = NULL; 1351 #if NBPFILTER > 0 1352 if (ic->ic_rawbpf != NULL) 1353 bpf_mtap(ic->ic_rawbpf, m0); 1354 #endif 1355 if (ural_tx_mgt(sc, m0, ni) != 0) 1356 break; 1357 1358 } else { 1359 if (ic->ic_state != IEEE80211_S_RUN) 1360 break; 1361 IFQ_DEQUEUE(&ifp->if_snd, m0); 1362 if (m0 == NULL) 1363 break; 1364 if (sc->tx_queued >= RAL_TX_LIST_COUNT) { 1365 IF_PREPEND(&ifp->if_snd, m0); 1366 ifp->if_flags |= IFF_OACTIVE; 1367 break; 1368 } 1369 1370 if (m0->m_len < sizeof (struct ether_header) && 1371 !(m0 = m_pullup(m0, sizeof (struct ether_header)))) 1372 continue; 1373 1374 eh = mtod(m0, struct ether_header *); 1375 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1376 if (ni == NULL) { 1377 m_freem(m0); 1378 continue; 1379 } 1380 #if NBPFILTER > 0 1381 if (ifp->if_bpf != NULL) 1382 bpf_mtap(ifp->if_bpf, m0); 1383 #endif 1384 m0 = ieee80211_encap(ic, m0, ni); 1385 if (m0 == NULL) { 1386 ieee80211_free_node(ni); 1387 continue; 1388 } 1389 #if NBPFILTER > 0 1390 if (ic->ic_rawbpf != NULL) 1391 bpf_mtap(ic->ic_rawbpf, m0); 1392 #endif 1393 if (ural_tx_data(sc, m0, ni) != 0) { 1394 ieee80211_free_node(ni); 1395 ifp->if_oerrors++; 1396 break; 1397 } 1398 } 1399 1400 sc->sc_tx_timer = 5; 1401 ifp->if_timer = 1; 1402 } 1403 } 1404 1405 Static void 1406 ural_watchdog(struct ifnet *ifp) 1407 { 1408 struct ural_softc *sc = ifp->if_softc; 1409 struct ieee80211com *ic = &sc->sc_ic; 1410 1411 ifp->if_timer = 0; 1412 1413 if (sc->sc_tx_timer > 0) { 1414 if (--sc->sc_tx_timer == 0) { 1415 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev)); 1416 /*ural_init(ifp); XXX needs a process context! */ 1417 ifp->if_oerrors++; 1418 return; 1419 } 1420 ifp->if_timer = 1; 1421 } 1422 1423 ieee80211_watchdog(ic); 1424 } 1425 1426 Static int 1427 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1428 { 1429 struct ural_softc *sc = ifp->if_softc; 1430 struct ieee80211com *ic = &sc->sc_ic; 1431 struct ifreq *ifr; 1432 int s, error = 0; 1433 1434 s = splnet(); 1435 1436 switch (cmd) { 1437 case SIOCSIFFLAGS: 1438 if (ifp->if_flags & IFF_UP) { 1439 if (ifp->if_flags & IFF_RUNNING) 1440 ural_update_promisc(sc); 1441 else 1442 ural_init(ifp); 1443 } else { 1444 if (ifp->if_flags & IFF_RUNNING) 1445 ural_stop(ifp, 1); 1446 } 1447 break; 1448 1449 case SIOCADDMULTI: 1450 case SIOCDELMULTI: 1451 ifr = (struct ifreq *)data; 1452 error = (cmd == SIOCADDMULTI) ? 1453 ether_addmulti(ifr, &sc->sc_ec) : 1454 ether_delmulti(ifr, &sc->sc_ec); 1455 1456 if (error == ENETRESET) 1457 error = 0; 1458 break; 1459 1460 case SIOCS80211CHANNEL: 1461 /* 1462 * This allows for fast channel switching in monitor mode 1463 * (used by kismet). In IBSS mode, we must explicitly reset 1464 * the interface to generate a new beacon frame. 1465 */ 1466 error = ieee80211_ioctl(ic, cmd, data); 1467 if (error == ENETRESET && 1468 ic->ic_opmode == IEEE80211_M_MONITOR) { 1469 ural_set_chan(sc, ic->ic_ibss_chan); 1470 error = 0; 1471 } 1472 break; 1473 1474 default: 1475 error = ieee80211_ioctl(ic, cmd, data); 1476 } 1477 1478 if (error == ENETRESET) { 1479 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1480 (IFF_UP | IFF_RUNNING)) 1481 ural_init(ifp); 1482 error = 0; 1483 } 1484 1485 splx(s); 1486 1487 return error; 1488 } 1489 1490 Static void 1491 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len) 1492 { 1493 usb_device_request_t req; 1494 usbd_status error; 1495 1496 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1497 req.bRequest = RAL_READ_EEPROM; 1498 USETW(req.wValue, 0); 1499 USETW(req.wIndex, addr); 1500 USETW(req.wLength, len); 1501 1502 error = usbd_do_request(sc->sc_udev, &req, buf); 1503 if (error != 0) { 1504 printf("%s: could not read EEPROM: %s\n", 1505 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1506 } 1507 } 1508 1509 Static uint16_t 1510 ural_read(struct ural_softc *sc, uint16_t reg) 1511 { 1512 usb_device_request_t req; 1513 usbd_status error; 1514 uint16_t val; 1515 1516 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1517 req.bRequest = RAL_READ_MAC; 1518 USETW(req.wValue, 0); 1519 USETW(req.wIndex, reg); 1520 USETW(req.wLength, sizeof (uint16_t)); 1521 1522 error = usbd_do_request(sc->sc_udev, &req, &val); 1523 if (error != 0) { 1524 printf("%s: could not read MAC register: %s\n", 1525 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1526 return 0; 1527 } 1528 1529 return le16toh(val); 1530 } 1531 1532 Static void 1533 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 1534 { 1535 usb_device_request_t req; 1536 usbd_status error; 1537 1538 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1539 req.bRequest = RAL_READ_MULTI_MAC; 1540 USETW(req.wValue, 0); 1541 USETW(req.wIndex, reg); 1542 USETW(req.wLength, len); 1543 1544 error = usbd_do_request(sc->sc_udev, &req, buf); 1545 if (error != 0) { 1546 printf("%s: could not read MAC register: %s\n", 1547 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1548 return; 1549 } 1550 } 1551 1552 Static void 1553 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val) 1554 { 1555 usb_device_request_t req; 1556 usbd_status error; 1557 1558 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1559 req.bRequest = RAL_WRITE_MAC; 1560 USETW(req.wValue, val); 1561 USETW(req.wIndex, reg); 1562 USETW(req.wLength, 0); 1563 1564 error = usbd_do_request(sc->sc_udev, &req, NULL); 1565 if (error != 0) { 1566 printf("%s: could not write MAC register: %s\n", 1567 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1568 } 1569 } 1570 1571 Static void 1572 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 1573 { 1574 usb_device_request_t req; 1575 usbd_status error; 1576 1577 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1578 req.bRequest = RAL_WRITE_MULTI_MAC; 1579 USETW(req.wValue, 0); 1580 USETW(req.wIndex, reg); 1581 USETW(req.wLength, len); 1582 1583 error = usbd_do_request(sc->sc_udev, &req, buf); 1584 if (error != 0) { 1585 printf("%s: could not write MAC register: %s\n", 1586 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1587 } 1588 } 1589 1590 Static void 1591 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val) 1592 { 1593 uint16_t tmp; 1594 int ntries; 1595 1596 for (ntries = 0; ntries < 5; ntries++) { 1597 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 1598 break; 1599 } 1600 if (ntries == 5) { 1601 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev)); 1602 return; 1603 } 1604 1605 tmp = reg << 8 | val; 1606 ural_write(sc, RAL_PHY_CSR7, tmp); 1607 } 1608 1609 Static uint8_t 1610 ural_bbp_read(struct ural_softc *sc, uint8_t reg) 1611 { 1612 uint16_t val; 1613 int ntries; 1614 1615 val = RAL_BBP_WRITE | reg << 8; 1616 ural_write(sc, RAL_PHY_CSR7, val); 1617 1618 for (ntries = 0; ntries < 5; ntries++) { 1619 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 1620 break; 1621 } 1622 if (ntries == 5) { 1623 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev)); 1624 return 0; 1625 } 1626 1627 return ural_read(sc, RAL_PHY_CSR7) & 0xff; 1628 } 1629 1630 Static void 1631 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val) 1632 { 1633 uint32_t tmp; 1634 int ntries; 1635 1636 for (ntries = 0; ntries < 5; ntries++) { 1637 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY)) 1638 break; 1639 } 1640 if (ntries == 5) { 1641 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev)); 1642 return; 1643 } 1644 1645 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3); 1646 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff); 1647 ural_write(sc, RAL_PHY_CSR10, tmp >> 16); 1648 1649 /* remember last written value in sc */ 1650 sc->rf_regs[reg] = val; 1651 1652 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff)); 1653 } 1654 1655 Static void 1656 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c) 1657 { 1658 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1659 struct ieee80211com *ic = &sc->sc_ic; 1660 uint8_t power, tmp; 1661 u_int i, chan; 1662 1663 chan = ieee80211_chan2ieee(ic, c); 1664 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1665 return; 1666 1667 if (IEEE80211_IS_CHAN_2GHZ(c)) 1668 power = min(sc->txpow[chan - 1], 31); 1669 else 1670 power = 31; 1671 1672 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power)); 1673 1674 switch (sc->rf_rev) { 1675 case RAL_RF_2522: 1676 ural_rf_write(sc, RAL_RF1, 0x00814); 1677 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]); 1678 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1679 break; 1680 1681 case RAL_RF_2523: 1682 ural_rf_write(sc, RAL_RF1, 0x08804); 1683 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]); 1684 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 1685 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1686 break; 1687 1688 case RAL_RF_2524: 1689 ural_rf_write(sc, RAL_RF1, 0x0c808); 1690 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]); 1691 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1692 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1693 break; 1694 1695 case RAL_RF_2525: 1696 ural_rf_write(sc, RAL_RF1, 0x08808); 1697 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]); 1698 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1699 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1700 1701 ural_rf_write(sc, RAL_RF1, 0x08808); 1702 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]); 1703 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1704 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1705 break; 1706 1707 case RAL_RF_2525E: 1708 ural_rf_write(sc, RAL_RF1, 0x08808); 1709 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]); 1710 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1711 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 1712 break; 1713 1714 case RAL_RF_2526: 1715 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]); 1716 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 1717 ural_rf_write(sc, RAL_RF1, 0x08804); 1718 1719 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]); 1720 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1721 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 1722 break; 1723 1724 /* dual-band RF */ 1725 case RAL_RF_5222: 1726 for (i = 0; i < N(ural_rf5222); i++) 1727 if (ural_rf5222[i].chan == chan) 1728 break; 1729 1730 if (i < N(ural_rf5222)) { 1731 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1); 1732 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2); 1733 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1734 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4); 1735 } 1736 break; 1737 } 1738 1739 if (ic->ic_opmode != IEEE80211_M_MONITOR && 1740 ic->ic_state != IEEE80211_S_SCAN) { 1741 /* set Japan filter bit for channel 14 */ 1742 tmp = ural_bbp_read(sc, 70); 1743 1744 tmp &= ~RAL_JAPAN_FILTER; 1745 if (chan == 14) 1746 tmp |= RAL_JAPAN_FILTER; 1747 1748 ural_bbp_write(sc, 70, tmp); 1749 1750 /* clear CRC errors */ 1751 ural_read(sc, RAL_STA_CSR0); 1752 1753 DELAY(1000); /* RF needs a 1ms delay here */ 1754 ural_disable_rf_tune(sc); 1755 } 1756 #undef N 1757 } 1758 1759 /* 1760 * Disable RF auto-tuning. 1761 */ 1762 Static void 1763 ural_disable_rf_tune(struct ural_softc *sc) 1764 { 1765 uint32_t tmp; 1766 1767 if (sc->rf_rev != RAL_RF_2523) { 1768 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 1769 ural_rf_write(sc, RAL_RF1, tmp); 1770 } 1771 1772 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 1773 ural_rf_write(sc, RAL_RF3, tmp); 1774 1775 DPRINTFN(2, ("disabling RF autotune\n")); 1776 } 1777 1778 /* 1779 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 1780 * synchronization. 1781 */ 1782 Static void 1783 ural_enable_tsf_sync(struct ural_softc *sc) 1784 { 1785 struct ieee80211com *ic = &sc->sc_ic; 1786 uint16_t logcwmin, preload, tmp; 1787 1788 /* first, disable TSF synchronization */ 1789 ural_write(sc, RAL_TXRX_CSR19, 0); 1790 1791 tmp = (16 * ic->ic_bss->ni_intval) << 4; 1792 ural_write(sc, RAL_TXRX_CSR18, tmp); 1793 1794 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0; 1795 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6; 1796 tmp = logcwmin << 12 | preload; 1797 ural_write(sc, RAL_TXRX_CSR20, tmp); 1798 1799 /* finally, enable TSF synchronization */ 1800 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN; 1801 if (ic->ic_opmode == IEEE80211_M_STA) 1802 tmp |= RAL_ENABLE_TSF_SYNC(1); 1803 else 1804 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR; 1805 ural_write(sc, RAL_TXRX_CSR19, tmp); 1806 1807 DPRINTF(("enabling TSF synchronization\n")); 1808 } 1809 1810 Static void 1811 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid) 1812 { 1813 uint16_t tmp; 1814 1815 tmp = bssid[0] | bssid[1] << 8; 1816 ural_write(sc, RAL_MAC_CSR5, tmp); 1817 1818 tmp = bssid[2] | bssid[3] << 8; 1819 ural_write(sc, RAL_MAC_CSR6, tmp); 1820 1821 tmp = bssid[4] | bssid[5] << 8; 1822 ural_write(sc, RAL_MAC_CSR7, tmp); 1823 1824 DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid))); 1825 } 1826 1827 Static void 1828 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr) 1829 { 1830 uint16_t tmp; 1831 1832 tmp = addr[0] | addr[1] << 8; 1833 ural_write(sc, RAL_MAC_CSR2, tmp); 1834 1835 tmp = addr[2] | addr[3] << 8; 1836 ural_write(sc, RAL_MAC_CSR3, tmp); 1837 1838 tmp = addr[4] | addr[5] << 8; 1839 ural_write(sc, RAL_MAC_CSR4, tmp); 1840 1841 DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr))); 1842 } 1843 1844 Static void 1845 ural_update_promisc(struct ural_softc *sc) 1846 { 1847 struct ifnet *ifp = &sc->sc_if; 1848 uint16_t tmp; 1849 1850 tmp = ural_read(sc, RAL_TXRX_CSR2); 1851 1852 tmp &= ~RAL_DROP_NOT_TO_ME; 1853 if (!(ifp->if_flags & IFF_PROMISC)) 1854 tmp |= RAL_DROP_NOT_TO_ME; 1855 1856 ural_write(sc, RAL_TXRX_CSR2, tmp); 1857 1858 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1859 "entering" : "leaving")); 1860 } 1861 1862 Static const char * 1863 ural_get_rf(int rev) 1864 { 1865 switch (rev) { 1866 case RAL_RF_2522: return "RT2522"; 1867 case RAL_RF_2523: return "RT2523"; 1868 case RAL_RF_2524: return "RT2524"; 1869 case RAL_RF_2525: return "RT2525"; 1870 case RAL_RF_2525E: return "RT2525e"; 1871 case RAL_RF_2526: return "RT2526"; 1872 case RAL_RF_5222: return "RT5222"; 1873 default: return "unknown"; 1874 } 1875 } 1876 1877 Static void 1878 ural_read_eeprom(struct ural_softc *sc) 1879 { 1880 struct ieee80211com *ic = &sc->sc_ic; 1881 uint16_t val; 1882 1883 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2); 1884 val = le16toh(val); 1885 sc->rf_rev = (val >> 11) & 0x7; 1886 sc->hw_radio = (val >> 10) & 0x1; 1887 sc->led_mode = (val >> 6) & 0x7; 1888 sc->rx_ant = (val >> 4) & 0x3; 1889 sc->tx_ant = (val >> 2) & 0x3; 1890 sc->nb_ant = val & 0x3; 1891 1892 /* read MAC address */ 1893 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6); 1894 1895 /* read default values for BBP registers */ 1896 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1897 1898 /* read Tx power for all b/g channels */ 1899 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14); 1900 } 1901 1902 Static int 1903 ural_bbp_init(struct ural_softc *sc) 1904 { 1905 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1906 int i, ntries; 1907 1908 /* wait for BBP to be ready */ 1909 for (ntries = 0; ntries < 100; ntries++) { 1910 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0) 1911 break; 1912 DELAY(1000); 1913 } 1914 if (ntries == 100) { 1915 printf("%s: timeout waiting for BBP\n", USBDEVNAME(sc->sc_dev)); 1916 return EIO; 1917 } 1918 1919 /* initialize BBP registers to default values */ 1920 for (i = 0; i < N(ural_def_bbp); i++) 1921 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val); 1922 1923 #if 0 1924 /* initialize BBP registers to values stored in EEPROM */ 1925 for (i = 0; i < 16; i++) { 1926 if (sc->bbp_prom[i].reg == 0xff) 1927 continue; 1928 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1929 } 1930 #endif 1931 1932 return 0; 1933 #undef N 1934 } 1935 1936 Static void 1937 ural_set_txantenna(struct ural_softc *sc, int antenna) 1938 { 1939 uint16_t tmp; 1940 uint8_t tx; 1941 1942 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK; 1943 if (antenna == 1) 1944 tx |= RAL_BBP_ANTA; 1945 else if (antenna == 2) 1946 tx |= RAL_BBP_ANTB; 1947 else 1948 tx |= RAL_BBP_DIVERSITY; 1949 1950 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 1951 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 || 1952 sc->rf_rev == RAL_RF_5222) 1953 tx |= RAL_BBP_FLIPIQ; 1954 1955 ural_bbp_write(sc, RAL_BBP_TX, tx); 1956 1957 /* update flags in PHY_CSR5 and PHY_CSR6 too */ 1958 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7; 1959 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7)); 1960 1961 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7; 1962 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7)); 1963 } 1964 1965 Static void 1966 ural_set_rxantenna(struct ural_softc *sc, int antenna) 1967 { 1968 uint8_t rx; 1969 1970 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK; 1971 if (antenna == 1) 1972 rx |= RAL_BBP_ANTA; 1973 else if (antenna == 2) 1974 rx |= RAL_BBP_ANTB; 1975 else 1976 rx |= RAL_BBP_DIVERSITY; 1977 1978 /* need to force no I/Q flip for RF 2525e and 2526 */ 1979 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526) 1980 rx &= ~RAL_BBP_FLIPIQ; 1981 1982 ural_bbp_write(sc, RAL_BBP_RX, rx); 1983 } 1984 1985 Static int 1986 ural_init(struct ifnet *ifp) 1987 { 1988 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1989 struct ural_softc *sc = ifp->if_softc; 1990 struct ieee80211com *ic = &sc->sc_ic; 1991 struct ieee80211_key *wk; 1992 struct ural_rx_data *data; 1993 uint16_t sta[11], tmp; 1994 usbd_status error; 1995 int i, ntries; 1996 1997 ural_stop(ifp, 0); 1998 1999 /* initialize MAC registers to default values */ 2000 for (i = 0; i < N(ural_def_mac); i++) 2001 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val); 2002 2003 /* wait for BBP and RF to wake up (this can take a long time!) */ 2004 for (ntries = 0; ntries < 100; ntries++) { 2005 tmp = ural_read(sc, RAL_MAC_CSR17); 2006 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) == 2007 (RAL_BBP_AWAKE | RAL_RF_AWAKE)) 2008 break; 2009 DELAY(1000); 2010 } 2011 if (ntries == 100) { 2012 printf("%s: timeout waiting for BBP/RF to wakeup\n", 2013 USBDEVNAME(sc->sc_dev)); 2014 error = EIO; 2015 goto fail; 2016 } 2017 2018 /* we're ready! */ 2019 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY); 2020 2021 /* set supported basic rates (1, 2, 6, 12, 24) */ 2022 ural_write(sc, RAL_TXRX_CSR11, 0x153); 2023 2024 error = ural_bbp_init(sc); 2025 if (error != 0) 2026 goto fail; 2027 2028 /* set default BSS channel */ 2029 ic->ic_bss->ni_chan = ic->ic_ibss_chan; 2030 ural_set_chan(sc, ic->ic_bss->ni_chan); 2031 2032 /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 2033 ural_read_multi(sc, RAL_STA_CSR0, sta, sizeof sta); 2034 2035 ural_set_txantenna(sc, 1); 2036 ural_set_rxantenna(sc, 1); 2037 2038 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 2039 ural_set_macaddr(sc, ic->ic_myaddr); 2040 2041 /* 2042 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31). 2043 */ 2044 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2045 wk = &ic->ic_nw_keys[i]; 2046 ural_write_multi(sc, wk->wk_keyix * IEEE80211_KEYBUF_SIZE + 2047 RAL_SEC_CSR0, wk->wk_key, IEEE80211_KEYBUF_SIZE); 2048 } 2049 2050 /* 2051 * Open Tx and Rx USB bulk pipes. 2052 */ 2053 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE, 2054 &sc->sc_tx_pipeh); 2055 if (error != 0) { 2056 printf("%s: could not open Tx pipe: %s\n", 2057 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 2058 goto fail; 2059 } 2060 2061 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE, 2062 &sc->sc_rx_pipeh); 2063 if (error != 0) { 2064 printf("%s: could not open Rx pipe: %s\n", 2065 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 2066 goto fail; 2067 } 2068 2069 /* 2070 * Allocate Tx and Rx xfer queues. 2071 */ 2072 error = ural_alloc_tx_list(sc); 2073 if (error != 0) { 2074 printf("%s: could not allocate Tx list\n", 2075 USBDEVNAME(sc->sc_dev)); 2076 goto fail; 2077 } 2078 2079 error = ural_alloc_rx_list(sc); 2080 if (error != 0) { 2081 printf("%s: could not allocate Rx list\n", 2082 USBDEVNAME(sc->sc_dev)); 2083 goto fail; 2084 } 2085 2086 /* 2087 * Start up the receive pipe. 2088 */ 2089 for (i = 0; i < RAL_RX_LIST_COUNT; i++) { 2090 data = &sc->rx_data[i]; 2091 2092 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf, 2093 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof); 2094 usbd_transfer(data->xfer); 2095 } 2096 2097 /* kick Rx */ 2098 tmp = RAL_DROP_PHY_ERROR | RAL_DROP_CRC_ERROR; 2099 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2100 tmp |= RAL_DROP_CTL | RAL_DROP_VERSION_ERROR; 2101 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2102 tmp |= RAL_DROP_TODS; 2103 if (!(ifp->if_flags & IFF_PROMISC)) 2104 tmp |= RAL_DROP_NOT_TO_ME; 2105 } 2106 ural_write(sc, RAL_TXRX_CSR2, tmp); 2107 2108 ifp->if_flags &= ~IFF_OACTIVE; 2109 ifp->if_flags |= IFF_RUNNING; 2110 2111 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2112 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2113 else 2114 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2115 2116 return 0; 2117 2118 fail: ural_stop(ifp, 1); 2119 return error; 2120 #undef N 2121 } 2122 2123 Static void 2124 ural_stop(struct ifnet *ifp, int disable) 2125 { 2126 struct ural_softc *sc = ifp->if_softc; 2127 struct ieee80211com *ic = &sc->sc_ic; 2128 2129 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2130 2131 sc->sc_tx_timer = 0; 2132 ifp->if_timer = 0; 2133 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2134 2135 /* disable Rx */ 2136 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX); 2137 2138 /* reset ASIC and BBP (but won't reset MAC registers!) */ 2139 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP); 2140 ural_write(sc, RAL_MAC_CSR1, 0); 2141 2142 if (sc->sc_rx_pipeh != NULL) { 2143 usbd_abort_pipe(sc->sc_rx_pipeh); 2144 usbd_close_pipe(sc->sc_rx_pipeh); 2145 sc->sc_rx_pipeh = NULL; 2146 } 2147 2148 if (sc->sc_tx_pipeh != NULL) { 2149 usbd_abort_pipe(sc->sc_tx_pipeh); 2150 usbd_close_pipe(sc->sc_tx_pipeh); 2151 sc->sc_tx_pipeh = NULL; 2152 } 2153 2154 ural_free_rx_list(sc); 2155 ural_free_tx_list(sc); 2156 } 2157 2158 int 2159 ural_activate(device_ptr_t self, enum devact act) 2160 { 2161 struct ural_softc *sc = (struct ural_softc *)self; 2162 2163 switch (act) { 2164 case DVACT_ACTIVATE: 2165 return EOPNOTSUPP; 2166 break; 2167 2168 case DVACT_DEACTIVATE: 2169 if_deactivate(&sc->sc_if); 2170 break; 2171 } 2172 2173 return 0; 2174 } 2175