1 /* $FreeBSD$ */ 2 3 /*- 4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org> 6 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.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 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 /*- 25 * Ralink Technology RT2501USB/RT2601USB chipset driver 26 * http://www.ralinktech.com.tw/ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/sockio.h> 31 #include <sys/sysctl.h> 32 #include <sys/lock.h> 33 #include <sys/mutex.h> 34 #include <sys/mbuf.h> 35 #include <sys/kernel.h> 36 #include <sys/socket.h> 37 #include <sys/systm.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/endian.h> 42 #include <sys/kdb.h> 43 44 #include <machine/bus.h> 45 #include <machine/resource.h> 46 #include <sys/rman.h> 47 48 #include <net/bpf.h> 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_arp.h> 52 #include <net/ethernet.h> 53 #include <net/if_dl.h> 54 #include <net/if_media.h> 55 #include <net/if_types.h> 56 57 #ifdef INET 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_var.h> 61 #include <netinet/if_ether.h> 62 #include <netinet/ip.h> 63 #endif 64 65 #include <net80211/ieee80211_var.h> 66 #include <net80211/ieee80211_regdomain.h> 67 #include <net80211/ieee80211_radiotap.h> 68 #include <net80211/ieee80211_ratectl.h> 69 70 #include <dev/usb/usb.h> 71 #include <dev/usb/usbdi.h> 72 #include "usbdevs.h" 73 74 #define USB_DEBUG_VAR rum_debug 75 #include <dev/usb/usb_debug.h> 76 77 #include <dev/usb/wlan/if_rumreg.h> 78 #include <dev/usb/wlan/if_rumvar.h> 79 #include <dev/usb/wlan/if_rumfw.h> 80 81 #ifdef USB_DEBUG 82 static int rum_debug = 0; 83 84 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum"); 85 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0, 86 "Debug level"); 87 #endif 88 89 #define N(a) ((int)(sizeof (a) / sizeof ((a)[0]))) 90 91 static const STRUCT_USB_HOST_ID rum_devs[] = { 92 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 93 RUM_DEV(ABOCOM, HWU54DM), 94 RUM_DEV(ABOCOM, RT2573_2), 95 RUM_DEV(ABOCOM, RT2573_3), 96 RUM_DEV(ABOCOM, RT2573_4), 97 RUM_DEV(ABOCOM, WUG2700), 98 RUM_DEV(AMIT, CGWLUSB2GO), 99 RUM_DEV(ASUS, RT2573_1), 100 RUM_DEV(ASUS, RT2573_2), 101 RUM_DEV(BELKIN, F5D7050A), 102 RUM_DEV(BELKIN, F5D9050V3), 103 RUM_DEV(CISCOLINKSYS, WUSB54GC), 104 RUM_DEV(CISCOLINKSYS, WUSB54GR), 105 RUM_DEV(CONCEPTRONIC2, C54RU2), 106 RUM_DEV(COREGA, CGWLUSB2GL), 107 RUM_DEV(COREGA, CGWLUSB2GPX), 108 RUM_DEV(DICKSMITH, CWD854F), 109 RUM_DEV(DICKSMITH, RT2573), 110 RUM_DEV(EDIMAX, EW7318USG), 111 RUM_DEV(DLINK2, DWLG122C1), 112 RUM_DEV(DLINK2, WUA1340), 113 RUM_DEV(DLINK2, DWA111), 114 RUM_DEV(DLINK2, DWA110), 115 RUM_DEV(GIGABYTE, GNWB01GS), 116 RUM_DEV(GIGABYTE, GNWI05GS), 117 RUM_DEV(GIGASET, RT2573), 118 RUM_DEV(GOODWAY, RT2573), 119 RUM_DEV(GUILLEMOT, HWGUSB254LB), 120 RUM_DEV(GUILLEMOT, HWGUSB254V2AP), 121 RUM_DEV(HUAWEI3COM, WUB320G), 122 RUM_DEV(MELCO, G54HP), 123 RUM_DEV(MELCO, SG54HP), 124 RUM_DEV(MELCO, SG54HG), 125 RUM_DEV(MELCO, WLIUCG), 126 RUM_DEV(MELCO, WLRUCG), 127 RUM_DEV(MELCO, WLRUCGAOSS), 128 RUM_DEV(MSI, RT2573_1), 129 RUM_DEV(MSI, RT2573_2), 130 RUM_DEV(MSI, RT2573_3), 131 RUM_DEV(MSI, RT2573_4), 132 RUM_DEV(NOVATECH, RT2573), 133 RUM_DEV(PLANEX2, GWUS54HP), 134 RUM_DEV(PLANEX2, GWUS54MINI2), 135 RUM_DEV(PLANEX2, GWUSMM), 136 RUM_DEV(QCOM, RT2573), 137 RUM_DEV(QCOM, RT2573_2), 138 RUM_DEV(QCOM, RT2573_3), 139 RUM_DEV(RALINK, RT2573), 140 RUM_DEV(RALINK, RT2573_2), 141 RUM_DEV(RALINK, RT2671), 142 RUM_DEV(SITECOMEU, WL113R2), 143 RUM_DEV(SITECOMEU, WL172), 144 RUM_DEV(SPARKLAN, RT2573), 145 RUM_DEV(SURECOM, RT2573), 146 #undef RUM_DEV 147 }; 148 149 static device_probe_t rum_match; 150 static device_attach_t rum_attach; 151 static device_detach_t rum_detach; 152 153 static usb_callback_t rum_bulk_read_callback; 154 static usb_callback_t rum_bulk_write_callback; 155 156 static usb_error_t rum_do_request(struct rum_softc *sc, 157 struct usb_device_request *req, void *data); 158 static struct ieee80211vap *rum_vap_create(struct ieee80211com *, 159 const char [IFNAMSIZ], int, enum ieee80211_opmode, 160 int, const uint8_t [IEEE80211_ADDR_LEN], 161 const uint8_t [IEEE80211_ADDR_LEN]); 162 static void rum_vap_delete(struct ieee80211vap *); 163 static void rum_tx_free(struct rum_tx_data *, int); 164 static void rum_setup_tx_list(struct rum_softc *); 165 static void rum_unsetup_tx_list(struct rum_softc *); 166 static int rum_newstate(struct ieee80211vap *, 167 enum ieee80211_state, int); 168 static void rum_setup_tx_desc(struct rum_softc *, 169 struct rum_tx_desc *, uint32_t, uint16_t, int, 170 int); 171 static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 172 struct ieee80211_node *); 173 static int rum_tx_raw(struct rum_softc *, struct mbuf *, 174 struct ieee80211_node *, 175 const struct ieee80211_bpf_params *); 176 static int rum_tx_data(struct rum_softc *, struct mbuf *, 177 struct ieee80211_node *); 178 static void rum_start(struct ifnet *); 179 static int rum_ioctl(struct ifnet *, u_long, caddr_t); 180 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 181 int); 182 static uint32_t rum_read(struct rum_softc *, uint16_t); 183 static void rum_read_multi(struct rum_softc *, uint16_t, void *, 184 int); 185 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t); 186 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *, 187 size_t); 188 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 189 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 190 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 191 static void rum_select_antenna(struct rum_softc *); 192 static void rum_enable_mrr(struct rum_softc *); 193 static void rum_set_txpreamble(struct rum_softc *); 194 static void rum_set_basicrates(struct rum_softc *); 195 static void rum_select_band(struct rum_softc *, 196 struct ieee80211_channel *); 197 static void rum_set_chan(struct rum_softc *, 198 struct ieee80211_channel *); 199 static void rum_enable_tsf_sync(struct rum_softc *); 200 static void rum_enable_tsf(struct rum_softc *); 201 static void rum_update_slot(struct ifnet *); 202 static void rum_set_bssid(struct rum_softc *, const uint8_t *); 203 static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 204 static void rum_update_mcast(struct ieee80211com *); 205 static void rum_update_promisc(struct ieee80211com *); 206 static void rum_setpromisc(struct rum_softc *); 207 static const char *rum_get_rf(int); 208 static void rum_read_eeprom(struct rum_softc *); 209 static int rum_bbp_init(struct rum_softc *); 210 static void rum_init_locked(struct rum_softc *); 211 static void rum_init(void *); 212 static void rum_stop(struct rum_softc *); 213 static void rum_load_microcode(struct rum_softc *, const uint8_t *, 214 size_t); 215 static void rum_prepare_beacon(struct rum_softc *, 216 struct ieee80211vap *); 217 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *, 218 const struct ieee80211_bpf_params *); 219 static void rum_scan_start(struct ieee80211com *); 220 static void rum_scan_end(struct ieee80211com *); 221 static void rum_set_channel(struct ieee80211com *); 222 static int rum_get_rssi(struct rum_softc *, uint8_t); 223 static void rum_ratectl_start(struct rum_softc *, 224 struct ieee80211_node *); 225 static void rum_ratectl_timeout(void *); 226 static void rum_ratectl_task(void *, int); 227 static int rum_pause(struct rum_softc *, int); 228 229 static const struct { 230 uint32_t reg; 231 uint32_t val; 232 } rum_def_mac[] = { 233 { RT2573_TXRX_CSR0, 0x025fb032 }, 234 { RT2573_TXRX_CSR1, 0x9eaa9eaf }, 235 { RT2573_TXRX_CSR2, 0x8a8b8c8d }, 236 { RT2573_TXRX_CSR3, 0x00858687 }, 237 { RT2573_TXRX_CSR7, 0x2e31353b }, 238 { RT2573_TXRX_CSR8, 0x2a2a2a2c }, 239 { RT2573_TXRX_CSR15, 0x0000000f }, 240 { RT2573_MAC_CSR6, 0x00000fff }, 241 { RT2573_MAC_CSR8, 0x016c030a }, 242 { RT2573_MAC_CSR10, 0x00000718 }, 243 { RT2573_MAC_CSR12, 0x00000004 }, 244 { RT2573_MAC_CSR13, 0x00007f00 }, 245 { RT2573_SEC_CSR0, 0x00000000 }, 246 { RT2573_SEC_CSR1, 0x00000000 }, 247 { RT2573_SEC_CSR5, 0x00000000 }, 248 { RT2573_PHY_CSR1, 0x000023b0 }, 249 { RT2573_PHY_CSR5, 0x00040a06 }, 250 { RT2573_PHY_CSR6, 0x00080606 }, 251 { RT2573_PHY_CSR7, 0x00000408 }, 252 { RT2573_AIFSN_CSR, 0x00002273 }, 253 { RT2573_CWMIN_CSR, 0x00002344 }, 254 { RT2573_CWMAX_CSR, 0x000034aa } 255 }; 256 257 static const struct { 258 uint8_t reg; 259 uint8_t val; 260 } rum_def_bbp[] = { 261 { 3, 0x80 }, 262 { 15, 0x30 }, 263 { 17, 0x20 }, 264 { 21, 0xc8 }, 265 { 22, 0x38 }, 266 { 23, 0x06 }, 267 { 24, 0xfe }, 268 { 25, 0x0a }, 269 { 26, 0x0d }, 270 { 32, 0x0b }, 271 { 34, 0x12 }, 272 { 37, 0x07 }, 273 { 39, 0xf8 }, 274 { 41, 0x60 }, 275 { 53, 0x10 }, 276 { 54, 0x18 }, 277 { 60, 0x10 }, 278 { 61, 0x04 }, 279 { 62, 0x04 }, 280 { 75, 0xfe }, 281 { 86, 0xfe }, 282 { 88, 0xfe }, 283 { 90, 0x0f }, 284 { 99, 0x00 }, 285 { 102, 0x16 }, 286 { 107, 0x04 } 287 }; 288 289 static const struct rfprog { 290 uint8_t chan; 291 uint32_t r1, r2, r3, r4; 292 } rum_rf5226[] = { 293 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 }, 294 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 }, 295 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 }, 296 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 }, 297 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 }, 298 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 }, 299 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 }, 300 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 }, 301 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 }, 302 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 }, 303 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 }, 304 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 }, 305 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 }, 306 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 }, 307 308 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 }, 309 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 }, 310 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 }, 311 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 }, 312 313 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 }, 314 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 }, 315 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 }, 316 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 }, 317 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 }, 318 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 }, 319 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 }, 320 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 }, 321 322 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 }, 323 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 }, 324 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 }, 325 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 }, 326 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 }, 327 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 }, 328 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 }, 329 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 }, 330 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 }, 331 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 }, 332 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 }, 333 334 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 }, 335 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 }, 336 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 }, 337 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 }, 338 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 } 339 }, rum_rf5225[] = { 340 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 }, 341 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 }, 342 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 }, 343 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 }, 344 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 }, 345 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 }, 346 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 }, 347 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 }, 348 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 }, 349 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 }, 350 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 }, 351 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 }, 352 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 }, 353 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 }, 354 355 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 }, 356 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 }, 357 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 }, 358 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 }, 359 360 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 }, 361 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 }, 362 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 }, 363 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 }, 364 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 }, 365 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 }, 366 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 }, 367 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 }, 368 369 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 }, 370 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 }, 371 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 }, 372 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 }, 373 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 }, 374 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 }, 375 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 }, 376 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 }, 377 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 }, 378 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 }, 379 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 }, 380 381 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 }, 382 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 }, 383 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 }, 384 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 }, 385 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 } 386 }; 387 388 static const struct usb_config rum_config[RUM_N_TRANSFER] = { 389 [RUM_BULK_WR] = { 390 .type = UE_BULK, 391 .endpoint = UE_ADDR_ANY, 392 .direction = UE_DIR_OUT, 393 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8), 394 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 395 .callback = rum_bulk_write_callback, 396 .timeout = 5000, /* ms */ 397 }, 398 [RUM_BULK_RD] = { 399 .type = UE_BULK, 400 .endpoint = UE_ADDR_ANY, 401 .direction = UE_DIR_IN, 402 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE), 403 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 404 .callback = rum_bulk_read_callback, 405 }, 406 }; 407 408 static int 409 rum_match(device_t self) 410 { 411 struct usb_attach_arg *uaa = device_get_ivars(self); 412 413 if (uaa->usb_mode != USB_MODE_HOST) 414 return (ENXIO); 415 if (uaa->info.bConfigIndex != 0) 416 return (ENXIO); 417 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX) 418 return (ENXIO); 419 420 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa)); 421 } 422 423 static int 424 rum_attach(device_t self) 425 { 426 struct usb_attach_arg *uaa = device_get_ivars(self); 427 struct rum_softc *sc = device_get_softc(self); 428 struct ieee80211com *ic; 429 struct ifnet *ifp; 430 uint8_t iface_index, bands; 431 uint32_t tmp; 432 int error, ntries; 433 434 device_set_usb_desc(self); 435 sc->sc_udev = uaa->device; 436 sc->sc_dev = self; 437 438 mtx_init(&sc->sc_mtx, device_get_nameunit(self), 439 MTX_NETWORK_LOCK, MTX_DEF); 440 441 iface_index = RT2573_IFACE_INDEX; 442 error = usbd_transfer_setup(uaa->device, &iface_index, 443 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx); 444 if (error) { 445 device_printf(self, "could not allocate USB transfers, " 446 "err=%s\n", usbd_errstr(error)); 447 goto detach; 448 } 449 450 RUM_LOCK(sc); 451 /* retrieve RT2573 rev. no */ 452 for (ntries = 0; ntries < 100; ntries++) { 453 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 454 break; 455 if (rum_pause(sc, hz / 100)) 456 break; 457 } 458 if (ntries == 100) { 459 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n"); 460 RUM_UNLOCK(sc); 461 goto detach; 462 } 463 464 /* retrieve MAC address and various other things from EEPROM */ 465 rum_read_eeprom(sc); 466 467 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n", 468 tmp, rum_get_rf(sc->rf_rev)); 469 470 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode)); 471 RUM_UNLOCK(sc); 472 473 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 474 if (ifp == NULL) { 475 device_printf(sc->sc_dev, "can not if_alloc()\n"); 476 goto detach; 477 } 478 ic = ifp->if_l2com; 479 480 ifp->if_softc = sc; 481 if_initname(ifp, "rum", device_get_unit(sc->sc_dev)); 482 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 483 ifp->if_init = rum_init; 484 ifp->if_ioctl = rum_ioctl; 485 ifp->if_start = rum_start; 486 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 487 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 488 IFQ_SET_READY(&ifp->if_snd); 489 490 ic->ic_ifp = ifp; 491 ic->ic_softc = sc; 492 ic->ic_name = device_get_nameunit(self); 493 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 494 495 /* set device capabilities */ 496 ic->ic_caps = 497 IEEE80211_C_STA /* station mode supported */ 498 | IEEE80211_C_IBSS /* IBSS mode supported */ 499 | IEEE80211_C_MONITOR /* monitor mode supported */ 500 | IEEE80211_C_HOSTAP /* HostAp mode supported */ 501 | IEEE80211_C_TXPMGT /* tx power management */ 502 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 503 | IEEE80211_C_SHSLOT /* short slot time supported */ 504 | IEEE80211_C_BGSCAN /* bg scanning supported */ 505 | IEEE80211_C_WPA /* 802.11i */ 506 ; 507 508 bands = 0; 509 setbit(&bands, IEEE80211_MODE_11B); 510 setbit(&bands, IEEE80211_MODE_11G); 511 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) 512 setbit(&bands, IEEE80211_MODE_11A); 513 ieee80211_init_channels(ic, NULL, &bands); 514 515 ieee80211_ifattach(ic, sc->sc_bssid); 516 ic->ic_update_promisc = rum_update_promisc; 517 ic->ic_raw_xmit = rum_raw_xmit; 518 ic->ic_scan_start = rum_scan_start; 519 ic->ic_scan_end = rum_scan_end; 520 ic->ic_set_channel = rum_set_channel; 521 522 ic->ic_vap_create = rum_vap_create; 523 ic->ic_vap_delete = rum_vap_delete; 524 ic->ic_update_mcast = rum_update_mcast; 525 526 ieee80211_radiotap_attach(ic, 527 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 528 RT2573_TX_RADIOTAP_PRESENT, 529 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 530 RT2573_RX_RADIOTAP_PRESENT); 531 532 if (bootverbose) 533 ieee80211_announce(ic); 534 535 return (0); 536 537 detach: 538 rum_detach(self); 539 return (ENXIO); /* failure */ 540 } 541 542 static int 543 rum_detach(device_t self) 544 { 545 struct rum_softc *sc = device_get_softc(self); 546 struct ifnet *ifp = sc->sc_ifp; 547 struct ieee80211com *ic; 548 549 /* Prevent further ioctls */ 550 RUM_LOCK(sc); 551 sc->sc_detached = 1; 552 RUM_UNLOCK(sc); 553 554 /* stop all USB transfers */ 555 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER); 556 557 /* free TX list, if any */ 558 RUM_LOCK(sc); 559 rum_unsetup_tx_list(sc); 560 RUM_UNLOCK(sc); 561 562 if (ifp) { 563 ic = ifp->if_l2com; 564 ieee80211_ifdetach(ic); 565 if_free(ifp); 566 } 567 mtx_destroy(&sc->sc_mtx); 568 return (0); 569 } 570 571 static usb_error_t 572 rum_do_request(struct rum_softc *sc, 573 struct usb_device_request *req, void *data) 574 { 575 usb_error_t err; 576 int ntries = 10; 577 578 while (ntries--) { 579 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 580 req, data, 0, NULL, 250 /* ms */); 581 if (err == 0) 582 break; 583 584 DPRINTFN(1, "Control request failed, %s (retrying)\n", 585 usbd_errstr(err)); 586 if (rum_pause(sc, hz / 100)) 587 break; 588 } 589 return (err); 590 } 591 592 static struct ieee80211vap * 593 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 594 enum ieee80211_opmode opmode, int flags, 595 const uint8_t bssid[IEEE80211_ADDR_LEN], 596 const uint8_t mac[IEEE80211_ADDR_LEN]) 597 { 598 struct rum_softc *sc = ic->ic_ifp->if_softc; 599 struct rum_vap *rvp; 600 struct ieee80211vap *vap; 601 602 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 603 return NULL; 604 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap), 605 M_80211_VAP, M_NOWAIT | M_ZERO); 606 if (rvp == NULL) 607 return NULL; 608 vap = &rvp->vap; 609 /* enable s/w bmiss handling for sta mode */ 610 611 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 612 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) { 613 /* out of memory */ 614 free(rvp, M_80211_VAP); 615 return (NULL); 616 } 617 618 /* override state transition machine */ 619 rvp->newstate = vap->iv_newstate; 620 vap->iv_newstate = rum_newstate; 621 622 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0); 623 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp); 624 ieee80211_ratectl_init(vap); 625 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 626 /* complete setup */ 627 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 628 ic->ic_opmode = opmode; 629 return vap; 630 } 631 632 static void 633 rum_vap_delete(struct ieee80211vap *vap) 634 { 635 struct rum_vap *rvp = RUM_VAP(vap); 636 struct ieee80211com *ic = vap->iv_ic; 637 638 usb_callout_drain(&rvp->ratectl_ch); 639 ieee80211_draintask(ic, &rvp->ratectl_task); 640 ieee80211_ratectl_deinit(vap); 641 ieee80211_vap_detach(vap); 642 free(rvp, M_80211_VAP); 643 } 644 645 static void 646 rum_tx_free(struct rum_tx_data *data, int txerr) 647 { 648 struct rum_softc *sc = data->sc; 649 650 if (data->m != NULL) { 651 if (data->m->m_flags & M_TXCB) 652 ieee80211_process_callback(data->ni, data->m, 653 txerr ? ETIMEDOUT : 0); 654 m_freem(data->m); 655 data->m = NULL; 656 657 ieee80211_free_node(data->ni); 658 data->ni = NULL; 659 } 660 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 661 sc->tx_nfree++; 662 } 663 664 static void 665 rum_setup_tx_list(struct rum_softc *sc) 666 { 667 struct rum_tx_data *data; 668 int i; 669 670 sc->tx_nfree = 0; 671 STAILQ_INIT(&sc->tx_q); 672 STAILQ_INIT(&sc->tx_free); 673 674 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 675 data = &sc->tx_data[i]; 676 677 data->sc = sc; 678 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 679 sc->tx_nfree++; 680 } 681 } 682 683 static void 684 rum_unsetup_tx_list(struct rum_softc *sc) 685 { 686 struct rum_tx_data *data; 687 int i; 688 689 /* make sure any subsequent use of the queues will fail */ 690 sc->tx_nfree = 0; 691 STAILQ_INIT(&sc->tx_q); 692 STAILQ_INIT(&sc->tx_free); 693 694 /* free up all node references and mbufs */ 695 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 696 data = &sc->tx_data[i]; 697 698 if (data->m != NULL) { 699 m_freem(data->m); 700 data->m = NULL; 701 } 702 if (data->ni != NULL) { 703 ieee80211_free_node(data->ni); 704 data->ni = NULL; 705 } 706 } 707 } 708 709 static int 710 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 711 { 712 struct rum_vap *rvp = RUM_VAP(vap); 713 struct ieee80211com *ic = vap->iv_ic; 714 struct rum_softc *sc = ic->ic_ifp->if_softc; 715 const struct ieee80211_txparam *tp; 716 enum ieee80211_state ostate; 717 struct ieee80211_node *ni; 718 uint32_t tmp; 719 720 ostate = vap->iv_state; 721 DPRINTF("%s -> %s\n", 722 ieee80211_state_name[ostate], 723 ieee80211_state_name[nstate]); 724 725 IEEE80211_UNLOCK(ic); 726 RUM_LOCK(sc); 727 usb_callout_stop(&rvp->ratectl_ch); 728 729 switch (nstate) { 730 case IEEE80211_S_INIT: 731 if (ostate == IEEE80211_S_RUN) { 732 /* abort TSF synchronization */ 733 tmp = rum_read(sc, RT2573_TXRX_CSR9); 734 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 735 } 736 break; 737 738 case IEEE80211_S_RUN: 739 ni = ieee80211_ref_node(vap->iv_bss); 740 741 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 742 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) { 743 RUM_UNLOCK(sc); 744 IEEE80211_LOCK(ic); 745 ieee80211_free_node(ni); 746 return (-1); 747 } 748 rum_update_slot(ic->ic_ifp); 749 rum_enable_mrr(sc); 750 rum_set_txpreamble(sc); 751 rum_set_basicrates(sc); 752 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 753 rum_set_bssid(sc, sc->sc_bssid); 754 } 755 756 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 757 vap->iv_opmode == IEEE80211_M_IBSS) 758 rum_prepare_beacon(sc, vap); 759 760 if (vap->iv_opmode != IEEE80211_M_MONITOR) 761 rum_enable_tsf_sync(sc); 762 else 763 rum_enable_tsf(sc); 764 765 /* enable automatic rate adaptation */ 766 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 767 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 768 rum_ratectl_start(sc, ni); 769 ieee80211_free_node(ni); 770 break; 771 default: 772 break; 773 } 774 RUM_UNLOCK(sc); 775 IEEE80211_LOCK(ic); 776 return (rvp->newstate(vap, nstate, arg)); 777 } 778 779 static void 780 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 781 { 782 struct rum_softc *sc = usbd_xfer_softc(xfer); 783 struct ifnet *ifp = sc->sc_ifp; 784 struct ieee80211vap *vap; 785 struct rum_tx_data *data; 786 struct mbuf *m; 787 struct usb_page_cache *pc; 788 unsigned int len; 789 int actlen, sumlen; 790 791 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 792 793 switch (USB_GET_STATE(xfer)) { 794 case USB_ST_TRANSFERRED: 795 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 796 797 /* free resources */ 798 data = usbd_xfer_get_priv(xfer); 799 rum_tx_free(data, 0); 800 usbd_xfer_set_priv(xfer, NULL); 801 802 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 803 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 804 805 /* FALLTHROUGH */ 806 case USB_ST_SETUP: 807 tr_setup: 808 data = STAILQ_FIRST(&sc->tx_q); 809 if (data) { 810 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 811 m = data->m; 812 813 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) { 814 DPRINTFN(0, "data overflow, %u bytes\n", 815 m->m_pkthdr.len); 816 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 817 } 818 pc = usbd_xfer_get_frame(xfer, 0); 819 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 820 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 821 m->m_pkthdr.len); 822 823 vap = data->ni->ni_vap; 824 if (ieee80211_radiotap_active_vap(vap)) { 825 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 826 827 tap->wt_flags = 0; 828 tap->wt_rate = data->rate; 829 tap->wt_antenna = sc->tx_ant; 830 831 ieee80211_radiotap_tx(vap, m); 832 } 833 834 /* align end on a 4-bytes boundary */ 835 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 836 if ((len % 64) == 0) 837 len += 4; 838 839 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 840 m->m_pkthdr.len, len); 841 842 usbd_xfer_set_frame_len(xfer, 0, len); 843 usbd_xfer_set_priv(xfer, data); 844 845 usbd_transfer_submit(xfer); 846 } 847 RUM_UNLOCK(sc); 848 rum_start(ifp); 849 RUM_LOCK(sc); 850 break; 851 852 default: /* Error */ 853 DPRINTFN(11, "transfer error, %s\n", 854 usbd_errstr(error)); 855 856 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 857 data = usbd_xfer_get_priv(xfer); 858 if (data != NULL) { 859 rum_tx_free(data, error); 860 usbd_xfer_set_priv(xfer, NULL); 861 } 862 863 if (error != USB_ERR_CANCELLED) { 864 if (error == USB_ERR_TIMEOUT) 865 device_printf(sc->sc_dev, "device timeout\n"); 866 867 /* 868 * Try to clear stall first, also if other 869 * errors occur, hence clearing stall 870 * introduces a 50 ms delay: 871 */ 872 usbd_xfer_set_stall(xfer); 873 goto tr_setup; 874 } 875 break; 876 } 877 } 878 879 static void 880 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 881 { 882 struct rum_softc *sc = usbd_xfer_softc(xfer); 883 struct ifnet *ifp = sc->sc_ifp; 884 struct ieee80211com *ic = ifp->if_l2com; 885 struct ieee80211_node *ni; 886 struct mbuf *m = NULL; 887 struct usb_page_cache *pc; 888 uint32_t flags; 889 uint8_t rssi = 0; 890 int len; 891 892 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 893 894 switch (USB_GET_STATE(xfer)) { 895 case USB_ST_TRANSFERRED: 896 897 DPRINTFN(15, "rx done, actlen=%d\n", len); 898 899 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) { 900 DPRINTF("%s: xfer too short %d\n", 901 device_get_nameunit(sc->sc_dev), len); 902 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 903 goto tr_setup; 904 } 905 906 len -= RT2573_RX_DESC_SIZE; 907 pc = usbd_xfer_get_frame(xfer, 0); 908 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 909 910 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 911 flags = le32toh(sc->sc_rx_desc.flags); 912 if (flags & RT2573_RX_CRC_ERROR) { 913 /* 914 * This should not happen since we did not 915 * request to receive those frames when we 916 * filled RUM_TXRX_CSR2: 917 */ 918 DPRINTFN(5, "PHY or CRC error\n"); 919 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 920 goto tr_setup; 921 } 922 923 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 924 if (m == NULL) { 925 DPRINTF("could not allocate mbuf\n"); 926 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 927 goto tr_setup; 928 } 929 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 930 mtod(m, uint8_t *), len); 931 932 /* finalize mbuf */ 933 m->m_pkthdr.rcvif = ifp; 934 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 935 936 if (ieee80211_radiotap_active(ic)) { 937 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 938 939 /* XXX read tsf */ 940 tap->wr_flags = 0; 941 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 942 (flags & RT2573_RX_OFDM) ? 943 IEEE80211_T_OFDM : IEEE80211_T_CCK); 944 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 945 tap->wr_antnoise = RT2573_NOISE_FLOOR; 946 tap->wr_antenna = sc->rx_ant; 947 } 948 /* FALLTHROUGH */ 949 case USB_ST_SETUP: 950 tr_setup: 951 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 952 usbd_transfer_submit(xfer); 953 954 /* 955 * At the end of a USB callback it is always safe to unlock 956 * the private mutex of a device! That is why we do the 957 * "ieee80211_input" here, and not some lines up! 958 */ 959 RUM_UNLOCK(sc); 960 if (m) { 961 ni = ieee80211_find_rxnode(ic, 962 mtod(m, struct ieee80211_frame_min *)); 963 if (ni != NULL) { 964 (void) ieee80211_input(ni, m, rssi, 965 RT2573_NOISE_FLOOR); 966 ieee80211_free_node(ni); 967 } else 968 (void) ieee80211_input_all(ic, m, rssi, 969 RT2573_NOISE_FLOOR); 970 } 971 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 972 !IFQ_IS_EMPTY(&ifp->if_snd)) 973 rum_start(ifp); 974 RUM_LOCK(sc); 975 return; 976 977 default: /* Error */ 978 if (error != USB_ERR_CANCELLED) { 979 /* try to clear stall first */ 980 usbd_xfer_set_stall(xfer); 981 goto tr_setup; 982 } 983 return; 984 } 985 } 986 987 static uint8_t 988 rum_plcp_signal(int rate) 989 { 990 switch (rate) { 991 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 992 case 12: return 0xb; 993 case 18: return 0xf; 994 case 24: return 0xa; 995 case 36: return 0xe; 996 case 48: return 0x9; 997 case 72: return 0xd; 998 case 96: return 0x8; 999 case 108: return 0xc; 1000 1001 /* CCK rates (NB: not IEEE std, device-specific) */ 1002 case 2: return 0x0; 1003 case 4: return 0x1; 1004 case 11: return 0x2; 1005 case 22: return 0x3; 1006 } 1007 return 0xff; /* XXX unsupported/unknown rate */ 1008 } 1009 1010 static void 1011 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 1012 uint32_t flags, uint16_t xflags, int len, int rate) 1013 { 1014 struct ifnet *ifp = sc->sc_ifp; 1015 struct ieee80211com *ic = ifp->if_l2com; 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(RT2573_QID(0) | RT2573_AIFSN(2) | 1026 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10)); 1027 1028 /* setup PLCP fields */ 1029 desc->plcp_signal = rum_plcp_signal(rate); 1030 desc->plcp_service = 4; 1031 1032 len += IEEE80211_CRC_LEN; 1033 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1034 desc->flags |= htole32(RT2573_TX_OFDM); 1035 1036 plcp_length = len & 0xfff; 1037 desc->plcp_length_hi = plcp_length >> 6; 1038 desc->plcp_length_lo = plcp_length & 0x3f; 1039 } else { 1040 if (rate == 0) 1041 rate = 2; /* avoid division by zero */ 1042 plcp_length = (16 * len + rate - 1) / rate; 1043 if (rate == 22) { 1044 remainder = (16 * len) % 22; 1045 if (remainder != 0 && remainder < 7) 1046 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1047 } 1048 desc->plcp_length_hi = plcp_length >> 8; 1049 desc->plcp_length_lo = plcp_length & 0xff; 1050 1051 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1052 desc->plcp_signal |= 0x08; 1053 } 1054 } 1055 1056 static int 1057 rum_sendprot(struct rum_softc *sc, 1058 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1059 { 1060 struct ieee80211com *ic = ni->ni_ic; 1061 const struct ieee80211_frame *wh; 1062 struct rum_tx_data *data; 1063 struct mbuf *mprot; 1064 int protrate, ackrate, pktlen, flags, isshort; 1065 uint16_t dur; 1066 1067 RUM_LOCK_ASSERT(sc, MA_OWNED); 1068 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1069 ("protection %d", prot)); 1070 1071 wh = mtod(m, const struct ieee80211_frame *); 1072 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1073 1074 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1075 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1076 1077 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1078 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1079 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1080 flags = RT2573_TX_MORE_FRAG; 1081 if (prot == IEEE80211_PROT_RTSCTS) { 1082 /* NB: CTS is the same size as an ACK */ 1083 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1084 flags |= RT2573_TX_NEED_ACK; 1085 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1086 } else { 1087 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1088 } 1089 if (mprot == NULL) { 1090 /* XXX stat + msg */ 1091 return (ENOBUFS); 1092 } 1093 data = STAILQ_FIRST(&sc->tx_free); 1094 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1095 sc->tx_nfree--; 1096 1097 data->m = mprot; 1098 data->ni = ieee80211_ref_node(ni); 1099 data->rate = protrate; 1100 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate); 1101 1102 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1103 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1104 1105 return 0; 1106 } 1107 1108 static int 1109 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1110 { 1111 struct ieee80211vap *vap = ni->ni_vap; 1112 struct ifnet *ifp = sc->sc_ifp; 1113 struct ieee80211com *ic = ifp->if_l2com; 1114 struct rum_tx_data *data; 1115 struct ieee80211_frame *wh; 1116 const struct ieee80211_txparam *tp; 1117 struct ieee80211_key *k; 1118 uint32_t flags = 0; 1119 uint16_t dur; 1120 1121 RUM_LOCK_ASSERT(sc, MA_OWNED); 1122 1123 data = STAILQ_FIRST(&sc->tx_free); 1124 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1125 sc->tx_nfree--; 1126 1127 wh = mtod(m0, struct ieee80211_frame *); 1128 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1129 k = ieee80211_crypto_encap(ni, m0); 1130 if (k == NULL) { 1131 m_freem(m0); 1132 return ENOBUFS; 1133 } 1134 wh = mtod(m0, struct ieee80211_frame *); 1135 } 1136 1137 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1138 1139 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1140 flags |= RT2573_TX_NEED_ACK; 1141 1142 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1143 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1144 USETW(wh->i_dur, dur); 1145 1146 /* tell hardware to add timestamp for probe responses */ 1147 if ((wh->i_fc[0] & 1148 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1149 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1150 flags |= RT2573_TX_TIMESTAMP; 1151 } 1152 1153 data->m = m0; 1154 data->ni = ni; 1155 data->rate = tp->mgmtrate; 1156 1157 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate); 1158 1159 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1160 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1161 1162 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1163 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1164 1165 return (0); 1166 } 1167 1168 static int 1169 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1170 const struct ieee80211_bpf_params *params) 1171 { 1172 struct ieee80211com *ic = ni->ni_ic; 1173 struct rum_tx_data *data; 1174 uint32_t flags; 1175 int rate, error; 1176 1177 RUM_LOCK_ASSERT(sc, MA_OWNED); 1178 KASSERT(params != NULL, ("no raw xmit params")); 1179 1180 rate = params->ibp_rate0; 1181 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1182 m_freem(m0); 1183 return EINVAL; 1184 } 1185 flags = 0; 1186 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1187 flags |= RT2573_TX_NEED_ACK; 1188 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1189 error = rum_sendprot(sc, m0, ni, 1190 params->ibp_flags & IEEE80211_BPF_RTS ? 1191 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1192 rate); 1193 if (error || sc->tx_nfree == 0) { 1194 m_freem(m0); 1195 return ENOBUFS; 1196 } 1197 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1198 } 1199 1200 data = STAILQ_FIRST(&sc->tx_free); 1201 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1202 sc->tx_nfree--; 1203 1204 data->m = m0; 1205 data->ni = ni; 1206 data->rate = rate; 1207 1208 /* XXX need to setup descriptor ourself */ 1209 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1210 1211 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1212 m0->m_pkthdr.len, rate); 1213 1214 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1215 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1216 1217 return 0; 1218 } 1219 1220 static int 1221 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1222 { 1223 struct ieee80211vap *vap = ni->ni_vap; 1224 struct ifnet *ifp = sc->sc_ifp; 1225 struct ieee80211com *ic = ifp->if_l2com; 1226 struct rum_tx_data *data; 1227 struct ieee80211_frame *wh; 1228 const struct ieee80211_txparam *tp; 1229 struct ieee80211_key *k; 1230 uint32_t flags = 0; 1231 uint16_t dur; 1232 int error, rate; 1233 1234 RUM_LOCK_ASSERT(sc, MA_OWNED); 1235 1236 wh = mtod(m0, struct ieee80211_frame *); 1237 1238 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1239 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1240 rate = tp->mcastrate; 1241 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1242 rate = tp->ucastrate; 1243 else 1244 rate = ni->ni_txrate; 1245 1246 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1247 k = ieee80211_crypto_encap(ni, m0); 1248 if (k == NULL) { 1249 m_freem(m0); 1250 return ENOBUFS; 1251 } 1252 1253 /* packet header may have moved, reset our local pointer */ 1254 wh = mtod(m0, struct ieee80211_frame *); 1255 } 1256 1257 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1258 int prot = IEEE80211_PROT_NONE; 1259 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1260 prot = IEEE80211_PROT_RTSCTS; 1261 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1262 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1263 prot = ic->ic_protmode; 1264 if (prot != IEEE80211_PROT_NONE) { 1265 error = rum_sendprot(sc, m0, ni, prot, rate); 1266 if (error || sc->tx_nfree == 0) { 1267 m_freem(m0); 1268 return ENOBUFS; 1269 } 1270 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1271 } 1272 } 1273 1274 data = STAILQ_FIRST(&sc->tx_free); 1275 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1276 sc->tx_nfree--; 1277 1278 data->m = m0; 1279 data->ni = ni; 1280 data->rate = rate; 1281 1282 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1283 flags |= RT2573_TX_NEED_ACK; 1284 flags |= RT2573_TX_MORE_FRAG; 1285 1286 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1287 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1288 USETW(wh->i_dur, dur); 1289 } 1290 1291 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1292 1293 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1294 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1295 1296 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1297 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1298 1299 return 0; 1300 } 1301 1302 static void 1303 rum_start(struct ifnet *ifp) 1304 { 1305 struct rum_softc *sc = ifp->if_softc; 1306 struct ieee80211_node *ni; 1307 struct mbuf *m; 1308 1309 RUM_LOCK(sc); 1310 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1311 RUM_UNLOCK(sc); 1312 return; 1313 } 1314 for (;;) { 1315 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1316 if (m == NULL) 1317 break; 1318 if (sc->tx_nfree < RUM_TX_MINFREE) { 1319 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1320 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1321 break; 1322 } 1323 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1324 if (rum_tx_data(sc, m, ni) != 0) { 1325 ieee80211_free_node(ni); 1326 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1327 break; 1328 } 1329 } 1330 RUM_UNLOCK(sc); 1331 } 1332 1333 static int 1334 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1335 { 1336 struct rum_softc *sc = ifp->if_softc; 1337 struct ieee80211com *ic = ifp->if_l2com; 1338 struct ifreq *ifr = (struct ifreq *) data; 1339 int error; 1340 int startall = 0; 1341 1342 RUM_LOCK(sc); 1343 error = sc->sc_detached ? ENXIO : 0; 1344 RUM_UNLOCK(sc); 1345 if (error) 1346 return (error); 1347 1348 switch (cmd) { 1349 case SIOCSIFFLAGS: 1350 RUM_LOCK(sc); 1351 if (ifp->if_flags & IFF_UP) { 1352 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1353 rum_init_locked(sc); 1354 startall = 1; 1355 } else 1356 rum_setpromisc(sc); 1357 } else { 1358 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1359 rum_stop(sc); 1360 } 1361 RUM_UNLOCK(sc); 1362 if (startall) 1363 ieee80211_start_all(ic); 1364 break; 1365 case SIOCGIFMEDIA: 1366 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1367 break; 1368 case SIOCGIFADDR: 1369 error = ether_ioctl(ifp, cmd, data); 1370 break; 1371 default: 1372 error = EINVAL; 1373 break; 1374 } 1375 return error; 1376 } 1377 1378 static void 1379 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1380 { 1381 struct usb_device_request req; 1382 usb_error_t error; 1383 1384 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1385 req.bRequest = RT2573_READ_EEPROM; 1386 USETW(req.wValue, 0); 1387 USETW(req.wIndex, addr); 1388 USETW(req.wLength, len); 1389 1390 error = rum_do_request(sc, &req, buf); 1391 if (error != 0) { 1392 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1393 usbd_errstr(error)); 1394 } 1395 } 1396 1397 static uint32_t 1398 rum_read(struct rum_softc *sc, uint16_t reg) 1399 { 1400 uint32_t val; 1401 1402 rum_read_multi(sc, reg, &val, sizeof val); 1403 1404 return le32toh(val); 1405 } 1406 1407 static void 1408 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1409 { 1410 struct usb_device_request req; 1411 usb_error_t error; 1412 1413 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1414 req.bRequest = RT2573_READ_MULTI_MAC; 1415 USETW(req.wValue, 0); 1416 USETW(req.wIndex, reg); 1417 USETW(req.wLength, len); 1418 1419 error = rum_do_request(sc, &req, buf); 1420 if (error != 0) { 1421 device_printf(sc->sc_dev, 1422 "could not multi read MAC register: %s\n", 1423 usbd_errstr(error)); 1424 } 1425 } 1426 1427 static usb_error_t 1428 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1429 { 1430 uint32_t tmp = htole32(val); 1431 1432 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1433 } 1434 1435 static usb_error_t 1436 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1437 { 1438 struct usb_device_request req; 1439 usb_error_t error; 1440 size_t offset; 1441 1442 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1443 req.bRequest = RT2573_WRITE_MULTI_MAC; 1444 USETW(req.wValue, 0); 1445 1446 /* write at most 64 bytes at a time */ 1447 for (offset = 0; offset < len; offset += 64) { 1448 USETW(req.wIndex, reg + offset); 1449 USETW(req.wLength, MIN(len - offset, 64)); 1450 1451 error = rum_do_request(sc, &req, (char *)buf + offset); 1452 if (error != 0) { 1453 device_printf(sc->sc_dev, 1454 "could not multi write MAC register: %s\n", 1455 usbd_errstr(error)); 1456 return (error); 1457 } 1458 } 1459 1460 return (USB_ERR_NORMAL_COMPLETION); 1461 } 1462 1463 static void 1464 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1465 { 1466 uint32_t tmp; 1467 int ntries; 1468 1469 DPRINTFN(2, "reg=0x%08x\n", reg); 1470 1471 for (ntries = 0; ntries < 100; ntries++) { 1472 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1473 break; 1474 if (rum_pause(sc, hz / 100)) 1475 break; 1476 } 1477 if (ntries == 100) { 1478 device_printf(sc->sc_dev, "could not write to BBP\n"); 1479 return; 1480 } 1481 1482 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1483 rum_write(sc, RT2573_PHY_CSR3, tmp); 1484 } 1485 1486 static uint8_t 1487 rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1488 { 1489 uint32_t val; 1490 int ntries; 1491 1492 DPRINTFN(2, "reg=0x%08x\n", reg); 1493 1494 for (ntries = 0; ntries < 100; ntries++) { 1495 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1496 break; 1497 if (rum_pause(sc, hz / 100)) 1498 break; 1499 } 1500 if (ntries == 100) { 1501 device_printf(sc->sc_dev, "could not read BBP\n"); 1502 return 0; 1503 } 1504 1505 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1506 rum_write(sc, RT2573_PHY_CSR3, val); 1507 1508 for (ntries = 0; ntries < 100; ntries++) { 1509 val = rum_read(sc, RT2573_PHY_CSR3); 1510 if (!(val & RT2573_BBP_BUSY)) 1511 return val & 0xff; 1512 if (rum_pause(sc, hz / 100)) 1513 break; 1514 } 1515 1516 device_printf(sc->sc_dev, "could not read BBP\n"); 1517 return 0; 1518 } 1519 1520 static void 1521 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1522 { 1523 uint32_t tmp; 1524 int ntries; 1525 1526 for (ntries = 0; ntries < 100; ntries++) { 1527 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1528 break; 1529 if (rum_pause(sc, hz / 100)) 1530 break; 1531 } 1532 if (ntries == 100) { 1533 device_printf(sc->sc_dev, "could not write to RF\n"); 1534 return; 1535 } 1536 1537 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1538 (reg & 3); 1539 rum_write(sc, RT2573_PHY_CSR4, tmp); 1540 1541 /* remember last written value in sc */ 1542 sc->rf_regs[reg] = val; 1543 1544 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1545 } 1546 1547 static void 1548 rum_select_antenna(struct rum_softc *sc) 1549 { 1550 uint8_t bbp4, bbp77; 1551 uint32_t tmp; 1552 1553 bbp4 = rum_bbp_read(sc, 4); 1554 bbp77 = rum_bbp_read(sc, 77); 1555 1556 /* TBD */ 1557 1558 /* make sure Rx is disabled before switching antenna */ 1559 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1560 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1561 1562 rum_bbp_write(sc, 4, bbp4); 1563 rum_bbp_write(sc, 77, bbp77); 1564 1565 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1566 } 1567 1568 /* 1569 * Enable multi-rate retries for frames sent at OFDM rates. 1570 * In 802.11b/g mode, allow fallback to CCK rates. 1571 */ 1572 static void 1573 rum_enable_mrr(struct rum_softc *sc) 1574 { 1575 struct ifnet *ifp = sc->sc_ifp; 1576 struct ieee80211com *ic = ifp->if_l2com; 1577 uint32_t tmp; 1578 1579 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1580 1581 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1582 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) 1583 tmp |= RT2573_MRR_CCK_FALLBACK; 1584 tmp |= RT2573_MRR_ENABLED; 1585 1586 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1587 } 1588 1589 static void 1590 rum_set_txpreamble(struct rum_softc *sc) 1591 { 1592 struct ifnet *ifp = sc->sc_ifp; 1593 struct ieee80211com *ic = ifp->if_l2com; 1594 uint32_t tmp; 1595 1596 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1597 1598 tmp &= ~RT2573_SHORT_PREAMBLE; 1599 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1600 tmp |= RT2573_SHORT_PREAMBLE; 1601 1602 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1603 } 1604 1605 static void 1606 rum_set_basicrates(struct rum_softc *sc) 1607 { 1608 struct ifnet *ifp = sc->sc_ifp; 1609 struct ieee80211com *ic = ifp->if_l2com; 1610 1611 /* update basic rate set */ 1612 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1613 /* 11b basic rates: 1, 2Mbps */ 1614 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1615 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1616 /* 11a basic rates: 6, 12, 24Mbps */ 1617 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1618 } else { 1619 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1620 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1621 } 1622 } 1623 1624 /* 1625 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1626 * driver. 1627 */ 1628 static void 1629 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1630 { 1631 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1632 uint32_t tmp; 1633 1634 /* update all BBP registers that depend on the band */ 1635 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1636 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1637 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1638 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1639 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1640 } 1641 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1642 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1643 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1644 } 1645 1646 sc->bbp17 = bbp17; 1647 rum_bbp_write(sc, 17, bbp17); 1648 rum_bbp_write(sc, 96, bbp96); 1649 rum_bbp_write(sc, 104, bbp104); 1650 1651 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1652 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1653 rum_bbp_write(sc, 75, 0x80); 1654 rum_bbp_write(sc, 86, 0x80); 1655 rum_bbp_write(sc, 88, 0x80); 1656 } 1657 1658 rum_bbp_write(sc, 35, bbp35); 1659 rum_bbp_write(sc, 97, bbp97); 1660 rum_bbp_write(sc, 98, bbp98); 1661 1662 tmp = rum_read(sc, RT2573_PHY_CSR0); 1663 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1664 if (IEEE80211_IS_CHAN_2GHZ(c)) 1665 tmp |= RT2573_PA_PE_2GHZ; 1666 else 1667 tmp |= RT2573_PA_PE_5GHZ; 1668 rum_write(sc, RT2573_PHY_CSR0, tmp); 1669 } 1670 1671 static void 1672 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1673 { 1674 struct ifnet *ifp = sc->sc_ifp; 1675 struct ieee80211com *ic = ifp->if_l2com; 1676 const struct rfprog *rfprog; 1677 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1678 int8_t power; 1679 int i, chan; 1680 1681 chan = ieee80211_chan2ieee(ic, c); 1682 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1683 return; 1684 1685 /* select the appropriate RF settings based on what EEPROM says */ 1686 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1687 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1688 1689 /* find the settings for this channel (we know it exists) */ 1690 for (i = 0; rfprog[i].chan != chan; i++); 1691 1692 power = sc->txpow[i]; 1693 if (power < 0) { 1694 bbp94 += power; 1695 power = 0; 1696 } else if (power > 31) { 1697 bbp94 += power - 31; 1698 power = 31; 1699 } 1700 1701 /* 1702 * If we are switching from the 2GHz band to the 5GHz band or 1703 * vice-versa, BBP registers need to be reprogrammed. 1704 */ 1705 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1706 rum_select_band(sc, c); 1707 rum_select_antenna(sc); 1708 } 1709 ic->ic_curchan = c; 1710 1711 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1712 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1713 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1714 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1715 1716 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1717 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1718 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1719 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 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_pause(sc, hz / 100); 1727 1728 /* enable smart mode for MIMO-capable RFs */ 1729 bbp3 = rum_bbp_read(sc, 3); 1730 1731 bbp3 &= ~RT2573_SMART_MODE; 1732 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1733 bbp3 |= RT2573_SMART_MODE; 1734 1735 rum_bbp_write(sc, 3, bbp3); 1736 1737 if (bbp94 != RT2573_BBPR94_DEFAULT) 1738 rum_bbp_write(sc, 94, bbp94); 1739 1740 /* give the chip some extra time to do the switchover */ 1741 rum_pause(sc, hz / 100); 1742 } 1743 1744 /* 1745 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1746 * and HostAP operating modes. 1747 */ 1748 static void 1749 rum_enable_tsf_sync(struct rum_softc *sc) 1750 { 1751 struct ifnet *ifp = sc->sc_ifp; 1752 struct ieee80211com *ic = ifp->if_l2com; 1753 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1754 uint32_t tmp; 1755 1756 if (vap->iv_opmode != IEEE80211_M_STA) { 1757 /* 1758 * Change default 16ms TBTT adjustment to 8ms. 1759 * Must be done before enabling beacon generation. 1760 */ 1761 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1762 } 1763 1764 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1765 1766 /* set beacon interval (in 1/16ms unit) */ 1767 tmp |= vap->iv_bss->ni_intval * 16; 1768 1769 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1770 if (vap->iv_opmode == IEEE80211_M_STA) 1771 tmp |= RT2573_TSF_MODE(1); 1772 else 1773 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1774 1775 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1776 } 1777 1778 static void 1779 rum_enable_tsf(struct rum_softc *sc) 1780 { 1781 rum_write(sc, RT2573_TXRX_CSR9, 1782 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) | 1783 RT2573_TSF_TICKING | RT2573_TSF_MODE(2)); 1784 } 1785 1786 static void 1787 rum_update_slot(struct ifnet *ifp) 1788 { 1789 struct rum_softc *sc = ifp->if_softc; 1790 struct ieee80211com *ic = ifp->if_l2com; 1791 uint8_t slottime; 1792 uint32_t tmp; 1793 1794 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1795 1796 tmp = rum_read(sc, RT2573_MAC_CSR9); 1797 tmp = (tmp & ~0xff) | slottime; 1798 rum_write(sc, RT2573_MAC_CSR9, tmp); 1799 1800 DPRINTF("setting slot time to %uus\n", slottime); 1801 } 1802 1803 static void 1804 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1805 { 1806 uint32_t tmp; 1807 1808 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1809 rum_write(sc, RT2573_MAC_CSR4, tmp); 1810 1811 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1812 rum_write(sc, RT2573_MAC_CSR5, tmp); 1813 } 1814 1815 static void 1816 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1817 { 1818 uint32_t tmp; 1819 1820 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1821 rum_write(sc, RT2573_MAC_CSR2, tmp); 1822 1823 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1824 rum_write(sc, RT2573_MAC_CSR3, tmp); 1825 } 1826 1827 static void 1828 rum_setpromisc(struct rum_softc *sc) 1829 { 1830 struct ifnet *ifp = sc->sc_ifp; 1831 uint32_t tmp; 1832 1833 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1834 1835 tmp &= ~RT2573_DROP_NOT_TO_ME; 1836 if (!(ifp->if_flags & IFF_PROMISC)) 1837 tmp |= RT2573_DROP_NOT_TO_ME; 1838 1839 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1840 1841 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1842 "entering" : "leaving"); 1843 } 1844 1845 static void 1846 rum_update_promisc(struct ieee80211com *ic) 1847 { 1848 struct rum_softc *sc = ic->ic_softc; 1849 1850 if ((ic->ic_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1851 return; 1852 1853 RUM_LOCK(sc); 1854 rum_setpromisc(sc); 1855 RUM_UNLOCK(sc); 1856 } 1857 1858 static void 1859 rum_update_mcast(struct ieee80211com *ic) 1860 { 1861 static int warning_printed; 1862 1863 if (warning_printed == 0) { 1864 ic_printf(ic, "need to implement %s\n", __func__); 1865 warning_printed = 1; 1866 } 1867 } 1868 1869 static const char * 1870 rum_get_rf(int rev) 1871 { 1872 switch (rev) { 1873 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1874 case RT2573_RF_2528: return "RT2528"; 1875 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1876 case RT2573_RF_5226: return "RT5226"; 1877 default: return "unknown"; 1878 } 1879 } 1880 1881 static void 1882 rum_read_eeprom(struct rum_softc *sc) 1883 { 1884 uint16_t val; 1885 #ifdef RUM_DEBUG 1886 int i; 1887 #endif 1888 1889 /* read MAC address */ 1890 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6); 1891 1892 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1893 val = le16toh(val); 1894 sc->rf_rev = (val >> 11) & 0x1f; 1895 sc->hw_radio = (val >> 10) & 0x1; 1896 sc->rx_ant = (val >> 4) & 0x3; 1897 sc->tx_ant = (val >> 2) & 0x3; 1898 sc->nb_ant = val & 0x3; 1899 1900 DPRINTF("RF revision=%d\n", sc->rf_rev); 1901 1902 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1903 val = le16toh(val); 1904 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1905 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1906 1907 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1908 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 1909 1910 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1911 val = le16toh(val); 1912 if ((val & 0xff) != 0xff) 1913 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1914 1915 /* Only [-10, 10] is valid */ 1916 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 1917 sc->rssi_2ghz_corr = 0; 1918 1919 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1920 val = le16toh(val); 1921 if ((val & 0xff) != 0xff) 1922 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1923 1924 /* Only [-10, 10] is valid */ 1925 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 1926 sc->rssi_5ghz_corr = 0; 1927 1928 if (sc->ext_2ghz_lna) 1929 sc->rssi_2ghz_corr -= 14; 1930 if (sc->ext_5ghz_lna) 1931 sc->rssi_5ghz_corr -= 14; 1932 1933 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1934 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 1935 1936 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1937 val = le16toh(val); 1938 if ((val & 0xff) != 0xff) 1939 sc->rffreq = val & 0xff; 1940 1941 DPRINTF("RF freq=%d\n", sc->rffreq); 1942 1943 /* read Tx power for all a/b/g channels */ 1944 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1945 /* XXX default Tx power for 802.11a channels */ 1946 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1947 #ifdef RUM_DEBUG 1948 for (i = 0; i < 14; i++) 1949 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 1950 #endif 1951 1952 /* read default values for BBP registers */ 1953 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1954 #ifdef RUM_DEBUG 1955 for (i = 0; i < 14; i++) { 1956 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1957 continue; 1958 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1959 sc->bbp_prom[i].val); 1960 } 1961 #endif 1962 } 1963 1964 static int 1965 rum_bbp_init(struct rum_softc *sc) 1966 { 1967 int i, ntries; 1968 1969 /* wait for BBP to be ready */ 1970 for (ntries = 0; ntries < 100; ntries++) { 1971 const uint8_t val = rum_bbp_read(sc, 0); 1972 if (val != 0 && val != 0xff) 1973 break; 1974 if (rum_pause(sc, hz / 100)) 1975 break; 1976 } 1977 if (ntries == 100) { 1978 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 1979 return EIO; 1980 } 1981 1982 /* initialize BBP registers to default values */ 1983 for (i = 0; i < N(rum_def_bbp); i++) 1984 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1985 1986 /* write vendor-specific BBP values (from EEPROM) */ 1987 for (i = 0; i < 16; i++) { 1988 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1989 continue; 1990 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1991 } 1992 1993 return 0; 1994 } 1995 1996 static void 1997 rum_init_locked(struct rum_softc *sc) 1998 { 1999 struct ifnet *ifp = sc->sc_ifp; 2000 struct ieee80211com *ic = ifp->if_l2com; 2001 uint32_t tmp; 2002 usb_error_t error; 2003 int i, ntries; 2004 2005 RUM_LOCK_ASSERT(sc, MA_OWNED); 2006 2007 rum_stop(sc); 2008 2009 /* initialize MAC registers to default values */ 2010 for (i = 0; i < N(rum_def_mac); i++) 2011 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 2012 2013 /* set host ready */ 2014 rum_write(sc, RT2573_MAC_CSR1, 3); 2015 rum_write(sc, RT2573_MAC_CSR1, 0); 2016 2017 /* wait for BBP/RF to wakeup */ 2018 for (ntries = 0; ntries < 100; ntries++) { 2019 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 2020 break; 2021 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 2022 if (rum_pause(sc, hz / 100)) 2023 break; 2024 } 2025 if (ntries == 100) { 2026 device_printf(sc->sc_dev, 2027 "timeout waiting for BBP/RF to wakeup\n"); 2028 goto fail; 2029 } 2030 2031 if ((error = rum_bbp_init(sc)) != 0) 2032 goto fail; 2033 2034 /* select default channel */ 2035 rum_select_band(sc, ic->ic_curchan); 2036 rum_select_antenna(sc); 2037 rum_set_chan(sc, ic->ic_curchan); 2038 2039 /* clear STA registers */ 2040 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2041 2042 rum_set_macaddr(sc, IF_LLADDR(ifp)); 2043 2044 /* initialize ASIC */ 2045 rum_write(sc, RT2573_MAC_CSR1, 4); 2046 2047 /* 2048 * Allocate Tx and Rx xfer queues. 2049 */ 2050 rum_setup_tx_list(sc); 2051 2052 /* update Rx filter */ 2053 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2054 2055 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2056 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2057 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2058 RT2573_DROP_ACKCTS; 2059 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2060 tmp |= RT2573_DROP_TODS; 2061 if (!(ifp->if_flags & IFF_PROMISC)) 2062 tmp |= RT2573_DROP_NOT_TO_ME; 2063 } 2064 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2065 2066 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2067 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2068 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2069 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2070 return; 2071 2072 fail: rum_stop(sc); 2073 #undef N 2074 } 2075 2076 static void 2077 rum_init(void *priv) 2078 { 2079 struct rum_softc *sc = priv; 2080 struct ifnet *ifp = sc->sc_ifp; 2081 struct ieee80211com *ic = ifp->if_l2com; 2082 2083 RUM_LOCK(sc); 2084 rum_init_locked(sc); 2085 RUM_UNLOCK(sc); 2086 2087 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2088 ieee80211_start_all(ic); /* start all vap's */ 2089 } 2090 2091 static void 2092 rum_stop(struct rum_softc *sc) 2093 { 2094 struct ifnet *ifp = sc->sc_ifp; 2095 uint32_t tmp; 2096 2097 RUM_LOCK_ASSERT(sc, MA_OWNED); 2098 2099 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2100 2101 RUM_UNLOCK(sc); 2102 2103 /* 2104 * Drain the USB transfers, if not already drained: 2105 */ 2106 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2107 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2108 2109 RUM_LOCK(sc); 2110 2111 rum_unsetup_tx_list(sc); 2112 2113 /* disable Rx */ 2114 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2115 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2116 2117 /* reset ASIC */ 2118 rum_write(sc, RT2573_MAC_CSR1, 3); 2119 rum_write(sc, RT2573_MAC_CSR1, 0); 2120 } 2121 2122 static void 2123 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2124 { 2125 struct usb_device_request req; 2126 uint16_t reg = RT2573_MCU_CODE_BASE; 2127 usb_error_t err; 2128 2129 /* copy firmware image into NIC */ 2130 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2131 err = rum_write(sc, reg, UGETDW(ucode)); 2132 if (err) { 2133 /* firmware already loaded ? */ 2134 device_printf(sc->sc_dev, "Firmware load " 2135 "failure! (ignored)\n"); 2136 break; 2137 } 2138 } 2139 2140 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2141 req.bRequest = RT2573_MCU_CNTL; 2142 USETW(req.wValue, RT2573_MCU_RUN); 2143 USETW(req.wIndex, 0); 2144 USETW(req.wLength, 0); 2145 2146 err = rum_do_request(sc, &req, NULL); 2147 if (err != 0) { 2148 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2149 usbd_errstr(err)); 2150 } 2151 2152 /* give the chip some time to boot */ 2153 rum_pause(sc, hz / 8); 2154 } 2155 2156 static void 2157 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2158 { 2159 struct ieee80211com *ic = vap->iv_ic; 2160 const struct ieee80211_txparam *tp; 2161 struct rum_tx_desc desc; 2162 struct mbuf *m0; 2163 2164 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC) 2165 return; 2166 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 2167 return; 2168 2169 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo); 2170 if (m0 == NULL) 2171 return; 2172 2173 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2174 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2175 m0->m_pkthdr.len, tp->mgmtrate); 2176 2177 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2178 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2179 2180 /* copy beacon header and payload into NIC memory */ 2181 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2182 m0->m_pkthdr.len); 2183 2184 m_freem(m0); 2185 } 2186 2187 static int 2188 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2189 const struct ieee80211_bpf_params *params) 2190 { 2191 struct ifnet *ifp = ni->ni_ic->ic_ifp; 2192 struct rum_softc *sc = ifp->if_softc; 2193 2194 RUM_LOCK(sc); 2195 /* prevent management frames from being sent if we're not ready */ 2196 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2197 RUM_UNLOCK(sc); 2198 m_freem(m); 2199 ieee80211_free_node(ni); 2200 return ENETDOWN; 2201 } 2202 if (sc->tx_nfree < RUM_TX_MINFREE) { 2203 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2204 RUM_UNLOCK(sc); 2205 m_freem(m); 2206 ieee80211_free_node(ni); 2207 return EIO; 2208 } 2209 2210 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2211 2212 if (params == NULL) { 2213 /* 2214 * Legacy path; interpret frame contents to decide 2215 * precisely how to send the frame. 2216 */ 2217 if (rum_tx_mgt(sc, m, ni) != 0) 2218 goto bad; 2219 } else { 2220 /* 2221 * Caller supplied explicit parameters to use in 2222 * sending the frame. 2223 */ 2224 if (rum_tx_raw(sc, m, ni, params) != 0) 2225 goto bad; 2226 } 2227 RUM_UNLOCK(sc); 2228 2229 return 0; 2230 bad: 2231 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2232 RUM_UNLOCK(sc); 2233 ieee80211_free_node(ni); 2234 return EIO; 2235 } 2236 2237 static void 2238 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2239 { 2240 struct ieee80211vap *vap = ni->ni_vap; 2241 struct rum_vap *rvp = RUM_VAP(vap); 2242 2243 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2244 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2245 2246 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2247 } 2248 2249 static void 2250 rum_ratectl_timeout(void *arg) 2251 { 2252 struct rum_vap *rvp = arg; 2253 struct ieee80211vap *vap = &rvp->vap; 2254 struct ieee80211com *ic = vap->iv_ic; 2255 2256 ieee80211_runtask(ic, &rvp->ratectl_task); 2257 } 2258 2259 static void 2260 rum_ratectl_task(void *arg, int pending) 2261 { 2262 struct rum_vap *rvp = arg; 2263 struct ieee80211vap *vap = &rvp->vap; 2264 struct ieee80211com *ic = vap->iv_ic; 2265 struct ifnet *ifp = ic->ic_ifp; 2266 struct rum_softc *sc = ifp->if_softc; 2267 struct ieee80211_node *ni; 2268 int ok, fail; 2269 int sum, retrycnt; 2270 2271 RUM_LOCK(sc); 2272 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2273 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2274 2275 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */ 2276 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */ 2277 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2278 sum = ok+fail; 2279 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail; 2280 2281 ni = ieee80211_ref_node(vap->iv_bss); 2282 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2283 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2284 ieee80211_free_node(ni); 2285 2286 if_inc_counter(ifp, IFCOUNTER_OERRORS, fail); /* count TX retry-fail as Tx errors */ 2287 2288 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2289 RUM_UNLOCK(sc); 2290 } 2291 2292 static void 2293 rum_scan_start(struct ieee80211com *ic) 2294 { 2295 struct ifnet *ifp = ic->ic_ifp; 2296 struct rum_softc *sc = ifp->if_softc; 2297 uint32_t tmp; 2298 2299 RUM_LOCK(sc); 2300 /* abort TSF synchronization */ 2301 tmp = rum_read(sc, RT2573_TXRX_CSR9); 2302 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 2303 rum_set_bssid(sc, ifp->if_broadcastaddr); 2304 RUM_UNLOCK(sc); 2305 2306 } 2307 2308 static void 2309 rum_scan_end(struct ieee80211com *ic) 2310 { 2311 struct rum_softc *sc = ic->ic_ifp->if_softc; 2312 2313 RUM_LOCK(sc); 2314 rum_enable_tsf_sync(sc); 2315 rum_set_bssid(sc, sc->sc_bssid); 2316 RUM_UNLOCK(sc); 2317 2318 } 2319 2320 static void 2321 rum_set_channel(struct ieee80211com *ic) 2322 { 2323 struct rum_softc *sc = ic->ic_ifp->if_softc; 2324 2325 RUM_LOCK(sc); 2326 rum_set_chan(sc, ic->ic_curchan); 2327 RUM_UNLOCK(sc); 2328 } 2329 2330 static int 2331 rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2332 { 2333 struct ifnet *ifp = sc->sc_ifp; 2334 struct ieee80211com *ic = ifp->if_l2com; 2335 int lna, agc, rssi; 2336 2337 lna = (raw >> 5) & 0x3; 2338 agc = raw & 0x1f; 2339 2340 if (lna == 0) { 2341 /* 2342 * No RSSI mapping 2343 * 2344 * NB: Since RSSI is relative to noise floor, -1 is 2345 * adequate for caller to know error happened. 2346 */ 2347 return -1; 2348 } 2349 2350 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2351 2352 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2353 rssi += sc->rssi_2ghz_corr; 2354 2355 if (lna == 1) 2356 rssi -= 64; 2357 else if (lna == 2) 2358 rssi -= 74; 2359 else if (lna == 3) 2360 rssi -= 90; 2361 } else { 2362 rssi += sc->rssi_5ghz_corr; 2363 2364 if (!sc->ext_5ghz_lna && lna != 1) 2365 rssi += 4; 2366 2367 if (lna == 1) 2368 rssi -= 64; 2369 else if (lna == 2) 2370 rssi -= 86; 2371 else if (lna == 3) 2372 rssi -= 100; 2373 } 2374 return rssi; 2375 } 2376 2377 static int 2378 rum_pause(struct rum_softc *sc, int timeout) 2379 { 2380 2381 usb_pause_mtx(&sc->sc_mtx, timeout); 2382 return (0); 2383 } 2384 2385 static device_method_t rum_methods[] = { 2386 /* Device interface */ 2387 DEVMETHOD(device_probe, rum_match), 2388 DEVMETHOD(device_attach, rum_attach), 2389 DEVMETHOD(device_detach, rum_detach), 2390 DEVMETHOD_END 2391 }; 2392 2393 static driver_t rum_driver = { 2394 .name = "rum", 2395 .methods = rum_methods, 2396 .size = sizeof(struct rum_softc), 2397 }; 2398 2399 static devclass_t rum_devclass; 2400 2401 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0); 2402 MODULE_DEPEND(rum, wlan, 1, 1, 1); 2403 MODULE_DEPEND(rum, usb, 1, 1, 1); 2404 MODULE_VERSION(rum, 1); 2405