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