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