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