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