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