102ac6454SAndrew Thompson
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson * Copyright (c) 2005, 2006
402ac6454SAndrew Thompson * Damien Bergamini <damien.bergamini@free.fr>
502ac6454SAndrew Thompson *
602ac6454SAndrew Thompson * Copyright (c) 2006, 2008
75d38a4d4SEd Schouten * Hans Petter Selasky <hselasky@FreeBSD.org>
802ac6454SAndrew Thompson *
902ac6454SAndrew Thompson * Permission to use, copy, modify, and distribute this software for any
1002ac6454SAndrew Thompson * purpose with or without fee is hereby granted, provided that the above
1102ac6454SAndrew Thompson * copyright notice and this permission notice appear in all copies.
1202ac6454SAndrew Thompson *
1302ac6454SAndrew Thompson * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1402ac6454SAndrew Thompson * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1502ac6454SAndrew Thompson * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1602ac6454SAndrew Thompson * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1702ac6454SAndrew Thompson * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1802ac6454SAndrew Thompson * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1902ac6454SAndrew Thompson * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2002ac6454SAndrew Thompson */
2102ac6454SAndrew Thompson
2202ac6454SAndrew Thompson /*-
2302ac6454SAndrew Thompson * Ralink Technology RT2500USB chipset driver
2402ac6454SAndrew Thompson * http://www.ralinktech.com/
2502ac6454SAndrew Thompson */
2602ac6454SAndrew Thompson
2763a55eabSAndriy Voskoboinyk #include "opt_wlan.h"
2863a55eabSAndriy Voskoboinyk
295efea30fSAndrew Thompson #include <sys/param.h>
305efea30fSAndrew Thompson #include <sys/sockio.h>
315efea30fSAndrew Thompson #include <sys/sysctl.h>
325efea30fSAndrew Thompson #include <sys/lock.h>
335efea30fSAndrew Thompson #include <sys/mutex.h>
345efea30fSAndrew Thompson #include <sys/mbuf.h>
355efea30fSAndrew Thompson #include <sys/kernel.h>
365efea30fSAndrew Thompson #include <sys/socket.h>
375efea30fSAndrew Thompson #include <sys/systm.h>
385efea30fSAndrew Thompson #include <sys/malloc.h>
395efea30fSAndrew Thompson #include <sys/module.h>
405efea30fSAndrew Thompson #include <sys/bus.h>
415efea30fSAndrew Thompson #include <sys/endian.h>
425efea30fSAndrew Thompson #include <sys/kdb.h>
435efea30fSAndrew Thompson
445efea30fSAndrew Thompson #include <net/bpf.h>
455efea30fSAndrew Thompson #include <net/if.h>
4676039bc8SGleb Smirnoff #include <net/if_var.h>
475efea30fSAndrew Thompson #include <net/if_arp.h>
485efea30fSAndrew Thompson #include <net/ethernet.h>
495efea30fSAndrew Thompson #include <net/if_dl.h>
505efea30fSAndrew Thompson #include <net/if_media.h>
515efea30fSAndrew Thompson #include <net/if_types.h>
525efea30fSAndrew Thompson
535efea30fSAndrew Thompson #ifdef INET
545efea30fSAndrew Thompson #include <netinet/in.h>
555efea30fSAndrew Thompson #include <netinet/in_systm.h>
565efea30fSAndrew Thompson #include <netinet/in_var.h>
575efea30fSAndrew Thompson #include <netinet/if_ether.h>
585efea30fSAndrew Thompson #include <netinet/ip.h>
595efea30fSAndrew Thompson #endif
605efea30fSAndrew Thompson
615efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
625efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
635efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
64b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
6502ac6454SAndrew Thompson
665efea30fSAndrew Thompson #include <dev/usb/usb.h>
67ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
685efea30fSAndrew Thompson #include "usbdevs.h"
6902ac6454SAndrew Thompson
70ed6d949aSAndrew Thompson #define USB_DEBUG_VAR ural_debug
71ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
725efea30fSAndrew Thompson
7302ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralreg.h>
7402ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralvar.h>
7502ac6454SAndrew Thompson
76b850ecc1SAndrew Thompson #ifdef USB_DEBUG
7702ac6454SAndrew Thompson static int ural_debug = 0;
7802ac6454SAndrew Thompson
79f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
80f8d2b1f3SPawel Biernacki "USB ural");
81ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RWTUN, &ural_debug, 0,
8202ac6454SAndrew Thompson "Debug level");
8302ac6454SAndrew Thompson #endif
8402ac6454SAndrew Thompson
8502ac6454SAndrew Thompson #define URAL_RSSI(rssi) \
8602ac6454SAndrew Thompson ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
8702ac6454SAndrew Thompson ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
8802ac6454SAndrew Thompson
8902ac6454SAndrew Thompson /* various supported device vendors/products */
90f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ural_devs[] = {
919e6b5313SAndrew Thompson #define URAL_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
929e6b5313SAndrew Thompson URAL_DEV(ASUS, WL167G),
939e6b5313SAndrew Thompson URAL_DEV(ASUS, RT2570),
949e6b5313SAndrew Thompson URAL_DEV(BELKIN, F5D7050),
959e6b5313SAndrew Thompson URAL_DEV(BELKIN, F5D7051),
969e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, HU200TS),
979e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, WUSB54G),
989e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, WUSB54GP),
999e6b5313SAndrew Thompson URAL_DEV(CONCEPTRONIC2, C54RU),
1009e6b5313SAndrew Thompson URAL_DEV(DLINK, DWLG122),
1019e6b5313SAndrew Thompson URAL_DEV(GIGABYTE, GN54G),
1029e6b5313SAndrew Thompson URAL_DEV(GIGABYTE, GNWBKG),
1039e6b5313SAndrew Thompson URAL_DEV(GUILLEMOT, HWGUSB254),
1049e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54),
1059e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54AI),
1069e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54YB),
1079e6b5313SAndrew Thompson URAL_DEV(MELCO, NINWIFI),
1089e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570),
1099e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570_2),
1109e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570_3),
1119e6b5313SAndrew Thompson URAL_DEV(NOVATECH, NV902),
1129e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570),
1139e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570_2),
1149e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570_3),
1159e6b5313SAndrew Thompson URAL_DEV(SIEMENS2, WL54G),
1169e6b5313SAndrew Thompson URAL_DEV(SMC, 2862WG),
1179e6b5313SAndrew Thompson URAL_DEV(SPHAIRON, UB801R),
1189e6b5313SAndrew Thompson URAL_DEV(SURECOM, RT2570),
1199e6b5313SAndrew Thompson URAL_DEV(VTECH, RT2570),
1209e6b5313SAndrew Thompson URAL_DEV(ZINWELL, RT2570),
1219e6b5313SAndrew Thompson #undef URAL_DEV
12202ac6454SAndrew Thompson };
12302ac6454SAndrew Thompson
124e0a69b51SAndrew Thompson static usb_callback_t ural_bulk_read_callback;
125e0a69b51SAndrew Thompson static usb_callback_t ural_bulk_write_callback;
12602ac6454SAndrew Thompson
127e0a69b51SAndrew Thompson static usb_error_t ural_do_request(struct ural_softc *sc,
128760bc48eSAndrew Thompson struct usb_device_request *req, void *data);
12902ac6454SAndrew Thompson static struct ieee80211vap *ural_vap_create(struct ieee80211com *,
130fcd9500fSBernhard Schmidt const char [IFNAMSIZ], int, enum ieee80211_opmode,
131fcd9500fSBernhard Schmidt int, const uint8_t [IEEE80211_ADDR_LEN],
132fcd9500fSBernhard Schmidt const uint8_t [IEEE80211_ADDR_LEN]);
13302ac6454SAndrew Thompson static void ural_vap_delete(struct ieee80211vap *);
13402ac6454SAndrew Thompson static void ural_tx_free(struct ural_tx_data *, int);
13502ac6454SAndrew Thompson static void ural_setup_tx_list(struct ural_softc *);
13602ac6454SAndrew Thompson static void ural_unsetup_tx_list(struct ural_softc *);
13702ac6454SAndrew Thompson static int ural_newstate(struct ieee80211vap *,
13802ac6454SAndrew Thompson enum ieee80211_state, int);
13902ac6454SAndrew Thompson static void ural_setup_tx_desc(struct ural_softc *,
14002ac6454SAndrew Thompson struct ural_tx_desc *, uint32_t, int, int);
14102ac6454SAndrew Thompson static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
14202ac6454SAndrew Thompson struct ieee80211_node *);
14302ac6454SAndrew Thompson static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
14402ac6454SAndrew Thompson struct ieee80211_node *);
14502ac6454SAndrew Thompson static int ural_tx_data(struct ural_softc *, struct mbuf *,
14602ac6454SAndrew Thompson struct ieee80211_node *);
1477a79cebfSGleb Smirnoff static int ural_transmit(struct ieee80211com *, struct mbuf *);
1487a79cebfSGleb Smirnoff static void ural_start(struct ural_softc *);
1497a79cebfSGleb Smirnoff static void ural_parent(struct ieee80211com *);
15002ac6454SAndrew Thompson static void ural_set_testmode(struct ural_softc *);
15102ac6454SAndrew Thompson static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
15202ac6454SAndrew Thompson int);
15302ac6454SAndrew Thompson static uint16_t ural_read(struct ural_softc *, uint16_t);
15402ac6454SAndrew Thompson static void ural_read_multi(struct ural_softc *, uint16_t, void *,
15502ac6454SAndrew Thompson int);
15602ac6454SAndrew Thompson static void ural_write(struct ural_softc *, uint16_t, uint16_t);
15702ac6454SAndrew Thompson static void ural_write_multi(struct ural_softc *, uint16_t, void *,
15802ac6454SAndrew Thompson int) __unused;
15902ac6454SAndrew Thompson static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
16002ac6454SAndrew Thompson static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
16102ac6454SAndrew Thompson static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
16202ac6454SAndrew Thompson static void ural_scan_start(struct ieee80211com *);
16302ac6454SAndrew Thompson static void ural_scan_end(struct ieee80211com *);
16483829b48SAndriy Voskoboinyk static void ural_getradiocaps(struct ieee80211com *, int, int *,
16583829b48SAndriy Voskoboinyk struct ieee80211_channel[]);
16602ac6454SAndrew Thompson static void ural_set_channel(struct ieee80211com *);
16702ac6454SAndrew Thompson static void ural_set_chan(struct ural_softc *,
16802ac6454SAndrew Thompson struct ieee80211_channel *);
16902ac6454SAndrew Thompson static void ural_disable_rf_tune(struct ural_softc *);
17002ac6454SAndrew Thompson static void ural_enable_tsf_sync(struct ural_softc *);
1715463c4a4SSam Leffler static void ural_enable_tsf(struct ural_softc *);
1727a79cebfSGleb Smirnoff static void ural_update_slot(struct ural_softc *);
17302ac6454SAndrew Thompson static void ural_set_txpreamble(struct ural_softc *);
17402ac6454SAndrew Thompson static void ural_set_basicrates(struct ural_softc *,
17502ac6454SAndrew Thompson const struct ieee80211_channel *);
17602ac6454SAndrew Thompson static void ural_set_bssid(struct ural_softc *, const uint8_t *);
1777a79cebfSGleb Smirnoff static void ural_set_macaddr(struct ural_softc *, const uint8_t *);
178272f6adeSGleb Smirnoff static void ural_update_promisc(struct ieee80211com *);
1795efea30fSAndrew Thompson static void ural_setpromisc(struct ural_softc *);
18002ac6454SAndrew Thompson static const char *ural_get_rf(int);
18102ac6454SAndrew Thompson static void ural_read_eeprom(struct ural_softc *);
18202ac6454SAndrew Thompson static int ural_bbp_init(struct ural_softc *);
18302ac6454SAndrew Thompson static void ural_set_txantenna(struct ural_softc *, int);
18402ac6454SAndrew Thompson static void ural_set_rxantenna(struct ural_softc *, int);
1857a79cebfSGleb Smirnoff static void ural_init(struct ural_softc *);
1865efea30fSAndrew Thompson static void ural_stop(struct ural_softc *);
18702ac6454SAndrew Thompson static int ural_raw_xmit(struct ieee80211_node *, struct mbuf *,
18802ac6454SAndrew Thompson const struct ieee80211_bpf_params *);
189b6108616SRui Paulo static void ural_ratectl_start(struct ural_softc *,
19002ac6454SAndrew Thompson struct ieee80211_node *);
191b6108616SRui Paulo static void ural_ratectl_timeout(void *);
192b6108616SRui Paulo static void ural_ratectl_task(void *, int);
19302ac6454SAndrew Thompson static int ural_pause(struct ural_softc *sc, int timeout);
19402ac6454SAndrew Thompson
19502ac6454SAndrew Thompson /*
19602ac6454SAndrew Thompson * Default values for MAC registers; values taken from the reference driver.
19702ac6454SAndrew Thompson */
19802ac6454SAndrew Thompson static const struct {
19902ac6454SAndrew Thompson uint16_t reg;
20002ac6454SAndrew Thompson uint16_t val;
20102ac6454SAndrew Thompson } ural_def_mac[] = {
20202ac6454SAndrew Thompson { RAL_TXRX_CSR5, 0x8c8d },
20302ac6454SAndrew Thompson { RAL_TXRX_CSR6, 0x8b8a },
20402ac6454SAndrew Thompson { RAL_TXRX_CSR7, 0x8687 },
20502ac6454SAndrew Thompson { RAL_TXRX_CSR8, 0x0085 },
20602ac6454SAndrew Thompson { RAL_MAC_CSR13, 0x1111 },
20702ac6454SAndrew Thompson { RAL_MAC_CSR14, 0x1e11 },
20802ac6454SAndrew Thompson { RAL_TXRX_CSR21, 0xe78f },
20902ac6454SAndrew Thompson { RAL_MAC_CSR9, 0xff1d },
21002ac6454SAndrew Thompson { RAL_MAC_CSR11, 0x0002 },
21102ac6454SAndrew Thompson { RAL_MAC_CSR22, 0x0053 },
21202ac6454SAndrew Thompson { RAL_MAC_CSR15, 0x0000 },
21302ac6454SAndrew Thompson { RAL_MAC_CSR8, RAL_FRAME_SIZE },
21402ac6454SAndrew Thompson { RAL_TXRX_CSR19, 0x0000 },
21502ac6454SAndrew Thompson { RAL_TXRX_CSR18, 0x005a },
21602ac6454SAndrew Thompson { RAL_PHY_CSR2, 0x0000 },
21702ac6454SAndrew Thompson { RAL_TXRX_CSR0, 0x1ec0 },
21802ac6454SAndrew Thompson { RAL_PHY_CSR4, 0x000f }
21902ac6454SAndrew Thompson };
22002ac6454SAndrew Thompson
22102ac6454SAndrew Thompson /*
22202ac6454SAndrew Thompson * Default values for BBP registers; values taken from the reference driver.
22302ac6454SAndrew Thompson */
22402ac6454SAndrew Thompson static const struct {
22502ac6454SAndrew Thompson uint8_t reg;
22602ac6454SAndrew Thompson uint8_t val;
22702ac6454SAndrew Thompson } ural_def_bbp[] = {
22802ac6454SAndrew Thompson { 3, 0x02 },
22902ac6454SAndrew Thompson { 4, 0x19 },
23002ac6454SAndrew Thompson { 14, 0x1c },
23102ac6454SAndrew Thompson { 15, 0x30 },
23202ac6454SAndrew Thompson { 16, 0xac },
23302ac6454SAndrew Thompson { 17, 0x48 },
23402ac6454SAndrew Thompson { 18, 0x18 },
23502ac6454SAndrew Thompson { 19, 0xff },
23602ac6454SAndrew Thompson { 20, 0x1e },
23702ac6454SAndrew Thompson { 21, 0x08 },
23802ac6454SAndrew Thompson { 22, 0x08 },
23902ac6454SAndrew Thompson { 23, 0x08 },
24002ac6454SAndrew Thompson { 24, 0x80 },
24102ac6454SAndrew Thompson { 25, 0x50 },
24202ac6454SAndrew Thompson { 26, 0x08 },
24302ac6454SAndrew Thompson { 27, 0x23 },
24402ac6454SAndrew Thompson { 30, 0x10 },
24502ac6454SAndrew Thompson { 31, 0x2b },
24602ac6454SAndrew Thompson { 32, 0xb9 },
24702ac6454SAndrew Thompson { 34, 0x12 },
24802ac6454SAndrew Thompson { 35, 0x50 },
24902ac6454SAndrew Thompson { 39, 0xc4 },
25002ac6454SAndrew Thompson { 40, 0x02 },
25102ac6454SAndrew Thompson { 41, 0x60 },
25202ac6454SAndrew Thompson { 53, 0x10 },
25302ac6454SAndrew Thompson { 54, 0x18 },
25402ac6454SAndrew Thompson { 56, 0x08 },
25502ac6454SAndrew Thompson { 57, 0x10 },
25602ac6454SAndrew Thompson { 58, 0x08 },
25702ac6454SAndrew Thompson { 61, 0x60 },
25802ac6454SAndrew Thompson { 62, 0x10 },
25902ac6454SAndrew Thompson { 75, 0xff }
26002ac6454SAndrew Thompson };
26102ac6454SAndrew Thompson
26202ac6454SAndrew Thompson /*
26302ac6454SAndrew Thompson * Default values for RF register R2 indexed by channel numbers.
26402ac6454SAndrew Thompson */
26502ac6454SAndrew Thompson static const uint32_t ural_rf2522_r2[] = {
26602ac6454SAndrew Thompson 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
26702ac6454SAndrew Thompson 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
26802ac6454SAndrew Thompson };
26902ac6454SAndrew Thompson
27002ac6454SAndrew Thompson static const uint32_t ural_rf2523_r2[] = {
27102ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
27202ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
27302ac6454SAndrew Thompson };
27402ac6454SAndrew Thompson
27502ac6454SAndrew Thompson static const uint32_t ural_rf2524_r2[] = {
27602ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
27702ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
27802ac6454SAndrew Thompson };
27902ac6454SAndrew Thompson
28002ac6454SAndrew Thompson static const uint32_t ural_rf2525_r2[] = {
28102ac6454SAndrew Thompson 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
28202ac6454SAndrew Thompson 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
28302ac6454SAndrew Thompson };
28402ac6454SAndrew Thompson
28502ac6454SAndrew Thompson static const uint32_t ural_rf2525_hi_r2[] = {
28602ac6454SAndrew Thompson 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
28702ac6454SAndrew Thompson 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
28802ac6454SAndrew Thompson };
28902ac6454SAndrew Thompson
29002ac6454SAndrew Thompson static const uint32_t ural_rf2525e_r2[] = {
29102ac6454SAndrew Thompson 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
29202ac6454SAndrew Thompson 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
29302ac6454SAndrew Thompson };
29402ac6454SAndrew Thompson
29502ac6454SAndrew Thompson static const uint32_t ural_rf2526_hi_r2[] = {
29602ac6454SAndrew Thompson 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
29702ac6454SAndrew Thompson 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
29802ac6454SAndrew Thompson };
29902ac6454SAndrew Thompson
30002ac6454SAndrew Thompson static const uint32_t ural_rf2526_r2[] = {
30102ac6454SAndrew Thompson 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
30202ac6454SAndrew Thompson 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
30302ac6454SAndrew Thompson };
30402ac6454SAndrew Thompson
30502ac6454SAndrew Thompson /*
30602ac6454SAndrew Thompson * For dual-band RF, RF registers R1 and R4 also depend on channel number;
30702ac6454SAndrew Thompson * values taken from the reference driver.
30802ac6454SAndrew Thompson */
30902ac6454SAndrew Thompson static const struct {
31002ac6454SAndrew Thompson uint8_t chan;
31102ac6454SAndrew Thompson uint32_t r1;
31202ac6454SAndrew Thompson uint32_t r2;
31302ac6454SAndrew Thompson uint32_t r4;
31402ac6454SAndrew Thompson } ural_rf5222[] = {
31502ac6454SAndrew Thompson { 1, 0x08808, 0x0044d, 0x00282 },
31602ac6454SAndrew Thompson { 2, 0x08808, 0x0044e, 0x00282 },
31702ac6454SAndrew Thompson { 3, 0x08808, 0x0044f, 0x00282 },
31802ac6454SAndrew Thompson { 4, 0x08808, 0x00460, 0x00282 },
31902ac6454SAndrew Thompson { 5, 0x08808, 0x00461, 0x00282 },
32002ac6454SAndrew Thompson { 6, 0x08808, 0x00462, 0x00282 },
32102ac6454SAndrew Thompson { 7, 0x08808, 0x00463, 0x00282 },
32202ac6454SAndrew Thompson { 8, 0x08808, 0x00464, 0x00282 },
32302ac6454SAndrew Thompson { 9, 0x08808, 0x00465, 0x00282 },
32402ac6454SAndrew Thompson { 10, 0x08808, 0x00466, 0x00282 },
32502ac6454SAndrew Thompson { 11, 0x08808, 0x00467, 0x00282 },
32602ac6454SAndrew Thompson { 12, 0x08808, 0x00468, 0x00282 },
32702ac6454SAndrew Thompson { 13, 0x08808, 0x00469, 0x00282 },
32802ac6454SAndrew Thompson { 14, 0x08808, 0x0046b, 0x00286 },
32902ac6454SAndrew Thompson
33002ac6454SAndrew Thompson { 36, 0x08804, 0x06225, 0x00287 },
33102ac6454SAndrew Thompson { 40, 0x08804, 0x06226, 0x00287 },
33202ac6454SAndrew Thompson { 44, 0x08804, 0x06227, 0x00287 },
33302ac6454SAndrew Thompson { 48, 0x08804, 0x06228, 0x00287 },
33402ac6454SAndrew Thompson { 52, 0x08804, 0x06229, 0x00287 },
33502ac6454SAndrew Thompson { 56, 0x08804, 0x0622a, 0x00287 },
33602ac6454SAndrew Thompson { 60, 0x08804, 0x0622b, 0x00287 },
33702ac6454SAndrew Thompson { 64, 0x08804, 0x0622c, 0x00287 },
33802ac6454SAndrew Thompson
33902ac6454SAndrew Thompson { 100, 0x08804, 0x02200, 0x00283 },
34002ac6454SAndrew Thompson { 104, 0x08804, 0x02201, 0x00283 },
34102ac6454SAndrew Thompson { 108, 0x08804, 0x02202, 0x00283 },
34202ac6454SAndrew Thompson { 112, 0x08804, 0x02203, 0x00283 },
34302ac6454SAndrew Thompson { 116, 0x08804, 0x02204, 0x00283 },
34402ac6454SAndrew Thompson { 120, 0x08804, 0x02205, 0x00283 },
34502ac6454SAndrew Thompson { 124, 0x08804, 0x02206, 0x00283 },
34602ac6454SAndrew Thompson { 128, 0x08804, 0x02207, 0x00283 },
34702ac6454SAndrew Thompson { 132, 0x08804, 0x02208, 0x00283 },
34802ac6454SAndrew Thompson { 136, 0x08804, 0x02209, 0x00283 },
34902ac6454SAndrew Thompson { 140, 0x08804, 0x0220a, 0x00283 },
35002ac6454SAndrew Thompson
35102ac6454SAndrew Thompson { 149, 0x08808, 0x02429, 0x00281 },
35202ac6454SAndrew Thompson { 153, 0x08808, 0x0242b, 0x00281 },
35302ac6454SAndrew Thompson { 157, 0x08808, 0x0242d, 0x00281 },
35402ac6454SAndrew Thompson { 161, 0x08808, 0x0242f, 0x00281 }
35502ac6454SAndrew Thompson };
35602ac6454SAndrew Thompson
35783829b48SAndriy Voskoboinyk static const uint8_t ural_chan_5ghz[] =
35883829b48SAndriy Voskoboinyk { 36, 40, 44, 48, 52, 56, 60, 64,
35983829b48SAndriy Voskoboinyk 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
36083829b48SAndriy Voskoboinyk 149, 153, 157, 161 };
36183829b48SAndriy Voskoboinyk
362760bc48eSAndrew Thompson static const struct usb_config ural_config[URAL_N_TRANSFER] = {
36302ac6454SAndrew Thompson [URAL_BULK_WR] = {
36402ac6454SAndrew Thompson .type = UE_BULK,
36502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
36602ac6454SAndrew Thompson .direction = UE_DIR_OUT,
3674eae601eSAndrew Thompson .bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4),
3684eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
3694eae601eSAndrew Thompson .callback = ural_bulk_write_callback,
3704eae601eSAndrew Thompson .timeout = 5000, /* ms */
37102ac6454SAndrew Thompson },
37202ac6454SAndrew Thompson [URAL_BULK_RD] = {
37302ac6454SAndrew Thompson .type = UE_BULK,
37402ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
37502ac6454SAndrew Thompson .direction = UE_DIR_IN,
3764eae601eSAndrew Thompson .bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE),
3774eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3784eae601eSAndrew Thompson .callback = ural_bulk_read_callback,
37902ac6454SAndrew Thompson },
38002ac6454SAndrew Thompson };
38102ac6454SAndrew Thompson
38202ac6454SAndrew Thompson static device_probe_t ural_match;
38302ac6454SAndrew Thompson static device_attach_t ural_attach;
38402ac6454SAndrew Thompson static device_detach_t ural_detach;
38502ac6454SAndrew Thompson
38602ac6454SAndrew Thompson static device_method_t ural_methods[] = {
38702ac6454SAndrew Thompson /* Device interface */
38802ac6454SAndrew Thompson DEVMETHOD(device_probe, ural_match),
38902ac6454SAndrew Thompson DEVMETHOD(device_attach, ural_attach),
39002ac6454SAndrew Thompson DEVMETHOD(device_detach, ural_detach),
391645e4d17SHans Petter Selasky DEVMETHOD_END
39202ac6454SAndrew Thompson };
39302ac6454SAndrew Thompson
39402ac6454SAndrew Thompson static driver_t ural_driver = {
39502ac6454SAndrew Thompson .name = "ural",
39602ac6454SAndrew Thompson .methods = ural_methods,
39702ac6454SAndrew Thompson .size = sizeof(struct ural_softc),
39802ac6454SAndrew Thompson };
39902ac6454SAndrew Thompson
400bc9372d7SJohn Baldwin DRIVER_MODULE(ural, uhub, ural_driver, NULL, NULL);
40102ac6454SAndrew Thompson MODULE_DEPEND(ural, usb, 1, 1, 1);
40202ac6454SAndrew Thompson MODULE_DEPEND(ural, wlan, 1, 1, 1);
403910cb8feSAndrew Thompson MODULE_VERSION(ural, 1);
404f809f280SWarner Losh USB_PNP_HOST_INFO(ural_devs);
40502ac6454SAndrew Thompson
40602ac6454SAndrew Thompson static int
ural_match(device_t self)40702ac6454SAndrew Thompson ural_match(device_t self)
40802ac6454SAndrew Thompson {
409760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(self);
41002ac6454SAndrew Thompson
411f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST)
41202ac6454SAndrew Thompson return (ENXIO);
41302ac6454SAndrew Thompson if (uaa->info.bConfigIndex != 0)
41402ac6454SAndrew Thompson return (ENXIO);
41502ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX)
41602ac6454SAndrew Thompson return (ENXIO);
41702ac6454SAndrew Thompson
418a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa));
41902ac6454SAndrew Thompson }
42002ac6454SAndrew Thompson
42102ac6454SAndrew Thompson static int
ural_attach(device_t self)42202ac6454SAndrew Thompson ural_attach(device_t self)
42302ac6454SAndrew Thompson {
424760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(self);
42502ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self);
4267a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
4270046e186SAndriy Voskoboinyk uint8_t iface_index;
42802ac6454SAndrew Thompson int error;
42902ac6454SAndrew Thompson
430a593f6b8SAndrew Thompson device_set_usb_desc(self);
43102ac6454SAndrew Thompson sc->sc_udev = uaa->device;
43202ac6454SAndrew Thompson sc->sc_dev = self;
43302ac6454SAndrew Thompson
43402ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, device_get_nameunit(self),
43502ac6454SAndrew Thompson MTX_NETWORK_LOCK, MTX_DEF);
4367a79cebfSGleb Smirnoff mbufq_init(&sc->sc_snd, ifqmaxlen);
43702ac6454SAndrew Thompson
43802ac6454SAndrew Thompson iface_index = RAL_IFACE_INDEX;
439a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device,
44002ac6454SAndrew Thompson &iface_index, sc->sc_xfer, ural_config,
44102ac6454SAndrew Thompson URAL_N_TRANSFER, sc, &sc->sc_mtx);
44202ac6454SAndrew Thompson if (error) {
44302ac6454SAndrew Thompson device_printf(self, "could not allocate USB transfers, "
444a593f6b8SAndrew Thompson "err=%s\n", usbd_errstr(error));
44502ac6454SAndrew Thompson goto detach;
44602ac6454SAndrew Thompson }
44702ac6454SAndrew Thompson
44802ac6454SAndrew Thompson RAL_LOCK(sc);
44902ac6454SAndrew Thompson /* retrieve RT2570 rev. no */
45002ac6454SAndrew Thompson sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
45102ac6454SAndrew Thompson
45202ac6454SAndrew Thompson /* retrieve MAC address and various other things from EEPROM */
45302ac6454SAndrew Thompson ural_read_eeprom(sc);
45402ac6454SAndrew Thompson RAL_UNLOCK(sc);
45502ac6454SAndrew Thompson
4565efea30fSAndrew Thompson device_printf(self, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
45702ac6454SAndrew Thompson sc->asic_rev, ural_get_rf(sc->rf_rev));
45802ac6454SAndrew Thompson
45959686fe9SGleb Smirnoff ic->ic_softc = sc;
460c8550c02SGleb Smirnoff ic->ic_name = device_get_nameunit(self);
46102ac6454SAndrew Thompson ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
46202ac6454SAndrew Thompson
46302ac6454SAndrew Thompson /* set device capabilities */
46402ac6454SAndrew Thompson ic->ic_caps =
46502ac6454SAndrew Thompson IEEE80211_C_STA /* station mode supported */
46602ac6454SAndrew Thompson | IEEE80211_C_IBSS /* IBSS mode supported */
46702ac6454SAndrew Thompson | IEEE80211_C_MONITOR /* monitor mode supported */
46802ac6454SAndrew Thompson | IEEE80211_C_HOSTAP /* HostAp mode supported */
46902ac6454SAndrew Thompson | IEEE80211_C_TXPMGT /* tx power management */
47002ac6454SAndrew Thompson | IEEE80211_C_SHPREAMBLE /* short preamble supported */
47102ac6454SAndrew Thompson | IEEE80211_C_SHSLOT /* short slot time supported */
47202ac6454SAndrew Thompson | IEEE80211_C_BGSCAN /* bg scanning supported */
47302ac6454SAndrew Thompson | IEEE80211_C_WPA /* 802.11i */
47402ac6454SAndrew Thompson ;
47502ac6454SAndrew Thompson
47683829b48SAndriy Voskoboinyk ural_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
47783829b48SAndriy Voskoboinyk ic->ic_channels);
47802ac6454SAndrew Thompson
4797a79cebfSGleb Smirnoff ieee80211_ifattach(ic);
48077ddf3d3SAndrew Thompson ic->ic_update_promisc = ural_update_promisc;
48102ac6454SAndrew Thompson ic->ic_raw_xmit = ural_raw_xmit;
48202ac6454SAndrew Thompson ic->ic_scan_start = ural_scan_start;
48302ac6454SAndrew Thompson ic->ic_scan_end = ural_scan_end;
48483829b48SAndriy Voskoboinyk ic->ic_getradiocaps = ural_getradiocaps;
48502ac6454SAndrew Thompson ic->ic_set_channel = ural_set_channel;
4867a79cebfSGleb Smirnoff ic->ic_parent = ural_parent;
4877a79cebfSGleb Smirnoff ic->ic_transmit = ural_transmit;
48802ac6454SAndrew Thompson ic->ic_vap_create = ural_vap_create;
48902ac6454SAndrew Thompson ic->ic_vap_delete = ural_vap_delete;
49002ac6454SAndrew Thompson
4915463c4a4SSam Leffler ieee80211_radiotap_attach(ic,
4925463c4a4SSam Leffler &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
4935463c4a4SSam Leffler RAL_TX_RADIOTAP_PRESENT,
4945463c4a4SSam Leffler &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
4955463c4a4SSam Leffler RAL_RX_RADIOTAP_PRESENT);
49602ac6454SAndrew Thompson
49702ac6454SAndrew Thompson if (bootverbose)
49802ac6454SAndrew Thompson ieee80211_announce(ic);
49902ac6454SAndrew Thompson
5005efea30fSAndrew Thompson return (0);
5015efea30fSAndrew Thompson
5025efea30fSAndrew Thompson detach:
5035efea30fSAndrew Thompson ural_detach(self);
5045efea30fSAndrew Thompson return (ENXIO); /* failure */
50502ac6454SAndrew Thompson }
50602ac6454SAndrew Thompson
50702ac6454SAndrew Thompson static int
ural_detach(device_t self)50802ac6454SAndrew Thompson ural_detach(device_t self)
50902ac6454SAndrew Thompson {
51002ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self);
5117a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
51202ac6454SAndrew Thompson
513645e4d17SHans Petter Selasky /* prevent further ioctls */
514645e4d17SHans Petter Selasky RAL_LOCK(sc);
515645e4d17SHans Petter Selasky sc->sc_detached = 1;
516645e4d17SHans Petter Selasky RAL_UNLOCK(sc);
517645e4d17SHans Petter Selasky
51802ac6454SAndrew Thompson /* stop all USB transfers */
519a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER);
52002ac6454SAndrew Thompson
52102ac6454SAndrew Thompson /* free TX list, if any */
52202ac6454SAndrew Thompson RAL_LOCK(sc);
52302ac6454SAndrew Thompson ural_unsetup_tx_list(sc);
52402ac6454SAndrew Thompson RAL_UNLOCK(sc);
52502ac6454SAndrew Thompson
5267a79cebfSGleb Smirnoff if (ic->ic_softc == sc)
52702ac6454SAndrew Thompson ieee80211_ifdetach(ic);
5287a79cebfSGleb Smirnoff mbufq_drain(&sc->sc_snd);
52902ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx);
53002ac6454SAndrew Thompson
53102ac6454SAndrew Thompson return (0);
53202ac6454SAndrew Thompson }
53302ac6454SAndrew Thompson
534e0a69b51SAndrew Thompson static usb_error_t
ural_do_request(struct ural_softc * sc,struct usb_device_request * req,void * data)53577ddf3d3SAndrew Thompson ural_do_request(struct ural_softc *sc,
536760bc48eSAndrew Thompson struct usb_device_request *req, void *data)
53777ddf3d3SAndrew Thompson {
538e0a69b51SAndrew Thompson usb_error_t err;
53977ddf3d3SAndrew Thompson int ntries = 10;
54077ddf3d3SAndrew Thompson
54177ddf3d3SAndrew Thompson while (ntries--) {
542a593f6b8SAndrew Thompson err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
54377ddf3d3SAndrew Thompson req, data, 0, NULL, 250 /* ms */);
54477ddf3d3SAndrew Thompson if (err == 0)
54577ddf3d3SAndrew Thompson break;
54677ddf3d3SAndrew Thompson
54777ddf3d3SAndrew Thompson DPRINTFN(1, "Control request failed, %s (retrying)\n",
548a593f6b8SAndrew Thompson usbd_errstr(err));
54977ddf3d3SAndrew Thompson if (ural_pause(sc, hz / 100))
55077ddf3d3SAndrew Thompson break;
55177ddf3d3SAndrew Thompson }
55277ddf3d3SAndrew Thompson return (err);
55377ddf3d3SAndrew Thompson }
55477ddf3d3SAndrew Thompson
55502ac6454SAndrew Thompson static struct ieee80211vap *
ural_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])556fcd9500fSBernhard Schmidt ural_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
557fcd9500fSBernhard Schmidt enum ieee80211_opmode opmode, int flags,
55802ac6454SAndrew Thompson const uint8_t bssid[IEEE80211_ADDR_LEN],
55902ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN])
56002ac6454SAndrew Thompson {
561d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
56202ac6454SAndrew Thompson struct ural_vap *uvp;
56302ac6454SAndrew Thompson struct ieee80211vap *vap;
56402ac6454SAndrew Thompson
56502ac6454SAndrew Thompson if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
56602ac6454SAndrew Thompson return NULL;
56797ad4604SKevin Lo uvp = malloc(sizeof(struct ural_vap), M_80211_VAP, M_WAITOK | M_ZERO);
56802ac6454SAndrew Thompson vap = &uvp->vap;
56902ac6454SAndrew Thompson /* enable s/w bmiss handling for sta mode */
570bb2f69e8SHans Petter Selasky
571bb2f69e8SHans Petter Selasky if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
5727a79cebfSGleb Smirnoff flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
573bb2f69e8SHans Petter Selasky /* out of memory */
574bb2f69e8SHans Petter Selasky free(uvp, M_80211_VAP);
575bb2f69e8SHans Petter Selasky return (NULL);
576bb2f69e8SHans Petter Selasky }
57702ac6454SAndrew Thompson
57802ac6454SAndrew Thompson /* override state transition machine */
57902ac6454SAndrew Thompson uvp->newstate = vap->iv_newstate;
58002ac6454SAndrew Thompson vap->iv_newstate = ural_newstate;
58102ac6454SAndrew Thompson
582b6108616SRui Paulo usb_callout_init_mtx(&uvp->ratectl_ch, &sc->sc_mtx, 0);
583b6108616SRui Paulo TASK_INIT(&uvp->ratectl_task, 0, ural_ratectl_task, uvp);
584b6108616SRui Paulo ieee80211_ratectl_init(vap);
585b6108616SRui Paulo ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
58602ac6454SAndrew Thompson
58702ac6454SAndrew Thompson /* complete setup */
5887a79cebfSGleb Smirnoff ieee80211_vap_attach(vap, ieee80211_media_change,
5897a79cebfSGleb Smirnoff ieee80211_media_status, mac);
59002ac6454SAndrew Thompson ic->ic_opmode = opmode;
59102ac6454SAndrew Thompson return vap;
59202ac6454SAndrew Thompson }
59302ac6454SAndrew Thompson
59402ac6454SAndrew Thompson static void
ural_vap_delete(struct ieee80211vap * vap)59502ac6454SAndrew Thompson ural_vap_delete(struct ieee80211vap *vap)
59602ac6454SAndrew Thompson {
59702ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap);
5985efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic;
59902ac6454SAndrew Thompson
600b6108616SRui Paulo usb_callout_drain(&uvp->ratectl_ch);
601b6108616SRui Paulo ieee80211_draintask(ic, &uvp->ratectl_task);
602b6108616SRui Paulo ieee80211_ratectl_deinit(vap);
60302ac6454SAndrew Thompson ieee80211_vap_detach(vap);
60402ac6454SAndrew Thompson free(uvp, M_80211_VAP);
60502ac6454SAndrew Thompson }
60602ac6454SAndrew Thompson
60702ac6454SAndrew Thompson static void
ural_tx_free(struct ural_tx_data * data,int txerr)60802ac6454SAndrew Thompson ural_tx_free(struct ural_tx_data *data, int txerr)
60902ac6454SAndrew Thompson {
61002ac6454SAndrew Thompson struct ural_softc *sc = data->sc;
61102ac6454SAndrew Thompson
61202ac6454SAndrew Thompson if (data->m != NULL) {
6137a79cebfSGleb Smirnoff ieee80211_tx_complete(data->ni, data->m, txerr);
61402ac6454SAndrew Thompson data->m = NULL;
61502ac6454SAndrew Thompson data->ni = NULL;
61602ac6454SAndrew Thompson }
61702ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
61802ac6454SAndrew Thompson sc->tx_nfree++;
61902ac6454SAndrew Thompson }
62002ac6454SAndrew Thompson
62102ac6454SAndrew Thompson static void
ural_setup_tx_list(struct ural_softc * sc)62202ac6454SAndrew Thompson ural_setup_tx_list(struct ural_softc *sc)
62302ac6454SAndrew Thompson {
62402ac6454SAndrew Thompson struct ural_tx_data *data;
62502ac6454SAndrew Thompson int i;
62602ac6454SAndrew Thompson
62702ac6454SAndrew Thompson sc->tx_nfree = 0;
62802ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q);
62902ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free);
63002ac6454SAndrew Thompson
63102ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
63202ac6454SAndrew Thompson data = &sc->tx_data[i];
63302ac6454SAndrew Thompson
63402ac6454SAndrew Thompson data->sc = sc;
63502ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
63602ac6454SAndrew Thompson sc->tx_nfree++;
63702ac6454SAndrew Thompson }
63802ac6454SAndrew Thompson }
63902ac6454SAndrew Thompson
64002ac6454SAndrew Thompson static void
ural_unsetup_tx_list(struct ural_softc * sc)64102ac6454SAndrew Thompson ural_unsetup_tx_list(struct ural_softc *sc)
64202ac6454SAndrew Thompson {
64302ac6454SAndrew Thompson struct ural_tx_data *data;
64402ac6454SAndrew Thompson int i;
64502ac6454SAndrew Thompson
64602ac6454SAndrew Thompson /* make sure any subsequent use of the queues will fail */
64702ac6454SAndrew Thompson sc->tx_nfree = 0;
64802ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q);
64902ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free);
65002ac6454SAndrew Thompson
65102ac6454SAndrew Thompson /* free up all node references and mbufs */
65202ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
65302ac6454SAndrew Thompson data = &sc->tx_data[i];
65402ac6454SAndrew Thompson
65502ac6454SAndrew Thompson if (data->m != NULL) {
65602ac6454SAndrew Thompson m_freem(data->m);
65702ac6454SAndrew Thompson data->m = NULL;
65802ac6454SAndrew Thompson }
65902ac6454SAndrew Thompson if (data->ni != NULL) {
66002ac6454SAndrew Thompson ieee80211_free_node(data->ni);
66102ac6454SAndrew Thompson data->ni = NULL;
66202ac6454SAndrew Thompson }
66302ac6454SAndrew Thompson }
66402ac6454SAndrew Thompson }
66502ac6454SAndrew Thompson
6665efea30fSAndrew Thompson static int
ural_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)6675efea30fSAndrew Thompson ural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
66802ac6454SAndrew Thompson {
66902ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap);
6705efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic;
671d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
67202ac6454SAndrew Thompson const struct ieee80211_txparam *tp;
67302ac6454SAndrew Thompson struct ieee80211_node *ni;
67402ac6454SAndrew Thompson struct mbuf *m;
67502ac6454SAndrew Thompson
6765efea30fSAndrew Thompson DPRINTF("%s -> %s\n",
6775efea30fSAndrew Thompson ieee80211_state_name[vap->iv_state],
6785efea30fSAndrew Thompson ieee80211_state_name[nstate]);
67902ac6454SAndrew Thompson
6805efea30fSAndrew Thompson IEEE80211_UNLOCK(ic);
6815efea30fSAndrew Thompson RAL_LOCK(sc);
682b6108616SRui Paulo usb_callout_stop(&uvp->ratectl_ch);
6835efea30fSAndrew Thompson
6845efea30fSAndrew Thompson switch (nstate) {
68502ac6454SAndrew Thompson case IEEE80211_S_INIT:
6865efea30fSAndrew Thompson if (vap->iv_state == IEEE80211_S_RUN) {
68702ac6454SAndrew Thompson /* abort TSF synchronization */
68802ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0);
68902ac6454SAndrew Thompson
69002ac6454SAndrew Thompson /* force tx led to stop blinking */
69102ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 0);
69202ac6454SAndrew Thompson }
69302ac6454SAndrew Thompson break;
69402ac6454SAndrew Thompson
69502ac6454SAndrew Thompson case IEEE80211_S_RUN:
6968d8bdb01SAndrew Thompson ni = ieee80211_ref_node(vap->iv_bss);
69702ac6454SAndrew Thompson
69802ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR) {
6991517a7baSAdrian Chadd if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
7001517a7baSAdrian Chadd goto fail;
7011517a7baSAdrian Chadd
7027a79cebfSGleb Smirnoff ural_update_slot(sc);
70302ac6454SAndrew Thompson ural_set_txpreamble(sc);
70402ac6454SAndrew Thompson ural_set_basicrates(sc, ic->ic_bsschan);
705ae132f11SAndriy Voskoboinyk IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
706ae132f11SAndriy Voskoboinyk ural_set_bssid(sc, sc->sc_bssid);
70702ac6454SAndrew Thompson }
70802ac6454SAndrew Thompson
70902ac6454SAndrew Thompson if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
71002ac6454SAndrew Thompson vap->iv_opmode == IEEE80211_M_IBSS) {
711210ab3c2SAdrian Chadd m = ieee80211_beacon_alloc(ni);
71202ac6454SAndrew Thompson if (m == NULL) {
71302ac6454SAndrew Thompson device_printf(sc->sc_dev,
71402ac6454SAndrew Thompson "could not allocate beacon\n");
7151517a7baSAdrian Chadd goto fail;
71602ac6454SAndrew Thompson }
7176fca0210SAndrew Thompson ieee80211_ref_node(ni);
71802ac6454SAndrew Thompson if (ural_tx_bcn(sc, m, ni) != 0) {
71902ac6454SAndrew Thompson device_printf(sc->sc_dev,
72002ac6454SAndrew Thompson "could not send beacon\n");
7211517a7baSAdrian Chadd goto fail;
72202ac6454SAndrew Thompson }
72302ac6454SAndrew Thompson }
72402ac6454SAndrew Thompson
72502ac6454SAndrew Thompson /* make tx led blink on tx (controlled by ASIC) */
72602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 1);
72702ac6454SAndrew Thompson
72802ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR)
72902ac6454SAndrew Thompson ural_enable_tsf_sync(sc);
7305463c4a4SSam Leffler else
7315463c4a4SSam Leffler ural_enable_tsf(sc);
73202ac6454SAndrew Thompson
73302ac6454SAndrew Thompson /* enable automatic rate adaptation */
7345463c4a4SSam Leffler /* XXX should use ic_bsschan but not valid until after newstate call below */
7355463c4a4SSam Leffler tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
73602ac6454SAndrew Thompson if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
737b6108616SRui Paulo ural_ratectl_start(sc, ni);
7388d8bdb01SAndrew Thompson ieee80211_free_node(ni);
73902ac6454SAndrew Thompson break;
74002ac6454SAndrew Thompson
74102ac6454SAndrew Thompson default:
74202ac6454SAndrew Thompson break;
74302ac6454SAndrew Thompson }
74402ac6454SAndrew Thompson RAL_UNLOCK(sc);
74502ac6454SAndrew Thompson IEEE80211_LOCK(ic);
7465efea30fSAndrew Thompson return (uvp->newstate(vap, nstate, arg));
7471517a7baSAdrian Chadd
7481517a7baSAdrian Chadd fail:
7491517a7baSAdrian Chadd RAL_UNLOCK(sc);
7501517a7baSAdrian Chadd IEEE80211_LOCK(ic);
7511517a7baSAdrian Chadd ieee80211_free_node(ni);
7521517a7baSAdrian Chadd return (-1);
75302ac6454SAndrew Thompson }
75402ac6454SAndrew Thompson
75502ac6454SAndrew Thompson static void
ural_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)756ed6d949aSAndrew Thompson ural_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
75702ac6454SAndrew Thompson {
758ed6d949aSAndrew Thompson struct ural_softc *sc = usbd_xfer_softc(xfer);
7595463c4a4SSam Leffler struct ieee80211vap *vap;
76002ac6454SAndrew Thompson struct ural_tx_data *data;
76102ac6454SAndrew Thompson struct mbuf *m;
762ed6d949aSAndrew Thompson struct usb_page_cache *pc;
763ed6d949aSAndrew Thompson int len;
764ed6d949aSAndrew Thompson
765ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
76602ac6454SAndrew Thompson
76702ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
76802ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
769ed6d949aSAndrew Thompson DPRINTFN(11, "transfer complete, %d bytes\n", len);
77002ac6454SAndrew Thompson
77102ac6454SAndrew Thompson /* free resources */
772ed6d949aSAndrew Thompson data = usbd_xfer_get_priv(xfer);
77302ac6454SAndrew Thompson ural_tx_free(data, 0);
774ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, NULL);
77502ac6454SAndrew Thompson
77602ac6454SAndrew Thompson /* FALLTHROUGH */
77702ac6454SAndrew Thompson case USB_ST_SETUP:
77802ac6454SAndrew Thompson tr_setup:
77902ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_q);
78002ac6454SAndrew Thompson if (data) {
78102ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_q, next);
78202ac6454SAndrew Thompson m = data->m;
78302ac6454SAndrew Thompson
7846d917491SHans Petter Selasky if (m->m_pkthdr.len > (int)(RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) {
78502ac6454SAndrew Thompson DPRINTFN(0, "data overflow, %u bytes\n",
78602ac6454SAndrew Thompson m->m_pkthdr.len);
78702ac6454SAndrew Thompson m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE);
78802ac6454SAndrew Thompson }
789ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
790ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &data->desc, RAL_TX_DESC_SIZE);
791ed6d949aSAndrew Thompson usbd_m_copy_in(pc, RAL_TX_DESC_SIZE, m, 0,
79202ac6454SAndrew Thompson m->m_pkthdr.len);
79302ac6454SAndrew Thompson
7945463c4a4SSam Leffler vap = data->ni->ni_vap;
7955463c4a4SSam Leffler if (ieee80211_radiotap_active_vap(vap)) {
79602ac6454SAndrew Thompson struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
79702ac6454SAndrew Thompson
79802ac6454SAndrew Thompson tap->wt_flags = 0;
79902ac6454SAndrew Thompson tap->wt_rate = data->rate;
80002ac6454SAndrew Thompson tap->wt_antenna = sc->tx_ant;
80102ac6454SAndrew Thompson
8025463c4a4SSam Leffler ieee80211_radiotap_tx(vap, m);
80302ac6454SAndrew Thompson }
80402ac6454SAndrew Thompson
80502ac6454SAndrew Thompson /* xfer length needs to be a multiple of two! */
80602ac6454SAndrew Thompson len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1;
80702ac6454SAndrew Thompson if ((len % 64) == 0)
80802ac6454SAndrew Thompson len += 2;
80902ac6454SAndrew Thompson
81002ac6454SAndrew Thompson DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
81102ac6454SAndrew Thompson m->m_pkthdr.len, len);
81202ac6454SAndrew Thompson
813ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, len);
814ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, data);
81502ac6454SAndrew Thompson
816a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
81702ac6454SAndrew Thompson }
8187a79cebfSGleb Smirnoff ural_start(sc);
81902ac6454SAndrew Thompson break;
82002ac6454SAndrew Thompson
82102ac6454SAndrew Thompson default: /* Error */
82202ac6454SAndrew Thompson DPRINTFN(11, "transfer error, %s\n",
823ed6d949aSAndrew Thompson usbd_errstr(error));
82402ac6454SAndrew Thompson
825ed6d949aSAndrew Thompson data = usbd_xfer_get_priv(xfer);
82602ac6454SAndrew Thompson if (data != NULL) {
827ed6d949aSAndrew Thompson ural_tx_free(data, error);
828ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, NULL);
82902ac6454SAndrew Thompson }
83002ac6454SAndrew Thompson
831ed6d949aSAndrew Thompson if (error == USB_ERR_STALLED) {
83202ac6454SAndrew Thompson /* try to clear stall first */
833ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
83402ac6454SAndrew Thompson goto tr_setup;
83502ac6454SAndrew Thompson }
836ed6d949aSAndrew Thompson if (error == USB_ERR_TIMEOUT)
83702ac6454SAndrew Thompson device_printf(sc->sc_dev, "device timeout\n");
83802ac6454SAndrew Thompson break;
83902ac6454SAndrew Thompson }
84002ac6454SAndrew Thompson }
84102ac6454SAndrew Thompson
84202ac6454SAndrew Thompson static void
ural_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)843ed6d949aSAndrew Thompson ural_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
84402ac6454SAndrew Thompson {
845ed6d949aSAndrew Thompson struct ural_softc *sc = usbd_xfer_softc(xfer);
8467a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
84702ac6454SAndrew Thompson struct ieee80211_node *ni;
84802ac6454SAndrew Thompson struct mbuf *m = NULL;
849ed6d949aSAndrew Thompson struct usb_page_cache *pc;
85002ac6454SAndrew Thompson uint32_t flags;
8515463c4a4SSam Leffler int8_t rssi = 0, nf = 0;
852ed6d949aSAndrew Thompson int len;
853ed6d949aSAndrew Thompson
854ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
85502ac6454SAndrew Thompson
85602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
85702ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
85802ac6454SAndrew Thompson
859ed6d949aSAndrew Thompson DPRINTFN(15, "rx done, actlen=%d\n", len);
86002ac6454SAndrew Thompson
8616d917491SHans Petter Selasky if (len < (int)(RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
86202ac6454SAndrew Thompson DPRINTF("%s: xfer too short %d\n",
86302ac6454SAndrew Thompson device_get_nameunit(sc->sc_dev), len);
8647a79cebfSGleb Smirnoff counter_u64_add(ic->ic_ierrors, 1);
86502ac6454SAndrew Thompson goto tr_setup;
86602ac6454SAndrew Thompson }
86702ac6454SAndrew Thompson
86802ac6454SAndrew Thompson len -= RAL_RX_DESC_SIZE;
86902ac6454SAndrew Thompson /* rx descriptor is located at the end */
870ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
871ed6d949aSAndrew Thompson usbd_copy_out(pc, len, &sc->sc_rx_desc, RAL_RX_DESC_SIZE);
87202ac6454SAndrew Thompson
87302ac6454SAndrew Thompson rssi = URAL_RSSI(sc->sc_rx_desc.rssi);
8745463c4a4SSam Leffler nf = RAL_NOISE_FLOOR;
87502ac6454SAndrew Thompson flags = le32toh(sc->sc_rx_desc.flags);
87602ac6454SAndrew Thompson if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
87702ac6454SAndrew Thompson /*
87802ac6454SAndrew Thompson * This should not happen since we did not
87902ac6454SAndrew Thompson * request to receive those frames when we
88002ac6454SAndrew Thompson * filled RAL_TXRX_CSR2:
88102ac6454SAndrew Thompson */
88202ac6454SAndrew Thompson DPRINTFN(5, "PHY or CRC error\n");
8837a79cebfSGleb Smirnoff counter_u64_add(ic->ic_ierrors, 1);
88402ac6454SAndrew Thompson goto tr_setup;
88502ac6454SAndrew Thompson }
88602ac6454SAndrew Thompson
887c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
88802ac6454SAndrew Thompson if (m == NULL) {
88902ac6454SAndrew Thompson DPRINTF("could not allocate mbuf\n");
8907a79cebfSGleb Smirnoff counter_u64_add(ic->ic_ierrors, 1);
89102ac6454SAndrew Thompson goto tr_setup;
89202ac6454SAndrew Thompson }
893ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, mtod(m, uint8_t *), len);
89402ac6454SAndrew Thompson
89502ac6454SAndrew Thompson /* finalize mbuf */
89602ac6454SAndrew Thompson m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
89702ac6454SAndrew Thompson
8985463c4a4SSam Leffler if (ieee80211_radiotap_active(ic)) {
89902ac6454SAndrew Thompson struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
90002ac6454SAndrew Thompson
9015463c4a4SSam Leffler /* XXX set once */
9025463c4a4SSam Leffler tap->wr_flags = 0;
90302ac6454SAndrew Thompson tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
90402ac6454SAndrew Thompson (flags & RAL_RX_OFDM) ?
90502ac6454SAndrew Thompson IEEE80211_T_OFDM : IEEE80211_T_CCK);
90602ac6454SAndrew Thompson tap->wr_antenna = sc->rx_ant;
9075463c4a4SSam Leffler tap->wr_antsignal = nf + rssi;
9085463c4a4SSam Leffler tap->wr_antnoise = nf;
90902ac6454SAndrew Thompson }
91002ac6454SAndrew Thompson /* Strip trailing 802.11 MAC FCS. */
91102ac6454SAndrew Thompson m_adj(m, -IEEE80211_CRC_LEN);
91202ac6454SAndrew Thompson
91302ac6454SAndrew Thompson /* FALLTHROUGH */
91402ac6454SAndrew Thompson case USB_ST_SETUP:
91502ac6454SAndrew Thompson tr_setup:
916ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
917a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
91802ac6454SAndrew Thompson
91902ac6454SAndrew Thompson /*
92002ac6454SAndrew Thompson * At the end of a USB callback it is always safe to unlock
92102ac6454SAndrew Thompson * the private mutex of a device! That is why we do the
92202ac6454SAndrew Thompson * "ieee80211_input" here, and not some lines up!
92302ac6454SAndrew Thompson */
92402ac6454SAndrew Thompson RAL_UNLOCK(sc);
925941da6d3SWeongyo Jeong if (m) {
92602ac6454SAndrew Thompson ni = ieee80211_find_rxnode(ic,
92702ac6454SAndrew Thompson mtod(m, struct ieee80211_frame_min *));
92802ac6454SAndrew Thompson if (ni != NULL) {
9295463c4a4SSam Leffler (void) ieee80211_input(ni, m, rssi, nf);
93002ac6454SAndrew Thompson ieee80211_free_node(ni);
93102ac6454SAndrew Thompson } else
9325463c4a4SSam Leffler (void) ieee80211_input_all(ic, m, rssi, nf);
93302ac6454SAndrew Thompson }
934941da6d3SWeongyo Jeong RAL_LOCK(sc);
9357a79cebfSGleb Smirnoff ural_start(sc);
93602ac6454SAndrew Thompson return;
93702ac6454SAndrew Thompson
93802ac6454SAndrew Thompson default: /* Error */
939ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
94002ac6454SAndrew Thompson /* try to clear stall first */
941ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
94202ac6454SAndrew Thompson goto tr_setup;
94302ac6454SAndrew Thompson }
94402ac6454SAndrew Thompson return;
94502ac6454SAndrew Thompson }
94602ac6454SAndrew Thompson }
94702ac6454SAndrew Thompson
94802ac6454SAndrew Thompson static uint8_t
ural_plcp_signal(int rate)94902ac6454SAndrew Thompson ural_plcp_signal(int rate)
95002ac6454SAndrew Thompson {
95102ac6454SAndrew Thompson switch (rate) {
95202ac6454SAndrew Thompson /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
95302ac6454SAndrew Thompson case 12: return 0xb;
95402ac6454SAndrew Thompson case 18: return 0xf;
95502ac6454SAndrew Thompson case 24: return 0xa;
95602ac6454SAndrew Thompson case 36: return 0xe;
95702ac6454SAndrew Thompson case 48: return 0x9;
95802ac6454SAndrew Thompson case 72: return 0xd;
95902ac6454SAndrew Thompson case 96: return 0x8;
96002ac6454SAndrew Thompson case 108: return 0xc;
96102ac6454SAndrew Thompson
96202ac6454SAndrew Thompson /* CCK rates (NB: not IEEE std, device-specific) */
96302ac6454SAndrew Thompson case 2: return 0x0;
96402ac6454SAndrew Thompson case 4: return 0x1;
96502ac6454SAndrew Thompson case 11: return 0x2;
96602ac6454SAndrew Thompson case 22: return 0x3;
96702ac6454SAndrew Thompson }
96802ac6454SAndrew Thompson return 0xff; /* XXX unsupported/unknown rate */
96902ac6454SAndrew Thompson }
97002ac6454SAndrew Thompson
97102ac6454SAndrew Thompson static void
ural_setup_tx_desc(struct ural_softc * sc,struct ural_tx_desc * desc,uint32_t flags,int len,int rate)97202ac6454SAndrew Thompson ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
97302ac6454SAndrew Thompson uint32_t flags, int len, int rate)
97402ac6454SAndrew Thompson {
9757a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
97602ac6454SAndrew Thompson uint16_t plcp_length;
97702ac6454SAndrew Thompson int remainder;
97802ac6454SAndrew Thompson
97902ac6454SAndrew Thompson desc->flags = htole32(flags);
98002ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_NEWSEQ);
98102ac6454SAndrew Thompson desc->flags |= htole32(len << 16);
98202ac6454SAndrew Thompson
98302ac6454SAndrew Thompson desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
98402ac6454SAndrew Thompson desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
98502ac6454SAndrew Thompson
98602ac6454SAndrew Thompson /* setup PLCP fields */
98702ac6454SAndrew Thompson desc->plcp_signal = ural_plcp_signal(rate);
98802ac6454SAndrew Thompson desc->plcp_service = 4;
98902ac6454SAndrew Thompson
99002ac6454SAndrew Thompson len += IEEE80211_CRC_LEN;
99126d39e2cSSam Leffler if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
99202ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_OFDM);
99302ac6454SAndrew Thompson
99402ac6454SAndrew Thompson plcp_length = len & 0xfff;
99502ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 6;
99602ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0x3f;
99702ac6454SAndrew Thompson } else {
998b8e3094cSHans Petter Selasky if (rate == 0)
999b8e3094cSHans Petter Selasky rate = 2; /* avoid division by zero */
1000057b4402SPedro F. Giffuni plcp_length = howmany(16 * len, rate);
100102ac6454SAndrew Thompson if (rate == 22) {
100202ac6454SAndrew Thompson remainder = (16 * len) % 22;
100302ac6454SAndrew Thompson if (remainder != 0 && remainder < 7)
100402ac6454SAndrew Thompson desc->plcp_service |= RAL_PLCP_LENGEXT;
100502ac6454SAndrew Thompson }
100602ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 8;
100702ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0xff;
100802ac6454SAndrew Thompson
100902ac6454SAndrew Thompson if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
101002ac6454SAndrew Thompson desc->plcp_signal |= 0x08;
101102ac6454SAndrew Thompson }
101202ac6454SAndrew Thompson
101302ac6454SAndrew Thompson desc->iv = 0;
101402ac6454SAndrew Thompson desc->eiv = 0;
101502ac6454SAndrew Thompson }
101602ac6454SAndrew Thompson
101702ac6454SAndrew Thompson #define RAL_TX_TIMEOUT 5000
101802ac6454SAndrew Thompson
101902ac6454SAndrew Thompson static int
ural_tx_bcn(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)102002ac6454SAndrew Thompson ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
102102ac6454SAndrew Thompson {
102202ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap;
102302ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic;
102402ac6454SAndrew Thompson const struct ieee80211_txparam *tp;
102502ac6454SAndrew Thompson struct ural_tx_data *data;
102602ac6454SAndrew Thompson
102702ac6454SAndrew Thompson if (sc->tx_nfree == 0) {
102802ac6454SAndrew Thompson m_freem(m0);
102902ac6454SAndrew Thompson ieee80211_free_node(ni);
1030bb571462SHans Petter Selasky return (EIO);
1031bb571462SHans Petter Selasky }
1032bb571462SHans Petter Selasky if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1033bb571462SHans Petter Selasky m_freem(m0);
1034bb571462SHans Petter Selasky ieee80211_free_node(ni);
1035bb571462SHans Petter Selasky return (ENXIO);
103602ac6454SAndrew Thompson }
103702ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free);
103802ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next);
103902ac6454SAndrew Thompson sc->tx_nfree--;
104002ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
104102ac6454SAndrew Thompson
104202ac6454SAndrew Thompson data->m = m0;
104302ac6454SAndrew Thompson data->ni = ni;
104402ac6454SAndrew Thompson data->rate = tp->mgmtrate;
104502ac6454SAndrew Thompson
104602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc,
104702ac6454SAndrew Thompson RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len,
104802ac6454SAndrew Thompson tp->mgmtrate);
104902ac6454SAndrew Thompson
105002ac6454SAndrew Thompson DPRINTFN(10, "sending beacon frame len=%u rate=%u\n",
105102ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate);
105202ac6454SAndrew Thompson
105302ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1054a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
105502ac6454SAndrew Thompson
105602ac6454SAndrew Thompson return (0);
105702ac6454SAndrew Thompson }
105802ac6454SAndrew Thompson
105902ac6454SAndrew Thompson static int
ural_tx_mgt(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)106002ac6454SAndrew Thompson ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
106102ac6454SAndrew Thompson {
1062f6313575SAndriy Voskoboinyk const struct ieee80211_txparam *tp = ni->ni_txparms;
106302ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic;
106402ac6454SAndrew Thompson struct ural_tx_data *data;
106502ac6454SAndrew Thompson struct ieee80211_frame *wh;
106602ac6454SAndrew Thompson struct ieee80211_key *k;
106702ac6454SAndrew Thompson uint32_t flags;
106802ac6454SAndrew Thompson uint16_t dur;
106902ac6454SAndrew Thompson
107002ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED);
107102ac6454SAndrew Thompson
107202ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free);
107302ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next);
107402ac6454SAndrew Thompson sc->tx_nfree--;
107502ac6454SAndrew Thompson
107602ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *);
10775945b5f5SKevin Lo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
107802ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0);
107902ac6454SAndrew Thompson if (k == NULL) {
108002ac6454SAndrew Thompson m_freem(m0);
108102ac6454SAndrew Thompson return ENOBUFS;
108202ac6454SAndrew Thompson }
108302ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *);
108402ac6454SAndrew Thompson }
108502ac6454SAndrew Thompson
108602ac6454SAndrew Thompson data->m = m0;
108702ac6454SAndrew Thompson data->ni = ni;
108802ac6454SAndrew Thompson data->rate = tp->mgmtrate;
108902ac6454SAndrew Thompson
109002ac6454SAndrew Thompson flags = 0;
109102ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
109202ac6454SAndrew Thompson flags |= RAL_TX_ACK;
109302ac6454SAndrew Thompson
109426d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
109502ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE);
10968cfe5440SHans Petter Selasky USETW(wh->i_dur, dur);
109702ac6454SAndrew Thompson
109802ac6454SAndrew Thompson /* tell hardware to add timestamp for probe responses */
1099c249cc38SAdrian Chadd if (IEEE80211_IS_MGMT_PROBE_RESP(wh))
110002ac6454SAndrew Thompson flags |= RAL_TX_TIMESTAMP;
110102ac6454SAndrew Thompson }
110202ac6454SAndrew Thompson
110302ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate);
110402ac6454SAndrew Thompson
110502ac6454SAndrew Thompson DPRINTFN(10, "sending mgt frame len=%u rate=%u\n",
110602ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate);
110702ac6454SAndrew Thompson
110802ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1109a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
111002ac6454SAndrew Thompson
111102ac6454SAndrew Thompson return 0;
111202ac6454SAndrew Thompson }
111302ac6454SAndrew Thompson
111402ac6454SAndrew Thompson static int
ural_sendprot(struct ural_softc * sc,const struct mbuf * m,struct ieee80211_node * ni,int prot,int rate)111502ac6454SAndrew Thompson ural_sendprot(struct ural_softc *sc,
111602ac6454SAndrew Thompson const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
111702ac6454SAndrew Thompson {
111802ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic;
111902ac6454SAndrew Thompson struct ural_tx_data *data;
112002ac6454SAndrew Thompson struct mbuf *mprot;
1121d1b67106SAndriy Voskoboinyk int protrate, flags;
112202ac6454SAndrew Thompson
1123d1b67106SAndriy Voskoboinyk mprot = ieee80211_alloc_prot(ni, m, rate, prot);
112402ac6454SAndrew Thompson if (mprot == NULL) {
1125d1b67106SAndriy Voskoboinyk if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
1126d1b67106SAndriy Voskoboinyk device_printf(sc->sc_dev,
1127d1b67106SAndriy Voskoboinyk "could not allocate mbuf for protection mode %d\n", prot);
112802ac6454SAndrew Thompson return ENOBUFS;
112902ac6454SAndrew Thompson }
1130d1b67106SAndriy Voskoboinyk
1131d1b67106SAndriy Voskoboinyk protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1132d1b67106SAndriy Voskoboinyk flags = RAL_TX_RETRY(7);
1133d1b67106SAndriy Voskoboinyk if (prot == IEEE80211_PROT_RTSCTS)
1134d1b67106SAndriy Voskoboinyk flags |= RAL_TX_ACK;
1135d1b67106SAndriy Voskoboinyk
113602ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free);
113702ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next);
113802ac6454SAndrew Thompson sc->tx_nfree--;
113902ac6454SAndrew Thompson
114002ac6454SAndrew Thompson data->m = mprot;
114102ac6454SAndrew Thompson data->ni = ieee80211_ref_node(ni);
114202ac6454SAndrew Thompson data->rate = protrate;
114302ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate);
114402ac6454SAndrew Thompson
114502ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1146a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
114702ac6454SAndrew Thompson
114802ac6454SAndrew Thompson return 0;
114902ac6454SAndrew Thompson }
115002ac6454SAndrew Thompson
115102ac6454SAndrew Thompson static int
ural_tx_raw(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni,const struct ieee80211_bpf_params * params)115202ac6454SAndrew Thompson ural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
115302ac6454SAndrew Thompson const struct ieee80211_bpf_params *params)
115402ac6454SAndrew Thompson {
1155515db61dSSam Leffler struct ieee80211com *ic = ni->ni_ic;
115602ac6454SAndrew Thompson struct ural_tx_data *data;
115702ac6454SAndrew Thompson uint32_t flags;
115802ac6454SAndrew Thompson int error;
115902ac6454SAndrew Thompson int rate;
116002ac6454SAndrew Thompson
116102ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED);
116202ac6454SAndrew Thompson KASSERT(params != NULL, ("no raw xmit params"));
116302ac6454SAndrew Thompson
1164515db61dSSam Leffler rate = params->ibp_rate0;
1165515db61dSSam Leffler if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
116602ac6454SAndrew Thompson m_freem(m0);
116702ac6454SAndrew Thompson return EINVAL;
116802ac6454SAndrew Thompson }
116902ac6454SAndrew Thompson flags = 0;
117002ac6454SAndrew Thompson if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
117102ac6454SAndrew Thompson flags |= RAL_TX_ACK;
117202ac6454SAndrew Thompson if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
117302ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni,
117402ac6454SAndrew Thompson params->ibp_flags & IEEE80211_BPF_RTS ?
117502ac6454SAndrew Thompson IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
117602ac6454SAndrew Thompson rate);
117796ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) {
117802ac6454SAndrew Thompson m_freem(m0);
117996ca458fSAndrew Thompson return ENOBUFS;
118002ac6454SAndrew Thompson }
118102ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS;
118202ac6454SAndrew Thompson }
118302ac6454SAndrew Thompson
118496ca458fSAndrew Thompson data = STAILQ_FIRST(&sc->tx_free);
118596ca458fSAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next);
118696ca458fSAndrew Thompson sc->tx_nfree--;
118796ca458fSAndrew Thompson
118802ac6454SAndrew Thompson data->m = m0;
118902ac6454SAndrew Thompson data->ni = ni;
119002ac6454SAndrew Thompson data->rate = rate;
119102ac6454SAndrew Thompson
119202ac6454SAndrew Thompson /* XXX need to setup descriptor ourself */
119302ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
119402ac6454SAndrew Thompson
119502ac6454SAndrew Thompson DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
119602ac6454SAndrew Thompson m0->m_pkthdr.len, rate);
119702ac6454SAndrew Thompson
119802ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1199a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
120002ac6454SAndrew Thompson
120102ac6454SAndrew Thompson return 0;
120202ac6454SAndrew Thompson }
120302ac6454SAndrew Thompson
120402ac6454SAndrew Thompson static int
ural_tx_data(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)120502ac6454SAndrew Thompson ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
120602ac6454SAndrew Thompson {
120702ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap;
120802ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic;
120902ac6454SAndrew Thompson struct ural_tx_data *data;
121002ac6454SAndrew Thompson struct ieee80211_frame *wh;
1211f6313575SAndriy Voskoboinyk const struct ieee80211_txparam *tp = ni->ni_txparms;
121202ac6454SAndrew Thompson struct ieee80211_key *k;
121302ac6454SAndrew Thompson uint32_t flags = 0;
121402ac6454SAndrew Thompson uint16_t dur;
121502ac6454SAndrew Thompson int error, rate;
121602ac6454SAndrew Thompson
121702ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED);
121802ac6454SAndrew Thompson
121902ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *);
122002ac6454SAndrew Thompson
1221f6313575SAndriy Voskoboinyk if (m0->m_flags & M_EAPOL)
1222f6313575SAndriy Voskoboinyk rate = tp->mgmtrate;
1223f6313575SAndriy Voskoboinyk else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
122402ac6454SAndrew Thompson rate = tp->mcastrate;
122502ac6454SAndrew Thompson else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
122602ac6454SAndrew Thompson rate = tp->ucastrate;
1227f6930becSAndriy Voskoboinyk else {
1228f6930becSAndriy Voskoboinyk (void) ieee80211_ratectl_rate(ni, NULL, 0);
1229*70674500SAdrian Chadd rate = ieee80211_node_get_txrate_dot11rate(ni);
1230f6930becSAndriy Voskoboinyk }
123102ac6454SAndrew Thompson
12325945b5f5SKevin Lo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
123302ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0);
123402ac6454SAndrew Thompson if (k == NULL) {
123502ac6454SAndrew Thompson m_freem(m0);
123602ac6454SAndrew Thompson return ENOBUFS;
123702ac6454SAndrew Thompson }
123802ac6454SAndrew Thompson /* packet header may have moved, reset our local pointer */
123902ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *);
124002ac6454SAndrew Thompson }
124102ac6454SAndrew Thompson
124202ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
124302ac6454SAndrew Thompson int prot = IEEE80211_PROT_NONE;
124402ac6454SAndrew Thompson if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
124502ac6454SAndrew Thompson prot = IEEE80211_PROT_RTSCTS;
124602ac6454SAndrew Thompson else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
124726d39e2cSSam Leffler ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
124802ac6454SAndrew Thompson prot = ic->ic_protmode;
124902ac6454SAndrew Thompson if (prot != IEEE80211_PROT_NONE) {
125002ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni, prot, rate);
125196ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) {
125202ac6454SAndrew Thompson m_freem(m0);
125396ca458fSAndrew Thompson return ENOBUFS;
125402ac6454SAndrew Thompson }
125502ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS;
125602ac6454SAndrew Thompson }
125702ac6454SAndrew Thompson }
125802ac6454SAndrew Thompson
125902ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free);
126002ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next);
126102ac6454SAndrew Thompson sc->tx_nfree--;
126202ac6454SAndrew Thompson
126302ac6454SAndrew Thompson data->m = m0;
126402ac6454SAndrew Thompson data->ni = ni;
126502ac6454SAndrew Thompson data->rate = rate;
126602ac6454SAndrew Thompson
126702ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
126802ac6454SAndrew Thompson flags |= RAL_TX_ACK;
126902ac6454SAndrew Thompson flags |= RAL_TX_RETRY(7);
127002ac6454SAndrew Thompson
127126d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, rate,
127202ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE);
12738cfe5440SHans Petter Selasky USETW(wh->i_dur, dur);
127402ac6454SAndrew Thompson }
127502ac6454SAndrew Thompson
127602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
127702ac6454SAndrew Thompson
127802ac6454SAndrew Thompson DPRINTFN(10, "sending data frame len=%u rate=%u\n",
127902ac6454SAndrew Thompson m0->m_pkthdr.len, rate);
128002ac6454SAndrew Thompson
128102ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1282a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
128302ac6454SAndrew Thompson
128402ac6454SAndrew Thompson return 0;
128502ac6454SAndrew Thompson }
128602ac6454SAndrew Thompson
12877a79cebfSGleb Smirnoff static int
ural_transmit(struct ieee80211com * ic,struct mbuf * m)12887a79cebfSGleb Smirnoff ural_transmit(struct ieee80211com *ic, struct mbuf *m)
128979d2c5e8SGleb Smirnoff {
12907a79cebfSGleb Smirnoff struct ural_softc *sc = ic->ic_softc;
12917a79cebfSGleb Smirnoff int error;
12927a79cebfSGleb Smirnoff
12937a79cebfSGleb Smirnoff RAL_LOCK(sc);
12947a79cebfSGleb Smirnoff if (!sc->sc_running) {
12957a79cebfSGleb Smirnoff RAL_UNLOCK(sc);
12967a79cebfSGleb Smirnoff return (ENXIO);
12977a79cebfSGleb Smirnoff }
12987a79cebfSGleb Smirnoff error = mbufq_enqueue(&sc->sc_snd, m);
12997a79cebfSGleb Smirnoff if (error) {
13007a79cebfSGleb Smirnoff RAL_UNLOCK(sc);
13017a79cebfSGleb Smirnoff return (error);
13027a79cebfSGleb Smirnoff }
13037a79cebfSGleb Smirnoff ural_start(sc);
13047a79cebfSGleb Smirnoff RAL_UNLOCK(sc);
13057a79cebfSGleb Smirnoff
13067a79cebfSGleb Smirnoff return (0);
13077a79cebfSGleb Smirnoff }
13087a79cebfSGleb Smirnoff
13097a79cebfSGleb Smirnoff static void
ural_start(struct ural_softc * sc)13107a79cebfSGleb Smirnoff ural_start(struct ural_softc *sc)
13117a79cebfSGleb Smirnoff {
131202ac6454SAndrew Thompson struct ieee80211_node *ni;
131302ac6454SAndrew Thompson struct mbuf *m;
131402ac6454SAndrew Thompson
13157a79cebfSGleb Smirnoff RAL_LOCK_ASSERT(sc, MA_OWNED);
13167a79cebfSGleb Smirnoff
13177a79cebfSGleb Smirnoff if (sc->sc_running == 0)
131802ac6454SAndrew Thompson return;
13197a79cebfSGleb Smirnoff
13207a79cebfSGleb Smirnoff while (sc->tx_nfree >= RAL_TX_MINFREE &&
13217a79cebfSGleb Smirnoff (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
132202ac6454SAndrew Thompson ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
132302ac6454SAndrew Thompson if (ural_tx_data(sc, m, ni) != 0) {
13247a79cebfSGleb Smirnoff if_inc_counter(ni->ni_vap->iv_ifp,
13257a79cebfSGleb Smirnoff IFCOUNTER_OERRORS, 1);
132602ac6454SAndrew Thompson ieee80211_free_node(ni);
132702ac6454SAndrew Thompson break;
132802ac6454SAndrew Thompson }
132902ac6454SAndrew Thompson }
133002ac6454SAndrew Thompson }
133102ac6454SAndrew Thompson
13327a79cebfSGleb Smirnoff static void
ural_parent(struct ieee80211com * ic)13337a79cebfSGleb Smirnoff ural_parent(struct ieee80211com *ic)
133402ac6454SAndrew Thompson {
13357a79cebfSGleb Smirnoff struct ural_softc *sc = ic->ic_softc;
1336645e4d17SHans Petter Selasky int startall = 0;
1337645e4d17SHans Petter Selasky
1338645e4d17SHans Petter Selasky RAL_LOCK(sc);
13397a79cebfSGleb Smirnoff if (sc->sc_detached) {
1340645e4d17SHans Petter Selasky RAL_UNLOCK(sc);
13417a79cebfSGleb Smirnoff return;
13427a79cebfSGleb Smirnoff }
13437a79cebfSGleb Smirnoff if (ic->ic_nrunning > 0) {
13447a79cebfSGleb Smirnoff if (sc->sc_running == 0) {
13457a79cebfSGleb Smirnoff ural_init(sc);
134602ac6454SAndrew Thompson startall = 1;
134702ac6454SAndrew Thompson } else
13485efea30fSAndrew Thompson ural_setpromisc(sc);
13497a79cebfSGleb Smirnoff } else if (sc->sc_running)
13505efea30fSAndrew Thompson ural_stop(sc);
135102ac6454SAndrew Thompson RAL_UNLOCK(sc);
135202ac6454SAndrew Thompson if (startall)
135302ac6454SAndrew Thompson ieee80211_start_all(ic);
135402ac6454SAndrew Thompson }
135502ac6454SAndrew Thompson
135602ac6454SAndrew Thompson static void
ural_set_testmode(struct ural_softc * sc)135702ac6454SAndrew Thompson ural_set_testmode(struct ural_softc *sc)
135802ac6454SAndrew Thompson {
1359760bc48eSAndrew Thompson struct usb_device_request req;
1360e0a69b51SAndrew Thompson usb_error_t error;
136102ac6454SAndrew Thompson
136202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
136302ac6454SAndrew Thompson req.bRequest = RAL_VENDOR_REQUEST;
136402ac6454SAndrew Thompson USETW(req.wValue, 4);
136502ac6454SAndrew Thompson USETW(req.wIndex, 1);
136602ac6454SAndrew Thompson USETW(req.wLength, 0);
136702ac6454SAndrew Thompson
136802ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL);
136902ac6454SAndrew Thompson if (error != 0) {
137002ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not set test mode: %s\n",
1371a593f6b8SAndrew Thompson usbd_errstr(error));
137202ac6454SAndrew Thompson }
137302ac6454SAndrew Thompson }
137402ac6454SAndrew Thompson
137502ac6454SAndrew Thompson static void
ural_eeprom_read(struct ural_softc * sc,uint16_t addr,void * buf,int len)137602ac6454SAndrew Thompson ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
137702ac6454SAndrew Thompson {
1378760bc48eSAndrew Thompson struct usb_device_request req;
1379e0a69b51SAndrew Thompson usb_error_t error;
138002ac6454SAndrew Thompson
138102ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE;
138202ac6454SAndrew Thompson req.bRequest = RAL_READ_EEPROM;
138302ac6454SAndrew Thompson USETW(req.wValue, 0);
138402ac6454SAndrew Thompson USETW(req.wIndex, addr);
138502ac6454SAndrew Thompson USETW(req.wLength, len);
138602ac6454SAndrew Thompson
138702ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf);
138802ac6454SAndrew Thompson if (error != 0) {
138902ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1390a593f6b8SAndrew Thompson usbd_errstr(error));
139102ac6454SAndrew Thompson }
139202ac6454SAndrew Thompson }
139302ac6454SAndrew Thompson
139402ac6454SAndrew Thompson static uint16_t
ural_read(struct ural_softc * sc,uint16_t reg)139502ac6454SAndrew Thompson ural_read(struct ural_softc *sc, uint16_t reg)
139602ac6454SAndrew Thompson {
1397760bc48eSAndrew Thompson struct usb_device_request req;
1398e0a69b51SAndrew Thompson usb_error_t error;
139902ac6454SAndrew Thompson uint16_t val;
140002ac6454SAndrew Thompson
140102ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE;
140202ac6454SAndrew Thompson req.bRequest = RAL_READ_MAC;
140302ac6454SAndrew Thompson USETW(req.wValue, 0);
140402ac6454SAndrew Thompson USETW(req.wIndex, reg);
140502ac6454SAndrew Thompson USETW(req.wLength, sizeof (uint16_t));
140602ac6454SAndrew Thompson
140702ac6454SAndrew Thompson error = ural_do_request(sc, &req, &val);
140802ac6454SAndrew Thompson if (error != 0) {
140902ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1410a593f6b8SAndrew Thompson usbd_errstr(error));
141102ac6454SAndrew Thompson return 0;
141202ac6454SAndrew Thompson }
141302ac6454SAndrew Thompson
141402ac6454SAndrew Thompson return le16toh(val);
141502ac6454SAndrew Thompson }
141602ac6454SAndrew Thompson
141702ac6454SAndrew Thompson static void
ural_read_multi(struct ural_softc * sc,uint16_t reg,void * buf,int len)141802ac6454SAndrew Thompson ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
141902ac6454SAndrew Thompson {
1420760bc48eSAndrew Thompson struct usb_device_request req;
1421e0a69b51SAndrew Thompson usb_error_t error;
142202ac6454SAndrew Thompson
142302ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE;
142402ac6454SAndrew Thompson req.bRequest = RAL_READ_MULTI_MAC;
142502ac6454SAndrew Thompson USETW(req.wValue, 0);
142602ac6454SAndrew Thompson USETW(req.wIndex, reg);
142702ac6454SAndrew Thompson USETW(req.wLength, len);
142802ac6454SAndrew Thompson
142902ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf);
143002ac6454SAndrew Thompson if (error != 0) {
143102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1432a593f6b8SAndrew Thompson usbd_errstr(error));
143302ac6454SAndrew Thompson }
143402ac6454SAndrew Thompson }
143502ac6454SAndrew Thompson
143602ac6454SAndrew Thompson static void
ural_write(struct ural_softc * sc,uint16_t reg,uint16_t val)143702ac6454SAndrew Thompson ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
143802ac6454SAndrew Thompson {
1439760bc48eSAndrew Thompson struct usb_device_request req;
1440e0a69b51SAndrew Thompson usb_error_t error;
144102ac6454SAndrew Thompson
144202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
144302ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MAC;
144402ac6454SAndrew Thompson USETW(req.wValue, val);
144502ac6454SAndrew Thompson USETW(req.wIndex, reg);
144602ac6454SAndrew Thompson USETW(req.wLength, 0);
144702ac6454SAndrew Thompson
144802ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL);
144902ac6454SAndrew Thompson if (error != 0) {
145002ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1451a593f6b8SAndrew Thompson usbd_errstr(error));
145202ac6454SAndrew Thompson }
145302ac6454SAndrew Thompson }
145402ac6454SAndrew Thompson
145502ac6454SAndrew Thompson static void
ural_write_multi(struct ural_softc * sc,uint16_t reg,void * buf,int len)145602ac6454SAndrew Thompson ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
145702ac6454SAndrew Thompson {
1458760bc48eSAndrew Thompson struct usb_device_request req;
1459e0a69b51SAndrew Thompson usb_error_t error;
146002ac6454SAndrew Thompson
146102ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
146202ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MULTI_MAC;
146302ac6454SAndrew Thompson USETW(req.wValue, 0);
146402ac6454SAndrew Thompson USETW(req.wIndex, reg);
146502ac6454SAndrew Thompson USETW(req.wLength, len);
146602ac6454SAndrew Thompson
146702ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf);
146802ac6454SAndrew Thompson if (error != 0) {
146902ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1470a593f6b8SAndrew Thompson usbd_errstr(error));
147102ac6454SAndrew Thompson }
147202ac6454SAndrew Thompson }
147302ac6454SAndrew Thompson
147402ac6454SAndrew Thompson static void
ural_bbp_write(struct ural_softc * sc,uint8_t reg,uint8_t val)147502ac6454SAndrew Thompson ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
147602ac6454SAndrew Thompson {
147702ac6454SAndrew Thompson uint16_t tmp;
147802ac6454SAndrew Thompson int ntries;
147902ac6454SAndrew Thompson
148002ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) {
148102ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
148202ac6454SAndrew Thompson break;
148302ac6454SAndrew Thompson if (ural_pause(sc, hz / 100))
148402ac6454SAndrew Thompson break;
148502ac6454SAndrew Thompson }
148602ac6454SAndrew Thompson if (ntries == 100) {
148702ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to BBP\n");
148802ac6454SAndrew Thompson return;
148902ac6454SAndrew Thompson }
149002ac6454SAndrew Thompson
149102ac6454SAndrew Thompson tmp = reg << 8 | val;
149202ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, tmp);
149302ac6454SAndrew Thompson }
149402ac6454SAndrew Thompson
149502ac6454SAndrew Thompson static uint8_t
ural_bbp_read(struct ural_softc * sc,uint8_t reg)149602ac6454SAndrew Thompson ural_bbp_read(struct ural_softc *sc, uint8_t reg)
149702ac6454SAndrew Thompson {
149802ac6454SAndrew Thompson uint16_t val;
149902ac6454SAndrew Thompson int ntries;
150002ac6454SAndrew Thompson
150102ac6454SAndrew Thompson val = RAL_BBP_WRITE | reg << 8;
150202ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, val);
150302ac6454SAndrew Thompson
150402ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) {
150502ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
150602ac6454SAndrew Thompson break;
150702ac6454SAndrew Thompson if (ural_pause(sc, hz / 100))
150802ac6454SAndrew Thompson break;
150902ac6454SAndrew Thompson }
151002ac6454SAndrew Thompson if (ntries == 100) {
151102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read BBP\n");
151202ac6454SAndrew Thompson return 0;
151302ac6454SAndrew Thompson }
151402ac6454SAndrew Thompson
151502ac6454SAndrew Thompson return ural_read(sc, RAL_PHY_CSR7) & 0xff;
151602ac6454SAndrew Thompson }
151702ac6454SAndrew Thompson
151802ac6454SAndrew Thompson static void
ural_rf_write(struct ural_softc * sc,uint8_t reg,uint32_t val)151902ac6454SAndrew Thompson ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
152002ac6454SAndrew Thompson {
152102ac6454SAndrew Thompson uint32_t tmp;
152202ac6454SAndrew Thompson int ntries;
152302ac6454SAndrew Thompson
152402ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) {
152502ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
152602ac6454SAndrew Thompson break;
152702ac6454SAndrew Thompson if (ural_pause(sc, hz / 100))
152802ac6454SAndrew Thompson break;
152902ac6454SAndrew Thompson }
153002ac6454SAndrew Thompson if (ntries == 100) {
153102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to RF\n");
153202ac6454SAndrew Thompson return;
153302ac6454SAndrew Thompson }
153402ac6454SAndrew Thompson
153502ac6454SAndrew Thompson tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
153602ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
153702ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
153802ac6454SAndrew Thompson
153902ac6454SAndrew Thompson /* remember last written value in sc */
154002ac6454SAndrew Thompson sc->rf_regs[reg] = val;
154102ac6454SAndrew Thompson
154202ac6454SAndrew Thompson DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
154302ac6454SAndrew Thompson }
154402ac6454SAndrew Thompson
154502ac6454SAndrew Thompson static void
ural_scan_start(struct ieee80211com * ic)154602ac6454SAndrew Thompson ural_scan_start(struct ieee80211com *ic)
154702ac6454SAndrew Thompson {
1548d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
154902ac6454SAndrew Thompson
155002ac6454SAndrew Thompson RAL_LOCK(sc);
15515efea30fSAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0);
15527a79cebfSGleb Smirnoff ural_set_bssid(sc, ieee80211broadcastaddr);
155302ac6454SAndrew Thompson RAL_UNLOCK(sc);
155402ac6454SAndrew Thompson }
155502ac6454SAndrew Thompson
155602ac6454SAndrew Thompson static void
ural_scan_end(struct ieee80211com * ic)155702ac6454SAndrew Thompson ural_scan_end(struct ieee80211com *ic)
155802ac6454SAndrew Thompson {
1559d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
156002ac6454SAndrew Thompson
156102ac6454SAndrew Thompson RAL_LOCK(sc);
15625efea30fSAndrew Thompson ural_enable_tsf_sync(sc);
1563ae132f11SAndriy Voskoboinyk ural_set_bssid(sc, sc->sc_bssid);
156402ac6454SAndrew Thompson RAL_UNLOCK(sc);
156502ac6454SAndrew Thompson
156602ac6454SAndrew Thompson }
156702ac6454SAndrew Thompson
156802ac6454SAndrew Thompson static void
ural_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])156983829b48SAndriy Voskoboinyk ural_getradiocaps(struct ieee80211com *ic,
157083829b48SAndriy Voskoboinyk int maxchans, int *nchans, struct ieee80211_channel chans[])
157183829b48SAndriy Voskoboinyk {
157283829b48SAndriy Voskoboinyk struct ural_softc *sc = ic->ic_softc;
157383829b48SAndriy Voskoboinyk uint8_t bands[IEEE80211_MODE_BYTES];
157483829b48SAndriy Voskoboinyk
157583829b48SAndriy Voskoboinyk memset(bands, 0, sizeof(bands));
157683829b48SAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11B);
157783829b48SAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11G);
1578b84b3638SAndriy Voskoboinyk ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
157983829b48SAndriy Voskoboinyk
158083829b48SAndriy Voskoboinyk if (sc->rf_rev == RAL_RF_5222) {
158183829b48SAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11A);
158283829b48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
158383829b48SAndriy Voskoboinyk ural_chan_5ghz, nitems(ural_chan_5ghz), bands, 0);
158483829b48SAndriy Voskoboinyk }
158583829b48SAndriy Voskoboinyk }
158683829b48SAndriy Voskoboinyk
158783829b48SAndriy Voskoboinyk static void
ural_set_channel(struct ieee80211com * ic)158802ac6454SAndrew Thompson ural_set_channel(struct ieee80211com *ic)
158902ac6454SAndrew Thompson {
1590d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
159102ac6454SAndrew Thompson
159202ac6454SAndrew Thompson RAL_LOCK(sc);
15935efea30fSAndrew Thompson ural_set_chan(sc, ic->ic_curchan);
159402ac6454SAndrew Thompson RAL_UNLOCK(sc);
159502ac6454SAndrew Thompson }
159602ac6454SAndrew Thompson
159702ac6454SAndrew Thompson static void
ural_set_chan(struct ural_softc * sc,struct ieee80211_channel * c)159802ac6454SAndrew Thompson ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
159902ac6454SAndrew Thompson {
16007a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
160102ac6454SAndrew Thompson uint8_t power, tmp;
160202ac6454SAndrew Thompson int i, chan;
160302ac6454SAndrew Thompson
160402ac6454SAndrew Thompson chan = ieee80211_chan2ieee(ic, c);
160502ac6454SAndrew Thompson if (chan == 0 || chan == IEEE80211_CHAN_ANY)
160602ac6454SAndrew Thompson return;
160702ac6454SAndrew Thompson
160802ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_2GHZ(c))
160902ac6454SAndrew Thompson power = min(sc->txpow[chan - 1], 31);
161002ac6454SAndrew Thompson else
161102ac6454SAndrew Thompson power = 31;
161202ac6454SAndrew Thompson
161302ac6454SAndrew Thompson /* adjust txpower using ifconfig settings */
161402ac6454SAndrew Thompson power -= (100 - ic->ic_txpowlimit) / 8;
161502ac6454SAndrew Thompson
161602ac6454SAndrew Thompson DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power);
161702ac6454SAndrew Thompson
161802ac6454SAndrew Thompson switch (sc->rf_rev) {
161902ac6454SAndrew Thompson case RAL_RF_2522:
162002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x00814);
162102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
162202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
162302ac6454SAndrew Thompson break;
162402ac6454SAndrew Thompson
162502ac6454SAndrew Thompson case RAL_RF_2523:
162602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804);
162702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
162802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
162902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
163002ac6454SAndrew Thompson break;
163102ac6454SAndrew Thompson
163202ac6454SAndrew Thompson case RAL_RF_2524:
163302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x0c808);
163402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
163502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
163602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
163702ac6454SAndrew Thompson break;
163802ac6454SAndrew Thompson
163902ac6454SAndrew Thompson case RAL_RF_2525:
164002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808);
164102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
164202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
164302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
164402ac6454SAndrew Thompson
164502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808);
164602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
164702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
164802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
164902ac6454SAndrew Thompson break;
165002ac6454SAndrew Thompson
165102ac6454SAndrew Thompson case RAL_RF_2525E:
165202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808);
165302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
165402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
165502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
165602ac6454SAndrew Thompson break;
165702ac6454SAndrew Thompson
165802ac6454SAndrew Thompson case RAL_RF_2526:
165902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
166002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
166102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804);
166202ac6454SAndrew Thompson
166302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
166402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
166502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
166602ac6454SAndrew Thompson break;
166702ac6454SAndrew Thompson
166802ac6454SAndrew Thompson /* dual-band RF */
166902ac6454SAndrew Thompson case RAL_RF_5222:
167002ac6454SAndrew Thompson for (i = 0; ural_rf5222[i].chan != chan; i++);
167102ac6454SAndrew Thompson
167202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
167302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
167402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
167502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
167602ac6454SAndrew Thompson break;
167702ac6454SAndrew Thompson }
167802ac6454SAndrew Thompson
167902ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR &&
168002ac6454SAndrew Thompson (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
168102ac6454SAndrew Thompson /* set Japan filter bit for channel 14 */
168202ac6454SAndrew Thompson tmp = ural_bbp_read(sc, 70);
168302ac6454SAndrew Thompson
168402ac6454SAndrew Thompson tmp &= ~RAL_JAPAN_FILTER;
168502ac6454SAndrew Thompson if (chan == 14)
168602ac6454SAndrew Thompson tmp |= RAL_JAPAN_FILTER;
168702ac6454SAndrew Thompson
168802ac6454SAndrew Thompson ural_bbp_write(sc, 70, tmp);
168902ac6454SAndrew Thompson
169002ac6454SAndrew Thompson /* clear CRC errors */
169102ac6454SAndrew Thompson ural_read(sc, RAL_STA_CSR0);
169202ac6454SAndrew Thompson
169302ac6454SAndrew Thompson ural_pause(sc, hz / 100);
169402ac6454SAndrew Thompson ural_disable_rf_tune(sc);
169502ac6454SAndrew Thompson }
169602ac6454SAndrew Thompson
169702ac6454SAndrew Thompson /* XXX doesn't belong here */
169802ac6454SAndrew Thompson /* update basic rate set */
169902ac6454SAndrew Thompson ural_set_basicrates(sc, c);
170077ddf3d3SAndrew Thompson
170177ddf3d3SAndrew Thompson /* give the hardware some time to do the switchover */
170277ddf3d3SAndrew Thompson ural_pause(sc, hz / 100);
170302ac6454SAndrew Thompson }
170402ac6454SAndrew Thompson
170502ac6454SAndrew Thompson /*
170602ac6454SAndrew Thompson * Disable RF auto-tuning.
170702ac6454SAndrew Thompson */
170802ac6454SAndrew Thompson static void
ural_disable_rf_tune(struct ural_softc * sc)170902ac6454SAndrew Thompson ural_disable_rf_tune(struct ural_softc *sc)
171002ac6454SAndrew Thompson {
171102ac6454SAndrew Thompson uint32_t tmp;
171202ac6454SAndrew Thompson
171302ac6454SAndrew Thompson if (sc->rf_rev != RAL_RF_2523) {
171402ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
171502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, tmp);
171602ac6454SAndrew Thompson }
171702ac6454SAndrew Thompson
171802ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
171902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, tmp);
172002ac6454SAndrew Thompson
172102ac6454SAndrew Thompson DPRINTFN(2, "disabling RF autotune\n");
172202ac6454SAndrew Thompson }
172302ac6454SAndrew Thompson
172402ac6454SAndrew Thompson /*
172502ac6454SAndrew Thompson * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
172602ac6454SAndrew Thompson * synchronization.
172702ac6454SAndrew Thompson */
172802ac6454SAndrew Thompson static void
ural_enable_tsf_sync(struct ural_softc * sc)172902ac6454SAndrew Thompson ural_enable_tsf_sync(struct ural_softc *sc)
173002ac6454SAndrew Thompson {
17317a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
173202ac6454SAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
173302ac6454SAndrew Thompson uint16_t logcwmin, preload, tmp;
173402ac6454SAndrew Thompson
173502ac6454SAndrew Thompson /* first, disable TSF synchronization */
173602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0);
173702ac6454SAndrew Thompson
173802ac6454SAndrew Thompson tmp = (16 * vap->iv_bss->ni_intval) << 4;
173902ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR18, tmp);
174002ac6454SAndrew Thompson
174102ac6454SAndrew Thompson logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
174202ac6454SAndrew Thompson preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
174302ac6454SAndrew Thompson tmp = logcwmin << 12 | preload;
174402ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR20, tmp);
174502ac6454SAndrew Thompson
174602ac6454SAndrew Thompson /* finally, enable TSF synchronization */
174702ac6454SAndrew Thompson tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
174802ac6454SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_STA)
174902ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(1);
175002ac6454SAndrew Thompson else
175102ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
175202ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, tmp);
175302ac6454SAndrew Thompson
175402ac6454SAndrew Thompson DPRINTF("enabling TSF synchronization\n");
175502ac6454SAndrew Thompson }
175602ac6454SAndrew Thompson
17575463c4a4SSam Leffler static void
ural_enable_tsf(struct ural_softc * sc)17585463c4a4SSam Leffler ural_enable_tsf(struct ural_softc *sc)
17595463c4a4SSam Leffler {
17605463c4a4SSam Leffler /* first, disable TSF synchronization */
17615463c4a4SSam Leffler ural_write(sc, RAL_TXRX_CSR19, 0);
17625463c4a4SSam Leffler ural_write(sc, RAL_TXRX_CSR19, RAL_ENABLE_TSF | RAL_ENABLE_TSF_SYNC(2));
17635463c4a4SSam Leffler }
17645463c4a4SSam Leffler
176502ac6454SAndrew Thompson #define RAL_RXTX_TURNAROUND 5 /* us */
176602ac6454SAndrew Thompson static void
ural_update_slot(struct ural_softc * sc)17677a79cebfSGleb Smirnoff ural_update_slot(struct ural_softc *sc)
176802ac6454SAndrew Thompson {
17697a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
177002ac6454SAndrew Thompson uint16_t slottime, sifs, eifs;
177102ac6454SAndrew Thompson
1772bdfff33fSAndriy Voskoboinyk slottime = IEEE80211_GET_SLOTTIME(ic);
177302ac6454SAndrew Thompson
177402ac6454SAndrew Thompson /*
177502ac6454SAndrew Thompson * These settings may sound a bit inconsistent but this is what the
177602ac6454SAndrew Thompson * reference driver does.
177702ac6454SAndrew Thompson */
177802ac6454SAndrew Thompson if (ic->ic_curmode == IEEE80211_MODE_11B) {
177902ac6454SAndrew Thompson sifs = 16 - RAL_RXTX_TURNAROUND;
178002ac6454SAndrew Thompson eifs = 364;
178102ac6454SAndrew Thompson } else {
178202ac6454SAndrew Thompson sifs = 10 - RAL_RXTX_TURNAROUND;
178302ac6454SAndrew Thompson eifs = 64;
178402ac6454SAndrew Thompson }
178502ac6454SAndrew Thompson
178602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR10, slottime);
178702ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR11, sifs);
178802ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR12, eifs);
178902ac6454SAndrew Thompson }
179002ac6454SAndrew Thompson
179102ac6454SAndrew Thompson static void
ural_set_txpreamble(struct ural_softc * sc)179202ac6454SAndrew Thompson ural_set_txpreamble(struct ural_softc *sc)
179302ac6454SAndrew Thompson {
17947a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
179502ac6454SAndrew Thompson uint16_t tmp;
179602ac6454SAndrew Thompson
179702ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR10);
179802ac6454SAndrew Thompson
179902ac6454SAndrew Thompson tmp &= ~RAL_SHORT_PREAMBLE;
180002ac6454SAndrew Thompson if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
180102ac6454SAndrew Thompson tmp |= RAL_SHORT_PREAMBLE;
180202ac6454SAndrew Thompson
180302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR10, tmp);
180402ac6454SAndrew Thompson }
180502ac6454SAndrew Thompson
180602ac6454SAndrew Thompson static void
ural_set_basicrates(struct ural_softc * sc,const struct ieee80211_channel * c)180702ac6454SAndrew Thompson ural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c)
180802ac6454SAndrew Thompson {
180902ac6454SAndrew Thompson /* XXX wrong, take from rate set */
181002ac6454SAndrew Thompson /* update basic rate set */
181102ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_5GHZ(c)) {
181202ac6454SAndrew Thompson /* 11a basic rates: 6, 12, 24Mbps */
181302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x150);
181402ac6454SAndrew Thompson } else if (IEEE80211_IS_CHAN_ANYG(c)) {
181502ac6454SAndrew Thompson /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
181602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f);
181702ac6454SAndrew Thompson } else {
181802ac6454SAndrew Thompson /* 11b basic rates: 1, 2Mbps */
181902ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x3);
182002ac6454SAndrew Thompson }
182102ac6454SAndrew Thompson }
182202ac6454SAndrew Thompson
182302ac6454SAndrew Thompson static void
ural_set_bssid(struct ural_softc * sc,const uint8_t * bssid)182402ac6454SAndrew Thompson ural_set_bssid(struct ural_softc *sc, const uint8_t *bssid)
182502ac6454SAndrew Thompson {
182602ac6454SAndrew Thompson uint16_t tmp;
182702ac6454SAndrew Thompson
182802ac6454SAndrew Thompson tmp = bssid[0] | bssid[1] << 8;
182902ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR5, tmp);
183002ac6454SAndrew Thompson
183102ac6454SAndrew Thompson tmp = bssid[2] | bssid[3] << 8;
183202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR6, tmp);
183302ac6454SAndrew Thompson
183402ac6454SAndrew Thompson tmp = bssid[4] | bssid[5] << 8;
183502ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR7, tmp);
183602ac6454SAndrew Thompson
183702ac6454SAndrew Thompson DPRINTF("setting BSSID to %6D\n", bssid, ":");
183802ac6454SAndrew Thompson }
183902ac6454SAndrew Thompson
184002ac6454SAndrew Thompson static void
ural_set_macaddr(struct ural_softc * sc,const uint8_t * addr)18417a79cebfSGleb Smirnoff ural_set_macaddr(struct ural_softc *sc, const uint8_t *addr)
184202ac6454SAndrew Thompson {
184302ac6454SAndrew Thompson uint16_t tmp;
184402ac6454SAndrew Thompson
184502ac6454SAndrew Thompson tmp = addr[0] | addr[1] << 8;
184602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR2, tmp);
184702ac6454SAndrew Thompson
184802ac6454SAndrew Thompson tmp = addr[2] | addr[3] << 8;
184902ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR3, tmp);
185002ac6454SAndrew Thompson
185102ac6454SAndrew Thompson tmp = addr[4] | addr[5] << 8;
185202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR4, tmp);
185302ac6454SAndrew Thompson
185402ac6454SAndrew Thompson DPRINTF("setting MAC address to %6D\n", addr, ":");
185502ac6454SAndrew Thompson }
185602ac6454SAndrew Thompson
185702ac6454SAndrew Thompson static void
ural_setpromisc(struct ural_softc * sc)18585efea30fSAndrew Thompson ural_setpromisc(struct ural_softc *sc)
185902ac6454SAndrew Thompson {
186002ac6454SAndrew Thompson uint32_t tmp;
186102ac6454SAndrew Thompson
186202ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR2);
186302ac6454SAndrew Thompson
186402ac6454SAndrew Thompson tmp &= ~RAL_DROP_NOT_TO_ME;
18657a79cebfSGleb Smirnoff if (sc->sc_ic.ic_promisc == 0)
186602ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME;
186702ac6454SAndrew Thompson
186802ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp);
186902ac6454SAndrew Thompson
18707a79cebfSGleb Smirnoff DPRINTF("%s promiscuous mode\n", sc->sc_ic.ic_promisc ?
187102ac6454SAndrew Thompson "entering" : "leaving");
187202ac6454SAndrew Thompson }
187302ac6454SAndrew Thompson
187477ddf3d3SAndrew Thompson static void
ural_update_promisc(struct ieee80211com * ic)1875272f6adeSGleb Smirnoff ural_update_promisc(struct ieee80211com *ic)
187677ddf3d3SAndrew Thompson {
1877272f6adeSGleb Smirnoff struct ural_softc *sc = ic->ic_softc;
187877ddf3d3SAndrew Thompson
187977ddf3d3SAndrew Thompson RAL_LOCK(sc);
18807a79cebfSGleb Smirnoff if (sc->sc_running)
18815efea30fSAndrew Thompson ural_setpromisc(sc);
188277ddf3d3SAndrew Thompson RAL_UNLOCK(sc);
188377ddf3d3SAndrew Thompson }
188477ddf3d3SAndrew Thompson
188502ac6454SAndrew Thompson static const char *
ural_get_rf(int rev)188602ac6454SAndrew Thompson ural_get_rf(int rev)
188702ac6454SAndrew Thompson {
188802ac6454SAndrew Thompson switch (rev) {
188902ac6454SAndrew Thompson case RAL_RF_2522: return "RT2522";
189002ac6454SAndrew Thompson case RAL_RF_2523: return "RT2523";
189102ac6454SAndrew Thompson case RAL_RF_2524: return "RT2524";
189202ac6454SAndrew Thompson case RAL_RF_2525: return "RT2525";
189302ac6454SAndrew Thompson case RAL_RF_2525E: return "RT2525e";
189402ac6454SAndrew Thompson case RAL_RF_2526: return "RT2526";
189502ac6454SAndrew Thompson case RAL_RF_5222: return "RT5222";
189602ac6454SAndrew Thompson default: return "unknown";
189702ac6454SAndrew Thompson }
189802ac6454SAndrew Thompson }
189902ac6454SAndrew Thompson
190002ac6454SAndrew Thompson static void
ural_read_eeprom(struct ural_softc * sc)190102ac6454SAndrew Thompson ural_read_eeprom(struct ural_softc *sc)
190202ac6454SAndrew Thompson {
19037a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
190402ac6454SAndrew Thompson uint16_t val;
190502ac6454SAndrew Thompson
190602ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
190702ac6454SAndrew Thompson val = le16toh(val);
190802ac6454SAndrew Thompson sc->rf_rev = (val >> 11) & 0x7;
190902ac6454SAndrew Thompson sc->hw_radio = (val >> 10) & 0x1;
191002ac6454SAndrew Thompson sc->led_mode = (val >> 6) & 0x7;
191102ac6454SAndrew Thompson sc->rx_ant = (val >> 4) & 0x3;
191202ac6454SAndrew Thompson sc->tx_ant = (val >> 2) & 0x3;
191302ac6454SAndrew Thompson sc->nb_ant = val & 0x3;
191402ac6454SAndrew Thompson
191502ac6454SAndrew Thompson /* read MAC address */
19167a79cebfSGleb Smirnoff ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_macaddr, 6);
191702ac6454SAndrew Thompson
191802ac6454SAndrew Thompson /* read default values for BBP registers */
191902ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
192002ac6454SAndrew Thompson
192102ac6454SAndrew Thompson /* read Tx power for all b/g channels */
192202ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
192302ac6454SAndrew Thompson }
192402ac6454SAndrew Thompson
192502ac6454SAndrew Thompson static int
ural_bbp_init(struct ural_softc * sc)192602ac6454SAndrew Thompson ural_bbp_init(struct ural_softc *sc)
192702ac6454SAndrew Thompson {
192802ac6454SAndrew Thompson int i, ntries;
192902ac6454SAndrew Thompson
193002ac6454SAndrew Thompson /* wait for BBP to be ready */
193102ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) {
193202ac6454SAndrew Thompson if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
193302ac6454SAndrew Thompson break;
193402ac6454SAndrew Thompson if (ural_pause(sc, hz / 100))
193502ac6454SAndrew Thompson break;
193602ac6454SAndrew Thompson }
193702ac6454SAndrew Thompson if (ntries == 100) {
193802ac6454SAndrew Thompson device_printf(sc->sc_dev, "timeout waiting for BBP\n");
193902ac6454SAndrew Thompson return EIO;
194002ac6454SAndrew Thompson }
194102ac6454SAndrew Thompson
194202ac6454SAndrew Thompson /* initialize BBP registers to default values */
194382e8c646SAdrian Chadd for (i = 0; i < nitems(ural_def_bbp); i++)
194402ac6454SAndrew Thompson ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
194502ac6454SAndrew Thompson
194602ac6454SAndrew Thompson #if 0
194702ac6454SAndrew Thompson /* initialize BBP registers to values stored in EEPROM */
194802ac6454SAndrew Thompson for (i = 0; i < 16; i++) {
194902ac6454SAndrew Thompson if (sc->bbp_prom[i].reg == 0xff)
195002ac6454SAndrew Thompson continue;
195102ac6454SAndrew Thompson ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
195202ac6454SAndrew Thompson }
195302ac6454SAndrew Thompson #endif
195402ac6454SAndrew Thompson
195502ac6454SAndrew Thompson return 0;
195602ac6454SAndrew Thompson }
195702ac6454SAndrew Thompson
195802ac6454SAndrew Thompson static void
ural_set_txantenna(struct ural_softc * sc,int antenna)195902ac6454SAndrew Thompson ural_set_txantenna(struct ural_softc *sc, int antenna)
196002ac6454SAndrew Thompson {
196102ac6454SAndrew Thompson uint16_t tmp;
196202ac6454SAndrew Thompson uint8_t tx;
196302ac6454SAndrew Thompson
196402ac6454SAndrew Thompson tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
196502ac6454SAndrew Thompson if (antenna == 1)
196602ac6454SAndrew Thompson tx |= RAL_BBP_ANTA;
196702ac6454SAndrew Thompson else if (antenna == 2)
196802ac6454SAndrew Thompson tx |= RAL_BBP_ANTB;
196902ac6454SAndrew Thompson else
197002ac6454SAndrew Thompson tx |= RAL_BBP_DIVERSITY;
197102ac6454SAndrew Thompson
197202ac6454SAndrew Thompson /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
197302ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
197402ac6454SAndrew Thompson sc->rf_rev == RAL_RF_5222)
197502ac6454SAndrew Thompson tx |= RAL_BBP_FLIPIQ;
197602ac6454SAndrew Thompson
197702ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_TX, tx);
197802ac6454SAndrew Thompson
197902ac6454SAndrew Thompson /* update values in PHY_CSR5 and PHY_CSR6 */
198002ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
198102ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
198202ac6454SAndrew Thompson
198302ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
198402ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
198502ac6454SAndrew Thompson }
198602ac6454SAndrew Thompson
198702ac6454SAndrew Thompson static void
ural_set_rxantenna(struct ural_softc * sc,int antenna)198802ac6454SAndrew Thompson ural_set_rxantenna(struct ural_softc *sc, int antenna)
198902ac6454SAndrew Thompson {
199002ac6454SAndrew Thompson uint8_t rx;
199102ac6454SAndrew Thompson
199202ac6454SAndrew Thompson rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
199302ac6454SAndrew Thompson if (antenna == 1)
199402ac6454SAndrew Thompson rx |= RAL_BBP_ANTA;
199502ac6454SAndrew Thompson else if (antenna == 2)
199602ac6454SAndrew Thompson rx |= RAL_BBP_ANTB;
199702ac6454SAndrew Thompson else
199802ac6454SAndrew Thompson rx |= RAL_BBP_DIVERSITY;
199902ac6454SAndrew Thompson
200002ac6454SAndrew Thompson /* need to force no I/Q flip for RF 2525e and 2526 */
200102ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
200202ac6454SAndrew Thompson rx &= ~RAL_BBP_FLIPIQ;
200302ac6454SAndrew Thompson
200402ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_RX, rx);
200502ac6454SAndrew Thompson }
200602ac6454SAndrew Thompson
200702ac6454SAndrew Thompson static void
ural_init(struct ural_softc * sc)20087a79cebfSGleb Smirnoff ural_init(struct ural_softc *sc)
200902ac6454SAndrew Thompson {
20107a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
20117a79cebfSGleb Smirnoff struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
201202ac6454SAndrew Thompson uint16_t tmp;
201302ac6454SAndrew Thompson int i, ntries;
201402ac6454SAndrew Thompson
201502ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED);
201602ac6454SAndrew Thompson
201702ac6454SAndrew Thompson ural_set_testmode(sc);
201802ac6454SAndrew Thompson ural_write(sc, 0x308, 0x00f0); /* XXX magic */
201902ac6454SAndrew Thompson
20205efea30fSAndrew Thompson ural_stop(sc);
202102ac6454SAndrew Thompson
202202ac6454SAndrew Thompson /* initialize MAC registers to default values */
202382e8c646SAdrian Chadd for (i = 0; i < nitems(ural_def_mac); i++)
202402ac6454SAndrew Thompson ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
202502ac6454SAndrew Thompson
202602ac6454SAndrew Thompson /* wait for BBP and RF to wake up (this can take a long time!) */
202702ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) {
202802ac6454SAndrew Thompson tmp = ural_read(sc, RAL_MAC_CSR17);
202902ac6454SAndrew Thompson if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
203002ac6454SAndrew Thompson (RAL_BBP_AWAKE | RAL_RF_AWAKE))
203102ac6454SAndrew Thompson break;
203202ac6454SAndrew Thompson if (ural_pause(sc, hz / 100))
203302ac6454SAndrew Thompson break;
203402ac6454SAndrew Thompson }
203502ac6454SAndrew Thompson if (ntries == 100) {
203602ac6454SAndrew Thompson device_printf(sc->sc_dev,
203702ac6454SAndrew Thompson "timeout waiting for BBP/RF to wakeup\n");
203802ac6454SAndrew Thompson goto fail;
203902ac6454SAndrew Thompson }
204002ac6454SAndrew Thompson
204102ac6454SAndrew Thompson /* we're ready! */
204202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
204302ac6454SAndrew Thompson
204402ac6454SAndrew Thompson /* set basic rate set (will be updated later) */
204502ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f);
204602ac6454SAndrew Thompson
204702ac6454SAndrew Thompson if (ural_bbp_init(sc) != 0)
204802ac6454SAndrew Thompson goto fail;
204902ac6454SAndrew Thompson
205002ac6454SAndrew Thompson ural_set_chan(sc, ic->ic_curchan);
205102ac6454SAndrew Thompson
205202ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */
205302ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
205402ac6454SAndrew Thompson
205502ac6454SAndrew Thompson ural_set_txantenna(sc, sc->tx_ant);
205602ac6454SAndrew Thompson ural_set_rxantenna(sc, sc->rx_ant);
205702ac6454SAndrew Thompson
20587a79cebfSGleb Smirnoff ural_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
205902ac6454SAndrew Thompson
206002ac6454SAndrew Thompson /*
206102ac6454SAndrew Thompson * Allocate Tx and Rx xfer queues.
206202ac6454SAndrew Thompson */
206302ac6454SAndrew Thompson ural_setup_tx_list(sc);
206402ac6454SAndrew Thompson
206502ac6454SAndrew Thompson /* kick Rx */
206602ac6454SAndrew Thompson tmp = RAL_DROP_PHY | RAL_DROP_CRC;
206702ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR) {
206802ac6454SAndrew Thompson tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
206902ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_HOSTAP)
207002ac6454SAndrew Thompson tmp |= RAL_DROP_TODS;
20717a79cebfSGleb Smirnoff if (ic->ic_promisc == 0)
207202ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME;
207302ac6454SAndrew Thompson }
207402ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp);
207502ac6454SAndrew Thompson
20767a79cebfSGleb Smirnoff sc->sc_running = 1;
2077ed6d949aSAndrew Thompson usbd_xfer_set_stall(sc->sc_xfer[URAL_BULK_WR]);
2078a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_RD]);
207902ac6454SAndrew Thompson return;
208002ac6454SAndrew Thompson
20815efea30fSAndrew Thompson fail: ural_stop(sc);
208202ac6454SAndrew Thompson }
208302ac6454SAndrew Thompson
208402ac6454SAndrew Thompson static void
ural_stop(struct ural_softc * sc)20855efea30fSAndrew Thompson ural_stop(struct ural_softc *sc)
208602ac6454SAndrew Thompson {
208702ac6454SAndrew Thompson
208802ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED);
208902ac6454SAndrew Thompson
20907a79cebfSGleb Smirnoff sc->sc_running = 0;
209102ac6454SAndrew Thompson
209202ac6454SAndrew Thompson /*
209302ac6454SAndrew Thompson * Drain all the transfers, if not already drained:
209402ac6454SAndrew Thompson */
209502ac6454SAndrew Thompson RAL_UNLOCK(sc);
2096a593f6b8SAndrew Thompson usbd_transfer_drain(sc->sc_xfer[URAL_BULK_WR]);
2097a593f6b8SAndrew Thompson usbd_transfer_drain(sc->sc_xfer[URAL_BULK_RD]);
209802ac6454SAndrew Thompson RAL_LOCK(sc);
209902ac6454SAndrew Thompson
210002ac6454SAndrew Thompson ural_unsetup_tx_list(sc);
210102ac6454SAndrew Thompson
210202ac6454SAndrew Thompson /* disable Rx */
210302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
210402ac6454SAndrew Thompson /* reset ASIC and BBP (but won't reset MAC registers!) */
210502ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
210602ac6454SAndrew Thompson /* wait a little */
210702ac6454SAndrew Thompson ural_pause(sc, hz / 10);
210802ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, 0);
210977ddf3d3SAndrew Thompson /* wait a little */
211077ddf3d3SAndrew Thompson ural_pause(sc, hz / 10);
211102ac6454SAndrew Thompson }
211202ac6454SAndrew Thompson
211302ac6454SAndrew Thompson static int
ural_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)211402ac6454SAndrew Thompson ural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
211502ac6454SAndrew Thompson const struct ieee80211_bpf_params *params)
211602ac6454SAndrew Thompson {
211702ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic;
2118d3fdd08cSAdrian Chadd struct ural_softc *sc = ic->ic_softc;
211902ac6454SAndrew Thompson
212002ac6454SAndrew Thompson RAL_LOCK(sc);
212102ac6454SAndrew Thompson /* prevent management frames from being sent if we're not ready */
21227a79cebfSGleb Smirnoff if (!sc->sc_running) {
212302ac6454SAndrew Thompson RAL_UNLOCK(sc);
212402ac6454SAndrew Thompson m_freem(m);
212502ac6454SAndrew Thompson return ENETDOWN;
212602ac6454SAndrew Thompson }
212796ca458fSAndrew Thompson if (sc->tx_nfree < RAL_TX_MINFREE) {
212802ac6454SAndrew Thompson RAL_UNLOCK(sc);
212902ac6454SAndrew Thompson m_freem(m);
213002ac6454SAndrew Thompson return EIO;
213102ac6454SAndrew Thompson }
213202ac6454SAndrew Thompson
213302ac6454SAndrew Thompson if (params == NULL) {
213402ac6454SAndrew Thompson /*
213502ac6454SAndrew Thompson * Legacy path; interpret frame contents to decide
213602ac6454SAndrew Thompson * precisely how to send the frame.
213702ac6454SAndrew Thompson */
213802ac6454SAndrew Thompson if (ural_tx_mgt(sc, m, ni) != 0)
213902ac6454SAndrew Thompson goto bad;
214002ac6454SAndrew Thompson } else {
214102ac6454SAndrew Thompson /*
214202ac6454SAndrew Thompson * Caller supplied explicit parameters to use in
214302ac6454SAndrew Thompson * sending the frame.
214402ac6454SAndrew Thompson */
214502ac6454SAndrew Thompson if (ural_tx_raw(sc, m, ni, params) != 0)
214602ac6454SAndrew Thompson goto bad;
214702ac6454SAndrew Thompson }
214802ac6454SAndrew Thompson RAL_UNLOCK(sc);
214902ac6454SAndrew Thompson return 0;
215002ac6454SAndrew Thompson bad:
215102ac6454SAndrew Thompson RAL_UNLOCK(sc);
215202ac6454SAndrew Thompson return EIO; /* XXX */
215302ac6454SAndrew Thompson }
215402ac6454SAndrew Thompson
215502ac6454SAndrew Thompson static void
ural_ratectl_start(struct ural_softc * sc,struct ieee80211_node * ni)2156b6108616SRui Paulo ural_ratectl_start(struct ural_softc *sc, struct ieee80211_node *ni)
215702ac6454SAndrew Thompson {
215802ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap;
215902ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap);
216002ac6454SAndrew Thompson
216102ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */
216202ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
216302ac6454SAndrew Thompson
2164b6108616SRui Paulo usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
216502ac6454SAndrew Thompson }
216602ac6454SAndrew Thompson
216702ac6454SAndrew Thompson static void
ural_ratectl_timeout(void * arg)2168b6108616SRui Paulo ural_ratectl_timeout(void *arg)
216902ac6454SAndrew Thompson {
217002ac6454SAndrew Thompson struct ural_vap *uvp = arg;
21715efea30fSAndrew Thompson struct ieee80211vap *vap = &uvp->vap;
21725efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic;
217302ac6454SAndrew Thompson
2174b6108616SRui Paulo ieee80211_runtask(ic, &uvp->ratectl_task);
217502ac6454SAndrew Thompson }
217602ac6454SAndrew Thompson
217702ac6454SAndrew Thompson static void
ural_ratectl_task(void * arg,int pending)2178b6108616SRui Paulo ural_ratectl_task(void *arg, int pending)
217902ac6454SAndrew Thompson {
21805efea30fSAndrew Thompson struct ural_vap *uvp = arg;
21815efea30fSAndrew Thompson struct ieee80211vap *vap = &uvp->vap;
2182f6930becSAndriy Voskoboinyk struct ural_softc *sc = vap->iv_ic->ic_softc;
2183f6930becSAndriy Voskoboinyk struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
2184f6930becSAndriy Voskoboinyk int fail;
218502ac6454SAndrew Thompson
21865efea30fSAndrew Thompson RAL_LOCK(sc);
218702ac6454SAndrew Thompson /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
218802ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
218902ac6454SAndrew Thompson
2190f6930becSAndriy Voskoboinyk txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
2191f6930becSAndriy Voskoboinyk txs->nsuccess = sc->sta[7] + /* TX ok w/o retry */
219202ac6454SAndrew Thompson sc->sta[8]; /* TX ok w/ retry */
219302ac6454SAndrew Thompson fail = sc->sta[9]; /* TX retry-fail count */
2194f6930becSAndriy Voskoboinyk txs->nframes = txs->nsuccess + fail;
2195f6930becSAndriy Voskoboinyk /* XXX fail * maxretry */
2196f6930becSAndriy Voskoboinyk txs->nretries = sc->sta[8] + fail;
219702ac6454SAndrew Thompson
2198f6930becSAndriy Voskoboinyk ieee80211_ratectl_tx_update(vap, txs);
219902ac6454SAndrew Thompson
22007a79cebfSGleb Smirnoff /* count TX retry-fail as Tx errors */
2201f6930becSAndriy Voskoboinyk if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
220202ac6454SAndrew Thompson
2203b6108616SRui Paulo usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
22045efea30fSAndrew Thompson RAL_UNLOCK(sc);
220502ac6454SAndrew Thompson }
220602ac6454SAndrew Thompson
220702ac6454SAndrew Thompson static int
ural_pause(struct ural_softc * sc,int timeout)220802ac6454SAndrew Thompson ural_pause(struct ural_softc *sc, int timeout)
220902ac6454SAndrew Thompson {
221002ac6454SAndrew Thompson
2211a593f6b8SAndrew Thompson usb_pause_mtx(&sc->sc_mtx, timeout);
221202ac6454SAndrew Thompson return (0);
221302ac6454SAndrew Thompson }
2214