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