1 /* $OpenBSD: if_rum.c,v 1.98 2011/07/03 15:47:17 matthew 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 1490 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1491 req.bRequest = RT2573_WRITE_MULTI_MAC; 1492 USETW(req.wValue, 0); 1493 USETW(req.wIndex, reg); 1494 USETW(req.wLength, len); 1495 1496 error = usbd_do_request(sc->sc_udev, &req, buf); 1497 if (error != 0) { 1498 printf("%s: could not multi write MAC register: %s\n", 1499 sc->sc_dev.dv_xname, usbd_errstr(error)); 1500 } 1501 } 1502 1503 void 1504 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1505 { 1506 uint32_t tmp; 1507 int ntries; 1508 1509 for (ntries = 0; ntries < 5; ntries++) { 1510 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1511 break; 1512 } 1513 if (ntries == 5) { 1514 printf("%s: could not write to BBP\n", sc->sc_dev.dv_xname); 1515 return; 1516 } 1517 1518 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1519 rum_write(sc, RT2573_PHY_CSR3, tmp); 1520 } 1521 1522 uint8_t 1523 rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1524 { 1525 uint32_t val; 1526 int ntries; 1527 1528 for (ntries = 0; ntries < 5; ntries++) { 1529 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1530 break; 1531 } 1532 if (ntries == 5) { 1533 printf("%s: could not read BBP\n", sc->sc_dev.dv_xname); 1534 return 0; 1535 } 1536 1537 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1538 rum_write(sc, RT2573_PHY_CSR3, val); 1539 1540 for (ntries = 0; ntries < 100; ntries++) { 1541 val = rum_read(sc, RT2573_PHY_CSR3); 1542 if (!(val & RT2573_BBP_BUSY)) 1543 return val & 0xff; 1544 DELAY(1); 1545 } 1546 1547 printf("%s: could not read BBP\n", sc->sc_dev.dv_xname); 1548 return 0; 1549 } 1550 1551 void 1552 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1553 { 1554 uint32_t tmp; 1555 int ntries; 1556 1557 for (ntries = 0; ntries < 5; ntries++) { 1558 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1559 break; 1560 } 1561 if (ntries == 5) { 1562 printf("%s: could not write to RF\n", sc->sc_dev.dv_xname); 1563 return; 1564 } 1565 1566 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1567 (reg & 3); 1568 rum_write(sc, RT2573_PHY_CSR4, tmp); 1569 1570 /* remember last written value in sc */ 1571 sc->rf_regs[reg] = val; 1572 1573 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff)); 1574 } 1575 1576 void 1577 rum_select_antenna(struct rum_softc *sc) 1578 { 1579 uint8_t bbp4, bbp77; 1580 uint32_t tmp; 1581 1582 bbp4 = rum_bbp_read(sc, 4); 1583 bbp77 = rum_bbp_read(sc, 77); 1584 1585 /* TBD */ 1586 1587 /* make sure Rx is disabled before switching antenna */ 1588 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1589 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1590 1591 rum_bbp_write(sc, 4, bbp4); 1592 rum_bbp_write(sc, 77, bbp77); 1593 1594 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1595 } 1596 1597 /* 1598 * Enable multi-rate retries for frames sent at OFDM rates. 1599 * In 802.11b/g mode, allow fallback to CCK rates. 1600 */ 1601 void 1602 rum_enable_mrr(struct rum_softc *sc) 1603 { 1604 struct ieee80211com *ic = &sc->sc_ic; 1605 uint32_t tmp; 1606 1607 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1608 1609 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1610 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) 1611 tmp |= RT2573_MRR_CCK_FALLBACK; 1612 tmp |= RT2573_MRR_ENABLED; 1613 1614 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1615 } 1616 1617 void 1618 rum_set_txpreamble(struct rum_softc *sc) 1619 { 1620 uint32_t tmp; 1621 1622 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1623 1624 tmp &= ~RT2573_SHORT_PREAMBLE; 1625 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE) 1626 tmp |= RT2573_SHORT_PREAMBLE; 1627 1628 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1629 } 1630 1631 void 1632 rum_set_basicrates(struct rum_softc *sc) 1633 { 1634 struct ieee80211com *ic = &sc->sc_ic; 1635 1636 /* update basic rate set */ 1637 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1638 /* 11b basic rates: 1, 2Mbps */ 1639 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1640 } else if (ic->ic_curmode == IEEE80211_MODE_11A) { 1641 /* 11a basic rates: 6, 12, 24Mbps */ 1642 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1643 } else { 1644 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1645 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1646 } 1647 } 1648 1649 /* 1650 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1651 * driver. 1652 */ 1653 void 1654 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1655 { 1656 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1657 uint32_t tmp; 1658 1659 /* update all BBP registers that depend on the band */ 1660 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1661 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1662 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1663 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1664 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1665 } 1666 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1667 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1668 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1669 } 1670 1671 sc->bbp17 = bbp17; 1672 rum_bbp_write(sc, 17, bbp17); 1673 rum_bbp_write(sc, 96, bbp96); 1674 rum_bbp_write(sc, 104, bbp104); 1675 1676 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1677 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1678 rum_bbp_write(sc, 75, 0x80); 1679 rum_bbp_write(sc, 86, 0x80); 1680 rum_bbp_write(sc, 88, 0x80); 1681 } 1682 1683 rum_bbp_write(sc, 35, bbp35); 1684 rum_bbp_write(sc, 97, bbp97); 1685 rum_bbp_write(sc, 98, bbp98); 1686 1687 tmp = rum_read(sc, RT2573_PHY_CSR0); 1688 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1689 if (IEEE80211_IS_CHAN_2GHZ(c)) 1690 tmp |= RT2573_PA_PE_2GHZ; 1691 else 1692 tmp |= RT2573_PA_PE_5GHZ; 1693 rum_write(sc, RT2573_PHY_CSR0, tmp); 1694 1695 /* 802.11a uses a 16 microseconds short interframe space */ 1696 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10; 1697 } 1698 1699 void 1700 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1701 { 1702 struct ieee80211com *ic = &sc->sc_ic; 1703 const struct rfprog *rfprog; 1704 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1705 int8_t power; 1706 u_int i, chan; 1707 1708 chan = ieee80211_chan2ieee(ic, c); 1709 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1710 return; 1711 1712 /* select the appropriate RF settings based on what EEPROM says */ 1713 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1714 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1715 1716 /* find the settings for this channel (we know it exists) */ 1717 for (i = 0; rfprog[i].chan != chan; i++); 1718 1719 power = sc->txpow[i]; 1720 if (power < 0) { 1721 bbp94 += power; 1722 power = 0; 1723 } else if (power > 31) { 1724 bbp94 += power - 31; 1725 power = 31; 1726 } 1727 1728 /* 1729 * If we are switching from the 2GHz band to the 5GHz band or 1730 * vice-versa, BBP registers need to be reprogrammed. 1731 */ 1732 if (c->ic_flags != sc->sc_curchan->ic_flags) { 1733 rum_select_band(sc, c); 1734 rum_select_antenna(sc); 1735 } 1736 sc->sc_curchan = c; 1737 1738 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1739 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1740 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1741 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 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 | 1); 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); 1751 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1752 1753 DELAY(10); 1754 1755 /* enable smart mode for MIMO-capable RFs */ 1756 bbp3 = rum_bbp_read(sc, 3); 1757 1758 bbp3 &= ~RT2573_SMART_MODE; 1759 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1760 bbp3 |= RT2573_SMART_MODE; 1761 1762 rum_bbp_write(sc, 3, bbp3); 1763 1764 if (bbp94 != RT2573_BBPR94_DEFAULT) 1765 rum_bbp_write(sc, 94, bbp94); 1766 } 1767 1768 /* 1769 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1770 * and HostAP operating modes. 1771 */ 1772 void 1773 rum_enable_tsf_sync(struct rum_softc *sc) 1774 { 1775 struct ieee80211com *ic = &sc->sc_ic; 1776 uint32_t tmp; 1777 1778 #ifndef IEEE80211_STA_ONLY 1779 if (ic->ic_opmode != IEEE80211_M_STA) { 1780 /* 1781 * Change default 16ms TBTT adjustment to 8ms. 1782 * Must be done before enabling beacon generation. 1783 */ 1784 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1785 } 1786 #endif 1787 1788 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1789 1790 /* set beacon interval (in 1/16ms unit) */ 1791 tmp |= ic->ic_bss->ni_intval * 16; 1792 1793 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1794 if (ic->ic_opmode == IEEE80211_M_STA) 1795 tmp |= RT2573_TSF_MODE(1); 1796 #ifndef IEEE80211_STA_ONLY 1797 else 1798 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1799 #endif 1800 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1801 } 1802 1803 void 1804 rum_update_slot(struct rum_softc *sc) 1805 { 1806 struct ieee80211com *ic = &sc->sc_ic; 1807 uint8_t slottime; 1808 uint32_t tmp; 1809 1810 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1811 1812 tmp = rum_read(sc, RT2573_MAC_CSR9); 1813 tmp = (tmp & ~0xff) | slottime; 1814 rum_write(sc, RT2573_MAC_CSR9, tmp); 1815 1816 DPRINTF(("setting slot time to %uus\n", slottime)); 1817 } 1818 1819 void 1820 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1821 { 1822 uint32_t tmp; 1823 1824 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1825 rum_write(sc, RT2573_MAC_CSR4, tmp); 1826 1827 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1828 rum_write(sc, RT2573_MAC_CSR5, tmp); 1829 } 1830 1831 void 1832 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1833 { 1834 uint32_t tmp; 1835 1836 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1837 rum_write(sc, RT2573_MAC_CSR2, tmp); 1838 1839 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1840 rum_write(sc, RT2573_MAC_CSR3, tmp); 1841 } 1842 1843 void 1844 rum_update_promisc(struct rum_softc *sc) 1845 { 1846 struct ifnet *ifp = &sc->sc_ic.ic_if; 1847 uint32_t tmp; 1848 1849 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1850 1851 tmp &= ~RT2573_DROP_NOT_TO_ME; 1852 if (!(ifp->if_flags & IFF_PROMISC)) 1853 tmp |= RT2573_DROP_NOT_TO_ME; 1854 1855 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1856 1857 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1858 "entering" : "leaving")); 1859 } 1860 1861 const char * 1862 rum_get_rf(int rev) 1863 { 1864 switch (rev) { 1865 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1866 case RT2573_RF_2528: return "RT2528"; 1867 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1868 case RT2573_RF_5226: return "RT5226"; 1869 default: return "unknown"; 1870 } 1871 } 1872 1873 void 1874 rum_read_eeprom(struct rum_softc *sc) 1875 { 1876 struct ieee80211com *ic = &sc->sc_ic; 1877 uint16_t val; 1878 #ifdef RUM_DEBUG 1879 int i; 1880 #endif 1881 1882 /* read MAC/BBP type */ 1883 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2); 1884 sc->macbbp_rev = letoh16(val); 1885 1886 /* read MAC address */ 1887 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6); 1888 1889 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1890 val = letoh16(val); 1891 sc->rf_rev = (val >> 11) & 0x1f; 1892 sc->hw_radio = (val >> 10) & 0x1; 1893 sc->rx_ant = (val >> 4) & 0x3; 1894 sc->tx_ant = (val >> 2) & 0x3; 1895 sc->nb_ant = val & 0x3; 1896 1897 DPRINTF(("RF revision=%d\n", sc->rf_rev)); 1898 1899 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1900 val = letoh16(val); 1901 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1902 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1903 1904 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1905 sc->ext_2ghz_lna, sc->ext_5ghz_lna)); 1906 1907 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1908 val = letoh16(val); 1909 if ((val & 0xff) != 0xff) 1910 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1911 1912 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1913 val = letoh16(val); 1914 if ((val & 0xff) != 0xff) 1915 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1916 1917 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1918 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr)); 1919 1920 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1921 val = letoh16(val); 1922 if ((val & 0xff) != 0xff) 1923 sc->rffreq = val & 0xff; 1924 1925 DPRINTF(("RF freq=%d\n", sc->rffreq)); 1926 1927 /* read Tx power for all a/b/g channels */ 1928 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1929 /* XXX default Tx power for 802.11a channels */ 1930 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1931 #ifdef RUM_DEBUG 1932 for (i = 0; i < 14; i++) 1933 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i])); 1934 #endif 1935 1936 /* read default values for BBP registers */ 1937 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1938 #ifdef RUM_DEBUG 1939 for (i = 0; i < 14; i++) { 1940 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1941 continue; 1942 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1943 sc->bbp_prom[i].val)); 1944 } 1945 #endif 1946 } 1947 1948 int 1949 rum_bbp_init(struct rum_softc *sc) 1950 { 1951 int i, ntries; 1952 1953 /* wait for BBP to be ready */ 1954 for (ntries = 0; ntries < 100; ntries++) { 1955 const uint8_t val = rum_bbp_read(sc, 0); 1956 if (val != 0 && val != 0xff) 1957 break; 1958 DELAY(1000); 1959 } 1960 if (ntries == 100) { 1961 printf("%s: timeout waiting for BBP\n", 1962 sc->sc_dev.dv_xname); 1963 return EIO; 1964 } 1965 1966 /* initialize BBP registers to default values */ 1967 for (i = 0; i < nitems(rum_def_bbp); i++) 1968 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1969 1970 /* write vendor-specific BBP values (from EEPROM) */ 1971 for (i = 0; i < 16; i++) { 1972 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1973 continue; 1974 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1975 } 1976 1977 return 0; 1978 } 1979 1980 int 1981 rum_init(struct ifnet *ifp) 1982 { 1983 struct rum_softc *sc = ifp->if_softc; 1984 struct ieee80211com *ic = &sc->sc_ic; 1985 uint32_t tmp; 1986 usbd_status error; 1987 int i, ntries; 1988 1989 rum_stop(ifp, 0); 1990 1991 /* initialize MAC registers to default values */ 1992 for (i = 0; i < nitems(rum_def_mac); i++) 1993 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 1994 1995 /* set host ready */ 1996 rum_write(sc, RT2573_MAC_CSR1, 3); 1997 rum_write(sc, RT2573_MAC_CSR1, 0); 1998 1999 /* wait for BBP/RF to wakeup */ 2000 for (ntries = 0; ntries < 1000; ntries++) { 2001 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 2002 break; 2003 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 2004 DELAY(1000); 2005 } 2006 if (ntries == 1000) { 2007 printf("%s: timeout waiting for BBP/RF to wakeup\n", 2008 sc->sc_dev.dv_xname); 2009 error = ENODEV; 2010 goto fail; 2011 } 2012 2013 if ((error = rum_bbp_init(sc)) != 0) 2014 goto fail; 2015 2016 /* select default channel */ 2017 sc->sc_curchan = ic->ic_bss->ni_chan = ic->ic_ibss_chan; 2018 rum_select_band(sc, sc->sc_curchan); 2019 rum_select_antenna(sc); 2020 rum_set_chan(sc, sc->sc_curchan); 2021 2022 /* clear STA registers */ 2023 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2024 2025 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 2026 rum_set_macaddr(sc, ic->ic_myaddr); 2027 2028 /* initialize ASIC */ 2029 rum_write(sc, RT2573_MAC_CSR1, 4); 2030 2031 /* 2032 * Allocate xfer for AMRR statistics requests. 2033 */ 2034 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev); 2035 if (sc->amrr_xfer == NULL) { 2036 printf("%s: could not allocate AMRR xfer\n", 2037 sc->sc_dev.dv_xname); 2038 goto fail; 2039 } 2040 2041 /* 2042 * Open Tx and Rx USB bulk pipes. 2043 */ 2044 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE, 2045 &sc->sc_tx_pipeh); 2046 if (error != 0) { 2047 printf("%s: could not open Tx pipe: %s\n", 2048 sc->sc_dev.dv_xname, usbd_errstr(error)); 2049 goto fail; 2050 } 2051 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE, 2052 &sc->sc_rx_pipeh); 2053 if (error != 0) { 2054 printf("%s: could not open Rx pipe: %s\n", 2055 sc->sc_dev.dv_xname, usbd_errstr(error)); 2056 goto fail; 2057 } 2058 2059 /* 2060 * Allocate Tx and Rx xfer queues. 2061 */ 2062 error = rum_alloc_tx_list(sc); 2063 if (error != 0) { 2064 printf("%s: could not allocate Tx list\n", 2065 sc->sc_dev.dv_xname); 2066 goto fail; 2067 } 2068 error = rum_alloc_rx_list(sc); 2069 if (error != 0) { 2070 printf("%s: could not allocate Rx list\n", 2071 sc->sc_dev.dv_xname); 2072 goto fail; 2073 } 2074 2075 /* 2076 * Start up the receive pipe. 2077 */ 2078 for (i = 0; i < RUM_RX_LIST_COUNT; i++) { 2079 struct rum_rx_data *data = &sc->rx_data[i]; 2080 2081 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf, 2082 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof); 2083 error = usbd_transfer(data->xfer); 2084 if (error != 0 && error != USBD_IN_PROGRESS) { 2085 printf("%s: could not queue Rx transfer\n", 2086 sc->sc_dev.dv_xname); 2087 goto fail; 2088 } 2089 } 2090 2091 /* update Rx filter */ 2092 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2093 2094 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2095 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2096 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2097 RT2573_DROP_ACKCTS; 2098 #ifndef IEEE80211_STA_ONLY 2099 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2100 #endif 2101 tmp |= RT2573_DROP_TODS; 2102 if (!(ifp->if_flags & IFF_PROMISC)) 2103 tmp |= RT2573_DROP_NOT_TO_ME; 2104 } 2105 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2106 2107 ifp->if_flags &= ~IFF_OACTIVE; 2108 ifp->if_flags |= IFF_RUNNING; 2109 2110 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2111 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2112 else 2113 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2114 2115 return 0; 2116 2117 fail: rum_stop(ifp, 1); 2118 return error; 2119 } 2120 2121 void 2122 rum_stop(struct ifnet *ifp, int disable) 2123 { 2124 struct rum_softc *sc = ifp->if_softc; 2125 struct ieee80211com *ic = &sc->sc_ic; 2126 uint32_t tmp; 2127 2128 sc->sc_tx_timer = 0; 2129 ifp->if_timer = 0; 2130 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2131 2132 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */ 2133 2134 /* disable Rx */ 2135 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2136 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2137 2138 /* reset ASIC */ 2139 rum_write(sc, RT2573_MAC_CSR1, 3); 2140 rum_write(sc, RT2573_MAC_CSR1, 0); 2141 2142 if (sc->amrr_xfer != NULL) { 2143 usbd_free_xfer(sc->amrr_xfer); 2144 sc->amrr_xfer = NULL; 2145 } 2146 if (sc->sc_rx_pipeh != NULL) { 2147 usbd_abort_pipe(sc->sc_rx_pipeh); 2148 usbd_close_pipe(sc->sc_rx_pipeh); 2149 sc->sc_rx_pipeh = NULL; 2150 } 2151 if (sc->sc_tx_pipeh != NULL) { 2152 usbd_abort_pipe(sc->sc_tx_pipeh); 2153 usbd_close_pipe(sc->sc_tx_pipeh); 2154 sc->sc_tx_pipeh = NULL; 2155 } 2156 2157 rum_free_rx_list(sc); 2158 rum_free_tx_list(sc); 2159 } 2160 2161 int 2162 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size) 2163 { 2164 usb_device_request_t req; 2165 uint16_t reg = RT2573_MCU_CODE_BASE; 2166 usbd_status error; 2167 2168 /* copy firmware image into NIC */ 2169 for (; size >= 4; reg += 4, ucode += 4, size -= 4) 2170 rum_write(sc, reg, UGETDW(ucode)); 2171 2172 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2173 req.bRequest = RT2573_MCU_CNTL; 2174 USETW(req.wValue, RT2573_MCU_RUN); 2175 USETW(req.wIndex, 0); 2176 USETW(req.wLength, 0); 2177 2178 error = usbd_do_request(sc->sc_udev, &req, NULL); 2179 if (error != 0) { 2180 printf("%s: could not run firmware: %s\n", 2181 sc->sc_dev.dv_xname, usbd_errstr(error)); 2182 } 2183 return error; 2184 } 2185 2186 #ifndef IEEE80211_STA_ONLY 2187 int 2188 rum_prepare_beacon(struct rum_softc *sc) 2189 { 2190 struct ieee80211com *ic = &sc->sc_ic; 2191 struct rum_tx_desc desc; 2192 struct mbuf *m0; 2193 int rate; 2194 2195 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss); 2196 if (m0 == NULL) { 2197 printf("%s: could not allocate beacon frame\n", 2198 sc->sc_dev.dv_xname); 2199 return ENOBUFS; 2200 } 2201 2202 /* send beacons at the lowest available rate */ 2203 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2; 2204 2205 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2206 m0->m_pkthdr.len, rate); 2207 2208 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2209 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2210 2211 /* copy beacon header and payload into NIC memory */ 2212 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2213 m0->m_pkthdr.len); 2214 2215 m_freem(m0); 2216 2217 return 0; 2218 } 2219 #endif 2220 2221 void 2222 rum_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 2223 { 2224 /* start with lowest Tx rate */ 2225 ni->ni_txrate = 0; 2226 } 2227 2228 void 2229 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni) 2230 { 2231 int i; 2232 2233 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2234 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2235 2236 ieee80211_amrr_node_init(&sc->amrr, &sc->amn); 2237 2238 /* set rate to some reasonable initial value */ 2239 for (i = ni->ni_rates.rs_nrates - 1; 2240 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 2241 i--); 2242 ni->ni_txrate = i; 2243 2244 if (!usbd_is_dying(sc->sc_udev)) 2245 timeout_add_sec(&sc->amrr_to, 1); 2246 } 2247 2248 void 2249 rum_amrr_timeout(void *arg) 2250 { 2251 struct rum_softc *sc = arg; 2252 usb_device_request_t req; 2253 2254 if (usbd_is_dying(sc->sc_udev)) 2255 return; 2256 2257 /* 2258 * Asynchronously read statistic registers (cleared by read). 2259 */ 2260 req.bmRequestType = UT_READ_VENDOR_DEVICE; 2261 req.bRequest = RT2573_READ_MULTI_MAC; 2262 USETW(req.wValue, 0); 2263 USETW(req.wIndex, RT2573_STA_CSR0); 2264 USETW(req.wLength, sizeof sc->sta); 2265 2266 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc, 2267 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0, 2268 rum_amrr_update); 2269 (void)usbd_transfer(sc->amrr_xfer); 2270 } 2271 2272 void 2273 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv, 2274 usbd_status status) 2275 { 2276 struct rum_softc *sc = (struct rum_softc *)priv; 2277 struct ifnet *ifp = &sc->sc_ic.ic_if; 2278 2279 if (status != USBD_NORMAL_COMPLETION) { 2280 printf("%s: could not retrieve Tx statistics - cancelling " 2281 "automatic rate control\n", sc->sc_dev.dv_xname); 2282 return; 2283 } 2284 2285 /* count TX retry-fail as Tx errors */ 2286 ifp->if_oerrors += letoh32(sc->sta[5]) >> 16; 2287 2288 sc->amn.amn_retrycnt = 2289 (letoh32(sc->sta[4]) >> 16) + /* TX one-retry ok count */ 2290 (letoh32(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */ 2291 (letoh32(sc->sta[5]) >> 16); /* TX retry-fail count */ 2292 2293 sc->amn.amn_txcnt = 2294 sc->amn.amn_retrycnt + 2295 (letoh32(sc->sta[4]) & 0xffff); /* TX no-retry ok count */ 2296 2297 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn); 2298 2299 if (!usbd_is_dying(sc->sc_udev)) 2300 timeout_add_sec(&sc->amrr_to, 1); 2301 } 2302 2303 int 2304 rum_activate(struct device *self, int act) 2305 { 2306 struct rum_softc *sc = (struct rum_softc *)self; 2307 2308 switch (act) { 2309 case DVACT_DEACTIVATE: 2310 usbd_deactivate(sc->sc_udev); 2311 break; 2312 } 2313 2314 return 0; 2315 } 2316