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