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