1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */ 2 /* $NetBSD: if_rum.c,v 1.30 2009/12/06 20:20:12 dyoung Exp $ */ 3 4 /*- 5 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr> 6 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /*- 22 * Ralink Technology RT2501USB/RT2601USB chipset driver 23 * http://www.ralinktech.com.tw/ 24 */ 25 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: if_rum.c,v 1.30 2009/12/06 20:20:12 dyoung Exp $"); 28 29 #include "bpfilter.h" 30 31 #include <sys/param.h> 32 #include <sys/sockio.h> 33 #include <sys/sysctl.h> 34 #include <sys/mbuf.h> 35 #include <sys/kernel.h> 36 #include <sys/socket.h> 37 #include <sys/systm.h> 38 #include <sys/malloc.h> 39 #include <sys/conf.h> 40 #include <sys/device.h> 41 42 #include <sys/bus.h> 43 #include <machine/endian.h> 44 #include <sys/intr.h> 45 46 #if NBPFILTER > 0 47 #include <net/bpf.h> 48 #endif 49 #include <net/if.h> 50 #include <net/if_arp.h> 51 #include <net/if_dl.h> 52 #include <net/if_ether.h> 53 #include <net/if_media.h> 54 #include <net/if_types.h> 55 56 #include <netinet/in.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip.h> 60 61 #include <net80211/ieee80211_netbsd.h> 62 #include <net80211/ieee80211_var.h> 63 #include <net80211/ieee80211_amrr.h> 64 #include <net80211/ieee80211_radiotap.h> 65 66 #include <dev/firmload.h> 67 68 #include <dev/usb/usb.h> 69 #include <dev/usb/usbdi.h> 70 #include <dev/usb/usbdi_util.h> 71 #include <dev/usb/usbdevs.h> 72 73 #include <dev/usb/if_rumreg.h> 74 #include <dev/usb/if_rumvar.h> 75 76 #ifdef USB_DEBUG 77 #define RUM_DEBUG 78 #endif 79 80 #ifdef RUM_DEBUG 81 #define DPRINTF(x) do { if (rum_debug) logprintf x; } while (0) 82 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) logprintf x; } while (0) 83 int rum_debug = 1; 84 #else 85 #define DPRINTF(x) 86 #define DPRINTFN(n, x) 87 #endif 88 89 /* various supported device vendors/products */ 90 static const struct usb_devno rum_devs[] = { 91 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM }, 92 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 }, 93 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 }, 94 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 }, 95 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 }, 96 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO }, 97 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_2 }, 98 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_3 }, 99 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A }, 100 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 }, 101 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC }, 102 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR }, 103 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 }, 104 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL }, 105 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX }, 106 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F }, 107 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 }, 108 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 }, 109 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 }, 110 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA111 }, 111 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS }, 112 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS }, 113 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 }, 114 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 }, 115 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB }, 116 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP }, 117 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 }, 118 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP }, 119 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP }, 120 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_WLIUCG }, 121 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 }, 122 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 }, 123 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 }, 124 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 }, 125 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 }, 126 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP }, 127 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 }, 128 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM }, 129 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 }, 130 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 }, 131 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_3 }, 132 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 }, 133 { USB_VENDOR_RALINK_2, USB_PRODUCT_RALINK_2_RT2573 }, 134 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 }, 135 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 }, 136 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 }, 137 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 } 138 }; 139 140 Static int rum_attachhook(void *); 141 Static int rum_alloc_tx_list(struct rum_softc *); 142 Static void rum_free_tx_list(struct rum_softc *); 143 Static int rum_alloc_rx_list(struct rum_softc *); 144 Static void rum_free_rx_list(struct rum_softc *); 145 Static int rum_media_change(struct ifnet *); 146 Static void rum_next_scan(void *); 147 Static void rum_task(void *); 148 Static int rum_newstate(struct ieee80211com *, 149 enum ieee80211_state, int); 150 Static void rum_txeof(usbd_xfer_handle, usbd_private_handle, 151 usbd_status); 152 Static void rum_rxeof(usbd_xfer_handle, usbd_private_handle, 153 usbd_status); 154 #if NBPFILTER > 0 155 Static uint8_t rum_rxrate(const struct rum_rx_desc *); 156 #endif 157 Static int rum_ack_rate(struct ieee80211com *, int); 158 Static uint16_t rum_txtime(int, int, uint32_t); 159 Static uint8_t rum_plcp_signal(int); 160 Static void rum_setup_tx_desc(struct rum_softc *, 161 struct rum_tx_desc *, uint32_t, uint16_t, int, 162 int); 163 Static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 164 struct ieee80211_node *); 165 Static int rum_tx_data(struct rum_softc *, struct mbuf *, 166 struct ieee80211_node *); 167 Static void rum_start(struct ifnet *); 168 Static void rum_watchdog(struct ifnet *); 169 Static int rum_ioctl(struct ifnet *, u_long, void *); 170 Static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 171 int); 172 Static uint32_t rum_read(struct rum_softc *, uint16_t); 173 Static void rum_read_multi(struct rum_softc *, uint16_t, void *, 174 int); 175 Static void rum_write(struct rum_softc *, uint16_t, uint32_t); 176 Static void rum_write_multi(struct rum_softc *, uint16_t, void *, 177 size_t); 178 Static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 179 Static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 180 Static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 181 Static void rum_select_antenna(struct rum_softc *); 182 Static void rum_enable_mrr(struct rum_softc *); 183 Static void rum_set_txpreamble(struct rum_softc *); 184 Static void rum_set_basicrates(struct rum_softc *); 185 Static void rum_select_band(struct rum_softc *, 186 struct ieee80211_channel *); 187 Static void rum_set_chan(struct rum_softc *, 188 struct ieee80211_channel *); 189 Static void rum_enable_tsf_sync(struct rum_softc *); 190 Static void rum_update_slot(struct rum_softc *); 191 Static void rum_set_bssid(struct rum_softc *, const uint8_t *); 192 Static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 193 Static void rum_update_promisc(struct rum_softc *); 194 Static const char *rum_get_rf(int); 195 Static void rum_read_eeprom(struct rum_softc *); 196 Static int rum_bbp_init(struct rum_softc *); 197 Static int rum_init(struct ifnet *); 198 Static void rum_stop(struct ifnet *, int); 199 Static int rum_load_microcode(struct rum_softc *, const u_char *, 200 size_t); 201 Static int rum_prepare_beacon(struct rum_softc *); 202 Static void rum_newassoc(struct ieee80211_node *, int); 203 Static void rum_amrr_start(struct rum_softc *, 204 struct ieee80211_node *); 205 Static void rum_amrr_timeout(void *); 206 Static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle, 207 usbd_status status); 208 209 /* 210 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 211 */ 212 static const struct ieee80211_rateset rum_rateset_11a = 213 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 214 215 static const struct ieee80211_rateset rum_rateset_11b = 216 { 4, { 2, 4, 11, 22 } }; 217 218 static const struct ieee80211_rateset rum_rateset_11g = 219 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 220 221 static const struct { 222 uint32_t reg; 223 uint32_t val; 224 } rum_def_mac[] = { 225 RT2573_DEF_MAC 226 }; 227 228 static const struct { 229 uint8_t reg; 230 uint8_t val; 231 } rum_def_bbp[] = { 232 RT2573_DEF_BBP 233 }; 234 235 static const struct rfprog { 236 uint8_t chan; 237 uint32_t r1, r2, r3, r4; 238 } rum_rf5226[] = { 239 RT2573_RF5226 240 }, rum_rf5225[] = { 241 RT2573_RF5225 242 }; 243 244 USB_DECLARE_DRIVER(rum); 245 246 USB_MATCH(rum) 247 { 248 USB_MATCH_START(rum, uaa); 249 250 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ? 251 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 252 } 253 254 Static int 255 rum_attachhook(void *xsc) 256 { 257 struct rum_softc *sc = xsc; 258 firmware_handle_t fwh; 259 const char *name = "rum-rt2573"; 260 u_char *ucode; 261 size_t size; 262 int error; 263 264 if ((error = firmware_open("rum", name, &fwh)) != 0) { 265 printf("%s: failed loadfirmware of file %s (error %d)\n", 266 USBDEVNAME(sc->sc_dev), name, error); 267 return error; 268 } 269 size = firmware_get_size(fwh); 270 ucode = firmware_malloc(size); 271 if (ucode == NULL) { 272 printf("%s: failed to allocate firmware memory\n", 273 USBDEVNAME(sc->sc_dev)); 274 firmware_close(fwh); 275 return ENOMEM; 276 } 277 error = firmware_read(fwh, 0, ucode, size); 278 firmware_close(fwh); 279 if (error != 0) { 280 printf("%s: failed to read firmware (error %d)\n", 281 USBDEVNAME(sc->sc_dev), error); 282 firmware_free(ucode, 0); 283 return error; 284 } 285 286 if (rum_load_microcode(sc, ucode, size) != 0) { 287 printf("%s: could not load 8051 microcode\n", 288 USBDEVNAME(sc->sc_dev)); 289 firmware_free(ucode, 0); 290 return ENXIO; 291 } 292 293 firmware_free(ucode, 0); 294 sc->sc_flags |= RT2573_FWLOADED; 295 296 return 0; 297 } 298 299 USB_ATTACH(rum) 300 { 301 USB_ATTACH_START(rum, sc, uaa); 302 struct ieee80211com *ic = &sc->sc_ic; 303 struct ifnet *ifp = &sc->sc_if; 304 usb_interface_descriptor_t *id; 305 usb_endpoint_descriptor_t *ed; 306 usbd_status error; 307 char *devinfop; 308 int i, ntries; 309 uint32_t tmp; 310 311 sc->sc_dev = self; 312 sc->sc_udev = uaa->device; 313 sc->sc_flags = 0; 314 315 aprint_naive("\n"); 316 aprint_normal("\n"); 317 318 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 319 aprint_normal_dev(self, "%s\n", devinfop); 320 usbd_devinfo_free(devinfop); 321 322 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) { 323 aprint_error_dev(self, "could not set configuration no\n"); 324 USB_ATTACH_ERROR_RETURN; 325 } 326 327 /* get the first interface handle */ 328 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX, 329 &sc->sc_iface); 330 if (error != 0) { 331 aprint_error_dev(self, "could not get interface handle\n"); 332 USB_ATTACH_ERROR_RETURN; 333 } 334 335 /* 336 * Find endpoints. 337 */ 338 id = usbd_get_interface_descriptor(sc->sc_iface); 339 340 sc->sc_rx_no = sc->sc_tx_no = -1; 341 for (i = 0; i < id->bNumEndpoints; i++) { 342 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 343 if (ed == NULL) { 344 aprint_error_dev(self, 345 "no endpoint descriptor for iface %d\n", i); 346 USB_ATTACH_ERROR_RETURN; 347 } 348 349 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 350 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 351 sc->sc_rx_no = ed->bEndpointAddress; 352 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 353 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 354 sc->sc_tx_no = ed->bEndpointAddress; 355 } 356 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) { 357 aprint_error_dev(self, "missing endpoint\n"); 358 USB_ATTACH_ERROR_RETURN; 359 } 360 361 usb_init_task(&sc->sc_task, rum_task, sc); 362 usb_callout_init(sc->sc_scan_ch); 363 364 sc->amrr.amrr_min_success_threshold = 1; 365 sc->amrr.amrr_max_success_threshold = 10; 366 usb_callout_init(sc->sc_amrr_ch); 367 368 /* retrieve RT2573 rev. no */ 369 for (ntries = 0; ntries < 1000; ntries++) { 370 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 371 break; 372 DELAY(1000); 373 } 374 if (ntries == 1000) { 375 aprint_error_dev(self, "timeout waiting for chip to settle\n"); 376 USB_ATTACH_ERROR_RETURN; 377 } 378 379 /* retrieve MAC address and various other things from EEPROM */ 380 rum_read_eeprom(sc); 381 382 aprint_normal_dev(self, 383 "MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n", 384 sc->macbbp_rev, tmp, 385 rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr)); 386 387 ic->ic_ifp = ifp; 388 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 389 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 390 ic->ic_state = IEEE80211_S_INIT; 391 392 /* set device capabilities */ 393 ic->ic_caps = 394 IEEE80211_C_IBSS | /* IBSS mode supported */ 395 IEEE80211_C_MONITOR | /* monitor mode supported */ 396 IEEE80211_C_HOSTAP | /* HostAp mode supported */ 397 IEEE80211_C_TXPMGT | /* tx power management */ 398 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 399 IEEE80211_C_SHSLOT | /* short slot time supported */ 400 IEEE80211_C_WPA; /* 802.11i */ 401 402 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) { 403 /* set supported .11a rates */ 404 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a; 405 406 /* set supported .11a channels */ 407 for (i = 34; i <= 46; i += 4) { 408 ic->ic_channels[i].ic_freq = 409 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 410 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 411 } 412 for (i = 36; i <= 64; i += 4) { 413 ic->ic_channels[i].ic_freq = 414 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 415 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 416 } 417 for (i = 100; i <= 140; i += 4) { 418 ic->ic_channels[i].ic_freq = 419 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 420 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 421 } 422 for (i = 149; i <= 165; i += 4) { 423 ic->ic_channels[i].ic_freq = 424 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 425 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 426 } 427 } 428 429 /* set supported .11b and .11g rates */ 430 ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b; 431 ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g; 432 433 /* set supported .11b and .11g channels (1 through 14) */ 434 for (i = 1; i <= 14; i++) { 435 ic->ic_channels[i].ic_freq = 436 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 437 ic->ic_channels[i].ic_flags = 438 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 439 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 440 } 441 442 ifp->if_softc = sc; 443 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 444 ifp->if_init = rum_init; 445 ifp->if_ioctl = rum_ioctl; 446 ifp->if_start = rum_start; 447 ifp->if_watchdog = rum_watchdog; 448 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 449 IFQ_SET_READY(&ifp->if_snd); 450 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ); 451 452 if_attach(ifp); 453 ieee80211_ifattach(ic); 454 ic->ic_newassoc = rum_newassoc; 455 456 /* override state transition machine */ 457 sc->sc_newstate = ic->ic_newstate; 458 ic->ic_newstate = rum_newstate; 459 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status); 460 461 #if NBPFILTER > 0 462 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 463 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, &sc->sc_drvbpf); 464 465 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 466 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 467 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT); 468 469 sc->sc_txtap_len = sizeof sc->sc_txtapu; 470 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 471 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT); 472 #endif 473 474 ieee80211_announce(ic); 475 476 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 477 USBDEV(sc->sc_dev)); 478 479 USB_ATTACH_SUCCESS_RETURN; 480 } 481 482 USB_DETACH(rum) 483 { 484 USB_DETACH_START(rum, sc); 485 struct ieee80211com *ic = &sc->sc_ic; 486 struct ifnet *ifp = &sc->sc_if; 487 int s; 488 489 if (!ifp->if_softc) 490 return 0; 491 492 s = splusb(); 493 494 rum_stop(ifp, 1); 495 usb_rem_task(sc->sc_udev, &sc->sc_task); 496 usb_uncallout(sc->sc_scan_ch, rum_next_scan, sc); 497 usb_uncallout(sc->sc_amrr_ch, rum_amrr_timeout, sc); 498 499 if (sc->amrr_xfer != NULL) { 500 usbd_free_xfer(sc->amrr_xfer); 501 sc->amrr_xfer = NULL; 502 } 503 504 if (sc->sc_rx_pipeh != NULL) { 505 usbd_abort_pipe(sc->sc_rx_pipeh); 506 usbd_close_pipe(sc->sc_rx_pipeh); 507 } 508 509 if (sc->sc_tx_pipeh != NULL) { 510 usbd_abort_pipe(sc->sc_tx_pipeh); 511 usbd_close_pipe(sc->sc_tx_pipeh); 512 } 513 514 #if NBPFILTER > 0 515 bpfdetach(ifp); 516 #endif 517 ieee80211_ifdetach(ic); /* free all nodes */ 518 if_detach(ifp); 519 520 splx(s); 521 522 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 523 USBDEV(sc->sc_dev)); 524 525 return 0; 526 } 527 528 Static int 529 rum_alloc_tx_list(struct rum_softc *sc) 530 { 531 struct rum_tx_data *data; 532 int i, error; 533 534 sc->tx_queued = 0; 535 536 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 537 data = &sc->tx_data[i]; 538 539 data->sc = sc; 540 541 data->xfer = usbd_alloc_xfer(sc->sc_udev); 542 if (data->xfer == NULL) { 543 printf("%s: could not allocate tx xfer\n", 544 USBDEVNAME(sc->sc_dev)); 545 error = ENOMEM; 546 goto fail; 547 } 548 549 data->buf = usbd_alloc_buffer(data->xfer, 550 RT2573_TX_DESC_SIZE + MCLBYTES); 551 if (data->buf == NULL) { 552 printf("%s: could not allocate tx buffer\n", 553 USBDEVNAME(sc->sc_dev)); 554 error = ENOMEM; 555 goto fail; 556 } 557 558 /* clean Tx descriptor */ 559 memset(data->buf, 0, RT2573_TX_DESC_SIZE); 560 } 561 562 return 0; 563 564 fail: rum_free_tx_list(sc); 565 return error; 566 } 567 568 Static void 569 rum_free_tx_list(struct rum_softc *sc) 570 { 571 struct rum_tx_data *data; 572 int i; 573 574 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 575 data = &sc->tx_data[i]; 576 577 if (data->xfer != NULL) { 578 usbd_free_xfer(data->xfer); 579 data->xfer = NULL; 580 } 581 582 if (data->ni != NULL) { 583 ieee80211_free_node(data->ni); 584 data->ni = NULL; 585 } 586 } 587 } 588 589 Static int 590 rum_alloc_rx_list(struct rum_softc *sc) 591 { 592 struct rum_rx_data *data; 593 int i, error; 594 595 for (i = 0; i < RUM_RX_LIST_COUNT; i++) { 596 data = &sc->rx_data[i]; 597 598 data->sc = sc; 599 600 data->xfer = usbd_alloc_xfer(sc->sc_udev); 601 if (data->xfer == NULL) { 602 printf("%s: could not allocate rx xfer\n", 603 USBDEVNAME(sc->sc_dev)); 604 error = ENOMEM; 605 goto fail; 606 } 607 608 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) { 609 printf("%s: could not allocate rx buffer\n", 610 USBDEVNAME(sc->sc_dev)); 611 error = ENOMEM; 612 goto fail; 613 } 614 615 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 616 if (data->m == NULL) { 617 printf("%s: could not allocate rx mbuf\n", 618 USBDEVNAME(sc->sc_dev)); 619 error = ENOMEM; 620 goto fail; 621 } 622 623 MCLGET(data->m, M_DONTWAIT); 624 if (!(data->m->m_flags & M_EXT)) { 625 printf("%s: could not allocate rx mbuf cluster\n", 626 USBDEVNAME(sc->sc_dev)); 627 error = ENOMEM; 628 goto fail; 629 } 630 631 data->buf = mtod(data->m, uint8_t *); 632 } 633 634 return 0; 635 636 fail: rum_free_tx_list(sc); 637 return error; 638 } 639 640 Static void 641 rum_free_rx_list(struct rum_softc *sc) 642 { 643 struct rum_rx_data *data; 644 int i; 645 646 for (i = 0; i < RUM_RX_LIST_COUNT; i++) { 647 data = &sc->rx_data[i]; 648 649 if (data->xfer != NULL) { 650 usbd_free_xfer(data->xfer); 651 data->xfer = NULL; 652 } 653 654 if (data->m != NULL) { 655 m_freem(data->m); 656 data->m = NULL; 657 } 658 } 659 } 660 661 Static int 662 rum_media_change(struct ifnet *ifp) 663 { 664 int error; 665 666 error = ieee80211_media_change(ifp); 667 if (error != ENETRESET) 668 return error; 669 670 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 671 rum_init(ifp); 672 673 return 0; 674 } 675 676 /* 677 * This function is called periodically (every 200ms) during scanning to 678 * switch from one channel to another. 679 */ 680 Static void 681 rum_next_scan(void *arg) 682 { 683 struct rum_softc *sc = arg; 684 struct ieee80211com *ic = &sc->sc_ic; 685 686 if (ic->ic_state == IEEE80211_S_SCAN) 687 ieee80211_next_scan(ic); 688 } 689 690 Static void 691 rum_task(void *arg) 692 { 693 struct rum_softc *sc = arg; 694 struct ieee80211com *ic = &sc->sc_ic; 695 enum ieee80211_state ostate; 696 struct ieee80211_node *ni; 697 uint32_t tmp; 698 699 ostate = ic->ic_state; 700 701 switch (sc->sc_state) { 702 case IEEE80211_S_INIT: 703 if (ostate == IEEE80211_S_RUN) { 704 /* abort TSF synchronization */ 705 tmp = rum_read(sc, RT2573_TXRX_CSR9); 706 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 707 } 708 break; 709 710 case IEEE80211_S_SCAN: 711 rum_set_chan(sc, ic->ic_curchan); 712 usb_callout(sc->sc_scan_ch, hz / 5, rum_next_scan, sc); 713 break; 714 715 case IEEE80211_S_AUTH: 716 rum_set_chan(sc, ic->ic_curchan); 717 break; 718 719 case IEEE80211_S_ASSOC: 720 rum_set_chan(sc, ic->ic_curchan); 721 break; 722 723 case IEEE80211_S_RUN: 724 rum_set_chan(sc, ic->ic_curchan); 725 726 ni = ic->ic_bss; 727 728 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 729 rum_update_slot(sc); 730 rum_enable_mrr(sc); 731 rum_set_txpreamble(sc); 732 rum_set_basicrates(sc); 733 rum_set_bssid(sc, ni->ni_bssid); 734 } 735 736 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 737 ic->ic_opmode == IEEE80211_M_IBSS) 738 rum_prepare_beacon(sc); 739 740 if (ic->ic_opmode != IEEE80211_M_MONITOR) 741 rum_enable_tsf_sync(sc); 742 743 if (ic->ic_opmode == IEEE80211_M_STA) { 744 /* fake a join to init the tx rate */ 745 rum_newassoc(ic->ic_bss, 1); 746 747 /* enable automatic rate adaptation in STA mode */ 748 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 749 rum_amrr_start(sc, ni); 750 } 751 752 break; 753 } 754 755 sc->sc_newstate(ic, sc->sc_state, -1); 756 } 757 758 Static int 759 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 760 { 761 struct rum_softc *sc = ic->ic_ifp->if_softc; 762 763 usb_rem_task(sc->sc_udev, &sc->sc_task); 764 usb_uncallout(sc->sc_scan_ch, rum_next_scan, sc); 765 usb_uncallout(sc->sc_amrr_ch, rum_amrr_timeout, sc); 766 767 /* do it in a process context */ 768 sc->sc_state = nstate; 769 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER); 770 771 return 0; 772 } 773 774 /* quickly determine if a given rate is CCK or OFDM */ 775 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 776 777 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */ 778 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */ 779 780 Static void 781 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 782 { 783 struct rum_tx_data *data = priv; 784 struct rum_softc *sc = data->sc; 785 struct ifnet *ifp = &sc->sc_if; 786 int s; 787 788 if (status != USBD_NORMAL_COMPLETION) { 789 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 790 return; 791 792 printf("%s: could not transmit buffer: %s\n", 793 USBDEVNAME(sc->sc_dev), usbd_errstr(status)); 794 795 if (status == USBD_STALLED) 796 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh); 797 798 ifp->if_oerrors++; 799 return; 800 } 801 802 s = splnet(); 803 804 ieee80211_free_node(data->ni); 805 data->ni = NULL; 806 807 sc->tx_queued--; 808 ifp->if_opackets++; 809 810 DPRINTFN(10, ("tx done\n")); 811 812 sc->sc_tx_timer = 0; 813 ifp->if_flags &= ~IFF_OACTIVE; 814 rum_start(ifp); 815 816 splx(s); 817 } 818 819 Static void 820 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 821 { 822 struct rum_rx_data *data = priv; 823 struct rum_softc *sc = data->sc; 824 struct ieee80211com *ic = &sc->sc_ic; 825 struct ifnet *ifp = &sc->sc_if; 826 struct rum_rx_desc *desc; 827 struct ieee80211_frame *wh; 828 struct ieee80211_node *ni; 829 struct mbuf *mnew, *m; 830 int s, len; 831 832 if (status != USBD_NORMAL_COMPLETION) { 833 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 834 return; 835 836 if (status == USBD_STALLED) 837 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh); 838 goto skip; 839 } 840 841 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 842 843 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) { 844 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev), 845 len)); 846 ifp->if_ierrors++; 847 goto skip; 848 } 849 850 desc = (struct rum_rx_desc *)data->buf; 851 852 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) { 853 /* 854 * This should not happen since we did not request to receive 855 * those frames when we filled RT2573_TXRX_CSR0. 856 */ 857 DPRINTFN(5, ("CRC error\n")); 858 ifp->if_ierrors++; 859 goto skip; 860 } 861 862 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 863 if (mnew == NULL) { 864 printf("%s: could not allocate rx mbuf\n", 865 USBDEVNAME(sc->sc_dev)); 866 ifp->if_ierrors++; 867 goto skip; 868 } 869 870 MCLGET(mnew, M_DONTWAIT); 871 if (!(mnew->m_flags & M_EXT)) { 872 printf("%s: could not allocate rx mbuf cluster\n", 873 USBDEVNAME(sc->sc_dev)); 874 m_freem(mnew); 875 ifp->if_ierrors++; 876 goto skip; 877 } 878 879 m = data->m; 880 data->m = mnew; 881 data->buf = mtod(data->m, uint8_t *); 882 883 /* finalize mbuf */ 884 m->m_pkthdr.rcvif = ifp; 885 m->m_data = (void *)(desc + 1); 886 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff; 887 888 s = splnet(); 889 890 #if NBPFILTER > 0 891 if (sc->sc_drvbpf != NULL) { 892 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 893 894 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 895 tap->wr_rate = rum_rxrate(desc); 896 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 897 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 898 tap->wr_antenna = sc->rx_ant; 899 tap->wr_antsignal = desc->rssi; 900 901 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 902 } 903 #endif 904 905 wh = mtod(m, struct ieee80211_frame *); 906 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 907 908 /* send the frame to the 802.11 layer */ 909 ieee80211_input(ic, m, ni, desc->rssi, 0); 910 911 /* node is no longer needed */ 912 ieee80211_free_node(ni); 913 914 splx(s); 915 916 DPRINTFN(15, ("rx done\n")); 917 918 skip: /* setup a new transfer */ 919 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES, 920 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof); 921 usbd_transfer(xfer); 922 } 923 924 /* 925 * This function is only used by the Rx radiotap code. It returns the rate at 926 * which a given frame was received. 927 */ 928 #if NBPFILTER > 0 929 Static uint8_t 930 rum_rxrate(const struct rum_rx_desc *desc) 931 { 932 if (le32toh(desc->flags) & RT2573_RX_OFDM) { 933 /* reverse function of rum_plcp_signal */ 934 switch (desc->rate) { 935 case 0xb: return 12; 936 case 0xf: return 18; 937 case 0xa: return 24; 938 case 0xe: return 36; 939 case 0x9: return 48; 940 case 0xd: return 72; 941 case 0x8: return 96; 942 case 0xc: return 108; 943 } 944 } else { 945 if (desc->rate == 10) 946 return 2; 947 if (desc->rate == 20) 948 return 4; 949 if (desc->rate == 55) 950 return 11; 951 if (desc->rate == 110) 952 return 22; 953 } 954 return 2; /* should not get there */ 955 } 956 #endif 957 958 /* 959 * Return the expected ack rate for a frame transmitted at rate `rate'. 960 * XXX: this should depend on the destination node basic rate set. 961 */ 962 Static int 963 rum_ack_rate(struct ieee80211com *ic, int rate) 964 { 965 switch (rate) { 966 /* CCK rates */ 967 case 2: 968 return 2; 969 case 4: 970 case 11: 971 case 22: 972 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate; 973 974 /* OFDM rates */ 975 case 12: 976 case 18: 977 return 12; 978 case 24: 979 case 36: 980 return 24; 981 case 48: 982 case 72: 983 case 96: 984 case 108: 985 return 48; 986 } 987 988 /* default to 1Mbps */ 989 return 2; 990 } 991 992 /* 993 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'. 994 * The function automatically determines the operating mode depending on the 995 * given rate. `flags' indicates whether short preamble is in use or not. 996 */ 997 Static uint16_t 998 rum_txtime(int len, int rate, uint32_t flags) 999 { 1000 uint16_t txtime; 1001 1002 if (RUM_RATE_IS_OFDM(rate)) { 1003 /* IEEE Std 802.11a-1999, pp. 37 */ 1004 txtime = (8 + 4 * len + 3 + rate - 1) / rate; 1005 txtime = 16 + 4 + 4 * txtime + 6; 1006 } else { 1007 /* IEEE Std 802.11b-1999, pp. 28 */ 1008 txtime = (16 * len + rate - 1) / rate; 1009 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) 1010 txtime += 72 + 24; 1011 else 1012 txtime += 144 + 48; 1013 } 1014 return txtime; 1015 } 1016 1017 Static uint8_t 1018 rum_plcp_signal(int rate) 1019 { 1020 switch (rate) { 1021 /* CCK rates (returned values are device-dependent) */ 1022 case 2: return 0x0; 1023 case 4: return 0x1; 1024 case 11: return 0x2; 1025 case 22: return 0x3; 1026 1027 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1028 case 12: return 0xb; 1029 case 18: return 0xf; 1030 case 24: return 0xa; 1031 case 36: return 0xe; 1032 case 48: return 0x9; 1033 case 72: return 0xd; 1034 case 96: return 0x8; 1035 case 108: return 0xc; 1036 1037 /* unsupported rates (should not get there) */ 1038 default: return 0xff; 1039 } 1040 } 1041 1042 Static void 1043 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 1044 uint32_t flags, uint16_t xflags, int len, int rate) 1045 { 1046 struct ieee80211com *ic = &sc->sc_ic; 1047 uint16_t plcp_length; 1048 int remainder; 1049 1050 desc->flags = htole32(flags); 1051 desc->flags |= htole32(RT2573_TX_VALID); 1052 desc->flags |= htole32(len << 16); 1053 1054 desc->xflags = htole16(xflags); 1055 1056 desc->wme = htole16( 1057 RT2573_QID(0) | 1058 RT2573_AIFSN(2) | 1059 RT2573_LOGCWMIN(4) | 1060 RT2573_LOGCWMAX(10)); 1061 1062 /* setup PLCP fields */ 1063 desc->plcp_signal = rum_plcp_signal(rate); 1064 desc->plcp_service = 4; 1065 1066 len += IEEE80211_CRC_LEN; 1067 if (RUM_RATE_IS_OFDM(rate)) { 1068 desc->flags |= htole32(RT2573_TX_OFDM); 1069 1070 plcp_length = len & 0xfff; 1071 desc->plcp_length_hi = plcp_length >> 6; 1072 desc->plcp_length_lo = plcp_length & 0x3f; 1073 } else { 1074 plcp_length = (16 * len + rate - 1) / rate; 1075 if (rate == 22) { 1076 remainder = (16 * len) % 22; 1077 if (remainder != 0 && remainder < 7) 1078 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1079 } 1080 desc->plcp_length_hi = plcp_length >> 8; 1081 desc->plcp_length_lo = plcp_length & 0xff; 1082 1083 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1084 desc->plcp_signal |= 0x08; 1085 } 1086 } 1087 1088 #define RUM_TX_TIMEOUT 5000 1089 1090 Static int 1091 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1092 { 1093 struct ieee80211com *ic = &sc->sc_ic; 1094 struct rum_tx_desc *desc; 1095 struct rum_tx_data *data; 1096 struct ieee80211_frame *wh; 1097 struct ieee80211_key *k; 1098 uint32_t flags = 0; 1099 uint16_t dur; 1100 usbd_status error; 1101 int xferlen, rate; 1102 1103 data = &sc->tx_data[0]; 1104 desc = (struct rum_tx_desc *)data->buf; 1105 1106 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 1107 1108 data->m = m0; 1109 data->ni = ni; 1110 1111 wh = mtod(m0, struct ieee80211_frame *); 1112 1113 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1114 k = ieee80211_crypto_encap(ic, ni, m0); 1115 if (k == NULL) { 1116 m_freem(m0); 1117 return ENOBUFS; 1118 } 1119 } 1120 1121 wh = mtod(m0, struct ieee80211_frame *); 1122 1123 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1124 flags |= RT2573_TX_NEED_ACK; 1125 1126 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate), 1127 ic->ic_flags) + sc->sifs; 1128 *(uint16_t *)wh->i_dur = htole16(dur); 1129 1130 /* tell hardware to set timestamp in probe responses */ 1131 if ((wh->i_fc[0] & 1132 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1133 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1134 flags |= RT2573_TX_TIMESTAMP; 1135 } 1136 1137 #if NBPFILTER > 0 1138 if (sc->sc_drvbpf != NULL) { 1139 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 1140 1141 tap->wt_flags = 0; 1142 tap->wt_rate = rate; 1143 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1144 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1145 tap->wt_antenna = sc->tx_ant; 1146 1147 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1148 } 1149 #endif 1150 1151 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE); 1152 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate); 1153 1154 /* align end on a 4-bytes boundary */ 1155 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3; 1156 1157 /* 1158 * No space left in the last URB to store the extra 4 bytes, force 1159 * sending of another URB. 1160 */ 1161 if ((xferlen % 64) == 0) 1162 xferlen += 4; 1163 1164 DPRINTFN(10, ("sending msg frame len=%zu rate=%u xfer len=%u\n", 1165 (size_t)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, 1166 rate, xferlen)); 1167 1168 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen, 1169 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof); 1170 1171 error = usbd_transfer(data->xfer); 1172 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) { 1173 m_freem(m0); 1174 return error; 1175 } 1176 1177 sc->tx_queued++; 1178 1179 return 0; 1180 } 1181 1182 Static int 1183 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1184 { 1185 struct ieee80211com *ic = &sc->sc_ic; 1186 struct rum_tx_desc *desc; 1187 struct rum_tx_data *data; 1188 struct ieee80211_frame *wh; 1189 struct ieee80211_key *k; 1190 uint32_t flags = 0; 1191 uint16_t dur; 1192 usbd_status error; 1193 int xferlen, rate; 1194 1195 wh = mtod(m0, struct ieee80211_frame *); 1196 1197 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) 1198 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate]; 1199 else 1200 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 1201 if (rate == 0) 1202 rate = 2; /* XXX should not happen */ 1203 rate &= IEEE80211_RATE_VAL; 1204 1205 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1206 k = ieee80211_crypto_encap(ic, ni, m0); 1207 if (k == NULL) { 1208 m_freem(m0); 1209 return ENOBUFS; 1210 } 1211 1212 /* packet header may have moved, reset our local pointer */ 1213 wh = mtod(m0, struct ieee80211_frame *); 1214 } 1215 1216 data = &sc->tx_data[0]; 1217 desc = (struct rum_tx_desc *)data->buf; 1218 1219 data->ni = ni; 1220 1221 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1222 flags |= RT2573_TX_NEED_ACK; 1223 1224 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate), 1225 ic->ic_flags) + sc->sifs; 1226 *(uint16_t *)wh->i_dur = htole16(dur); 1227 } 1228 1229 #if NBPFILTER > 0 1230 if (sc->sc_drvbpf != NULL) { 1231 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 1232 1233 tap->wt_flags = 0; 1234 tap->wt_rate = rate; 1235 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1236 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1237 tap->wt_antenna = sc->tx_ant; 1238 1239 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1240 } 1241 #endif 1242 1243 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE); 1244 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate); 1245 1246 /* align end on a 4-bytes boundary */ 1247 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3; 1248 1249 /* 1250 * No space left in the last URB to store the extra 4 bytes, force 1251 * sending of another URB. 1252 */ 1253 if ((xferlen % 64) == 0) 1254 xferlen += 4; 1255 1256 DPRINTFN(10, ("sending data frame len=%zu rate=%u xfer len=%u\n", 1257 (size_t)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, 1258 rate, xferlen)); 1259 1260 /* mbuf is no longer needed */ 1261 m_freem(m0); 1262 1263 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen, 1264 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof); 1265 1266 error = usbd_transfer(data->xfer); 1267 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) 1268 return error; 1269 1270 sc->tx_queued++; 1271 1272 return 0; 1273 } 1274 1275 Static void 1276 rum_start(struct ifnet *ifp) 1277 { 1278 struct rum_softc *sc = ifp->if_softc; 1279 struct ieee80211com *ic = &sc->sc_ic; 1280 struct ether_header *eh; 1281 struct ieee80211_node *ni; 1282 struct mbuf *m0; 1283 1284 for (;;) { 1285 IF_POLL(&ic->ic_mgtq, m0); 1286 if (m0 != NULL) { 1287 if (sc->tx_queued >= RUM_TX_LIST_COUNT) { 1288 ifp->if_flags |= IFF_OACTIVE; 1289 break; 1290 } 1291 IF_DEQUEUE(&ic->ic_mgtq, m0); 1292 1293 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1294 m0->m_pkthdr.rcvif = NULL; 1295 #if NBPFILTER > 0 1296 if (ic->ic_rawbpf != NULL) 1297 bpf_mtap(ic->ic_rawbpf, m0); 1298 #endif 1299 if (rum_tx_mgt(sc, m0, ni) != 0) 1300 break; 1301 1302 } else { 1303 if (ic->ic_state != IEEE80211_S_RUN) 1304 break; 1305 IFQ_POLL(&ifp->if_snd, m0); 1306 if (m0 == NULL) 1307 break; 1308 if (sc->tx_queued >= RUM_TX_LIST_COUNT) { 1309 ifp->if_flags |= IFF_OACTIVE; 1310 break; 1311 } 1312 IFQ_DEQUEUE(&ifp->if_snd, m0); 1313 if (m0->m_len < sizeof(struct ether_header) && 1314 !(m0 = m_pullup(m0, sizeof(struct ether_header)))) 1315 continue; 1316 1317 eh = mtod(m0, struct ether_header *); 1318 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1319 if (ni == NULL) { 1320 m_freem(m0); 1321 continue; 1322 } 1323 #if NBPFILTER > 0 1324 if (ifp->if_bpf != NULL) 1325 bpf_mtap(ifp->if_bpf, m0); 1326 #endif 1327 m0 = ieee80211_encap(ic, m0, ni); 1328 if (m0 == NULL) { 1329 ieee80211_free_node(ni); 1330 continue; 1331 } 1332 #if NBPFILTER > 0 1333 if (ic->ic_rawbpf != NULL) 1334 bpf_mtap(ic->ic_rawbpf, m0); 1335 #endif 1336 if (rum_tx_data(sc, m0, ni) != 0) { 1337 ieee80211_free_node(ni); 1338 ifp->if_oerrors++; 1339 break; 1340 } 1341 } 1342 1343 sc->sc_tx_timer = 5; 1344 ifp->if_timer = 1; 1345 } 1346 } 1347 1348 Static void 1349 rum_watchdog(struct ifnet *ifp) 1350 { 1351 struct rum_softc *sc = ifp->if_softc; 1352 struct ieee80211com *ic = &sc->sc_ic; 1353 1354 ifp->if_timer = 0; 1355 1356 if (sc->sc_tx_timer > 0) { 1357 if (--sc->sc_tx_timer == 0) { 1358 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev)); 1359 /*rum_init(ifp); XXX needs a process context! */ 1360 ifp->if_oerrors++; 1361 return; 1362 } 1363 ifp->if_timer = 1; 1364 } 1365 1366 ieee80211_watchdog(ic); 1367 } 1368 1369 Static int 1370 rum_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1371 { 1372 struct rum_softc *sc = ifp->if_softc; 1373 struct ieee80211com *ic = &sc->sc_ic; 1374 int s, error = 0; 1375 1376 s = splnet(); 1377 1378 switch (cmd) { 1379 case SIOCSIFFLAGS: 1380 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1381 break; 1382 /* XXX re-use ether_ioctl() */ 1383 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 1384 case IFF_UP|IFF_RUNNING: 1385 rum_update_promisc(sc); 1386 break; 1387 case IFF_UP: 1388 rum_init(ifp); 1389 break; 1390 case IFF_RUNNING: 1391 rum_stop(ifp, 1); 1392 break; 1393 case 0: 1394 break; 1395 } 1396 break; 1397 1398 default: 1399 error = ieee80211_ioctl(ic, cmd, data); 1400 } 1401 1402 if (error == ENETRESET) { 1403 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1404 (IFF_UP | IFF_RUNNING)) 1405 rum_init(ifp); 1406 error = 0; 1407 } 1408 1409 splx(s); 1410 1411 return error; 1412 } 1413 1414 Static void 1415 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1416 { 1417 usb_device_request_t req; 1418 usbd_status error; 1419 1420 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1421 req.bRequest = RT2573_READ_EEPROM; 1422 USETW(req.wValue, 0); 1423 USETW(req.wIndex, addr); 1424 USETW(req.wLength, len); 1425 1426 error = usbd_do_request(sc->sc_udev, &req, buf); 1427 if (error != 0) { 1428 printf("%s: could not read EEPROM: %s\n", 1429 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1430 } 1431 } 1432 1433 Static uint32_t 1434 rum_read(struct rum_softc *sc, uint16_t reg) 1435 { 1436 uint32_t val; 1437 1438 rum_read_multi(sc, reg, &val, sizeof val); 1439 1440 return le32toh(val); 1441 } 1442 1443 Static void 1444 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1445 { 1446 usb_device_request_t req; 1447 usbd_status error; 1448 1449 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1450 req.bRequest = RT2573_READ_MULTI_MAC; 1451 USETW(req.wValue, 0); 1452 USETW(req.wIndex, reg); 1453 USETW(req.wLength, len); 1454 1455 error = usbd_do_request(sc->sc_udev, &req, buf); 1456 if (error != 0) { 1457 printf("%s: could not multi read MAC register: %s\n", 1458 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1459 } 1460 } 1461 1462 Static void 1463 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1464 { 1465 uint32_t tmp = htole32(val); 1466 1467 rum_write_multi(sc, reg, &tmp, sizeof tmp); 1468 } 1469 1470 Static void 1471 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1472 { 1473 usb_device_request_t req; 1474 usbd_status error; 1475 1476 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1477 req.bRequest = RT2573_WRITE_MULTI_MAC; 1478 USETW(req.wValue, 0); 1479 USETW(req.wIndex, reg); 1480 USETW(req.wLength, len); 1481 1482 error = usbd_do_request(sc->sc_udev, &req, buf); 1483 if (error != 0) { 1484 printf("%s: could not multi write MAC register: %s\n", 1485 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 1486 } 1487 } 1488 1489 Static void 1490 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1491 { 1492 uint32_t tmp; 1493 int ntries; 1494 1495 for (ntries = 0; ntries < 5; ntries++) { 1496 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1497 break; 1498 } 1499 if (ntries == 5) { 1500 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev)); 1501 return; 1502 } 1503 1504 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1505 rum_write(sc, RT2573_PHY_CSR3, tmp); 1506 } 1507 1508 Static uint8_t 1509 rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1510 { 1511 uint32_t val; 1512 int ntries; 1513 1514 for (ntries = 0; ntries < 5; ntries++) { 1515 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1516 break; 1517 } 1518 if (ntries == 5) { 1519 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev)); 1520 return 0; 1521 } 1522 1523 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1524 rum_write(sc, RT2573_PHY_CSR3, val); 1525 1526 for (ntries = 0; ntries < 100; ntries++) { 1527 val = rum_read(sc, RT2573_PHY_CSR3); 1528 if (!(val & RT2573_BBP_BUSY)) 1529 return val & 0xff; 1530 DELAY(1); 1531 } 1532 1533 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev)); 1534 return 0; 1535 } 1536 1537 Static void 1538 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1539 { 1540 uint32_t tmp; 1541 int ntries; 1542 1543 for (ntries = 0; ntries < 5; ntries++) { 1544 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1545 break; 1546 } 1547 if (ntries == 5) { 1548 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev)); 1549 return; 1550 } 1551 1552 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1553 (reg & 3); 1554 rum_write(sc, RT2573_PHY_CSR4, tmp); 1555 1556 /* remember last written value in sc */ 1557 sc->rf_regs[reg] = val; 1558 1559 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff)); 1560 } 1561 1562 Static void 1563 rum_select_antenna(struct rum_softc *sc) 1564 { 1565 uint8_t bbp4, bbp77; 1566 uint32_t tmp; 1567 1568 bbp4 = rum_bbp_read(sc, 4); 1569 bbp77 = rum_bbp_read(sc, 77); 1570 1571 /* TBD */ 1572 1573 /* make sure Rx is disabled before switching antenna */ 1574 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1575 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1576 1577 rum_bbp_write(sc, 4, bbp4); 1578 rum_bbp_write(sc, 77, bbp77); 1579 1580 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1581 } 1582 1583 /* 1584 * Enable multi-rate retries for frames sent at OFDM rates. 1585 * In 802.11b/g mode, allow fallback to CCK rates. 1586 */ 1587 Static void 1588 rum_enable_mrr(struct rum_softc *sc) 1589 { 1590 struct ieee80211com *ic = &sc->sc_ic; 1591 uint32_t tmp; 1592 1593 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1594 1595 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1596 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 1597 tmp |= RT2573_MRR_CCK_FALLBACK; 1598 tmp |= RT2573_MRR_ENABLED; 1599 1600 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1601 } 1602 1603 Static void 1604 rum_set_txpreamble(struct rum_softc *sc) 1605 { 1606 uint32_t tmp; 1607 1608 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1609 1610 tmp &= ~RT2573_SHORT_PREAMBLE; 1611 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE) 1612 tmp |= RT2573_SHORT_PREAMBLE; 1613 1614 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1615 } 1616 1617 Static void 1618 rum_set_basicrates(struct rum_softc *sc) 1619 { 1620 struct ieee80211com *ic = &sc->sc_ic; 1621 1622 /* update basic rate set */ 1623 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1624 /* 11b basic rates: 1, 2Mbps */ 1625 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1626 } else if (ic->ic_curmode == IEEE80211_MODE_11A) { 1627 /* 11a basic rates: 6, 12, 24Mbps */ 1628 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1629 } else { 1630 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1631 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1632 } 1633 } 1634 1635 /* 1636 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1637 * driver. 1638 */ 1639 Static void 1640 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1641 { 1642 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1643 uint32_t tmp; 1644 1645 /* update all BBP registers that depend on the band */ 1646 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1647 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1648 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1649 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1650 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1651 } 1652 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1653 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1654 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1655 } 1656 1657 sc->bbp17 = bbp17; 1658 rum_bbp_write(sc, 17, bbp17); 1659 rum_bbp_write(sc, 96, bbp96); 1660 rum_bbp_write(sc, 104, bbp104); 1661 1662 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1663 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1664 rum_bbp_write(sc, 75, 0x80); 1665 rum_bbp_write(sc, 86, 0x80); 1666 rum_bbp_write(sc, 88, 0x80); 1667 } 1668 1669 rum_bbp_write(sc, 35, bbp35); 1670 rum_bbp_write(sc, 97, bbp97); 1671 rum_bbp_write(sc, 98, bbp98); 1672 1673 tmp = rum_read(sc, RT2573_PHY_CSR0); 1674 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1675 if (IEEE80211_IS_CHAN_2GHZ(c)) 1676 tmp |= RT2573_PA_PE_2GHZ; 1677 else 1678 tmp |= RT2573_PA_PE_5GHZ; 1679 rum_write(sc, RT2573_PHY_CSR0, tmp); 1680 1681 /* 802.11a uses a 16 microseconds short interframe space */ 1682 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10; 1683 } 1684 1685 Static void 1686 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1687 { 1688 struct ieee80211com *ic = &sc->sc_ic; 1689 const struct rfprog *rfprog; 1690 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1691 int8_t power; 1692 u_int i, chan; 1693 1694 chan = ieee80211_chan2ieee(ic, c); 1695 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1696 return; 1697 1698 /* select the appropriate RF settings based on what EEPROM says */ 1699 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1700 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1701 1702 /* find the settings for this channel (we know it exists) */ 1703 for (i = 0; rfprog[i].chan != chan; i++); 1704 1705 power = sc->txpow[i]; 1706 if (power < 0) { 1707 bbp94 += power; 1708 power = 0; 1709 } else if (power > 31) { 1710 bbp94 += power - 31; 1711 power = 31; 1712 } 1713 1714 /* 1715 * If we are switching from the 2GHz band to the 5GHz band or 1716 * vice-versa, BBP registers need to be reprogrammed. 1717 */ 1718 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1719 rum_select_band(sc, c); 1720 rum_select_antenna(sc); 1721 } 1722 ic->ic_curchan = c; 1723 1724 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1725 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1726 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1727 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1728 1729 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1730 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1731 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1732 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1733 1734 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1735 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1736 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1737 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1738 1739 DELAY(10); 1740 1741 /* enable smart mode for MIMO-capable RFs */ 1742 bbp3 = rum_bbp_read(sc, 3); 1743 1744 bbp3 &= ~RT2573_SMART_MODE; 1745 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1746 bbp3 |= RT2573_SMART_MODE; 1747 1748 rum_bbp_write(sc, 3, bbp3); 1749 1750 if (bbp94 != RT2573_BBPR94_DEFAULT) 1751 rum_bbp_write(sc, 94, bbp94); 1752 } 1753 1754 /* 1755 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1756 * and HostAP operating modes. 1757 */ 1758 Static void 1759 rum_enable_tsf_sync(struct rum_softc *sc) 1760 { 1761 struct ieee80211com *ic = &sc->sc_ic; 1762 uint32_t tmp; 1763 1764 if (ic->ic_opmode != IEEE80211_M_STA) { 1765 /* 1766 * Change default 16ms TBTT adjustment to 8ms. 1767 * Must be done before enabling beacon generation. 1768 */ 1769 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1770 } 1771 1772 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1773 1774 /* set beacon interval (in 1/16ms unit) */ 1775 tmp |= ic->ic_bss->ni_intval * 16; 1776 1777 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1778 if (ic->ic_opmode == IEEE80211_M_STA) 1779 tmp |= RT2573_TSF_MODE(1); 1780 else 1781 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1782 1783 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1784 } 1785 1786 Static void 1787 rum_update_slot(struct rum_softc *sc) 1788 { 1789 struct ieee80211com *ic = &sc->sc_ic; 1790 uint8_t slottime; 1791 uint32_t tmp; 1792 1793 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1794 1795 tmp = rum_read(sc, RT2573_MAC_CSR9); 1796 tmp = (tmp & ~0xff) | slottime; 1797 rum_write(sc, RT2573_MAC_CSR9, tmp); 1798 1799 DPRINTF(("setting slot time to %uus\n", slottime)); 1800 } 1801 1802 Static void 1803 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1804 { 1805 uint32_t tmp; 1806 1807 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1808 rum_write(sc, RT2573_MAC_CSR4, tmp); 1809 1810 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1811 rum_write(sc, RT2573_MAC_CSR5, tmp); 1812 } 1813 1814 Static void 1815 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1816 { 1817 uint32_t tmp; 1818 1819 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1820 rum_write(sc, RT2573_MAC_CSR2, tmp); 1821 1822 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1823 rum_write(sc, RT2573_MAC_CSR3, tmp); 1824 } 1825 1826 Static void 1827 rum_update_promisc(struct rum_softc *sc) 1828 { 1829 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1830 uint32_t tmp; 1831 1832 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1833 1834 tmp &= ~RT2573_DROP_NOT_TO_ME; 1835 if (!(ifp->if_flags & IFF_PROMISC)) 1836 tmp |= RT2573_DROP_NOT_TO_ME; 1837 1838 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1839 1840 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1841 "entering" : "leaving")); 1842 } 1843 1844 Static const char * 1845 rum_get_rf(int rev) 1846 { 1847 switch (rev) { 1848 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1849 case RT2573_RF_2528: return "RT2528"; 1850 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1851 case RT2573_RF_5226: return "RT5226"; 1852 default: return "unknown"; 1853 } 1854 } 1855 1856 Static void 1857 rum_read_eeprom(struct rum_softc *sc) 1858 { 1859 struct ieee80211com *ic = &sc->sc_ic; 1860 uint16_t val; 1861 #ifdef RUM_DEBUG 1862 int i; 1863 #endif 1864 1865 /* read MAC/BBP type */ 1866 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2); 1867 sc->macbbp_rev = le16toh(val); 1868 1869 /* read MAC address */ 1870 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6); 1871 1872 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1873 val = le16toh(val); 1874 sc->rf_rev = (val >> 11) & 0x1f; 1875 sc->hw_radio = (val >> 10) & 0x1; 1876 sc->rx_ant = (val >> 4) & 0x3; 1877 sc->tx_ant = (val >> 2) & 0x3; 1878 sc->nb_ant = val & 0x3; 1879 1880 DPRINTF(("RF revision=%d\n", sc->rf_rev)); 1881 1882 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1883 val = le16toh(val); 1884 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1885 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1886 1887 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1888 sc->ext_2ghz_lna, sc->ext_5ghz_lna)); 1889 1890 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1891 val = le16toh(val); 1892 if ((val & 0xff) != 0xff) 1893 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1894 1895 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1896 val = le16toh(val); 1897 if ((val & 0xff) != 0xff) 1898 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1899 1900 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1901 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr)); 1902 1903 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1904 val = le16toh(val); 1905 if ((val & 0xff) != 0xff) 1906 sc->rffreq = val & 0xff; 1907 1908 DPRINTF(("RF freq=%d\n", sc->rffreq)); 1909 1910 /* read Tx power for all a/b/g channels */ 1911 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1912 /* XXX default Tx power for 802.11a channels */ 1913 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1914 #ifdef RUM_DEBUG 1915 for (i = 0; i < 14; i++) 1916 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i])); 1917 #endif 1918 1919 /* read default values for BBP registers */ 1920 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1921 #ifdef RUM_DEBUG 1922 for (i = 0; i < 14; i++) { 1923 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1924 continue; 1925 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1926 sc->bbp_prom[i].val)); 1927 } 1928 #endif 1929 } 1930 1931 Static int 1932 rum_bbp_init(struct rum_softc *sc) 1933 { 1934 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1935 int i, ntries; 1936 uint8_t val; 1937 1938 /* wait for BBP to be ready */ 1939 for (ntries = 0; ntries < 100; ntries++) { 1940 val = rum_bbp_read(sc, 0); 1941 if (val != 0 && val != 0xff) 1942 break; 1943 DELAY(1000); 1944 } 1945 if (ntries == 100) { 1946 printf("%s: timeout waiting for BBP\n", 1947 USBDEVNAME(sc->sc_dev)); 1948 return EIO; 1949 } 1950 1951 /* initialize BBP registers to default values */ 1952 for (i = 0; i < N(rum_def_bbp); i++) 1953 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1954 1955 /* write vendor-specific BBP values (from EEPROM) */ 1956 for (i = 0; i < 16; i++) { 1957 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1958 continue; 1959 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1960 } 1961 1962 return 0; 1963 #undef N 1964 } 1965 1966 Static int 1967 rum_init(struct ifnet *ifp) 1968 { 1969 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1970 struct rum_softc *sc = ifp->if_softc; 1971 struct ieee80211com *ic = &sc->sc_ic; 1972 struct rum_rx_data *data; 1973 uint32_t tmp; 1974 usbd_status error = 0; 1975 int i, ntries; 1976 1977 if ((sc->sc_flags & RT2573_FWLOADED) == 0) { 1978 if (rum_attachhook(sc)) 1979 goto fail; 1980 } 1981 1982 rum_stop(ifp, 0); 1983 1984 /* initialize MAC registers to default values */ 1985 for (i = 0; i < N(rum_def_mac); i++) 1986 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 1987 1988 /* set host ready */ 1989 rum_write(sc, RT2573_MAC_CSR1, 3); 1990 rum_write(sc, RT2573_MAC_CSR1, 0); 1991 1992 /* wait for BBP/RF to wakeup */ 1993 for (ntries = 0; ntries < 1000; ntries++) { 1994 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 1995 break; 1996 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 1997 DELAY(1000); 1998 } 1999 if (ntries == 1000) { 2000 printf("%s: timeout waiting for BBP/RF to wakeup\n", 2001 USBDEVNAME(sc->sc_dev)); 2002 goto fail; 2003 } 2004 2005 if ((error = rum_bbp_init(sc)) != 0) 2006 goto fail; 2007 2008 /* select default channel */ 2009 rum_select_band(sc, ic->ic_curchan); 2010 rum_select_antenna(sc); 2011 rum_set_chan(sc, ic->ic_curchan); 2012 2013 /* clear STA registers */ 2014 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2015 2016 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 2017 rum_set_macaddr(sc, ic->ic_myaddr); 2018 2019 /* initialize ASIC */ 2020 rum_write(sc, RT2573_MAC_CSR1, 4); 2021 2022 /* 2023 * Allocate xfer for AMRR statistics requests. 2024 */ 2025 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev); 2026 if (sc->amrr_xfer == NULL) { 2027 printf("%s: could not allocate AMRR xfer\n", 2028 USBDEVNAME(sc->sc_dev)); 2029 goto fail; 2030 } 2031 2032 /* 2033 * Open Tx and Rx USB bulk pipes. 2034 */ 2035 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE, 2036 &sc->sc_tx_pipeh); 2037 if (error != 0) { 2038 printf("%s: could not open Tx pipe: %s\n", 2039 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 2040 goto fail; 2041 } 2042 2043 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE, 2044 &sc->sc_rx_pipeh); 2045 if (error != 0) { 2046 printf("%s: could not open Rx pipe: %s\n", 2047 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 2048 goto fail; 2049 } 2050 2051 /* 2052 * Allocate Tx and Rx xfer queues. 2053 */ 2054 error = rum_alloc_tx_list(sc); 2055 if (error != 0) { 2056 printf("%s: could not allocate Tx list\n", 2057 USBDEVNAME(sc->sc_dev)); 2058 goto fail; 2059 } 2060 2061 error = rum_alloc_rx_list(sc); 2062 if (error != 0) { 2063 printf("%s: could not allocate Rx list\n", 2064 USBDEVNAME(sc->sc_dev)); 2065 goto fail; 2066 } 2067 2068 /* 2069 * Start up the receive pipe. 2070 */ 2071 for (i = 0; i < RUM_RX_LIST_COUNT; i++) { 2072 data = &sc->rx_data[i]; 2073 2074 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf, 2075 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof); 2076 error = usbd_transfer(data->xfer); 2077 if (error != USBD_NORMAL_COMPLETION && 2078 error != USBD_IN_PROGRESS) { 2079 printf("%s: could not queue Rx transfer\n", 2080 USBDEVNAME(sc->sc_dev)); 2081 goto fail; 2082 } 2083 } 2084 2085 /* update Rx filter */ 2086 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2087 2088 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2089 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2090 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2091 RT2573_DROP_ACKCTS; 2092 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2093 tmp |= RT2573_DROP_TODS; 2094 if (!(ifp->if_flags & IFF_PROMISC)) 2095 tmp |= RT2573_DROP_NOT_TO_ME; 2096 } 2097 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2098 2099 ifp->if_flags &= ~IFF_OACTIVE; 2100 ifp->if_flags |= IFF_RUNNING; 2101 2102 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2103 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2104 else 2105 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2106 2107 return 0; 2108 2109 fail: rum_stop(ifp, 1); 2110 return error; 2111 #undef N 2112 } 2113 2114 Static void 2115 rum_stop(struct ifnet *ifp, int disable) 2116 { 2117 struct rum_softc *sc = ifp->if_softc; 2118 struct ieee80211com *ic = &sc->sc_ic; 2119 uint32_t tmp; 2120 2121 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */ 2122 2123 sc->sc_tx_timer = 0; 2124 ifp->if_timer = 0; 2125 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2126 2127 /* disable Rx */ 2128 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2129 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2130 2131 /* reset ASIC */ 2132 rum_write(sc, RT2573_MAC_CSR1, 3); 2133 rum_write(sc, RT2573_MAC_CSR1, 0); 2134 2135 if (sc->sc_rx_pipeh != NULL) { 2136 usbd_abort_pipe(sc->sc_rx_pipeh); 2137 usbd_close_pipe(sc->sc_rx_pipeh); 2138 sc->sc_rx_pipeh = NULL; 2139 } 2140 2141 if (sc->sc_tx_pipeh != NULL) { 2142 usbd_abort_pipe(sc->sc_tx_pipeh); 2143 usbd_close_pipe(sc->sc_tx_pipeh); 2144 sc->sc_tx_pipeh = NULL; 2145 } 2146 2147 rum_free_rx_list(sc); 2148 rum_free_tx_list(sc); 2149 } 2150 2151 Static int 2152 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size) 2153 { 2154 usb_device_request_t req; 2155 uint16_t reg = RT2573_MCU_CODE_BASE; 2156 usbd_status error; 2157 2158 /* copy firmware image into NIC */ 2159 for (; size >= 4; reg += 4, ucode += 4, size -= 4) 2160 rum_write(sc, reg, UGETDW(ucode)); 2161 2162 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2163 req.bRequest = RT2573_MCU_CNTL; 2164 USETW(req.wValue, RT2573_MCU_RUN); 2165 USETW(req.wIndex, 0); 2166 USETW(req.wLength, 0); 2167 2168 error = usbd_do_request(sc->sc_udev, &req, NULL); 2169 if (error != 0) { 2170 printf("%s: could not run firmware: %s\n", 2171 USBDEVNAME(sc->sc_dev), usbd_errstr(error)); 2172 } 2173 return error; 2174 } 2175 2176 Static int 2177 rum_prepare_beacon(struct rum_softc *sc) 2178 { 2179 struct ieee80211com *ic = &sc->sc_ic; 2180 struct rum_tx_desc desc; 2181 struct mbuf *m0; 2182 int rate; 2183 2184 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo); 2185 if (m0 == NULL) { 2186 aprint_error_dev(sc->sc_dev, 2187 "could not allocate beacon frame\n"); 2188 return ENOBUFS; 2189 } 2190 2191 /* send beacons at the lowest available rate */ 2192 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 2193 2194 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2195 m0->m_pkthdr.len, rate); 2196 2197 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2198 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2199 2200 /* copy beacon header and payload into NIC memory */ 2201 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2202 m0->m_pkthdr.len); 2203 2204 m_freem(m0); 2205 2206 return 0; 2207 } 2208 2209 Static void 2210 rum_newassoc(struct ieee80211_node *ni, int isnew) 2211 { 2212 /* start with lowest Tx rate */ 2213 ni->ni_txrate = 0; 2214 } 2215 2216 Static void 2217 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni) 2218 { 2219 int i; 2220 2221 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2222 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2223 2224 ieee80211_amrr_node_init(&sc->amrr, &sc->amn); 2225 2226 /* set rate to some reasonable initial value */ 2227 for (i = ni->ni_rates.rs_nrates - 1; 2228 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 2229 i--); 2230 ni->ni_txrate = i; 2231 2232 usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc); 2233 } 2234 2235 Static void 2236 rum_amrr_timeout(void *arg) 2237 { 2238 struct rum_softc *sc = arg; 2239 usb_device_request_t req; 2240 2241 /* 2242 * Asynchronously read statistic registers (cleared by read). 2243 */ 2244 req.bmRequestType = UT_READ_VENDOR_DEVICE; 2245 req.bRequest = RT2573_READ_MULTI_MAC; 2246 USETW(req.wValue, 0); 2247 USETW(req.wIndex, RT2573_STA_CSR0); 2248 USETW(req.wLength, sizeof sc->sta); 2249 2250 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc, 2251 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0, 2252 rum_amrr_update); 2253 (void)usbd_transfer(sc->amrr_xfer); 2254 } 2255 2256 Static void 2257 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv, 2258 usbd_status status) 2259 { 2260 struct rum_softc *sc = (struct rum_softc *)priv; 2261 struct ifnet *ifp = sc->sc_ic.ic_ifp; 2262 2263 if (status != USBD_NORMAL_COMPLETION) { 2264 printf("%s: could not retrieve Tx statistics - cancelling " 2265 "automatic rate control\n", USBDEVNAME(sc->sc_dev)); 2266 return; 2267 } 2268 2269 /* count TX retry-fail as Tx errors */ 2270 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16; 2271 2272 sc->amn.amn_retrycnt = 2273 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */ 2274 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */ 2275 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2276 2277 sc->amn.amn_txcnt = 2278 sc->amn.amn_retrycnt + 2279 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */ 2280 2281 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn); 2282 2283 usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc); 2284 } 2285 2286 int 2287 rum_activate(device_ptr_t self, enum devact act) 2288 { 2289 switch (act) { 2290 case DVACT_DEACTIVATE: 2291 /*if_deactivate(&sc->sc_ic.ic_if);*/ 2292 return 0; 2293 default: 2294 return EOPNOTSUPP; 2295 } 2296 } 2297