if_ural.c revision 188417
1184610Salfred/* $FreeBSD: head/sys/dev/usb2/wlan/if_ural2.c 188417 2009-02-09 22:12:47Z thompsa $ */ 2184610Salfred 3184610Salfred/*- 4184610Salfred * Copyright (c) 2005, 2006 5184610Salfred * Damien Bergamini <damien.bergamini@free.fr> 6184610Salfred * 7184610Salfred * Copyright (c) 2006, 2008 8184610Salfred * Hans Petter Selasky <hselasky@freebsd.org> 9184610Salfred * 10184610Salfred * Permission to use, copy, modify, and distribute this software for any 11184610Salfred * purpose with or without fee is hereby granted, provided that the above 12184610Salfred * copyright notice and this permission notice appear in all copies. 13184610Salfred * 14184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 15184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 17184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 20184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21184610Salfred */ 22184610Salfred 23184610Salfred#include <sys/cdefs.h> 24184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb2/wlan/if_ural2.c 188417 2009-02-09 22:12:47Z thompsa $"); 25184610Salfred 26184610Salfred/*- 27184610Salfred * Ralink Technology RT2500USB chipset driver 28184610Salfred * http://www.ralinktech.com/ 29184610Salfred */ 30184610Salfred 31184610Salfred#include <dev/usb2/include/usb2_devid.h> 32184610Salfred#include <dev/usb2/include/usb2_standard.h> 33184610Salfred#include <dev/usb2/include/usb2_mfunc.h> 34184610Salfred#include <dev/usb2/include/usb2_error.h> 35184610Salfred 36184610Salfred#define USB_DEBUG_VAR ural_debug 37184610Salfred 38184610Salfred#include <dev/usb2/core/usb2_core.h> 39184610Salfred#include <dev/usb2/core/usb2_lookup.h> 40184610Salfred#include <dev/usb2/core/usb2_process.h> 41184610Salfred#include <dev/usb2/core/usb2_debug.h> 42184610Salfred#include <dev/usb2/core/usb2_request.h> 43184610Salfred#include <dev/usb2/core/usb2_busdma.h> 44184610Salfred#include <dev/usb2/core/usb2_util.h> 45184610Salfred 46184610Salfred#include <dev/usb2/wlan/usb2_wlan.h> 47187192Sthompsa#include <dev/usb2/wlan/if_uralreg.h> 48187192Sthompsa#include <dev/usb2/wlan/if_uralvar.h> 49184610Salfred 50184610Salfred#if USB_DEBUG 51184610Salfredstatic int ural_debug = 0; 52184610Salfred 53184610SalfredSYSCTL_NODE(_hw_usb2, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural"); 54184610SalfredSYSCTL_INT(_hw_usb2_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0, 55184610Salfred "Debug level"); 56184610Salfred#endif 57184610Salfred 58188417Sthompsa#define ural_do_request(sc,req,data) \ 59188417Sthompsa usb2_do_request_proc((sc)->sc_udev, &(sc)->sc_tq, req, data, 0, NULL, 5000) 60184610Salfred 61188417Sthompsa#define URAL_RSSI(rssi) \ 62188417Sthompsa ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \ 63188417Sthompsa ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0) 64184610Salfred 65188417Sthompsa/* various supported device vendors/products */ 66188417Sthompsastatic const struct usb2_device_id ural_devs[] = { 67188417Sthompsa { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G) }, 68188417Sthompsa { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570) }, 69188417Sthompsa { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050) }, 70188417Sthompsa { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7051) }, 71188417Sthompsa { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_HU200TS) }, 72188417Sthompsa { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G) }, 73188417Sthompsa { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP) }, 74188417Sthompsa { USB_VP(USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU) }, 75188417Sthompsa { USB_VP(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122) }, 76188417Sthompsa { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GN54G) }, 77188417Sthompsa { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG) }, 78188417Sthompsa { USB_VP(USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254) }, 79188417Sthompsa { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54) }, 80188417Sthompsa { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI) }, 81188417Sthompsa { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB) }, 82188417Sthompsa { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI) }, 83188417Sthompsa { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570) }, 84188417Sthompsa { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2) }, 85188417Sthompsa { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3) }, 86188417Sthompsa { USB_VP(USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_NV902) }, 87188417Sthompsa { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570) }, 88188417Sthompsa { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2) }, 89188417Sthompsa { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_3) }, 90188417Sthompsa { USB_VP(USB_VENDOR_SIEMENS2, USB_PRODUCT_SIEMENS2_WL54G) }, 91188417Sthompsa { USB_VP(USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG) }, 92188417Sthompsa { USB_VP(USB_VENDOR_SPHAIRON, USB_PRODUCT_SPHAIRON_UB801R) }, 93188417Sthompsa { USB_VP(USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2570) }, 94188417Sthompsa { USB_VP(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570) }, 95188417Sthompsa { USB_VP(USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570) }, 96188417Sthompsa}; 97184610Salfred 98184610Salfredstatic usb2_callback_t ural_bulk_read_callback; 99184610Salfredstatic usb2_callback_t ural_bulk_write_callback; 100184610Salfred 101188417Sthompsastatic usb2_proc_callback_t ural_attach_post; 102188417Sthompsastatic usb2_proc_callback_t ural_task; 103188417Sthompsastatic usb2_proc_callback_t ural_scantask; 104188417Sthompsastatic usb2_proc_callback_t ural_promisctask; 105188417Sthompsastatic usb2_proc_callback_t ural_amrr_task; 106188417Sthompsastatic usb2_proc_callback_t ural_init_task; 107188417Sthompsastatic usb2_proc_callback_t ural_stop_task; 108184610Salfred 109185948Sthompsastatic struct ieee80211vap *ural_vap_create(struct ieee80211com *, 110188417Sthompsa const char name[IFNAMSIZ], int unit, int opmode, 111188417Sthompsa int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 112188417Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]); 113188417Sthompsastatic void ural_vap_delete(struct ieee80211vap *); 114188417Sthompsastatic void ural_tx_free(struct ural_tx_data *, int); 115188417Sthompsastatic void ural_setup_tx_list(struct ural_softc *); 116188417Sthompsastatic void ural_unsetup_tx_list(struct ural_softc *); 117188417Sthompsastatic int ural_newstate(struct ieee80211vap *, 118188417Sthompsa enum ieee80211_state, int); 119188417Sthompsastatic void ural_setup_tx_desc(struct ural_softc *, 120188417Sthompsa struct ural_tx_desc *, uint32_t, int, int); 121188417Sthompsastatic int ural_tx_bcn(struct ural_softc *, struct mbuf *, 122188417Sthompsa struct ieee80211_node *); 123188417Sthompsastatic int ural_tx_mgt(struct ural_softc *, struct mbuf *, 124188417Sthompsa struct ieee80211_node *); 125188417Sthompsastatic int ural_tx_data(struct ural_softc *, struct mbuf *, 126188417Sthompsa struct ieee80211_node *); 127188417Sthompsastatic void ural_start(struct ifnet *); 128188417Sthompsastatic int ural_ioctl(struct ifnet *, u_long, caddr_t); 129188417Sthompsastatic void ural_set_testmode(struct ural_softc *); 130188417Sthompsastatic void ural_eeprom_read(struct ural_softc *, uint16_t, void *, 131188417Sthompsa int); 132188417Sthompsastatic uint16_t ural_read(struct ural_softc *, uint16_t); 133188417Sthompsastatic void ural_read_multi(struct ural_softc *, uint16_t, void *, 134188417Sthompsa int); 135188417Sthompsastatic void ural_write(struct ural_softc *, uint16_t, uint16_t); 136188417Sthompsastatic void ural_write_multi(struct ural_softc *, uint16_t, void *, 137188417Sthompsa int) __unused; 138188417Sthompsastatic void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t); 139188417Sthompsastatic uint8_t ural_bbp_read(struct ural_softc *, uint8_t); 140188417Sthompsastatic void ural_rf_write(struct ural_softc *, uint8_t, uint32_t); 141185948Sthompsastatic struct ieee80211_node *ural_node_alloc(struct ieee80211vap *, 142188417Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]); 143188417Sthompsastatic void ural_newassoc(struct ieee80211_node *, int); 144188417Sthompsastatic void ural_scan_start(struct ieee80211com *); 145188417Sthompsastatic void ural_scan_end(struct ieee80211com *); 146188417Sthompsastatic void ural_set_channel(struct ieee80211com *); 147188417Sthompsastatic void ural_set_chan(struct ural_softc *, 148188417Sthompsa struct ieee80211_channel *); 149188417Sthompsastatic void ural_disable_rf_tune(struct ural_softc *); 150188417Sthompsastatic void ural_enable_tsf_sync(struct ural_softc *); 151188417Sthompsastatic void ural_update_slot(struct ifnet *); 152188417Sthompsastatic void ural_set_txpreamble(struct ural_softc *); 153188417Sthompsastatic void ural_set_basicrates(struct ural_softc *, 154188417Sthompsa const struct ieee80211_channel *); 155188417Sthompsastatic void ural_set_bssid(struct ural_softc *, const uint8_t *); 156188417Sthompsastatic void ural_set_macaddr(struct ural_softc *, uint8_t *); 157188417Sthompsastatic const char *ural_get_rf(int); 158188417Sthompsastatic void ural_read_eeprom(struct ural_softc *); 159188417Sthompsastatic int ural_bbp_init(struct ural_softc *); 160188417Sthompsastatic void ural_set_txantenna(struct ural_softc *, int); 161188417Sthompsastatic void ural_set_rxantenna(struct ural_softc *, int); 162188417Sthompsastatic void ural_init(void *); 163188417Sthompsastatic int ural_raw_xmit(struct ieee80211_node *, struct mbuf *, 164188417Sthompsa const struct ieee80211_bpf_params *); 165188417Sthompsastatic void ural_amrr_start(struct ural_softc *, 166188417Sthompsa struct ieee80211_node *); 167188417Sthompsastatic void ural_amrr_timeout(void *); 168188417Sthompsastatic void ural_queue_command(struct ural_softc *, 169188417Sthompsa usb2_proc_callback_t *, struct usb2_proc_msg *, 170188417Sthompsa struct usb2_proc_msg *); 171184610Salfred 172184610Salfred/* 173188417Sthompsa * Default values for MAC registers; values taken from the reference driver. 174184610Salfred */ 175188417Sthompsastatic const struct { 176188417Sthompsa uint16_t reg; 177188417Sthompsa uint16_t val; 178188417Sthompsa} ural_def_mac[] = { 179188417Sthompsa { RAL_TXRX_CSR5, 0x8c8d }, 180188417Sthompsa { RAL_TXRX_CSR6, 0x8b8a }, 181188417Sthompsa { RAL_TXRX_CSR7, 0x8687 }, 182188417Sthompsa { RAL_TXRX_CSR8, 0x0085 }, 183188417Sthompsa { RAL_MAC_CSR13, 0x1111 }, 184188417Sthompsa { RAL_MAC_CSR14, 0x1e11 }, 185188417Sthompsa { RAL_TXRX_CSR21, 0xe78f }, 186188417Sthompsa { RAL_MAC_CSR9, 0xff1d }, 187188417Sthompsa { RAL_MAC_CSR11, 0x0002 }, 188188417Sthompsa { RAL_MAC_CSR22, 0x0053 }, 189188417Sthompsa { RAL_MAC_CSR15, 0x0000 }, 190188417Sthompsa { RAL_MAC_CSR8, RAL_FRAME_SIZE }, 191188417Sthompsa { RAL_TXRX_CSR19, 0x0000 }, 192188417Sthompsa { RAL_TXRX_CSR18, 0x005a }, 193188417Sthompsa { RAL_PHY_CSR2, 0x0000 }, 194188417Sthompsa { RAL_TXRX_CSR0, 0x1ec0 }, 195188417Sthompsa { RAL_PHY_CSR4, 0x000f } 196184610Salfred}; 197184610Salfred 198184610Salfred/* 199184610Salfred * Default values for BBP registers; values taken from the reference driver. 200184610Salfred */ 201188417Sthompsastatic const struct { 202184610Salfred uint8_t reg; 203184610Salfred uint8_t val; 204188417Sthompsa} ural_def_bbp[] = { 205188417Sthompsa { 3, 0x02 }, 206188417Sthompsa { 4, 0x19 }, 207188417Sthompsa { 14, 0x1c }, 208188417Sthompsa { 15, 0x30 }, 209188417Sthompsa { 16, 0xac }, 210188417Sthompsa { 17, 0x48 }, 211188417Sthompsa { 18, 0x18 }, 212188417Sthompsa { 19, 0xff }, 213188417Sthompsa { 20, 0x1e }, 214188417Sthompsa { 21, 0x08 }, 215188417Sthompsa { 22, 0x08 }, 216188417Sthompsa { 23, 0x08 }, 217188417Sthompsa { 24, 0x80 }, 218188417Sthompsa { 25, 0x50 }, 219188417Sthompsa { 26, 0x08 }, 220188417Sthompsa { 27, 0x23 }, 221188417Sthompsa { 30, 0x10 }, 222188417Sthompsa { 31, 0x2b }, 223188417Sthompsa { 32, 0xb9 }, 224188417Sthompsa { 34, 0x12 }, 225188417Sthompsa { 35, 0x50 }, 226188417Sthompsa { 39, 0xc4 }, 227188417Sthompsa { 40, 0x02 }, 228188417Sthompsa { 41, 0x60 }, 229188417Sthompsa { 53, 0x10 }, 230188417Sthompsa { 54, 0x18 }, 231188417Sthompsa { 56, 0x08 }, 232188417Sthompsa { 57, 0x10 }, 233188417Sthompsa { 58, 0x08 }, 234188417Sthompsa { 61, 0x60 }, 235188417Sthompsa { 62, 0x10 }, 236188417Sthompsa { 75, 0xff } 237184610Salfred}; 238184610Salfred 239184610Salfred/* 240184610Salfred * Default values for RF register R2 indexed by channel numbers. 241184610Salfred */ 242184610Salfredstatic const uint32_t ural_rf2522_r2[] = { 243184610Salfred 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814, 244184610Salfred 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e 245184610Salfred}; 246184610Salfred 247184610Salfredstatic const uint32_t ural_rf2523_r2[] = { 248184610Salfred 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 249184610Salfred 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 250184610Salfred}; 251184610Salfred 252184610Salfredstatic const uint32_t ural_rf2524_r2[] = { 253184610Salfred 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 254184610Salfred 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 255184610Salfred}; 256184610Salfred 257184610Salfredstatic const uint32_t ural_rf2525_r2[] = { 258184610Salfred 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d, 259184610Salfred 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346 260184610Salfred}; 261184610Salfred 262184610Salfredstatic const uint32_t ural_rf2525_hi_r2[] = { 263184610Salfred 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345, 264184610Salfred 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e 265184610Salfred}; 266184610Salfred 267184610Salfredstatic const uint32_t ural_rf2525e_r2[] = { 268184610Salfred 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463, 269184610Salfred 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b 270184610Salfred}; 271184610Salfred 272184610Salfredstatic const uint32_t ural_rf2526_hi_r2[] = { 273184610Salfred 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d, 274184610Salfred 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241 275184610Salfred}; 276184610Salfred 277184610Salfredstatic const uint32_t ural_rf2526_r2[] = { 278184610Salfred 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229, 279184610Salfred 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d 280184610Salfred}; 281184610Salfred 282184610Salfred/* 283184610Salfred * For dual-band RF, RF registers R1 and R4 also depend on channel number; 284184610Salfred * values taken from the reference driver. 285184610Salfred */ 286188417Sthompsastatic const struct { 287188417Sthompsa uint8_t chan; 288188417Sthompsa uint32_t r1; 289188417Sthompsa uint32_t r2; 290188417Sthompsa uint32_t r4; 291188417Sthompsa} ural_rf5222[] = { 292188417Sthompsa { 1, 0x08808, 0x0044d, 0x00282 }, 293188417Sthompsa { 2, 0x08808, 0x0044e, 0x00282 }, 294188417Sthompsa { 3, 0x08808, 0x0044f, 0x00282 }, 295188417Sthompsa { 4, 0x08808, 0x00460, 0x00282 }, 296188417Sthompsa { 5, 0x08808, 0x00461, 0x00282 }, 297188417Sthompsa { 6, 0x08808, 0x00462, 0x00282 }, 298188417Sthompsa { 7, 0x08808, 0x00463, 0x00282 }, 299188417Sthompsa { 8, 0x08808, 0x00464, 0x00282 }, 300188417Sthompsa { 9, 0x08808, 0x00465, 0x00282 }, 301188417Sthompsa { 10, 0x08808, 0x00466, 0x00282 }, 302188417Sthompsa { 11, 0x08808, 0x00467, 0x00282 }, 303188417Sthompsa { 12, 0x08808, 0x00468, 0x00282 }, 304188417Sthompsa { 13, 0x08808, 0x00469, 0x00282 }, 305188417Sthompsa { 14, 0x08808, 0x0046b, 0x00286 }, 306184610Salfred 307188417Sthompsa { 36, 0x08804, 0x06225, 0x00287 }, 308188417Sthompsa { 40, 0x08804, 0x06226, 0x00287 }, 309188417Sthompsa { 44, 0x08804, 0x06227, 0x00287 }, 310188417Sthompsa { 48, 0x08804, 0x06228, 0x00287 }, 311188417Sthompsa { 52, 0x08804, 0x06229, 0x00287 }, 312188417Sthompsa { 56, 0x08804, 0x0622a, 0x00287 }, 313188417Sthompsa { 60, 0x08804, 0x0622b, 0x00287 }, 314188417Sthompsa { 64, 0x08804, 0x0622c, 0x00287 }, 315184610Salfred 316188417Sthompsa { 100, 0x08804, 0x02200, 0x00283 }, 317188417Sthompsa { 104, 0x08804, 0x02201, 0x00283 }, 318188417Sthompsa { 108, 0x08804, 0x02202, 0x00283 }, 319188417Sthompsa { 112, 0x08804, 0x02203, 0x00283 }, 320188417Sthompsa { 116, 0x08804, 0x02204, 0x00283 }, 321188417Sthompsa { 120, 0x08804, 0x02205, 0x00283 }, 322188417Sthompsa { 124, 0x08804, 0x02206, 0x00283 }, 323188417Sthompsa { 128, 0x08804, 0x02207, 0x00283 }, 324188417Sthompsa { 132, 0x08804, 0x02208, 0x00283 }, 325188417Sthompsa { 136, 0x08804, 0x02209, 0x00283 }, 326188417Sthompsa { 140, 0x08804, 0x0220a, 0x00283 }, 327184610Salfred 328188417Sthompsa { 149, 0x08808, 0x02429, 0x00281 }, 329188417Sthompsa { 153, 0x08808, 0x0242b, 0x00281 }, 330188417Sthompsa { 157, 0x08808, 0x0242d, 0x00281 }, 331188417Sthompsa { 161, 0x08808, 0x0242f, 0x00281 } 332184610Salfred}; 333184610Salfred 334184610Salfredstatic const struct usb2_config ural_config[URAL_N_TRANSFER] = { 335188417Sthompsa [URAL_BULK_WR] = { 336184610Salfred .type = UE_BULK, 337184610Salfred .endpoint = UE_ADDR_ANY, 338184610Salfred .direction = UE_DIR_OUT, 339184610Salfred .mh.bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4), 340184610Salfred .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 341188417Sthompsa .mh.callback = ural_bulk_write_callback, 342184610Salfred .mh.timeout = 5000, /* ms */ 343184610Salfred }, 344188417Sthompsa [URAL_BULK_RD] = { 345184610Salfred .type = UE_BULK, 346184610Salfred .endpoint = UE_ADDR_ANY, 347184610Salfred .direction = UE_DIR_IN, 348184610Salfred .mh.bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE), 349184610Salfred .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 350188417Sthompsa .mh.callback = ural_bulk_read_callback, 351184610Salfred }, 352184610Salfred}; 353184610Salfred 354188417Sthompsastatic device_probe_t ural_match; 355188417Sthompsastatic device_attach_t ural_attach; 356188417Sthompsastatic device_detach_t ural_detach; 357184610Salfred 358184610Salfredstatic device_method_t ural_methods[] = { 359188417Sthompsa /* Device interface */ 360188417Sthompsa DEVMETHOD(device_probe, ural_match), 361188417Sthompsa DEVMETHOD(device_attach, ural_attach), 362188417Sthompsa DEVMETHOD(device_detach, ural_detach), 363188417Sthompsa 364188417Sthompsa { 0, 0 } 365184610Salfred}; 366184610Salfred 367184610Salfredstatic driver_t ural_driver = { 368184610Salfred .name = "ural", 369184610Salfred .methods = ural_methods, 370184610Salfred .size = sizeof(struct ural_softc), 371184610Salfred}; 372184610Salfred 373188417Sthompsastatic devclass_t ural_devclass; 374188417Sthompsa 375184610SalfredDRIVER_MODULE(ural, ushub, ural_driver, ural_devclass, NULL, 0); 376184610SalfredMODULE_DEPEND(ural, usb2_wlan, 1, 1, 1); 377184610SalfredMODULE_DEPEND(ural, usb2_core, 1, 1, 1); 378184610SalfredMODULE_DEPEND(ural, wlan, 1, 1, 1); 379184610SalfredMODULE_DEPEND(ural, wlan_amrr, 1, 1, 1); 380184610Salfred 381184610Salfredstatic int 382188417Sthompsaural_match(device_t self) 383184610Salfred{ 384188417Sthompsa struct usb2_attach_arg *uaa = device_get_ivars(self); 385184610Salfred 386188417Sthompsa if (uaa->usb2_mode != USB_MODE_HOST) 387184610Salfred return (ENXIO); 388188417Sthompsa if (uaa->info.bConfigIndex != 0) 389184610Salfred return (ENXIO); 390188417Sthompsa if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX) 391184610Salfred return (ENXIO); 392188417Sthompsa 393184610Salfred return (usb2_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa)); 394184610Salfred} 395184610Salfred 396184610Salfredstatic int 397188417Sthompsaural_attach(device_t self) 398184610Salfred{ 399188417Sthompsa struct usb2_attach_arg *uaa = device_get_ivars(self); 400188417Sthompsa struct ural_softc *sc = device_get_softc(self); 401184610Salfred int error; 402184610Salfred uint8_t iface_index; 403184610Salfred 404188417Sthompsa device_set_usb2_desc(self); 405184610Salfred sc->sc_udev = uaa->device; 406188417Sthompsa sc->sc_dev = self; 407184610Salfred 408188417Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(self), 409188417Sthompsa MTX_NETWORK_LOCK, MTX_DEF); 410184610Salfred 411184610Salfred iface_index = RAL_IFACE_INDEX; 412184610Salfred error = usb2_transfer_setup(uaa->device, 413184610Salfred &iface_index, sc->sc_xfer, ural_config, 414184610Salfred URAL_N_TRANSFER, sc, &sc->sc_mtx); 415184610Salfred if (error) { 416188417Sthompsa device_printf(self, "could not allocate USB transfers, " 417184610Salfred "err=%s\n", usb2_errstr(error)); 418184610Salfred goto detach; 419184610Salfred } 420188417Sthompsa error = usb2_proc_create(&sc->sc_tq, &sc->sc_mtx, 421188417Sthompsa device_get_nameunit(self), USB_PRI_MED); 422184610Salfred if (error) { 423188417Sthompsa device_printf(self, "could not setup config thread!\n"); 424184610Salfred goto detach; 425184610Salfred } 426184610Salfred 427188417Sthompsa /* fork rest of the attach code */ 428188417Sthompsa RAL_LOCK(sc); 429188417Sthompsa ural_queue_command(sc, ural_attach_post, 430188417Sthompsa &sc->sc_synctask[0].hdr, 431188417Sthompsa &sc->sc_synctask[1].hdr); 432188417Sthompsa RAL_UNLOCK(sc); 433188417Sthompsa return (0); 434184610Salfred 435184610Salfreddetach: 436188417Sthompsa ural_detach(self); 437184610Salfred return (ENXIO); /* failure */ 438184610Salfred} 439184610Salfred 440188417Sthompsastatic void 441188417Sthompsaural_attach_post(struct usb2_proc_msg *pm) 442184610Salfred{ 443188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 444188417Sthompsa struct ural_softc *sc = task->sc; 445188417Sthompsa struct ifnet *ifp; 446184610Salfred struct ieee80211com *ic; 447188417Sthompsa uint8_t bands; 448184610Salfred 449188417Sthompsa /* retrieve RT2570 rev. no */ 450188417Sthompsa sc->asic_rev = ural_read(sc, RAL_MAC_CSR0); 451184610Salfred 452188417Sthompsa /* retrieve MAC address and various other things from EEPROM */ 453188417Sthompsa ural_read_eeprom(sc); 454188417Sthompsa RAL_UNLOCK(sc); 455184610Salfred 456188417Sthompsa device_printf(sc->sc_dev, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n", 457188417Sthompsa sc->asic_rev, ural_get_rf(sc->rf_rev)); 458184610Salfred 459188417Sthompsa ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 460188417Sthompsa if (ifp == NULL) { 461188417Sthompsa device_printf(sc->sc_dev, "can not if_alloc()\n"); 462188417Sthompsa RAL_LOCK(sc); 463188417Sthompsa return; 464188417Sthompsa } 465184610Salfred ic = ifp->if_l2com; 466184610Salfred 467188417Sthompsa ifp->if_softc = sc; 468188417Sthompsa if_initname(ifp, "ural", device_get_unit(sc->sc_dev)); 469188417Sthompsa ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 470188417Sthompsa ifp->if_init = ural_init; 471188417Sthompsa ifp->if_ioctl = ural_ioctl; 472188417Sthompsa ifp->if_start = ural_start; 473188417Sthompsa IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 474188417Sthompsa ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 475188417Sthompsa IFQ_SET_READY(&ifp->if_snd); 476184610Salfred 477188417Sthompsa ic->ic_ifp = ifp; 478188417Sthompsa ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 479188417Sthompsa IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_bssid); 480184610Salfred 481188417Sthompsa /* set device capabilities */ 482188417Sthompsa ic->ic_caps = 483188417Sthompsa IEEE80211_C_STA /* station mode supported */ 484188417Sthompsa | IEEE80211_C_IBSS /* IBSS mode supported */ 485188417Sthompsa | IEEE80211_C_MONITOR /* monitor mode supported */ 486188417Sthompsa | IEEE80211_C_HOSTAP /* HostAp mode supported */ 487188417Sthompsa | IEEE80211_C_TXPMGT /* tx power management */ 488188417Sthompsa | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 489188417Sthompsa | IEEE80211_C_SHSLOT /* short slot time supported */ 490188417Sthompsa | IEEE80211_C_BGSCAN /* bg scanning supported */ 491188417Sthompsa | IEEE80211_C_WPA /* 802.11i */ 492188417Sthompsa ; 493184610Salfred 494188417Sthompsa bands = 0; 495188417Sthompsa setbit(&bands, IEEE80211_MODE_11B); 496188417Sthompsa setbit(&bands, IEEE80211_MODE_11G); 497188417Sthompsa if (sc->rf_rev == RAL_RF_5222) 498188417Sthompsa setbit(&bands, IEEE80211_MODE_11A); 499188417Sthompsa ieee80211_init_channels(ic, NULL, &bands); 500184610Salfred 501188417Sthompsa ieee80211_ifattach(ic); 502188417Sthompsa ic->ic_newassoc = ural_newassoc; 503188417Sthompsa ic->ic_raw_xmit = ural_raw_xmit; 504188417Sthompsa ic->ic_node_alloc = ural_node_alloc; 505188417Sthompsa ic->ic_scan_start = ural_scan_start; 506188417Sthompsa ic->ic_scan_end = ural_scan_end; 507188417Sthompsa ic->ic_set_channel = ural_set_channel; 508184610Salfred 509188417Sthompsa ic->ic_vap_create = ural_vap_create; 510188417Sthompsa ic->ic_vap_delete = ural_vap_delete; 511184610Salfred 512188417Sthompsa sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan); 513184610Salfred 514188417Sthompsa bpfattach(ifp, DLT_IEEE802_11_RADIO, 515188417Sthompsa sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap)); 516184610Salfred 517188417Sthompsa sc->sc_rxtap_len = sizeof sc->sc_rxtap; 518188417Sthompsa sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 519188417Sthompsa sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT); 520184610Salfred 521188417Sthompsa sc->sc_txtap_len = sizeof sc->sc_txtap; 522188417Sthompsa sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 523188417Sthompsa sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT); 524184610Salfred 525188417Sthompsa if (bootverbose) 526188417Sthompsa ieee80211_announce(ic); 527184610Salfred 528188417Sthompsa RAL_LOCK(sc); 529188417Sthompsa} 530184610Salfred 531188417Sthompsastatic int 532188417Sthompsaural_detach(device_t self) 533188417Sthompsa{ 534188417Sthompsa struct ural_softc *sc = device_get_softc(self); 535188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 536188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 537184610Salfred 538188417Sthompsa /* wait for any post attach or other command to complete */ 539188417Sthompsa usb2_proc_drain(&sc->sc_tq); 540184610Salfred 541188417Sthompsa /* stop all USB transfers */ 542188417Sthompsa usb2_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER); 543188417Sthompsa usb2_proc_free(&sc->sc_tq); 544184610Salfred 545188417Sthompsa /* free TX list, if any */ 546188417Sthompsa RAL_LOCK(sc); 547188417Sthompsa ural_unsetup_tx_list(sc); 548188417Sthompsa RAL_UNLOCK(sc); 549188417Sthompsa 550188417Sthompsa if (ifp) { 551188417Sthompsa bpfdetach(ifp); 552188417Sthompsa ieee80211_ifdetach(ic); 553188417Sthompsa if_free(ifp); 554184610Salfred } 555184610Salfred 556188417Sthompsa mtx_destroy(&sc->sc_mtx); 557184610Salfred 558188417Sthompsa return (0); 559184610Salfred} 560184610Salfred 561188417Sthompsastatic struct ieee80211vap * 562188417Sthompsaural_vap_create(struct ieee80211com *ic, 563188417Sthompsa const char name[IFNAMSIZ], int unit, int opmode, int flags, 564188417Sthompsa const uint8_t bssid[IEEE80211_ADDR_LEN], 565188417Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]) 566184610Salfred{ 567188417Sthompsa struct ural_softc *sc = ic->ic_ifp->if_softc; 568188417Sthompsa struct ural_vap *uvp; 569188417Sthompsa struct ieee80211vap *vap; 570184610Salfred 571188417Sthompsa if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 572188417Sthompsa return NULL; 573188417Sthompsa uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap), 574188417Sthompsa M_80211_VAP, M_NOWAIT | M_ZERO); 575188417Sthompsa if (uvp == NULL) 576188417Sthompsa return NULL; 577188417Sthompsa vap = &uvp->vap; 578188417Sthompsa /* enable s/w bmiss handling for sta mode */ 579188417Sthompsa ieee80211_vap_setup(ic, vap, name, unit, opmode, 580188417Sthompsa flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 581184610Salfred 582188417Sthompsa /* override state transition machine */ 583188417Sthompsa uvp->newstate = vap->iv_newstate; 584188417Sthompsa vap->iv_newstate = ural_newstate; 585184610Salfred 586188417Sthompsa uvp->sc = sc; 587188417Sthompsa usb2_callout_init_mtx(&uvp->amrr_ch, &sc->sc_mtx, 0); 588188417Sthompsa ieee80211_amrr_init(&uvp->amrr, vap, 589188417Sthompsa IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 590188417Sthompsa IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 591188417Sthompsa 1000 /* 1 sec */); 592184610Salfred 593188417Sthompsa /* complete setup */ 594188417Sthompsa ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 595188417Sthompsa ic->ic_opmode = opmode; 596188417Sthompsa return vap; 597184610Salfred} 598184610Salfred 599184610Salfredstatic void 600188417Sthompsaural_vap_delete(struct ieee80211vap *vap) 601184610Salfred{ 602188417Sthompsa struct ural_vap *uvp = URAL_VAP(vap); 603184610Salfred 604188417Sthompsa usb2_callout_drain(&uvp->amrr_ch); 605188417Sthompsa ieee80211_amrr_cleanup(&uvp->amrr); 606188417Sthompsa ieee80211_vap_detach(vap); 607188417Sthompsa free(uvp, M_80211_VAP); 608184610Salfred} 609184610Salfred 610184610Salfredstatic void 611188417Sthompsaural_tx_free(struct ural_tx_data *data, int txerr) 612184610Salfred{ 613188417Sthompsa struct ural_softc *sc = data->sc; 614184610Salfred 615188417Sthompsa if (data->m != NULL) { 616188417Sthompsa if (data->m->m_flags & M_TXCB) 617188417Sthompsa ieee80211_process_callback(data->ni, data->m, 618188417Sthompsa txerr ? ETIMEDOUT : 0); 619188417Sthompsa m_freem(data->m); 620188417Sthompsa data->m = NULL; 621184610Salfred 622188417Sthompsa ieee80211_free_node(data->ni); 623188417Sthompsa data->ni = NULL; 624188417Sthompsa } 625188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 626188417Sthompsa sc->tx_nfree++; 627184610Salfred} 628184610Salfred 629184610Salfredstatic void 630188417Sthompsaural_setup_tx_list(struct ural_softc *sc) 631184610Salfred{ 632188417Sthompsa struct ural_tx_data *data; 633188417Sthompsa int i; 634184610Salfred 635188417Sthompsa sc->tx_nfree = 0; 636188417Sthompsa STAILQ_INIT(&sc->tx_q); 637188417Sthompsa STAILQ_INIT(&sc->tx_free); 638184610Salfred 639188417Sthompsa for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 640188417Sthompsa data = &sc->tx_data[i]; 641184610Salfred 642188417Sthompsa data->sc = sc; 643188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 644188417Sthompsa sc->tx_nfree++; 645184610Salfred } 646184610Salfred} 647184610Salfred 648184610Salfredstatic void 649188417Sthompsaural_unsetup_tx_list(struct ural_softc *sc) 650184610Salfred{ 651188417Sthompsa struct ural_tx_data *data; 652188417Sthompsa int i; 653184610Salfred 654188417Sthompsa /* make sure any subsequent use of the queues will fail */ 655188417Sthompsa sc->tx_nfree = 0; 656188417Sthompsa STAILQ_INIT(&sc->tx_q); 657188417Sthompsa STAILQ_INIT(&sc->tx_free); 658184610Salfred 659188417Sthompsa /* free up all node references and mbufs */ 660188417Sthompsa for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 661188417Sthompsa data = &sc->tx_data[i]; 662184610Salfred 663188417Sthompsa if (data->m != NULL) { 664188417Sthompsa m_freem(data->m); 665188417Sthompsa data->m = NULL; 666188417Sthompsa } 667188417Sthompsa if (data->ni != NULL) { 668188417Sthompsa ieee80211_free_node(data->ni); 669188417Sthompsa data->ni = NULL; 670188417Sthompsa } 671184610Salfred } 672184610Salfred} 673184610Salfred 674184610Salfredstatic void 675188417Sthompsaural_task(struct usb2_proc_msg *pm) 676184610Salfred{ 677188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 678188417Sthompsa struct ural_softc *sc = task->sc; 679188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 680188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 681188417Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 682188417Sthompsa struct ural_vap *uvp = URAL_VAP(vap); 683188417Sthompsa const struct ieee80211_txparam *tp; 684188417Sthompsa enum ieee80211_state ostate; 685188417Sthompsa struct ieee80211_node *ni; 686188417Sthompsa struct mbuf *m; 687184610Salfred 688188417Sthompsa ostate = vap->iv_state; 689184610Salfred 690188417Sthompsa switch (sc->sc_state) { 691188417Sthompsa case IEEE80211_S_INIT: 692188417Sthompsa if (ostate == IEEE80211_S_RUN) { 693188417Sthompsa /* abort TSF synchronization */ 694188417Sthompsa ural_write(sc, RAL_TXRX_CSR19, 0); 695184610Salfred 696188417Sthompsa /* force tx led to stop blinking */ 697188417Sthompsa ural_write(sc, RAL_MAC_CSR20, 0); 698188417Sthompsa } 699188417Sthompsa break; 700184610Salfred 701188417Sthompsa case IEEE80211_S_RUN: 702188417Sthompsa ni = vap->iv_bss; 703188417Sthompsa 704188417Sthompsa if (vap->iv_opmode != IEEE80211_M_MONITOR) { 705188417Sthompsa ural_update_slot(ic->ic_ifp); 706188417Sthompsa ural_set_txpreamble(sc); 707188417Sthompsa ural_set_basicrates(sc, ic->ic_bsschan); 708188417Sthompsa IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 709188417Sthompsa ural_set_bssid(sc, sc->sc_bssid); 710188417Sthompsa } 711188417Sthompsa 712188417Sthompsa if (vap->iv_opmode == IEEE80211_M_HOSTAP || 713188417Sthompsa vap->iv_opmode == IEEE80211_M_IBSS) { 714188417Sthompsa m = ieee80211_beacon_alloc(ni, &uvp->bo); 715188417Sthompsa if (m == NULL) { 716188417Sthompsa device_printf(sc->sc_dev, 717188417Sthompsa "could not allocate beacon\n"); 718188417Sthompsa return; 719184610Salfred } 720188417Sthompsa 721188417Sthompsa if (ural_tx_bcn(sc, m, ni) != 0) { 722188417Sthompsa device_printf(sc->sc_dev, 723188417Sthompsa "could not send beacon\n"); 724184610Salfred return; 725184610Salfred } 726184610Salfred } 727184610Salfred 728188417Sthompsa /* make tx led blink on tx (controlled by ASIC) */ 729188417Sthompsa ural_write(sc, RAL_MAC_CSR20, 1); 730184610Salfred 731188417Sthompsa if (vap->iv_opmode != IEEE80211_M_MONITOR) 732188417Sthompsa ural_enable_tsf_sync(sc); 733184610Salfred 734188417Sthompsa /* enable automatic rate adaptation */ 735188417Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 736188417Sthompsa if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 737188417Sthompsa ural_amrr_start(sc, ni); 738184610Salfred 739188417Sthompsa break; 740184610Salfred 741188417Sthompsa default: 742188417Sthompsa break; 743188417Sthompsa } 744184610Salfred 745188417Sthompsa RAL_UNLOCK(sc); 746188417Sthompsa IEEE80211_LOCK(ic); 747188417Sthompsa uvp->newstate(vap, sc->sc_state, sc->sc_arg); 748188417Sthompsa if (vap->iv_newstate_cb != NULL) 749188417Sthompsa vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg); 750188417Sthompsa IEEE80211_UNLOCK(ic); 751188417Sthompsa RAL_LOCK(sc); 752188417Sthompsa} 753184610Salfred 754188417Sthompsastatic void 755188417Sthompsaural_scantask(struct usb2_proc_msg *pm) 756188417Sthompsa{ 757188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 758188417Sthompsa struct ural_softc *sc = task->sc; 759188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 760188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 761184610Salfred 762188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 763184610Salfred 764188417Sthompsa switch (sc->sc_scan_action) { 765188417Sthompsa case URAL_SCAN_START: 766188417Sthompsa /* abort TSF synchronization */ 767188417Sthompsa DPRINTF("starting scan\n"); 768188417Sthompsa ural_write(sc, RAL_TXRX_CSR19, 0); 769188417Sthompsa ural_set_bssid(sc, ifp->if_broadcastaddr); 770188417Sthompsa break; 771184610Salfred 772188417Sthompsa case URAL_SET_CHANNEL: 773188417Sthompsa ural_set_chan(sc, ic->ic_curchan); 774188417Sthompsa break; 775184610Salfred 776188417Sthompsa default: /* URAL_SCAN_END */ 777188417Sthompsa DPRINTF("stopping scan\n"); 778188417Sthompsa ural_enable_tsf_sync(sc); 779188417Sthompsa ural_set_bssid(sc, sc->sc_bssid); 780188417Sthompsa break; 781184610Salfred } 782188417Sthompsa} 783184610Salfred 784188417Sthompsastatic int 785188417Sthompsaural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 786188417Sthompsa{ 787188417Sthompsa struct ural_vap *uvp = URAL_VAP(vap); 788188417Sthompsa struct ieee80211com *ic = vap->iv_ic; 789188417Sthompsa struct ural_softc *sc = ic->ic_ifp->if_softc; 790184610Salfred 791188417Sthompsa DPRINTF("%s -> %s\n", 792188417Sthompsa ieee80211_state_name[vap->iv_state], 793188417Sthompsa ieee80211_state_name[nstate]); 794184610Salfred 795188417Sthompsa RAL_LOCK(sc); 796188417Sthompsa usb2_callout_stop(&uvp->amrr_ch); 797184610Salfred 798188417Sthompsa /* do it in a process context */ 799188417Sthompsa sc->sc_state = nstate; 800188417Sthompsa sc->sc_arg = arg; 801188417Sthompsa RAL_UNLOCK(sc); 802184610Salfred 803188417Sthompsa if (nstate == IEEE80211_S_INIT) { 804188417Sthompsa uvp->newstate(vap, nstate, arg); 805188417Sthompsa return 0; 806188417Sthompsa } else { 807188417Sthompsa RAL_LOCK(sc); 808188417Sthompsa ural_queue_command(sc, ural_task, &sc->sc_task[0].hdr, 809188417Sthompsa &sc->sc_task[1].hdr); 810188417Sthompsa RAL_UNLOCK(sc); 811188417Sthompsa return EINPROGRESS; 812184610Salfred } 813188417Sthompsa} 814184610Salfred 815184610Salfred 816188417Sthompsastatic void 817188417Sthompsaural_bulk_write_callback(struct usb2_xfer *xfer) 818188417Sthompsa{ 819188417Sthompsa struct ural_softc *sc = xfer->priv_sc; 820188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 821188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 822188417Sthompsa struct ieee80211_channel *c = ic->ic_curchan; 823188417Sthompsa struct ural_tx_data *data; 824188417Sthompsa struct mbuf *m; 825188417Sthompsa unsigned int len; 826184610Salfred 827188417Sthompsa switch (USB_GET_STATE(xfer)) { 828188417Sthompsa case USB_ST_TRANSFERRED: 829188417Sthompsa DPRINTFN(11, "transfer complete, %d bytes\n", xfer->actlen); 830184610Salfred 831188417Sthompsa /* free resources */ 832188417Sthompsa data = xfer->priv_fifo; 833188417Sthompsa ural_tx_free(data, 0); 834188417Sthompsa xfer->priv_fifo = NULL; 835184610Salfred 836188417Sthompsa ifp->if_opackets++; 837188417Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 838184610Salfred 839188417Sthompsa /* FALLTHROUGH */ 840188417Sthompsa case USB_ST_SETUP: 841188417Sthompsatr_setup: 842188417Sthompsa data = STAILQ_FIRST(&sc->tx_q); 843188417Sthompsa if (data) { 844188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_q, next); 845188417Sthompsa m = data->m; 846184610Salfred 847188417Sthompsa if (m->m_pkthdr.len > (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) { 848188417Sthompsa DPRINTFN(0, "data overflow, %u bytes\n", 849188417Sthompsa m->m_pkthdr.len); 850188417Sthompsa m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE); 851188417Sthompsa } 852188417Sthompsa usb2_copy_in(xfer->frbuffers, 0, &data->desc, 853188417Sthompsa RAL_TX_DESC_SIZE); 854188417Sthompsa usb2_m_copy_in(xfer->frbuffers, RAL_TX_DESC_SIZE, m, 0, 855188417Sthompsa m->m_pkthdr.len); 856184610Salfred 857188417Sthompsa if (bpf_peers_present(ifp->if_bpf)) { 858188417Sthompsa struct ural_tx_radiotap_header *tap = &sc->sc_txtap; 859184610Salfred 860188417Sthompsa tap->wt_flags = 0; 861188417Sthompsa tap->wt_rate = data->rate; 862188417Sthompsa tap->wt_chan_freq = htole16(c->ic_freq); 863188417Sthompsa tap->wt_chan_flags = htole16(c->ic_flags); 864188417Sthompsa tap->wt_antenna = sc->tx_ant; 865184610Salfred 866188417Sthompsa bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m); 867188417Sthompsa } 868184610Salfred 869188417Sthompsa /* xfer length needs to be a multiple of two! */ 870188417Sthompsa len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1; 871188417Sthompsa if ((len % 64) == 0) 872188417Sthompsa len += 2; 873184610Salfred 874188417Sthompsa DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 875188417Sthompsa m->m_pkthdr.len, len); 876184610Salfred 877188417Sthompsa xfer->frlengths[0] = len; 878188417Sthompsa xfer->priv_fifo = data; 879184610Salfred 880188417Sthompsa usb2_start_hardware(xfer); 881188417Sthompsa } 882188417Sthompsa break; 883184610Salfred 884188417Sthompsa default: /* Error */ 885188417Sthompsa DPRINTFN(11, "transfer error, %s\n", 886188417Sthompsa usb2_errstr(xfer->error)); 887184610Salfred 888188417Sthompsa ifp->if_oerrors++; 889188417Sthompsa data = xfer->priv_fifo; 890188417Sthompsa if (data != NULL) { 891188417Sthompsa ural_tx_free(data, xfer->error); 892188417Sthompsa xfer->priv_fifo = NULL; 893184610Salfred } 894184610Salfred 895188417Sthompsa if (xfer->error == USB_ERR_STALLED) { 896188417Sthompsa /* try to clear stall first */ 897188417Sthompsa xfer->flags.stall_pipe = 1; 898188417Sthompsa goto tr_setup; 899188417Sthompsa } 900188417Sthompsa if (xfer->error == USB_ERR_TIMEOUT) 901188417Sthompsa device_printf(sc->sc_dev, "device timeout\n"); 902188417Sthompsa break; 903184610Salfred } 904184610Salfred} 905184610Salfred 906184610Salfredstatic void 907184610Salfredural_bulk_read_callback(struct usb2_xfer *xfer) 908184610Salfred{ 909184610Salfred struct ural_softc *sc = xfer->priv_sc; 910184610Salfred struct ifnet *ifp = sc->sc_ifp; 911184610Salfred struct ieee80211com *ic = ifp->if_l2com; 912184610Salfred struct ieee80211_node *ni; 913184610Salfred struct mbuf *m = NULL; 914184610Salfred uint32_t flags; 915184610Salfred uint8_t rssi = 0; 916188417Sthompsa unsigned int len; 917184610Salfred 918184610Salfred switch (USB_GET_STATE(xfer)) { 919184610Salfred case USB_ST_TRANSFERRED: 920184610Salfred 921184610Salfred DPRINTFN(15, "rx done, actlen=%d\n", xfer->actlen); 922184610Salfred 923188417Sthompsa len = xfer->actlen; 924188417Sthompsa if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 925188417Sthompsa DPRINTF("%s: xfer too short %d\n", 926188417Sthompsa device_get_nameunit(sc->sc_dev), len); 927184610Salfred ifp->if_ierrors++; 928184610Salfred goto tr_setup; 929184610Salfred } 930184610Salfred 931188417Sthompsa len -= RAL_RX_DESC_SIZE; 932188417Sthompsa /* rx descriptor is located at the end */ 933188417Sthompsa usb2_copy_out(xfer->frbuffers, len, &sc->sc_rx_desc, 934188417Sthompsa RAL_RX_DESC_SIZE); 935184610Salfred 936188417Sthompsa rssi = URAL_RSSI(sc->sc_rx_desc.rssi); 937184610Salfred flags = le32toh(sc->sc_rx_desc.flags); 938184610Salfred if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) { 939184610Salfred /* 940184610Salfred * This should not happen since we did not 941184610Salfred * request to receive those frames when we 942184610Salfred * filled RAL_TXRX_CSR2: 943184610Salfred */ 944188417Sthompsa DPRINTFN(5, "PHY or CRC error\n"); 945184610Salfred ifp->if_ierrors++; 946184610Salfred goto tr_setup; 947184610Salfred } 948184610Salfred 949184610Salfred m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 950184610Salfred if (m == NULL) { 951184610Salfred DPRINTF("could not allocate mbuf\n"); 952184610Salfred ifp->if_ierrors++; 953184610Salfred goto tr_setup; 954184610Salfred } 955188417Sthompsa usb2_copy_out(xfer->frbuffers, 0, mtod(m, uint8_t *), len); 956184610Salfred 957184610Salfred /* finalize mbuf */ 958184610Salfred m->m_pkthdr.rcvif = ifp; 959188417Sthompsa m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 960184610Salfred 961184610Salfred if (bpf_peers_present(ifp->if_bpf)) { 962184610Salfred struct ural_rx_radiotap_header *tap = &sc->sc_rxtap; 963184610Salfred 964184610Salfred tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 965184610Salfred tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 966188417Sthompsa (flags & RAL_RX_OFDM) ? 967184610Salfred IEEE80211_T_OFDM : IEEE80211_T_CCK); 968184610Salfred tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 969184610Salfred tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 970188417Sthompsa tap->wr_antenna = sc->rx_ant; 971184610Salfred tap->wr_antsignal = rssi; 972184610Salfred 973184610Salfred bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m); 974184610Salfred } 975184610Salfred /* Strip trailing 802.11 MAC FCS. */ 976184610Salfred m_adj(m, -IEEE80211_CRC_LEN); 977184610Salfred 978188417Sthompsa /* FALLTHROUGH */ 979184610Salfred case USB_ST_SETUP: 980184610Salfredtr_setup: 981188417Sthompsa xfer->frlengths[0] = xfer->max_data_length; 982188417Sthompsa usb2_start_hardware(xfer); 983184610Salfred 984184610Salfred /* 985184610Salfred * At the end of a USB callback it is always safe to unlock 986184610Salfred * the private mutex of a device! That is why we do the 987184610Salfred * "ieee80211_input" here, and not some lines up! 988184610Salfred */ 989184610Salfred if (m) { 990188417Sthompsa RAL_UNLOCK(sc); 991188417Sthompsa ni = ieee80211_find_rxnode(ic, 992188417Sthompsa mtod(m, struct ieee80211_frame_min *)); 993188417Sthompsa if (ni != NULL) { 994188417Sthompsa (void) ieee80211_input(ni, m, rssi, 995188417Sthompsa RAL_NOISE_FLOOR, 0); 996184610Salfred ieee80211_free_node(ni); 997188417Sthompsa } else 998188417Sthompsa (void) ieee80211_input_all(ic, m, rssi, 999188417Sthompsa RAL_NOISE_FLOOR, 0); 1000188417Sthompsa RAL_LOCK(sc); 1001184610Salfred } 1002184610Salfred return; 1003184610Salfred 1004184610Salfred default: /* Error */ 1005184610Salfred if (xfer->error != USB_ERR_CANCELLED) { 1006184610Salfred /* try to clear stall first */ 1007188417Sthompsa xfer->flags.stall_pipe = 1; 1008188417Sthompsa goto tr_setup; 1009184610Salfred } 1010184610Salfred return; 1011184610Salfred } 1012184610Salfred} 1013184610Salfred 1014184610Salfredstatic uint8_t 1015188417Sthompsaural_plcp_signal(int rate) 1016184610Salfred{ 1017184610Salfred switch (rate) { 1018188417Sthompsa /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1019188417Sthompsa case 12: return 0xb; 1020188417Sthompsa case 18: return 0xf; 1021188417Sthompsa case 24: return 0xa; 1022188417Sthompsa case 36: return 0xe; 1023188417Sthompsa case 48: return 0x9; 1024188417Sthompsa case 72: return 0xd; 1025188417Sthompsa case 96: return 0x8; 1026188417Sthompsa case 108: return 0xc; 1027184610Salfred 1028188417Sthompsa /* CCK rates (NB: not IEEE std, device-specific) */ 1029188417Sthompsa case 2: return 0x0; 1030188417Sthompsa case 4: return 0x1; 1031188417Sthompsa case 11: return 0x2; 1032188417Sthompsa case 22: return 0x3; 1033184610Salfred } 1034188417Sthompsa return 0xff; /* XXX unsupported/unknown rate */ 1035184610Salfred} 1036184610Salfred 1037184610Salfredstatic void 1038188417Sthompsaural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc, 1039188417Sthompsa uint32_t flags, int len, int rate) 1040184610Salfred{ 1041184610Salfred struct ifnet *ifp = sc->sc_ifp; 1042184610Salfred struct ieee80211com *ic = ifp->if_l2com; 1043184610Salfred uint16_t plcp_length; 1044188417Sthompsa int remainder; 1045184610Salfred 1046188417Sthompsa desc->flags = htole32(flags); 1047188417Sthompsa desc->flags |= htole32(RAL_TX_NEWSEQ); 1048188417Sthompsa desc->flags |= htole32(len << 16); 1049184610Salfred 1050188417Sthompsa desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5)); 1051188417Sthompsa desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame))); 1052184610Salfred 1053188417Sthompsa /* setup PLCP fields */ 1054188417Sthompsa desc->plcp_signal = ural_plcp_signal(rate); 1055188417Sthompsa desc->plcp_service = 4; 1056188417Sthompsa 1057188417Sthompsa len += IEEE80211_CRC_LEN; 1058188417Sthompsa if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) { 1059188417Sthompsa desc->flags |= htole32(RAL_TX_OFDM); 1060188417Sthompsa 1061188417Sthompsa plcp_length = len & 0xfff; 1062188417Sthompsa desc->plcp_length_hi = plcp_length >> 6; 1063188417Sthompsa desc->plcp_length_lo = plcp_length & 0x3f; 1064188417Sthompsa } else { 1065188417Sthompsa plcp_length = (16 * len + rate - 1) / rate; 1066188417Sthompsa if (rate == 22) { 1067188417Sthompsa remainder = (16 * len) % 22; 1068188417Sthompsa if (remainder != 0 && remainder < 7) 1069188417Sthompsa desc->plcp_service |= RAL_PLCP_LENGEXT; 1070188417Sthompsa } 1071188417Sthompsa desc->plcp_length_hi = plcp_length >> 8; 1072188417Sthompsa desc->plcp_length_lo = plcp_length & 0xff; 1073188417Sthompsa 1074188417Sthompsa if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1075188417Sthompsa desc->plcp_signal |= 0x08; 1076184610Salfred } 1077184610Salfred 1078188417Sthompsa desc->iv = 0; 1079188417Sthompsa desc->eiv = 0; 1080188417Sthompsa} 1081184610Salfred 1082188417Sthompsa#define RAL_TX_TIMEOUT 5000 1083184610Salfred 1084188417Sthompsastatic int 1085188417Sthompsaural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1086188417Sthompsa{ 1087188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 1088188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 1089188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 1090188417Sthompsa const struct ieee80211_txparam *tp; 1091188417Sthompsa struct ural_tx_data *data; 1092188417Sthompsa 1093188417Sthompsa if (sc->tx_nfree == 0) { 1094188417Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1095188417Sthompsa m_freem(m0); 1096188417Sthompsa ieee80211_free_node(ni); 1097188417Sthompsa return EIO; 1098184610Salfred } 1099188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 1100188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1101188417Sthompsa sc->tx_nfree--; 1102188417Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 1103184610Salfred 1104188417Sthompsa data->m = m0; 1105188417Sthompsa data->ni = ni; 1106188417Sthompsa data->rate = tp->mgmtrate; 1107184610Salfred 1108188417Sthompsa ural_setup_tx_desc(sc, &data->desc, 1109188417Sthompsa RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len, 1110188417Sthompsa tp->mgmtrate); 1111184610Salfred 1112188417Sthompsa DPRINTFN(10, "sending beacon frame len=%u rate=%u\n", 1113188417Sthompsa m0->m_pkthdr.len, tp->mgmtrate); 1114184610Salfred 1115188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1116188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 1117184610Salfred 1118188417Sthompsa return (0); 1119188417Sthompsa} 1120184610Salfred 1121188417Sthompsastatic int 1122188417Sthompsaural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1123188417Sthompsa{ 1124188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 1125188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 1126188417Sthompsa const struct ieee80211_txparam *tp; 1127188417Sthompsa struct ural_tx_data *data; 1128188417Sthompsa struct ieee80211_frame *wh; 1129188417Sthompsa struct ieee80211_key *k; 1130188417Sthompsa uint32_t flags; 1131188417Sthompsa uint16_t dur; 1132184610Salfred 1133188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 1134184610Salfred 1135188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 1136188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1137188417Sthompsa sc->tx_nfree--; 1138184610Salfred 1139188417Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1140188417Sthompsa 1141188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 1142188417Sthompsa if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1143188417Sthompsa k = ieee80211_crypto_encap(ni, m0); 1144188417Sthompsa if (k == NULL) { 1145188417Sthompsa m_freem(m0); 1146188417Sthompsa return ENOBUFS; 1147184610Salfred } 1148188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 1149184610Salfred } 1150184610Salfred 1151188417Sthompsa data->m = m0; 1152188417Sthompsa data->ni = ni; 1153188417Sthompsa data->rate = tp->mgmtrate; 1154184610Salfred 1155188417Sthompsa flags = 0; 1156188417Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1157188417Sthompsa flags |= RAL_TX_ACK; 1158188417Sthompsa 1159188417Sthompsa dur = ieee80211_ack_duration(sc->sc_rates, tp->mgmtrate, 1160188417Sthompsa ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1161188417Sthompsa *(uint16_t *)wh->i_dur = htole16(dur); 1162188417Sthompsa 1163188417Sthompsa /* tell hardware to add timestamp for probe responses */ 1164188417Sthompsa if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1165188417Sthompsa IEEE80211_FC0_TYPE_MGT && 1166188417Sthompsa (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1167188417Sthompsa IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1168188417Sthompsa flags |= RAL_TX_TIMESTAMP; 1169184610Salfred } 1170184610Salfred 1171188417Sthompsa ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate); 1172184610Salfred 1173188417Sthompsa DPRINTFN(10, "sending mgt frame len=%u rate=%u\n", 1174188417Sthompsa m0->m_pkthdr.len, tp->mgmtrate); 1175184610Salfred 1176188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1177188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 1178184610Salfred 1179188417Sthompsa return 0; 1180184610Salfred} 1181184610Salfred 1182188417Sthompsastatic int 1183188417Sthompsaural_sendprot(struct ural_softc *sc, 1184188417Sthompsa const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1185184610Salfred{ 1186188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 1187188417Sthompsa const struct ieee80211_frame *wh; 1188188417Sthompsa struct ural_tx_data *data; 1189188417Sthompsa struct mbuf *mprot; 1190188417Sthompsa int protrate, ackrate, pktlen, flags, isshort; 1191188417Sthompsa uint16_t dur; 1192184610Salfred 1193188417Sthompsa KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1194188417Sthompsa ("protection %d", prot)); 1195184610Salfred 1196188417Sthompsa wh = mtod(m, const struct ieee80211_frame *); 1197188417Sthompsa pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1198184610Salfred 1199188417Sthompsa protrate = ieee80211_ctl_rate(sc->sc_rates, rate); 1200188417Sthompsa ackrate = ieee80211_ack_rate(sc->sc_rates, rate); 1201188417Sthompsa 1202188417Sthompsa isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1203188417Sthompsa dur = ieee80211_compute_duration(sc->sc_rates, pktlen, rate, isshort); 1204188417Sthompsa + ieee80211_ack_duration(sc->sc_rates, rate, isshort); 1205188417Sthompsa flags = RAL_TX_RETRY(7); 1206188417Sthompsa if (prot == IEEE80211_PROT_RTSCTS) { 1207188417Sthompsa /* NB: CTS is the same size as an ACK */ 1208188417Sthompsa dur += ieee80211_ack_duration(sc->sc_rates, rate, isshort); 1209188417Sthompsa flags |= RAL_TX_ACK; 1210188417Sthompsa mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1211188417Sthompsa } else { 1212188417Sthompsa mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1213188417Sthompsa } 1214188417Sthompsa if (mprot == NULL) { 1215188417Sthompsa /* XXX stat + msg */ 1216188417Sthompsa return ENOBUFS; 1217188417Sthompsa } 1218188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 1219188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1220188417Sthompsa sc->tx_nfree--; 1221188417Sthompsa 1222188417Sthompsa data->m = mprot; 1223188417Sthompsa data->ni = ieee80211_ref_node(ni); 1224188417Sthompsa data->rate = protrate; 1225188417Sthompsa ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate); 1226188417Sthompsa 1227188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1228188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 1229188417Sthompsa 1230188417Sthompsa return 0; 1231188417Sthompsa} 1232188417Sthompsa 1233188417Sthompsastatic int 1234188417Sthompsaural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1235188417Sthompsa const struct ieee80211_bpf_params *params) 1236188417Sthompsa{ 1237188417Sthompsa struct ural_tx_data *data; 1238188417Sthompsa uint32_t flags; 1239188417Sthompsa int error; 1240188417Sthompsa int rate; 1241188417Sthompsa 1242188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 1243188417Sthompsa KASSERT(params != NULL, ("no raw xmit params")); 1244188417Sthompsa 1245188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 1246188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1247188417Sthompsa sc->tx_nfree--; 1248188417Sthompsa 1249188417Sthompsa rate = params->ibp_rate0 & IEEE80211_RATE_VAL; 1250188417Sthompsa /* XXX validate */ 1251188417Sthompsa if (rate == 0) { 1252188417Sthompsa m_freem(m0); 1253188417Sthompsa return EINVAL; 1254188417Sthompsa } 1255188417Sthompsa flags = 0; 1256188417Sthompsa if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1257188417Sthompsa flags |= RAL_TX_ACK; 1258188417Sthompsa if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1259188417Sthompsa error = ural_sendprot(sc, m0, ni, 1260188417Sthompsa params->ibp_flags & IEEE80211_BPF_RTS ? 1261188417Sthompsa IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1262188417Sthompsa rate); 1263188417Sthompsa if (error) { 1264188417Sthompsa m_freem(m0); 1265188417Sthompsa return error; 1266184610Salfred } 1267188417Sthompsa flags |= RAL_TX_IFS_SIFS; 1268188417Sthompsa } 1269184610Salfred 1270188417Sthompsa data->m = m0; 1271188417Sthompsa data->ni = ni; 1272188417Sthompsa data->rate = rate; 1273184610Salfred 1274188417Sthompsa /* XXX need to setup descriptor ourself */ 1275188417Sthompsa ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 1276184610Salfred 1277188417Sthompsa DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1278188417Sthompsa m0->m_pkthdr.len, rate); 1279184610Salfred 1280188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1281188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 1282184610Salfred 1283188417Sthompsa return 0; 1284188417Sthompsa} 1285184610Salfred 1286188417Sthompsastatic int 1287188417Sthompsaural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1288188417Sthompsa{ 1289188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 1290188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 1291188417Sthompsa struct ural_tx_data *data; 1292188417Sthompsa struct ieee80211_frame *wh; 1293188417Sthompsa const struct ieee80211_txparam *tp; 1294188417Sthompsa struct ieee80211_key *k; 1295188417Sthompsa uint32_t flags = 0; 1296188417Sthompsa uint16_t dur; 1297188417Sthompsa int error, rate; 1298184610Salfred 1299188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 1300184610Salfred 1301188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 1302184610Salfred 1303188417Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1304188417Sthompsa if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1305188417Sthompsa rate = tp->mcastrate; 1306188417Sthompsa else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1307188417Sthompsa rate = tp->ucastrate; 1308188417Sthompsa else 1309188417Sthompsa rate = ni->ni_txrate; 1310188417Sthompsa 1311188417Sthompsa if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1312188417Sthompsa k = ieee80211_crypto_encap(ni, m0); 1313188417Sthompsa if (k == NULL) { 1314188417Sthompsa m_freem(m0); 1315188417Sthompsa return ENOBUFS; 1316184610Salfred } 1317188417Sthompsa /* packet header may have moved, reset our local pointer */ 1318188417Sthompsa wh = mtod(m0, struct ieee80211_frame *); 1319188417Sthompsa } 1320184610Salfred 1321188417Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1322188417Sthompsa int prot = IEEE80211_PROT_NONE; 1323188417Sthompsa if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1324188417Sthompsa prot = IEEE80211_PROT_RTSCTS; 1325188417Sthompsa else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1326188417Sthompsa ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) 1327188417Sthompsa prot = ic->ic_protmode; 1328188417Sthompsa if (prot != IEEE80211_PROT_NONE) { 1329188417Sthompsa error = ural_sendprot(sc, m0, ni, prot, rate); 1330188417Sthompsa if (error) { 1331188417Sthompsa m_freem(m0); 1332188417Sthompsa return error; 1333188417Sthompsa } 1334188417Sthompsa flags |= RAL_TX_IFS_SIFS; 1335184610Salfred } 1336184610Salfred } 1337184610Salfred 1338188417Sthompsa data = STAILQ_FIRST(&sc->tx_free); 1339188417Sthompsa STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1340188417Sthompsa sc->tx_nfree--; 1341184610Salfred 1342188417Sthompsa data->m = m0; 1343188417Sthompsa data->ni = ni; 1344188417Sthompsa data->rate = rate; 1345188417Sthompsa 1346188417Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1347188417Sthompsa flags |= RAL_TX_ACK; 1348188417Sthompsa flags |= RAL_TX_RETRY(7); 1349188417Sthompsa 1350188417Sthompsa dur = ieee80211_ack_duration(sc->sc_rates, rate, 1351188417Sthompsa ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1352188417Sthompsa *(uint16_t *)wh->i_dur = htole16(dur); 1353184610Salfred } 1354184610Salfred 1355188417Sthompsa ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 1356184610Salfred 1357188417Sthompsa DPRINTFN(10, "sending data frame len=%u rate=%u\n", 1358188417Sthompsa m0->m_pkthdr.len, rate); 1359184610Salfred 1360188417Sthompsa STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1361188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 1362188417Sthompsa 1363188417Sthompsa return 0; 1364184610Salfred} 1365184610Salfred 1366184610Salfredstatic void 1367188417Sthompsaural_start(struct ifnet *ifp) 1368184610Salfred{ 1369188417Sthompsa struct ural_softc *sc = ifp->if_softc; 1370188417Sthompsa struct ieee80211_node *ni; 1371188417Sthompsa struct mbuf *m; 1372184610Salfred 1373188417Sthompsa RAL_LOCK(sc); 1374188417Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1375188417Sthompsa RAL_UNLOCK(sc); 1376188417Sthompsa return; 1377188417Sthompsa } 1378188417Sthompsa for (;;) { 1379188417Sthompsa IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1380188417Sthompsa if (m == NULL) 1381188417Sthompsa break; 1382188417Sthompsa if (sc->tx_nfree == 0) { 1383188417Sthompsa IFQ_DRV_PREPEND(&ifp->if_snd, m); 1384188417Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1385188417Sthompsa break; 1386188417Sthompsa } 1387188417Sthompsa ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1388188417Sthompsa m = ieee80211_encap(ni, m); 1389188417Sthompsa if (m == NULL) { 1390188417Sthompsa ieee80211_free_node(ni); 1391188417Sthompsa continue; 1392188417Sthompsa } 1393188417Sthompsa if (ural_tx_data(sc, m, ni) != 0) { 1394188417Sthompsa ieee80211_free_node(ni); 1395188417Sthompsa ifp->if_oerrors++; 1396188417Sthompsa break; 1397188417Sthompsa } 1398188417Sthompsa } 1399188417Sthompsa RAL_UNLOCK(sc); 1400184610Salfred} 1401184610Salfred 1402184610Salfredstatic int 1403188417Sthompsaural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1404184610Salfred{ 1405184610Salfred struct ural_softc *sc = ifp->if_softc; 1406184610Salfred struct ieee80211com *ic = ifp->if_l2com; 1407188417Sthompsa struct ifreq *ifr = (struct ifreq *) data; 1408188417Sthompsa int error = 0, startall = 0; 1409184610Salfred 1410184610Salfred switch (cmd) { 1411184610Salfred case SIOCSIFFLAGS: 1412188417Sthompsa RAL_LOCK(sc); 1413184610Salfred if (ifp->if_flags & IFF_UP) { 1414188417Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1415188417Sthompsa ural_queue_command(sc, ural_init_task, 1416188417Sthompsa &sc->sc_synctask[0].hdr, 1417188417Sthompsa &sc->sc_synctask[1].hdr); 1418188417Sthompsa startall = 1; 1419188417Sthompsa } else 1420188417Sthompsa ural_queue_command(sc, ural_promisctask, 1421188417Sthompsa &sc->sc_promisctask[0].hdr, 1422188417Sthompsa &sc->sc_promisctask[1].hdr); 1423184610Salfred } else { 1424184610Salfred if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1425188417Sthompsa ural_queue_command(sc, ural_stop_task, 1426188417Sthompsa &sc->sc_synctask[0].hdr, 1427188417Sthompsa &sc->sc_synctask[1].hdr); 1428184610Salfred } 1429184610Salfred } 1430188417Sthompsa RAL_UNLOCK(sc); 1431188417Sthompsa if (startall) 1432188417Sthompsa ieee80211_start_all(ic); 1433184610Salfred break; 1434184610Salfred case SIOCGIFMEDIA: 1435184610Salfred case SIOCSIFMEDIA: 1436188417Sthompsa error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1437184610Salfred break; 1438184610Salfred default: 1439184610Salfred error = ether_ioctl(ifp, cmd, data); 1440188417Sthompsa break; 1441184610Salfred } 1442188417Sthompsa return error; 1443184610Salfred} 1444184610Salfred 1445184610Salfredstatic void 1446188417Sthompsaural_set_testmode(struct ural_softc *sc) 1447184610Salfred{ 1448188417Sthompsa struct usb2_device_request req; 1449188417Sthompsa usb2_error_t error; 1450184610Salfred 1451188417Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1452188417Sthompsa req.bRequest = RAL_VENDOR_REQUEST; 1453188417Sthompsa USETW(req.wValue, 4); 1454188417Sthompsa USETW(req.wIndex, 1); 1455188417Sthompsa USETW(req.wLength, 0); 1456188417Sthompsa 1457188417Sthompsa error = ural_do_request(sc, &req, NULL); 1458188417Sthompsa if (error != 0) { 1459188417Sthompsa device_printf(sc->sc_dev, "could not set test mode: %s\n", 1460188417Sthompsa usb2_errstr(error)); 1461188417Sthompsa } 1462184610Salfred} 1463184610Salfred 1464184610Salfredstatic void 1465188417Sthompsaural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len) 1466184610Salfred{ 1467188417Sthompsa struct usb2_device_request req; 1468188417Sthompsa usb2_error_t error; 1469184610Salfred 1470188417Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 1471188417Sthompsa req.bRequest = RAL_READ_EEPROM; 1472188417Sthompsa USETW(req.wValue, 0); 1473188417Sthompsa USETW(req.wIndex, addr); 1474188417Sthompsa USETW(req.wLength, len); 1475184610Salfred 1476188417Sthompsa error = ural_do_request(sc, &req, buf); 1477188417Sthompsa if (error != 0) { 1478188417Sthompsa device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1479188417Sthompsa usb2_errstr(error)); 1480184610Salfred } 1481188417Sthompsa} 1482184610Salfred 1483188417Sthompsastatic uint16_t 1484188417Sthompsaural_read(struct ural_softc *sc, uint16_t reg) 1485188417Sthompsa{ 1486188417Sthompsa struct usb2_device_request req; 1487188417Sthompsa usb2_error_t error; 1488188417Sthompsa uint16_t val; 1489184610Salfred 1490188417Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 1491188417Sthompsa req.bRequest = RAL_READ_MAC; 1492188417Sthompsa USETW(req.wValue, 0); 1493188417Sthompsa USETW(req.wIndex, reg); 1494188417Sthompsa USETW(req.wLength, sizeof (uint16_t)); 1495188417Sthompsa 1496188417Sthompsa error = ural_do_request(sc, &req, &val); 1497188417Sthompsa if (error != 0) { 1498188417Sthompsa device_printf(sc->sc_dev, "could not read MAC register: %s\n", 1499188417Sthompsa usb2_errstr(error)); 1500188417Sthompsa return 0; 1501184610Salfred } 1502184610Salfred 1503188417Sthompsa return le16toh(val); 1504184610Salfred} 1505184610Salfred 1506188417Sthompsastatic void 1507188417Sthompsaural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 1508184610Salfred{ 1509188417Sthompsa struct usb2_device_request req; 1510188417Sthompsa usb2_error_t error; 1511184610Salfred 1512188417Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 1513188417Sthompsa req.bRequest = RAL_READ_MULTI_MAC; 1514188417Sthompsa USETW(req.wValue, 0); 1515188417Sthompsa USETW(req.wIndex, reg); 1516188417Sthompsa USETW(req.wLength, len); 1517184610Salfred 1518188417Sthompsa error = ural_do_request(sc, &req, buf); 1519188417Sthompsa if (error != 0) { 1520188417Sthompsa device_printf(sc->sc_dev, "could not read MAC register: %s\n", 1521188417Sthompsa usb2_errstr(error)); 1522184610Salfred } 1523188417Sthompsa} 1524188417Sthompsa 1525188417Sthompsastatic void 1526188417Sthompsaural_write(struct ural_softc *sc, uint16_t reg, uint16_t val) 1527188417Sthompsa{ 1528188417Sthompsa struct usb2_device_request req; 1529188417Sthompsa usb2_error_t error; 1530188417Sthompsa 1531188417Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1532188417Sthompsa req.bRequest = RAL_WRITE_MAC; 1533188417Sthompsa USETW(req.wValue, val); 1534188417Sthompsa USETW(req.wIndex, reg); 1535188417Sthompsa USETW(req.wLength, 0); 1536188417Sthompsa 1537188417Sthompsa error = ural_do_request(sc, &req, NULL); 1538188417Sthompsa if (error != 0) { 1539188417Sthompsa device_printf(sc->sc_dev, "could not write MAC register: %s\n", 1540188417Sthompsa usb2_errstr(error)); 1541184610Salfred } 1542188417Sthompsa} 1543184610Salfred 1544188417Sthompsastatic void 1545188417Sthompsaural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 1546188417Sthompsa{ 1547188417Sthompsa struct usb2_device_request req; 1548188417Sthompsa usb2_error_t error; 1549184610Salfred 1550188417Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1551188417Sthompsa req.bRequest = RAL_WRITE_MULTI_MAC; 1552188417Sthompsa USETW(req.wValue, 0); 1553188417Sthompsa USETW(req.wIndex, reg); 1554188417Sthompsa USETW(req.wLength, len); 1555184610Salfred 1556188417Sthompsa error = ural_do_request(sc, &req, buf); 1557188417Sthompsa if (error != 0) { 1558188417Sthompsa device_printf(sc->sc_dev, "could not write MAC register: %s\n", 1559188417Sthompsa usb2_errstr(error)); 1560188417Sthompsa } 1561184610Salfred} 1562184610Salfred 1563184610Salfredstatic void 1564188417Sthompsaural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val) 1565184610Salfred{ 1566188417Sthompsa uint16_t tmp; 1567188417Sthompsa int ntries; 1568184610Salfred 1569188417Sthompsa for (ntries = 0; ntries < 5; ntries++) { 1570188417Sthompsa if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 1571188417Sthompsa break; 1572188417Sthompsa } 1573188417Sthompsa if (ntries == 5) { 1574188417Sthompsa device_printf(sc->sc_dev, "could not write to BBP\n"); 1575188417Sthompsa return; 1576188417Sthompsa } 1577184610Salfred 1578188417Sthompsa tmp = reg << 8 | val; 1579188417Sthompsa ural_write(sc, RAL_PHY_CSR7, tmp); 1580188417Sthompsa} 1581184610Salfred 1582188417Sthompsastatic uint8_t 1583188417Sthompsaural_bbp_read(struct ural_softc *sc, uint8_t reg) 1584188417Sthompsa{ 1585188417Sthompsa uint16_t val; 1586188417Sthompsa int ntries; 1587184610Salfred 1588188417Sthompsa val = RAL_BBP_WRITE | reg << 8; 1589188417Sthompsa ural_write(sc, RAL_PHY_CSR7, val); 1590188417Sthompsa 1591188417Sthompsa for (ntries = 0; ntries < 5; ntries++) { 1592188417Sthompsa if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 1593188417Sthompsa break; 1594188417Sthompsa } 1595188417Sthompsa if (ntries == 5) { 1596188417Sthompsa device_printf(sc->sc_dev, "could not read BBP\n"); 1597188417Sthompsa return 0; 1598188417Sthompsa } 1599188417Sthompsa 1600188417Sthompsa return ural_read(sc, RAL_PHY_CSR7) & 0xff; 1601184610Salfred} 1602184610Salfred 1603184610Salfredstatic void 1604188417Sthompsaural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val) 1605184610Salfred{ 1606188417Sthompsa uint32_t tmp; 1607188417Sthompsa int ntries; 1608188417Sthompsa 1609188417Sthompsa for (ntries = 0; ntries < 5; ntries++) { 1610188417Sthompsa if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY)) 1611188417Sthompsa break; 1612188417Sthompsa } 1613188417Sthompsa if (ntries == 5) { 1614188417Sthompsa device_printf(sc->sc_dev, "could not write to RF\n"); 1615188417Sthompsa return; 1616188417Sthompsa } 1617188417Sthompsa 1618188417Sthompsa tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3); 1619188417Sthompsa ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff); 1620188417Sthompsa ural_write(sc, RAL_PHY_CSR10, tmp >> 16); 1621188417Sthompsa 1622188417Sthompsa /* remember last written value in sc */ 1623188417Sthompsa sc->rf_regs[reg] = val; 1624188417Sthompsa 1625188417Sthompsa DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 1626184610Salfred} 1627184610Salfred 1628188417Sthompsa/* ARGUSED */ 1629188417Sthompsastatic struct ieee80211_node * 1630188417Sthompsaural_node_alloc(struct ieee80211vap *vap __unused, 1631188417Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 1632188417Sthompsa{ 1633188417Sthompsa struct ural_node *un; 1634188417Sthompsa 1635188417Sthompsa un = malloc(sizeof(struct ural_node), M_80211_NODE, M_NOWAIT | M_ZERO); 1636188417Sthompsa return un != NULL ? &un->ni : NULL; 1637188417Sthompsa} 1638188417Sthompsa 1639184610Salfredstatic void 1640188417Sthompsaural_newassoc(struct ieee80211_node *ni, int isnew) 1641184610Salfred{ 1642188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 1643188417Sthompsa 1644188417Sthompsa ieee80211_amrr_node_init(&URAL_VAP(vap)->amrr, &URAL_NODE(ni)->amn, ni); 1645184610Salfred} 1646184610Salfred 1647184610Salfredstatic void 1648188417Sthompsaural_scan_start(struct ieee80211com *ic) 1649184610Salfred{ 1650188417Sthompsa struct ural_softc *sc = ic->ic_ifp->if_softc; 1651188417Sthompsa 1652188417Sthompsa RAL_LOCK(sc); 1653188417Sthompsa /* do it in a process context */ 1654188417Sthompsa sc->sc_scan_action = URAL_SCAN_START; 1655188417Sthompsa ural_queue_command(sc, ural_scantask, 1656188417Sthompsa &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 1657188417Sthompsa RAL_UNLOCK(sc); 1658188417Sthompsa 1659184610Salfred} 1660184610Salfred 1661184610Salfredstatic void 1662188417Sthompsaural_scan_end(struct ieee80211com *ic) 1663184610Salfred{ 1664188417Sthompsa struct ural_softc *sc = ic->ic_ifp->if_softc; 1665188417Sthompsa 1666188417Sthompsa RAL_LOCK(sc); 1667188417Sthompsa /* do it in a process context */ 1668188417Sthompsa sc->sc_scan_action = URAL_SCAN_END; 1669188417Sthompsa ural_queue_command(sc, ural_scantask, 1670188417Sthompsa &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 1671188417Sthompsa RAL_UNLOCK(sc); 1672188417Sthompsa 1673184610Salfred} 1674184610Salfred 1675184610Salfredstatic void 1676188417Sthompsaural_set_channel(struct ieee80211com *ic) 1677184610Salfred{ 1678188417Sthompsa struct ural_softc *sc = ic->ic_ifp->if_softc; 1679188417Sthompsa 1680188417Sthompsa RAL_LOCK(sc); 1681188417Sthompsa /* do it in a process context */ 1682188417Sthompsa sc->sc_scan_action = URAL_SET_CHANNEL; 1683188417Sthompsa ural_queue_command(sc, ural_scantask, 1684188417Sthompsa &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 1685188417Sthompsa 1686188417Sthompsa sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan); 1687188417Sthompsa RAL_UNLOCK(sc); 1688184610Salfred} 1689184610Salfred 1690184610Salfredstatic void 1691188417Sthompsaural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c) 1692184610Salfred{ 1693188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 1694188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 1695188417Sthompsa uint8_t power, tmp; 1696188417Sthompsa u_int i, chan; 1697184610Salfred 1698188417Sthompsa chan = ieee80211_chan2ieee(ic, c); 1699188417Sthompsa if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1700188417Sthompsa return; 1701184610Salfred 1702188417Sthompsa if (IEEE80211_IS_CHAN_2GHZ(c)) 1703188417Sthompsa power = min(sc->txpow[chan - 1], 31); 1704184610Salfred else 1705184610Salfred power = 31; 1706184610Salfred 1707184610Salfred /* adjust txpower using ifconfig settings */ 1708188417Sthompsa power -= (100 - ic->ic_txpowlimit) / 8; 1709184610Salfred 1710188417Sthompsa DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power); 1711184610Salfred 1712188417Sthompsa switch (sc->rf_rev) { 1713184610Salfred case RAL_RF_2522: 1714188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x00814); 1715188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]); 1716188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1717184610Salfred break; 1718184610Salfred 1719184610Salfred case RAL_RF_2523: 1720188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x08804); 1721188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]); 1722188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 1723188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1724184610Salfred break; 1725184610Salfred 1726184610Salfred case RAL_RF_2524: 1727188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x0c808); 1728188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]); 1729188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1730188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1731184610Salfred break; 1732184610Salfred 1733184610Salfred case RAL_RF_2525: 1734188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x08808); 1735188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]); 1736188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1737188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1738184610Salfred 1739188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x08808); 1740188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]); 1741188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1742188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 1743184610Salfred break; 1744184610Salfred 1745184610Salfred case RAL_RF_2525E: 1746188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x08808); 1747188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]); 1748188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1749188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 1750184610Salfred break; 1751184610Salfred 1752184610Salfred case RAL_RF_2526: 1753188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]); 1754188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 1755188417Sthompsa ural_rf_write(sc, RAL_RF1, 0x08804); 1756184610Salfred 1757188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]); 1758188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 1759188417Sthompsa ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 1760184610Salfred break; 1761184610Salfred 1762188417Sthompsa /* dual-band RF */ 1763184610Salfred case RAL_RF_5222: 1764188417Sthompsa for (i = 0; ural_rf5222[i].chan != chan; i++); 1765188417Sthompsa 1766188417Sthompsa ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1); 1767188417Sthompsa ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2); 1768188417Sthompsa ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 1769188417Sthompsa ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4); 1770184610Salfred break; 1771184610Salfred } 1772184610Salfred 1773188417Sthompsa if (ic->ic_opmode != IEEE80211_M_MONITOR && 1774188417Sthompsa (ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1775184610Salfred /* set Japan filter bit for channel 14 */ 1776188417Sthompsa tmp = ural_bbp_read(sc, 70); 1777184610Salfred 1778188417Sthompsa tmp &= ~RAL_JAPAN_FILTER; 1779188417Sthompsa if (chan == 14) 1780184610Salfred tmp |= RAL_JAPAN_FILTER; 1781184610Salfred 1782188417Sthompsa ural_bbp_write(sc, 70, tmp); 1783184610Salfred 1784184610Salfred /* clear CRC errors */ 1785188417Sthompsa ural_read(sc, RAL_STA_CSR0); 1786184610Salfred 1787188417Sthompsa DELAY(10000); 1788188417Sthompsa ural_disable_rf_tune(sc); 1789184610Salfred } 1790188417Sthompsa 1791188417Sthompsa /* XXX doesn't belong here */ 1792184610Salfred /* update basic rate set */ 1793188417Sthompsa ural_set_basicrates(sc, c); 1794184610Salfred} 1795184610Salfred 1796188417Sthompsa/* 1797188417Sthompsa * Disable RF auto-tuning. 1798188417Sthompsa */ 1799184610Salfredstatic void 1800188417Sthompsaural_disable_rf_tune(struct ural_softc *sc) 1801184610Salfred{ 1802188417Sthompsa uint32_t tmp; 1803184610Salfred 1804188417Sthompsa if (sc->rf_rev != RAL_RF_2523) { 1805188417Sthompsa tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 1806188417Sthompsa ural_rf_write(sc, RAL_RF1, tmp); 1807184610Salfred } 1808184610Salfred 1809188417Sthompsa tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 1810188417Sthompsa ural_rf_write(sc, RAL_RF3, tmp); 1811184610Salfred 1812188417Sthompsa DPRINTFN(2, "disabling RF autotune\n"); 1813184610Salfred} 1814184610Salfred 1815188417Sthompsa/* 1816188417Sthompsa * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 1817188417Sthompsa * synchronization. 1818188417Sthompsa */ 1819184610Salfredstatic void 1820188417Sthompsaural_enable_tsf_sync(struct ural_softc *sc) 1821184610Salfred{ 1822188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 1823188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 1824188417Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1825188417Sthompsa uint16_t logcwmin, preload, tmp; 1826184610Salfred 1827184610Salfred /* first, disable TSF synchronization */ 1828188417Sthompsa ural_write(sc, RAL_TXRX_CSR19, 0); 1829184610Salfred 1830188417Sthompsa tmp = (16 * vap->iv_bss->ni_intval) << 4; 1831188417Sthompsa ural_write(sc, RAL_TXRX_CSR18, tmp); 1832184610Salfred 1833188417Sthompsa logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0; 1834188417Sthompsa preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6; 1835188417Sthompsa tmp = logcwmin << 12 | preload; 1836188417Sthompsa ural_write(sc, RAL_TXRX_CSR20, tmp); 1837184610Salfred 1838184610Salfred /* finally, enable TSF synchronization */ 1839184610Salfred tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN; 1840188417Sthompsa if (ic->ic_opmode == IEEE80211_M_STA) 1841184610Salfred tmp |= RAL_ENABLE_TSF_SYNC(1); 1842184610Salfred else 1843184610Salfred tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR; 1844188417Sthompsa ural_write(sc, RAL_TXRX_CSR19, tmp); 1845184610Salfred 1846184610Salfred DPRINTF("enabling TSF synchronization\n"); 1847184610Salfred} 1848184610Salfred 1849188417Sthompsa#define RAL_RXTX_TURNAROUND 5 /* us */ 1850184610Salfredstatic void 1851188417Sthompsaural_update_slot(struct ifnet *ifp) 1852184610Salfred{ 1853188417Sthompsa struct ural_softc *sc = ifp->if_softc; 1854188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 1855188417Sthompsa uint16_t slottime, sifs, eifs; 1856184610Salfred 1857188417Sthompsa slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1858184610Salfred 1859184610Salfred /* 1860184610Salfred * These settings may sound a bit inconsistent but this is what the 1861184610Salfred * reference driver does. 1862184610Salfred */ 1863188417Sthompsa if (ic->ic_curmode == IEEE80211_MODE_11B) { 1864184610Salfred sifs = 16 - RAL_RXTX_TURNAROUND; 1865184610Salfred eifs = 364; 1866184610Salfred } else { 1867184610Salfred sifs = 10 - RAL_RXTX_TURNAROUND; 1868184610Salfred eifs = 64; 1869184610Salfred } 1870184610Salfred 1871188417Sthompsa ural_write(sc, RAL_MAC_CSR10, slottime); 1872188417Sthompsa ural_write(sc, RAL_MAC_CSR11, sifs); 1873188417Sthompsa ural_write(sc, RAL_MAC_CSR12, eifs); 1874184610Salfred} 1875184610Salfred 1876184610Salfredstatic void 1877188417Sthompsaural_set_txpreamble(struct ural_softc *sc) 1878184610Salfred{ 1879188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 1880188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 1881184610Salfred uint16_t tmp; 1882184610Salfred 1883188417Sthompsa tmp = ural_read(sc, RAL_TXRX_CSR10); 1884184610Salfred 1885188417Sthompsa tmp &= ~RAL_SHORT_PREAMBLE; 1886188417Sthompsa if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1887184610Salfred tmp |= RAL_SHORT_PREAMBLE; 1888184610Salfred 1889188417Sthompsa ural_write(sc, RAL_TXRX_CSR10, tmp); 1890184610Salfred} 1891184610Salfred 1892184610Salfredstatic void 1893188417Sthompsaural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c) 1894184610Salfred{ 1895188417Sthompsa /* XXX wrong, take from rate set */ 1896188417Sthompsa /* update basic rate set */ 1897188417Sthompsa if (IEEE80211_IS_CHAN_5GHZ(c)) { 1898188417Sthompsa /* 11a basic rates: 6, 12, 24Mbps */ 1899188417Sthompsa ural_write(sc, RAL_TXRX_CSR11, 0x150); 1900188417Sthompsa } else if (IEEE80211_IS_CHAN_ANYG(c)) { 1901188417Sthompsa /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 1902188417Sthompsa ural_write(sc, RAL_TXRX_CSR11, 0x15f); 1903188417Sthompsa } else { 1904188417Sthompsa /* 11b basic rates: 1, 2Mbps */ 1905188417Sthompsa ural_write(sc, RAL_TXRX_CSR11, 0x3); 1906188417Sthompsa } 1907184610Salfred} 1908184610Salfred 1909184610Salfredstatic void 1910188417Sthompsaural_set_bssid(struct ural_softc *sc, const uint8_t *bssid) 1911184610Salfred{ 1912188417Sthompsa uint16_t tmp; 1913184610Salfred 1914188417Sthompsa tmp = bssid[0] | bssid[1] << 8; 1915188417Sthompsa ural_write(sc, RAL_MAC_CSR5, tmp); 1916188417Sthompsa 1917188417Sthompsa tmp = bssid[2] | bssid[3] << 8; 1918188417Sthompsa ural_write(sc, RAL_MAC_CSR6, tmp); 1919188417Sthompsa 1920188417Sthompsa tmp = bssid[4] | bssid[5] << 8; 1921188417Sthompsa ural_write(sc, RAL_MAC_CSR7, tmp); 1922188417Sthompsa 1923188417Sthompsa DPRINTF("setting BSSID to %6D\n", bssid, ":"); 1924184610Salfred} 1925184610Salfred 1926184610Salfredstatic void 1927188417Sthompsaural_set_macaddr(struct ural_softc *sc, uint8_t *addr) 1928184610Salfred{ 1929184610Salfred uint16_t tmp; 1930184610Salfred 1931188417Sthompsa tmp = addr[0] | addr[1] << 8; 1932188417Sthompsa ural_write(sc, RAL_MAC_CSR2, tmp); 1933184610Salfred 1934188417Sthompsa tmp = addr[2] | addr[3] << 8; 1935188417Sthompsa ural_write(sc, RAL_MAC_CSR3, tmp); 1936184610Salfred 1937188417Sthompsa tmp = addr[4] | addr[5] << 8; 1938188417Sthompsa ural_write(sc, RAL_MAC_CSR4, tmp); 1939184610Salfred 1940188417Sthompsa DPRINTF("setting MAC address to %6D\n", addr, ":"); 1941184610Salfred} 1942184610Salfred 1943184610Salfredstatic void 1944188417Sthompsaural_promisctask(struct usb2_proc_msg *pm) 1945184610Salfred{ 1946188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 1947188417Sthompsa struct ural_softc *sc = task->sc; 1948188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 1949188417Sthompsa uint32_t tmp; 1950184610Salfred 1951188417Sthompsa tmp = ural_read(sc, RAL_TXRX_CSR2); 1952184610Salfred 1953188417Sthompsa tmp &= ~RAL_DROP_NOT_TO_ME; 1954188417Sthompsa if (!(ifp->if_flags & IFF_PROMISC)) 1955188417Sthompsa tmp |= RAL_DROP_NOT_TO_ME; 1956184610Salfred 1957188417Sthompsa ural_write(sc, RAL_TXRX_CSR2, tmp); 1958184610Salfred 1959188417Sthompsa DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1960188417Sthompsa "entering" : "leaving"); 1961184610Salfred} 1962184610Salfred 1963188417Sthompsastatic const char * 1964188417Sthompsaural_get_rf(int rev) 1965184610Salfred{ 1966188417Sthompsa switch (rev) { 1967188417Sthompsa case RAL_RF_2522: return "RT2522"; 1968188417Sthompsa case RAL_RF_2523: return "RT2523"; 1969188417Sthompsa case RAL_RF_2524: return "RT2524"; 1970188417Sthompsa case RAL_RF_2525: return "RT2525"; 1971188417Sthompsa case RAL_RF_2525E: return "RT2525e"; 1972188417Sthompsa case RAL_RF_2526: return "RT2526"; 1973188417Sthompsa case RAL_RF_5222: return "RT5222"; 1974188417Sthompsa default: return "unknown"; 1975184610Salfred } 1976184610Salfred} 1977184610Salfred 1978184610Salfredstatic void 1979188417Sthompsaural_read_eeprom(struct ural_softc *sc) 1980184610Salfred{ 1981184610Salfred uint16_t val; 1982184610Salfred 1983188417Sthompsa ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2); 1984184610Salfred val = le16toh(val); 1985188417Sthompsa sc->rf_rev = (val >> 11) & 0x7; 1986188417Sthompsa sc->hw_radio = (val >> 10) & 0x1; 1987188417Sthompsa sc->led_mode = (val >> 6) & 0x7; 1988188417Sthompsa sc->rx_ant = (val >> 4) & 0x3; 1989188417Sthompsa sc->tx_ant = (val >> 2) & 0x3; 1990188417Sthompsa sc->nb_ant = val & 0x3; 1991184610Salfred 1992184610Salfred /* read MAC address */ 1993188417Sthompsa ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6); 1994184610Salfred 1995184610Salfred /* read default values for BBP registers */ 1996188417Sthompsa ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1997184610Salfred 1998184610Salfred /* read Tx power for all b/g channels */ 1999188417Sthompsa ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14); 2000184610Salfred} 2001184610Salfred 2002188417Sthompsastatic int 2003188417Sthompsaural_bbp_init(struct ural_softc *sc) 2004184610Salfred{ 2005188417Sthompsa#define N(a) (sizeof (a) / sizeof ((a)[0])) 2006188417Sthompsa int i, ntries; 2007184610Salfred 2008188417Sthompsa /* wait for BBP to be ready */ 2009188417Sthompsa for (ntries = 0; ntries < 100; ntries++) { 2010188417Sthompsa if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0) 2011188417Sthompsa break; 2012188417Sthompsa DELAY(1000); 2013184610Salfred } 2014188417Sthompsa if (ntries == 100) { 2015188417Sthompsa device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2016188417Sthompsa return EIO; 2017188417Sthompsa } 2018184610Salfred 2019184610Salfred /* initialize BBP registers to default values */ 2020188417Sthompsa for (i = 0; i < N(ural_def_bbp); i++) 2021188417Sthompsa ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val); 2022184610Salfred 2023184610Salfred#if 0 2024184610Salfred /* initialize BBP registers to values stored in EEPROM */ 2025184610Salfred for (i = 0; i < 16; i++) { 2026188417Sthompsa if (sc->bbp_prom[i].reg == 0xff) 2027184610Salfred continue; 2028188417Sthompsa ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2029184610Salfred } 2030184610Salfred#endif 2031188417Sthompsa 2032188417Sthompsa return 0; 2033188417Sthompsa#undef N 2034184610Salfred} 2035184610Salfred 2036184610Salfredstatic void 2037188417Sthompsaural_set_txantenna(struct ural_softc *sc, int antenna) 2038184610Salfred{ 2039188417Sthompsa uint16_t tmp; 2040188417Sthompsa uint8_t tx; 2041184610Salfred 2042188417Sthompsa tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK; 2043188417Sthompsa if (antenna == 1) 2044188417Sthompsa tx |= RAL_BBP_ANTA; 2045188417Sthompsa else if (antenna == 2) 2046188417Sthompsa tx |= RAL_BBP_ANTB; 2047188417Sthompsa else 2048188417Sthompsa tx |= RAL_BBP_DIVERSITY; 2049184610Salfred 2050188417Sthompsa /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2051188417Sthompsa if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 || 2052188417Sthompsa sc->rf_rev == RAL_RF_5222) 2053188417Sthompsa tx |= RAL_BBP_FLIPIQ; 2054184610Salfred 2055188417Sthompsa ural_bbp_write(sc, RAL_BBP_TX, tx); 2056184610Salfred 2057188417Sthompsa /* update values in PHY_CSR5 and PHY_CSR6 */ 2058188417Sthompsa tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7; 2059188417Sthompsa ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7)); 2060184610Salfred 2061188417Sthompsa tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7; 2062188417Sthompsa ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7)); 2063184610Salfred} 2064184610Salfred 2065184610Salfredstatic void 2066188417Sthompsaural_set_rxantenna(struct ural_softc *sc, int antenna) 2067184610Salfred{ 2068188417Sthompsa uint8_t rx; 2069188417Sthompsa 2070188417Sthompsa rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK; 2071188417Sthompsa if (antenna == 1) 2072188417Sthompsa rx |= RAL_BBP_ANTA; 2073188417Sthompsa else if (antenna == 2) 2074188417Sthompsa rx |= RAL_BBP_ANTB; 2075188417Sthompsa else 2076188417Sthompsa rx |= RAL_BBP_DIVERSITY; 2077188417Sthompsa 2078188417Sthompsa /* need to force no I/Q flip for RF 2525e and 2526 */ 2079188417Sthompsa if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526) 2080188417Sthompsa rx &= ~RAL_BBP_FLIPIQ; 2081188417Sthompsa 2082188417Sthompsa ural_bbp_write(sc, RAL_BBP_RX, rx); 2083188417Sthompsa} 2084188417Sthompsa 2085188417Sthompsastatic void 2086188417Sthompsaural_init_task(struct usb2_proc_msg *pm) 2087188417Sthompsa{ 2088188417Sthompsa#define N(a) (sizeof (a) / sizeof ((a)[0])) 2089188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 2090188417Sthompsa struct ural_softc *sc = task->sc; 2091188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 2092188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 2093184610Salfred uint16_t tmp; 2094188417Sthompsa int i, ntries; 2095184610Salfred 2096188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 2097184610Salfred 2098188417Sthompsa ural_set_testmode(sc); 2099188417Sthompsa ural_write(sc, 0x308, 0x00f0); /* XXX magic */ 2100184610Salfred 2101188417Sthompsa ural_stop_task(pm); 2102184610Salfred 2103184610Salfred /* initialize MAC registers to default values */ 2104188417Sthompsa for (i = 0; i < N(ural_def_mac); i++) 2105188417Sthompsa ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val); 2106184610Salfred 2107184610Salfred /* wait for BBP and RF to wake up (this can take a long time!) */ 2108188417Sthompsa for (ntries = 0; ntries < 100; ntries++) { 2109188417Sthompsa tmp = ural_read(sc, RAL_MAC_CSR17); 2110188417Sthompsa if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) == 2111188417Sthompsa (RAL_BBP_AWAKE | RAL_RF_AWAKE)) 2112188417Sthompsa break; 2113188417Sthompsa DELAY(1000); 2114184610Salfred } 2115188417Sthompsa if (ntries == 100) { 2116188417Sthompsa device_printf(sc->sc_dev, 2117188417Sthompsa "timeout waiting for BBP/RF to wakeup\n"); 2118188417Sthompsa goto fail; 2119188417Sthompsa } 2120184610Salfred 2121184610Salfred /* we're ready! */ 2122188417Sthompsa ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY); 2123184610Salfred 2124184610Salfred /* set basic rate set (will be updated later) */ 2125188417Sthompsa ural_write(sc, RAL_TXRX_CSR11, 0x15f); 2126184610Salfred 2127188417Sthompsa if (ural_bbp_init(sc) != 0) 2128184610Salfred goto fail; 2129184610Salfred 2130188417Sthompsa ural_set_chan(sc, ic->ic_curchan); 2131188417Sthompsa 2132184610Salfred /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 2133188417Sthompsa ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 2134184610Salfred 2135188417Sthompsa ural_set_txantenna(sc, sc->tx_ant); 2136188417Sthompsa ural_set_rxantenna(sc, sc->rx_ant); 2137184610Salfred 2138188417Sthompsa IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2139188417Sthompsa ural_set_macaddr(sc, ic->ic_myaddr); 2140184610Salfred 2141184610Salfred /* 2142188417Sthompsa * Allocate Tx and Rx xfer queues. 2143184610Salfred */ 2144188417Sthompsa ural_setup_tx_list(sc); 2145184610Salfred 2146188417Sthompsa /* kick Rx */ 2147184610Salfred tmp = RAL_DROP_PHY | RAL_DROP_CRC; 2148188417Sthompsa if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2149188417Sthompsa tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION; 2150188417Sthompsa if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2151184610Salfred tmp |= RAL_DROP_TODS; 2152188417Sthompsa if (!(ifp->if_flags & IFF_PROMISC)) 2153184610Salfred tmp |= RAL_DROP_NOT_TO_ME; 2154184610Salfred } 2155188417Sthompsa ural_write(sc, RAL_TXRX_CSR2, tmp); 2156184610Salfred 2157188417Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2158188417Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 2159188417Sthompsa usb2_transfer_start(sc->sc_xfer[URAL_BULK_RD]); 2160184610Salfred return; 2161184610Salfred 2162188417Sthompsafail: ural_stop_task(pm); 2163188417Sthompsa#undef N 2164184610Salfred} 2165184610Salfred 2166184610Salfredstatic void 2167188417Sthompsaural_init(void *priv) 2168184610Salfred{ 2169188417Sthompsa struct ural_softc *sc = priv; 2170184610Salfred struct ifnet *ifp = sc->sc_ifp; 2171188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 2172184610Salfred 2173188417Sthompsa RAL_LOCK(sc); 2174188417Sthompsa ural_queue_command(sc, ural_init_task, 2175188417Sthompsa &sc->sc_synctask[0].hdr, 2176188417Sthompsa &sc->sc_synctask[1].hdr); 2177188417Sthompsa RAL_UNLOCK(sc); 2178184610Salfred 2179188417Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2180188417Sthompsa ieee80211_start_all(ic); /* start all vap's */ 2181184610Salfred} 2182184610Salfred 2183184610Salfredstatic void 2184188417Sthompsaural_stop_task(struct usb2_proc_msg *pm) 2185184610Salfred{ 2186188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 2187188417Sthompsa struct ural_softc *sc = task->sc; 2188184610Salfred struct ifnet *ifp = sc->sc_ifp; 2189184610Salfred 2190188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 2191184610Salfred 2192188417Sthompsa ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2193184610Salfred 2194184610Salfred /* 2195188417Sthompsa * Drain all the transfers, if not already drained: 2196184610Salfred */ 2197188417Sthompsa RAL_UNLOCK(sc); 2198188417Sthompsa usb2_transfer_drain(sc->sc_xfer[URAL_BULK_WR]); 2199188417Sthompsa usb2_transfer_drain(sc->sc_xfer[URAL_BULK_RD]); 2200188417Sthompsa RAL_LOCK(sc); 2201184610Salfred 2202188417Sthompsa ural_unsetup_tx_list(sc); 2203184610Salfred 2204188417Sthompsa /* disable Rx */ 2205188417Sthompsa ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX); 2206188417Sthompsa /* reset ASIC and BBP (but won't reset MAC registers!) */ 2207188417Sthompsa ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP); 2208188417Sthompsa ural_write(sc, RAL_MAC_CSR1, 0); 2209184610Salfred} 2210184610Salfred 2211188417Sthompsastatic int 2212188417Sthompsaural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2213188417Sthompsa const struct ieee80211_bpf_params *params) 2214184610Salfred{ 2215188417Sthompsa struct ieee80211com *ic = ni->ni_ic; 2216188417Sthompsa struct ifnet *ifp = ic->ic_ifp; 2217188417Sthompsa struct ural_softc *sc = ifp->if_softc; 2218184610Salfred 2219188417Sthompsa RAL_LOCK(sc); 2220188417Sthompsa /* prevent management frames from being sent if we're not ready */ 2221188417Sthompsa if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2222188417Sthompsa RAL_UNLOCK(sc); 2223188417Sthompsa m_freem(m); 2224188417Sthompsa ieee80211_free_node(ni); 2225188417Sthompsa return ENETDOWN; 2226184610Salfred } 2227188417Sthompsa if (sc->tx_nfree == 0) { 2228188417Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2229188417Sthompsa RAL_UNLOCK(sc); 2230184610Salfred m_freem(m); 2231184610Salfred ieee80211_free_node(ni); 2232188417Sthompsa return EIO; 2233184610Salfred } 2234184610Salfred 2235188417Sthompsa ifp->if_opackets++; 2236184610Salfred 2237188417Sthompsa if (params == NULL) { 2238188417Sthompsa /* 2239188417Sthompsa * Legacy path; interpret frame contents to decide 2240188417Sthompsa * precisely how to send the frame. 2241188417Sthompsa */ 2242188417Sthompsa if (ural_tx_mgt(sc, m, ni) != 0) 2243188417Sthompsa goto bad; 2244188417Sthompsa } else { 2245188417Sthompsa /* 2246188417Sthompsa * Caller supplied explicit parameters to use in 2247188417Sthompsa * sending the frame. 2248188417Sthompsa */ 2249188417Sthompsa if (ural_tx_raw(sc, m, ni, params) != 0) 2250188417Sthompsa goto bad; 2251184610Salfred } 2252188417Sthompsa RAL_UNLOCK(sc); 2253188417Sthompsa return 0; 2254188417Sthompsabad: 2255188417Sthompsa ifp->if_oerrors++; 2256188417Sthompsa RAL_UNLOCK(sc); 2257188417Sthompsa ieee80211_free_node(ni); 2258188417Sthompsa return EIO; /* XXX */ 2259184610Salfred} 2260184610Salfred 2261184610Salfredstatic void 2262188417Sthompsaural_amrr_start(struct ural_softc *sc, struct ieee80211_node *ni) 2263184610Salfred{ 2264188417Sthompsa struct ieee80211vap *vap = ni->ni_vap; 2265188417Sthompsa struct ural_vap *uvp = URAL_VAP(vap); 2266184610Salfred 2267188417Sthompsa /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 2268188417Sthompsa ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 2269184610Salfred 2270188417Sthompsa ieee80211_amrr_node_init(&uvp->amrr, &URAL_NODE(ni)->amn, ni); 2271184610Salfred 2272188417Sthompsa usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 2273184610Salfred} 2274184610Salfred 2275184610Salfredstatic void 2276188417Sthompsaural_amrr_timeout(void *arg) 2277184610Salfred{ 2278188417Sthompsa struct ural_vap *uvp = arg; 2279188417Sthompsa struct ural_softc *sc = uvp->sc; 2280184610Salfred 2281188417Sthompsa ural_queue_command(sc, ural_amrr_task, 2282188417Sthompsa &uvp->amrr_task[0].hdr, &uvp->amrr_task[1].hdr); 2283184610Salfred} 2284184610Salfred 2285184610Salfredstatic void 2286188417Sthompsaural_amrr_task(struct usb2_proc_msg *pm) 2287184610Salfred{ 2288188417Sthompsa struct ural_task *task = (struct ural_task *)pm; 2289188417Sthompsa struct ural_softc *sc = task->sc; 2290188417Sthompsa struct ifnet *ifp = sc->sc_ifp; 2291188417Sthompsa struct ieee80211com *ic = ifp->if_l2com; 2292188417Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2293188417Sthompsa struct ural_vap *uvp = URAL_VAP(vap); 2294188417Sthompsa struct ieee80211_node *ni = vap->iv_bss; 2295188417Sthompsa int ok, fail; 2296184610Salfred 2297188417Sthompsa /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2298188417Sthompsa ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta)); 2299184610Salfred 2300188417Sthompsa ok = sc->sta[7] + /* TX ok w/o retry */ 2301188417Sthompsa sc->sta[8]; /* TX ok w/ retry */ 2302188417Sthompsa fail = sc->sta[9]; /* TX retry-fail count */ 2303184610Salfred 2304188417Sthompsa ieee80211_amrr_tx_update(&URAL_NODE(ni)->amn, 2305188417Sthompsa ok+fail, ok, sc->sta[8] + fail); 2306188417Sthompsa (void) ieee80211_amrr_choose(ni, &URAL_NODE(ni)->amn); 2307184610Salfred 2308188417Sthompsa ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 2309184610Salfred 2310188417Sthompsa usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 2311184610Salfred} 2312184610Salfred 2313184610Salfredstatic void 2314188417Sthompsaural_queue_command(struct ural_softc *sc, usb2_proc_callback_t *fn, 2315188417Sthompsa struct usb2_proc_msg *t0, struct usb2_proc_msg *t1) 2316184610Salfred{ 2317188417Sthompsa struct ural_task *task; 2318184610Salfred 2319188417Sthompsa RAL_LOCK_ASSERT(sc, MA_OWNED); 2320184610Salfred 2321188417Sthompsa if (usb2_proc_is_gone(&sc->sc_tq)) { 2322188417Sthompsa DPRINTF("proc is gone\n"); 2323188417Sthompsa return; /* nothing to do */ 2324184610Salfred } 2325188417Sthompsa /* 2326188417Sthompsa * NOTE: The task cannot get executed before we drop the 2327188417Sthompsa * "sc_mtx" mutex. It is safe to update fields in the message 2328188417Sthompsa * structure after that the message got queued. 2329188417Sthompsa */ 2330188417Sthompsa task = (struct ural_task *) 2331188417Sthompsa usb2_proc_msignal(&sc->sc_tq, t0, t1); 2332184610Salfred 2333188417Sthompsa /* Setup callback and softc pointers */ 2334188417Sthompsa task->hdr.pm_callback = fn; 2335188417Sthompsa task->sc = sc; 2336184610Salfred 2337188417Sthompsa /* 2338188417Sthompsa * Init and stop must be synchronous! 2339188417Sthompsa */ 2340188417Sthompsa if ((fn == ural_init_task) || (fn == ural_stop_task)) 2341188417Sthompsa usb2_proc_mwait(&sc->sc_tq, t0, t1); 2342184610Salfred} 2343184610Salfred 2344