if_ural.c revision 188969
1184610Salfred/*	$FreeBSD: head/sys/dev/usb/wlan/if_ural.c 188969 2009-02-23 23:46:56Z thompsa $	*/
2184610Salfred
3184610Salfred/*-
4184610Salfred * Copyright (c) 2005, 2006
5184610Salfred *	Damien Bergamini <damien.bergamini@free.fr>
6184610Salfred *
7184610Salfred * Copyright (c) 2006, 2008
8184610Salfred *	Hans Petter Selasky <hselasky@freebsd.org>
9184610Salfred *
10184610Salfred * Permission to use, copy, modify, and distribute this software for any
11184610Salfred * purpose with or without fee is hereby granted, provided that the above
12184610Salfred * copyright notice and this permission notice appear in all copies.
13184610Salfred *
14184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21184610Salfred */
22184610Salfred
23184610Salfred#include <sys/cdefs.h>
24184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_ural.c 188969 2009-02-23 23:46:56Z thompsa $");
25184610Salfred
26184610Salfred/*-
27184610Salfred * Ralink Technology RT2500USB chipset driver
28184610Salfred * http://www.ralinktech.com/
29184610Salfred */
30184610Salfred
31188746Sthompsa#include "usbdevs.h"
32188942Sthompsa#include <dev/usb/usb.h>
33188942Sthompsa#include <dev/usb/usb_mfunc.h>
34188942Sthompsa#include <dev/usb/usb_error.h>
35184610Salfred
36184610Salfred#define	USB_DEBUG_VAR ural_debug
37184610Salfred
38188942Sthompsa#include <dev/usb/usb_core.h>
39188942Sthompsa#include <dev/usb/usb_lookup.h>
40188942Sthompsa#include <dev/usb/usb_process.h>
41188942Sthompsa#include <dev/usb/usb_debug.h>
42188942Sthompsa#include <dev/usb/usb_request.h>
43188942Sthompsa#include <dev/usb/usb_busdma.h>
44188942Sthompsa#include <dev/usb/usb_util.h>
45184610Salfred
46188942Sthompsa#include <dev/usb/wlan/usb_wlan.h>
47188942Sthompsa#include <dev/usb/wlan/if_uralreg.h>
48188942Sthompsa#include <dev/usb/wlan/if_uralvar.h>
49184610Salfred
50184610Salfred#if USB_DEBUG
51184610Salfredstatic int ural_debug = 0;
52184610Salfred
53184610SalfredSYSCTL_NODE(_hw_usb2, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
54184610SalfredSYSCTL_INT(_hw_usb2_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0,
55184610Salfred    "Debug level");
56184610Salfred#endif
57184610Salfred
58188419Sthompsa#define	ural_do_request(sc,req,data) \
59188419Sthompsa    usb2_do_request_proc((sc)->sc_udev, &(sc)->sc_tq, req, data, 0, NULL, 5000)
60188419Sthompsa
61188417Sthompsa#define URAL_RSSI(rssi)					\
62188417Sthompsa	((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ?	\
63188417Sthompsa	 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
64184610Salfred
65188417Sthompsa/* various supported device vendors/products */
66188417Sthompsastatic const struct usb2_device_id ural_devs[] = {
67188417Sthompsa	{ USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G) },
68188417Sthompsa	{ USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570) },
69188417Sthompsa	{ USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050) },
70188417Sthompsa	{ USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7051) },
71188417Sthompsa	{ USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_HU200TS) },
72188417Sthompsa	{ USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G) },
73188417Sthompsa	{ USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP) },
74188417Sthompsa	{ USB_VP(USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU) },
75188417Sthompsa	{ USB_VP(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122) },
76188417Sthompsa	{ USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GN54G) },
77188417Sthompsa	{ USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG) },
78188417Sthompsa	{ USB_VP(USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254) },
79188417Sthompsa	{ USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54) },
80188417Sthompsa	{ USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI) },
81188417Sthompsa	{ USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB) },
82188417Sthompsa	{ USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI) },
83188417Sthompsa	{ USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570) },
84188417Sthompsa	{ USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2) },
85188417Sthompsa	{ USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3) },
86188417Sthompsa	{ USB_VP(USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_NV902) },
87188417Sthompsa	{ USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570) },
88188417Sthompsa	{ USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2) },
89188417Sthompsa	{ USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_3) },
90188417Sthompsa	{ USB_VP(USB_VENDOR_SIEMENS2, USB_PRODUCT_SIEMENS2_WL54G) },
91188417Sthompsa	{ USB_VP(USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG) },
92188417Sthompsa	{ USB_VP(USB_VENDOR_SPHAIRON, USB_PRODUCT_SPHAIRON_UB801R) },
93188417Sthompsa	{ USB_VP(USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2570) },
94188417Sthompsa	{ USB_VP(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570) },
95188417Sthompsa	{ USB_VP(USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570) },
96188417Sthompsa};
97184610Salfred
98184610Salfredstatic usb2_callback_t ural_bulk_read_callback;
99184610Salfredstatic usb2_callback_t ural_bulk_write_callback;
100184610Salfred
101188419Sthompsastatic usb2_proc_callback_t ural_attach_post;
102188417Sthompsastatic usb2_proc_callback_t ural_task;
103188417Sthompsastatic usb2_proc_callback_t ural_scantask;
104188417Sthompsastatic usb2_proc_callback_t ural_promisctask;
105188417Sthompsastatic usb2_proc_callback_t ural_amrr_task;
106188419Sthompsastatic usb2_proc_callback_t ural_init_task;
107188419Sthompsastatic usb2_proc_callback_t ural_stop_task;
108184610Salfred
109185948Sthompsastatic struct ieee80211vap *ural_vap_create(struct ieee80211com *,
110188417Sthompsa			    const char name[IFNAMSIZ], int unit, int opmode,
111188417Sthompsa			    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
112188417Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
113188417Sthompsastatic void		ural_vap_delete(struct ieee80211vap *);
114188417Sthompsastatic void		ural_tx_free(struct ural_tx_data *, int);
115188419Sthompsastatic void		ural_setup_tx_list(struct ural_softc *);
116188419Sthompsastatic void		ural_unsetup_tx_list(struct ural_softc *);
117188417Sthompsastatic int		ural_newstate(struct ieee80211vap *,
118188417Sthompsa			    enum ieee80211_state, int);
119188417Sthompsastatic void		ural_setup_tx_desc(struct ural_softc *,
120188417Sthompsa			    struct ural_tx_desc *, uint32_t, int, int);
121188417Sthompsastatic int		ural_tx_bcn(struct ural_softc *, struct mbuf *,
122188417Sthompsa			    struct ieee80211_node *);
123188417Sthompsastatic int		ural_tx_mgt(struct ural_softc *, struct mbuf *,
124188417Sthompsa			    struct ieee80211_node *);
125188417Sthompsastatic int		ural_tx_data(struct ural_softc *, struct mbuf *,
126188417Sthompsa			    struct ieee80211_node *);
127188417Sthompsastatic void		ural_start(struct ifnet *);
128188417Sthompsastatic int		ural_ioctl(struct ifnet *, u_long, caddr_t);
129188417Sthompsastatic void		ural_set_testmode(struct ural_softc *);
130188417Sthompsastatic void		ural_eeprom_read(struct ural_softc *, uint16_t, void *,
131188417Sthompsa			    int);
132188417Sthompsastatic uint16_t		ural_read(struct ural_softc *, uint16_t);
133188417Sthompsastatic void		ural_read_multi(struct ural_softc *, uint16_t, void *,
134188417Sthompsa			    int);
135188417Sthompsastatic void		ural_write(struct ural_softc *, uint16_t, uint16_t);
136188417Sthompsastatic void		ural_write_multi(struct ural_softc *, uint16_t, void *,
137188417Sthompsa			    int) __unused;
138188417Sthompsastatic void		ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
139188417Sthompsastatic uint8_t		ural_bbp_read(struct ural_softc *, uint8_t);
140188417Sthompsastatic void		ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
141185948Sthompsastatic struct ieee80211_node *ural_node_alloc(struct ieee80211vap *,
142188417Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
143188417Sthompsastatic void		ural_newassoc(struct ieee80211_node *, int);
144188417Sthompsastatic void		ural_scan_start(struct ieee80211com *);
145188417Sthompsastatic void		ural_scan_end(struct ieee80211com *);
146188417Sthompsastatic void		ural_set_channel(struct ieee80211com *);
147188417Sthompsastatic void		ural_set_chan(struct ural_softc *,
148188417Sthompsa			    struct ieee80211_channel *);
149188417Sthompsastatic void		ural_disable_rf_tune(struct ural_softc *);
150188417Sthompsastatic void		ural_enable_tsf_sync(struct ural_softc *);
151188417Sthompsastatic void		ural_update_slot(struct ifnet *);
152188417Sthompsastatic void		ural_set_txpreamble(struct ural_softc *);
153188417Sthompsastatic void		ural_set_basicrates(struct ural_softc *,
154188417Sthompsa			    const struct ieee80211_channel *);
155188417Sthompsastatic void		ural_set_bssid(struct ural_softc *, const uint8_t *);
156188417Sthompsastatic void		ural_set_macaddr(struct ural_softc *, uint8_t *);
157188417Sthompsastatic const char	*ural_get_rf(int);
158188417Sthompsastatic void		ural_read_eeprom(struct ural_softc *);
159188417Sthompsastatic int		ural_bbp_init(struct ural_softc *);
160188417Sthompsastatic void		ural_set_txantenna(struct ural_softc *, int);
161188417Sthompsastatic void		ural_set_rxantenna(struct ural_softc *, int);
162188417Sthompsastatic void		ural_init(void *);
163188417Sthompsastatic int		ural_raw_xmit(struct ieee80211_node *, struct mbuf *,
164188417Sthompsa			    const struct ieee80211_bpf_params *);
165188417Sthompsastatic void		ural_amrr_start(struct ural_softc *,
166188417Sthompsa			    struct ieee80211_node *);
167188417Sthompsastatic void		ural_amrr_timeout(void *);
168188619Sthompsastatic int		ural_pause(struct ural_softc *sc, int timeout);
169188417Sthompsastatic void		ural_queue_command(struct ural_softc *,
170188417Sthompsa			    usb2_proc_callback_t *, struct usb2_proc_msg *,
171188417Sthompsa			    struct usb2_proc_msg *);
172184610Salfred
173184610Salfred/*
174188417Sthompsa * Default values for MAC registers; values taken from the reference driver.
175184610Salfred */
176188417Sthompsastatic const struct {
177188417Sthompsa	uint16_t	reg;
178188417Sthompsa	uint16_t	val;
179188417Sthompsa} ural_def_mac[] = {
180188417Sthompsa	{ RAL_TXRX_CSR5,  0x8c8d },
181188417Sthompsa	{ RAL_TXRX_CSR6,  0x8b8a },
182188417Sthompsa	{ RAL_TXRX_CSR7,  0x8687 },
183188417Sthompsa	{ RAL_TXRX_CSR8,  0x0085 },
184188417Sthompsa	{ RAL_MAC_CSR13,  0x1111 },
185188417Sthompsa	{ RAL_MAC_CSR14,  0x1e11 },
186188417Sthompsa	{ RAL_TXRX_CSR21, 0xe78f },
187188417Sthompsa	{ RAL_MAC_CSR9,   0xff1d },
188188417Sthompsa	{ RAL_MAC_CSR11,  0x0002 },
189188417Sthompsa	{ RAL_MAC_CSR22,  0x0053 },
190188417Sthompsa	{ RAL_MAC_CSR15,  0x0000 },
191188417Sthompsa	{ RAL_MAC_CSR8,   RAL_FRAME_SIZE },
192188417Sthompsa	{ RAL_TXRX_CSR19, 0x0000 },
193188417Sthompsa	{ RAL_TXRX_CSR18, 0x005a },
194188417Sthompsa	{ RAL_PHY_CSR2,   0x0000 },
195188417Sthompsa	{ RAL_TXRX_CSR0,  0x1ec0 },
196188417Sthompsa	{ RAL_PHY_CSR4,   0x000f }
197184610Salfred};
198184610Salfred
199184610Salfred/*
200184610Salfred * Default values for BBP registers; values taken from the reference driver.
201184610Salfred */
202188417Sthompsastatic const struct {
203184610Salfred	uint8_t	reg;
204184610Salfred	uint8_t	val;
205188417Sthompsa} ural_def_bbp[] = {
206188417Sthompsa	{  3, 0x02 },
207188417Sthompsa	{  4, 0x19 },
208188417Sthompsa	{ 14, 0x1c },
209188417Sthompsa	{ 15, 0x30 },
210188417Sthompsa	{ 16, 0xac },
211188417Sthompsa	{ 17, 0x48 },
212188417Sthompsa	{ 18, 0x18 },
213188417Sthompsa	{ 19, 0xff },
214188417Sthompsa	{ 20, 0x1e },
215188417Sthompsa	{ 21, 0x08 },
216188417Sthompsa	{ 22, 0x08 },
217188417Sthompsa	{ 23, 0x08 },
218188417Sthompsa	{ 24, 0x80 },
219188417Sthompsa	{ 25, 0x50 },
220188417Sthompsa	{ 26, 0x08 },
221188417Sthompsa	{ 27, 0x23 },
222188417Sthompsa	{ 30, 0x10 },
223188417Sthompsa	{ 31, 0x2b },
224188417Sthompsa	{ 32, 0xb9 },
225188417Sthompsa	{ 34, 0x12 },
226188417Sthompsa	{ 35, 0x50 },
227188417Sthompsa	{ 39, 0xc4 },
228188417Sthompsa	{ 40, 0x02 },
229188417Sthompsa	{ 41, 0x60 },
230188417Sthompsa	{ 53, 0x10 },
231188417Sthompsa	{ 54, 0x18 },
232188417Sthompsa	{ 56, 0x08 },
233188417Sthompsa	{ 57, 0x10 },
234188417Sthompsa	{ 58, 0x08 },
235188417Sthompsa	{ 61, 0x60 },
236188417Sthompsa	{ 62, 0x10 },
237188417Sthompsa	{ 75, 0xff }
238184610Salfred};
239184610Salfred
240184610Salfred/*
241184610Salfred * Default values for RF register R2 indexed by channel numbers.
242184610Salfred */
243184610Salfredstatic const uint32_t ural_rf2522_r2[] = {
244184610Salfred	0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
245184610Salfred	0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
246184610Salfred};
247184610Salfred
248184610Salfredstatic const uint32_t ural_rf2523_r2[] = {
249184610Salfred	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
250184610Salfred	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
251184610Salfred};
252184610Salfred
253184610Salfredstatic const uint32_t ural_rf2524_r2[] = {
254184610Salfred	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
255184610Salfred	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
256184610Salfred};
257184610Salfred
258184610Salfredstatic const uint32_t ural_rf2525_r2[] = {
259184610Salfred	0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
260184610Salfred	0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
261184610Salfred};
262184610Salfred
263184610Salfredstatic const uint32_t ural_rf2525_hi_r2[] = {
264184610Salfred	0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
265184610Salfred	0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
266184610Salfred};
267184610Salfred
268184610Salfredstatic const uint32_t ural_rf2525e_r2[] = {
269184610Salfred	0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
270184610Salfred	0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
271184610Salfred};
272184610Salfred
273184610Salfredstatic const uint32_t ural_rf2526_hi_r2[] = {
274184610Salfred	0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
275184610Salfred	0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
276184610Salfred};
277184610Salfred
278184610Salfredstatic const uint32_t ural_rf2526_r2[] = {
279184610Salfred	0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
280184610Salfred	0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
281184610Salfred};
282184610Salfred
283184610Salfred/*
284184610Salfred * For dual-band RF, RF registers R1 and R4 also depend on channel number;
285184610Salfred * values taken from the reference driver.
286184610Salfred */
287188417Sthompsastatic const struct {
288188417Sthompsa	uint8_t		chan;
289188417Sthompsa	uint32_t	r1;
290188417Sthompsa	uint32_t	r2;
291188417Sthompsa	uint32_t	r4;
292188417Sthompsa} ural_rf5222[] = {
293188417Sthompsa	{   1, 0x08808, 0x0044d, 0x00282 },
294188417Sthompsa	{   2, 0x08808, 0x0044e, 0x00282 },
295188417Sthompsa	{   3, 0x08808, 0x0044f, 0x00282 },
296188417Sthompsa	{   4, 0x08808, 0x00460, 0x00282 },
297188417Sthompsa	{   5, 0x08808, 0x00461, 0x00282 },
298188417Sthompsa	{   6, 0x08808, 0x00462, 0x00282 },
299188417Sthompsa	{   7, 0x08808, 0x00463, 0x00282 },
300188417Sthompsa	{   8, 0x08808, 0x00464, 0x00282 },
301188417Sthompsa	{   9, 0x08808, 0x00465, 0x00282 },
302188417Sthompsa	{  10, 0x08808, 0x00466, 0x00282 },
303188417Sthompsa	{  11, 0x08808, 0x00467, 0x00282 },
304188417Sthompsa	{  12, 0x08808, 0x00468, 0x00282 },
305188417Sthompsa	{  13, 0x08808, 0x00469, 0x00282 },
306188417Sthompsa	{  14, 0x08808, 0x0046b, 0x00286 },
307184610Salfred
308188417Sthompsa	{  36, 0x08804, 0x06225, 0x00287 },
309188417Sthompsa	{  40, 0x08804, 0x06226, 0x00287 },
310188417Sthompsa	{  44, 0x08804, 0x06227, 0x00287 },
311188417Sthompsa	{  48, 0x08804, 0x06228, 0x00287 },
312188417Sthompsa	{  52, 0x08804, 0x06229, 0x00287 },
313188417Sthompsa	{  56, 0x08804, 0x0622a, 0x00287 },
314188417Sthompsa	{  60, 0x08804, 0x0622b, 0x00287 },
315188417Sthompsa	{  64, 0x08804, 0x0622c, 0x00287 },
316184610Salfred
317188417Sthompsa	{ 100, 0x08804, 0x02200, 0x00283 },
318188417Sthompsa	{ 104, 0x08804, 0x02201, 0x00283 },
319188417Sthompsa	{ 108, 0x08804, 0x02202, 0x00283 },
320188417Sthompsa	{ 112, 0x08804, 0x02203, 0x00283 },
321188417Sthompsa	{ 116, 0x08804, 0x02204, 0x00283 },
322188417Sthompsa	{ 120, 0x08804, 0x02205, 0x00283 },
323188417Sthompsa	{ 124, 0x08804, 0x02206, 0x00283 },
324188417Sthompsa	{ 128, 0x08804, 0x02207, 0x00283 },
325188417Sthompsa	{ 132, 0x08804, 0x02208, 0x00283 },
326188417Sthompsa	{ 136, 0x08804, 0x02209, 0x00283 },
327188417Sthompsa	{ 140, 0x08804, 0x0220a, 0x00283 },
328184610Salfred
329188417Sthompsa	{ 149, 0x08808, 0x02429, 0x00281 },
330188417Sthompsa	{ 153, 0x08808, 0x0242b, 0x00281 },
331188417Sthompsa	{ 157, 0x08808, 0x0242d, 0x00281 },
332188417Sthompsa	{ 161, 0x08808, 0x0242f, 0x00281 }
333184610Salfred};
334184610Salfred
335184610Salfredstatic const struct usb2_config ural_config[URAL_N_TRANSFER] = {
336188417Sthompsa	[URAL_BULK_WR] = {
337184610Salfred		.type = UE_BULK,
338184610Salfred		.endpoint = UE_ADDR_ANY,
339184610Salfred		.direction = UE_DIR_OUT,
340184610Salfred		.mh.bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4),
341184610Salfred		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
342188417Sthompsa		.mh.callback = ural_bulk_write_callback,
343184610Salfred		.mh.timeout = 5000,	/* ms */
344184610Salfred	},
345188417Sthompsa	[URAL_BULK_RD] = {
346184610Salfred		.type = UE_BULK,
347184610Salfred		.endpoint = UE_ADDR_ANY,
348184610Salfred		.direction = UE_DIR_IN,
349184610Salfred		.mh.bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE),
350184610Salfred		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
351188417Sthompsa		.mh.callback = ural_bulk_read_callback,
352184610Salfred	},
353184610Salfred};
354184610Salfred
355188417Sthompsastatic device_probe_t ural_match;
356188417Sthompsastatic device_attach_t ural_attach;
357188417Sthompsastatic device_detach_t ural_detach;
358184610Salfred
359184610Salfredstatic device_method_t ural_methods[] = {
360188417Sthompsa	/* Device interface */
361188417Sthompsa	DEVMETHOD(device_probe,		ural_match),
362188417Sthompsa	DEVMETHOD(device_attach,	ural_attach),
363188417Sthompsa	DEVMETHOD(device_detach,	ural_detach),
364188417Sthompsa
365188417Sthompsa	{ 0, 0 }
366184610Salfred};
367184610Salfred
368184610Salfredstatic driver_t ural_driver = {
369184610Salfred	.name = "ural",
370184610Salfred	.methods = ural_methods,
371184610Salfred	.size = sizeof(struct ural_softc),
372184610Salfred};
373184610Salfred
374188417Sthompsastatic devclass_t ural_devclass;
375188417Sthompsa
376184610SalfredDRIVER_MODULE(ural, ushub, ural_driver, ural_devclass, NULL, 0);
377188942SthompsaMODULE_DEPEND(ural, usb, 1, 1, 1);
378184610SalfredMODULE_DEPEND(ural, wlan, 1, 1, 1);
379184610SalfredMODULE_DEPEND(ural, wlan_amrr, 1, 1, 1);
380184610Salfred
381184610Salfredstatic int
382188417Sthompsaural_match(device_t self)
383184610Salfred{
384188417Sthompsa	struct usb2_attach_arg *uaa = device_get_ivars(self);
385184610Salfred
386188417Sthompsa	if (uaa->usb2_mode != USB_MODE_HOST)
387184610Salfred		return (ENXIO);
388188417Sthompsa	if (uaa->info.bConfigIndex != 0)
389184610Salfred		return (ENXIO);
390188417Sthompsa	if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX)
391184610Salfred		return (ENXIO);
392188417Sthompsa
393184610Salfred	return (usb2_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa));
394184610Salfred}
395184610Salfred
396184610Salfredstatic int
397188417Sthompsaural_attach(device_t self)
398184610Salfred{
399188417Sthompsa	struct usb2_attach_arg *uaa = device_get_ivars(self);
400188417Sthompsa	struct ural_softc *sc = device_get_softc(self);
401184610Salfred	int error;
402188419Sthompsa	uint8_t iface_index;
403184610Salfred
404188417Sthompsa	device_set_usb2_desc(self);
405184610Salfred	sc->sc_udev = uaa->device;
406188417Sthompsa	sc->sc_dev = self;
407184610Salfred
408188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
409188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
410184610Salfred
411184610Salfred	iface_index = RAL_IFACE_INDEX;
412184610Salfred	error = usb2_transfer_setup(uaa->device,
413184610Salfred	    &iface_index, sc->sc_xfer, ural_config,
414184610Salfred	    URAL_N_TRANSFER, sc, &sc->sc_mtx);
415184610Salfred	if (error) {
416188417Sthompsa		device_printf(self, "could not allocate USB transfers, "
417184610Salfred		    "err=%s\n", usb2_errstr(error));
418184610Salfred		goto detach;
419184610Salfred	}
420188417Sthompsa	error = usb2_proc_create(&sc->sc_tq, &sc->sc_mtx,
421188417Sthompsa	    device_get_nameunit(self), USB_PRI_MED);
422184610Salfred	if (error) {
423188417Sthompsa		device_printf(self, "could not setup config thread!\n");
424184610Salfred		goto detach;
425184610Salfred	}
426184610Salfred
427188419Sthompsa	/* fork rest of the attach code */
428188419Sthompsa	RAL_LOCK(sc);
429188419Sthompsa	ural_queue_command(sc, ural_attach_post,
430188419Sthompsa	    &sc->sc_synctask[0].hdr,
431188419Sthompsa	    &sc->sc_synctask[1].hdr);
432188419Sthompsa	RAL_UNLOCK(sc);
433188419Sthompsa	return (0);
434188418Sthompsa
435188419Sthompsadetach:
436188419Sthompsa	ural_detach(self);
437188419Sthompsa	return (ENXIO);			/* failure */
438188419Sthompsa}
439188419Sthompsa
440188419Sthompsastatic void
441188419Sthompsaural_attach_post(struct usb2_proc_msg *pm)
442188419Sthompsa{
443188419Sthompsa	struct ural_task *task = (struct ural_task *)pm;
444188419Sthompsa	struct ural_softc *sc = task->sc;
445188419Sthompsa	struct ifnet *ifp;
446188419Sthompsa	struct ieee80211com *ic;
447188419Sthompsa	uint8_t bands;
448188419Sthompsa
449188417Sthompsa	/* retrieve RT2570 rev. no */
450188417Sthompsa	sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
451184610Salfred
452188417Sthompsa	/* retrieve MAC address and various other things from EEPROM */
453188417Sthompsa	ural_read_eeprom(sc);
454188417Sthompsa	RAL_UNLOCK(sc);
455184610Salfred
456188417Sthompsa	device_printf(sc->sc_dev, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
457188417Sthompsa	    sc->asic_rev, ural_get_rf(sc->rf_rev));
458184610Salfred
459188419Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
460188419Sthompsa	if (ifp == NULL) {
461188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
462188419Sthompsa		RAL_LOCK(sc);
463188419Sthompsa		return;
464188419Sthompsa	}
465188419Sthompsa	ic = ifp->if_l2com;
466188419Sthompsa
467188417Sthompsa	ifp->if_softc = sc;
468188417Sthompsa	if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
469188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
470188417Sthompsa	ifp->if_init = ural_init;
471188417Sthompsa	ifp->if_ioctl = ural_ioctl;
472188417Sthompsa	ifp->if_start = ural_start;
473188417Sthompsa	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
474188417Sthompsa	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
475188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
476184610Salfred
477188417Sthompsa	ic->ic_ifp = ifp;
478188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
479188419Sthompsa	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_bssid);
480184610Salfred
481188417Sthompsa	/* set device capabilities */
482188417Sthompsa	ic->ic_caps =
483188417Sthompsa	      IEEE80211_C_STA		/* station mode supported */
484188417Sthompsa	    | IEEE80211_C_IBSS		/* IBSS mode supported */
485188417Sthompsa	    | IEEE80211_C_MONITOR	/* monitor mode supported */
486188417Sthompsa	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
487188417Sthompsa	    | IEEE80211_C_TXPMGT	/* tx power management */
488188417Sthompsa	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
489188417Sthompsa	    | IEEE80211_C_SHSLOT	/* short slot time supported */
490188417Sthompsa	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
491188417Sthompsa	    | IEEE80211_C_WPA		/* 802.11i */
492188417Sthompsa	    ;
493184610Salfred
494188417Sthompsa	bands = 0;
495188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
496188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
497188417Sthompsa	if (sc->rf_rev == RAL_RF_5222)
498188417Sthompsa		setbit(&bands, IEEE80211_MODE_11A);
499188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
500184610Salfred
501188417Sthompsa	ieee80211_ifattach(ic);
502188417Sthompsa	ic->ic_newassoc = ural_newassoc;
503188417Sthompsa	ic->ic_raw_xmit = ural_raw_xmit;
504188417Sthompsa	ic->ic_node_alloc = ural_node_alloc;
505188417Sthompsa	ic->ic_scan_start = ural_scan_start;
506188417Sthompsa	ic->ic_scan_end = ural_scan_end;
507188417Sthompsa	ic->ic_set_channel = ural_set_channel;
508184610Salfred
509188417Sthompsa	ic->ic_vap_create = ural_vap_create;
510188417Sthompsa	ic->ic_vap_delete = ural_vap_delete;
511184610Salfred
512188417Sthompsa	sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
513184610Salfred
514188417Sthompsa	bpfattach(ifp, DLT_IEEE802_11_RADIO,
515188417Sthompsa	    sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap));
516184610Salfred
517188417Sthompsa	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
518188417Sthompsa	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
519188417Sthompsa	sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
520184610Salfred
521188417Sthompsa	sc->sc_txtap_len = sizeof sc->sc_txtap;
522188417Sthompsa	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
523188417Sthompsa	sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
524184610Salfred
525188417Sthompsa	if (bootverbose)
526188417Sthompsa		ieee80211_announce(ic);
527184610Salfred
528188419Sthompsa	RAL_LOCK(sc);
529188417Sthompsa}
530184610Salfred
531188417Sthompsastatic int
532188417Sthompsaural_detach(device_t self)
533188417Sthompsa{
534188417Sthompsa	struct ural_softc *sc = device_get_softc(self);
535188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
536188619Sthompsa	struct ieee80211com *ic;
537184610Salfred
538188419Sthompsa	/* wait for any post attach or other command to complete */
539188419Sthompsa	usb2_proc_drain(&sc->sc_tq);
540184610Salfred
541188419Sthompsa	/* stop all USB transfers */
542188417Sthompsa	usb2_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER);
543188417Sthompsa	usb2_proc_free(&sc->sc_tq);
544184610Salfred
545188419Sthompsa	/* free TX list, if any */
546188419Sthompsa	RAL_LOCK(sc);
547188419Sthompsa	ural_unsetup_tx_list(sc);
548188419Sthompsa	RAL_UNLOCK(sc);
549188419Sthompsa
550188417Sthompsa	if (ifp) {
551188619Sthompsa		ic = ifp->if_l2com;
552188417Sthompsa		bpfdetach(ifp);
553188417Sthompsa		ieee80211_ifdetach(ic);
554188417Sthompsa		if_free(ifp);
555184610Salfred	}
556188419Sthompsa
557188417Sthompsa	mtx_destroy(&sc->sc_mtx);
558184610Salfred
559188417Sthompsa	return (0);
560184610Salfred}
561184610Salfred
562188417Sthompsastatic struct ieee80211vap *
563188417Sthompsaural_vap_create(struct ieee80211com *ic,
564188417Sthompsa	const char name[IFNAMSIZ], int unit, int opmode, int flags,
565188417Sthompsa	const uint8_t bssid[IEEE80211_ADDR_LEN],
566188417Sthompsa	const uint8_t mac[IEEE80211_ADDR_LEN])
567184610Salfred{
568188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
569188417Sthompsa	struct ural_vap *uvp;
570188417Sthompsa	struct ieee80211vap *vap;
571184610Salfred
572188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
573188417Sthompsa		return NULL;
574188417Sthompsa	uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap),
575188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
576188417Sthompsa	if (uvp == NULL)
577188417Sthompsa		return NULL;
578188417Sthompsa	vap = &uvp->vap;
579188417Sthompsa	/* enable s/w bmiss handling for sta mode */
580188417Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode,
581188417Sthompsa	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
582184610Salfred
583188417Sthompsa	/* override state transition machine */
584188417Sthompsa	uvp->newstate = vap->iv_newstate;
585188417Sthompsa	vap->iv_newstate = ural_newstate;
586184610Salfred
587188417Sthompsa	uvp->sc = sc;
588188417Sthompsa	usb2_callout_init_mtx(&uvp->amrr_ch, &sc->sc_mtx, 0);
589188417Sthompsa	ieee80211_amrr_init(&uvp->amrr, vap,
590188417Sthompsa	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
591188417Sthompsa	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
592188417Sthompsa	    1000 /* 1 sec */);
593184610Salfred
594188417Sthompsa	/* complete setup */
595188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
596188417Sthompsa	ic->ic_opmode = opmode;
597188417Sthompsa	return vap;
598184610Salfred}
599184610Salfred
600184610Salfredstatic void
601188417Sthompsaural_vap_delete(struct ieee80211vap *vap)
602184610Salfred{
603188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
604184610Salfred
605188419Sthompsa	usb2_callout_drain(&uvp->amrr_ch);
606188417Sthompsa	ieee80211_amrr_cleanup(&uvp->amrr);
607188417Sthompsa	ieee80211_vap_detach(vap);
608188417Sthompsa	free(uvp, M_80211_VAP);
609184610Salfred}
610184610Salfred
611184610Salfredstatic void
612188417Sthompsaural_tx_free(struct ural_tx_data *data, int txerr)
613184610Salfred{
614188417Sthompsa	struct ural_softc *sc = data->sc;
615184610Salfred
616188417Sthompsa	if (data->m != NULL) {
617188417Sthompsa		if (data->m->m_flags & M_TXCB)
618188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
619188417Sthompsa			    txerr ? ETIMEDOUT : 0);
620188417Sthompsa		m_freem(data->m);
621188417Sthompsa		data->m = NULL;
622184610Salfred
623188417Sthompsa		ieee80211_free_node(data->ni);
624188417Sthompsa		data->ni = NULL;
625188417Sthompsa	}
626188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
627188417Sthompsa	sc->tx_nfree++;
628184610Salfred}
629184610Salfred
630188419Sthompsastatic void
631188419Sthompsaural_setup_tx_list(struct ural_softc *sc)
632184610Salfred{
633188417Sthompsa	struct ural_tx_data *data;
634188417Sthompsa	int i;
635184610Salfred
636188417Sthompsa	sc->tx_nfree = 0;
637188417Sthompsa	STAILQ_INIT(&sc->tx_q);
638188417Sthompsa	STAILQ_INIT(&sc->tx_free);
639184610Salfred
640188417Sthompsa	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
641188417Sthompsa		data = &sc->tx_data[i];
642184610Salfred
643188417Sthompsa		data->sc = sc;
644188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
645188417Sthompsa		sc->tx_nfree++;
646184610Salfred	}
647184610Salfred}
648184610Salfred
649184610Salfredstatic void
650188419Sthompsaural_unsetup_tx_list(struct ural_softc *sc)
651184610Salfred{
652188417Sthompsa	struct ural_tx_data *data;
653188417Sthompsa	int i;
654184610Salfred
655188419Sthompsa	/* make sure any subsequent use of the queues will fail */
656188419Sthompsa	sc->tx_nfree = 0;
657188419Sthompsa	STAILQ_INIT(&sc->tx_q);
658188419Sthompsa	STAILQ_INIT(&sc->tx_free);
659184610Salfred
660188419Sthompsa	/* free up all node references and mbufs */
661188417Sthompsa	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
662188417Sthompsa		data = &sc->tx_data[i];
663184610Salfred
664188417Sthompsa		if (data->m != NULL) {
665188417Sthompsa			m_freem(data->m);
666188417Sthompsa			data->m = NULL;
667188417Sthompsa		}
668188417Sthompsa		if (data->ni != NULL) {
669188417Sthompsa			ieee80211_free_node(data->ni);
670188417Sthompsa			data->ni = NULL;
671188417Sthompsa		}
672184610Salfred	}
673184610Salfred}
674184610Salfred
675184610Salfredstatic void
676188417Sthompsaural_task(struct usb2_proc_msg *pm)
677184610Salfred{
678188417Sthompsa	struct ural_task *task = (struct ural_task *)pm;
679188417Sthompsa	struct ural_softc *sc = task->sc;
680188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
681188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
682188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
683188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
684188417Sthompsa	const struct ieee80211_txparam *tp;
685188417Sthompsa	enum ieee80211_state ostate;
686188417Sthompsa	struct ieee80211_node *ni;
687188417Sthompsa	struct mbuf *m;
688184610Salfred
689188417Sthompsa	ostate = vap->iv_state;
690184610Salfred
691188417Sthompsa	switch (sc->sc_state) {
692188417Sthompsa	case IEEE80211_S_INIT:
693188417Sthompsa		if (ostate == IEEE80211_S_RUN) {
694188417Sthompsa			/* abort TSF synchronization */
695188417Sthompsa			ural_write(sc, RAL_TXRX_CSR19, 0);
696184610Salfred
697188417Sthompsa			/* force tx led to stop blinking */
698188417Sthompsa			ural_write(sc, RAL_MAC_CSR20, 0);
699188417Sthompsa		}
700188417Sthompsa		break;
701184610Salfred
702188417Sthompsa	case IEEE80211_S_RUN:
703188417Sthompsa		ni = vap->iv_bss;
704188417Sthompsa
705188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
706188417Sthompsa			ural_update_slot(ic->ic_ifp);
707188417Sthompsa			ural_set_txpreamble(sc);
708188417Sthompsa			ural_set_basicrates(sc, ic->ic_bsschan);
709188419Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
710188419Sthompsa			ural_set_bssid(sc, sc->sc_bssid);
711188417Sthompsa		}
712188417Sthompsa
713188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
714188417Sthompsa		    vap->iv_opmode == IEEE80211_M_IBSS) {
715188417Sthompsa			m = ieee80211_beacon_alloc(ni, &uvp->bo);
716188417Sthompsa			if (m == NULL) {
717188417Sthompsa				device_printf(sc->sc_dev,
718188417Sthompsa				    "could not allocate beacon\n");
719188417Sthompsa				return;
720184610Salfred			}
721188417Sthompsa
722188417Sthompsa			if (ural_tx_bcn(sc, m, ni) != 0) {
723188417Sthompsa				device_printf(sc->sc_dev,
724188417Sthompsa				    "could not send beacon\n");
725184610Salfred				return;
726184610Salfred			}
727184610Salfred		}
728184610Salfred
729188417Sthompsa		/* make tx led blink on tx (controlled by ASIC) */
730188417Sthompsa		ural_write(sc, RAL_MAC_CSR20, 1);
731184610Salfred
732188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR)
733188417Sthompsa			ural_enable_tsf_sync(sc);
734184610Salfred
735188417Sthompsa		/* enable automatic rate adaptation */
736188417Sthompsa		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
737188417Sthompsa		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
738188417Sthompsa			ural_amrr_start(sc, ni);
739184610Salfred
740188417Sthompsa		break;
741184610Salfred
742188417Sthompsa	default:
743188417Sthompsa		break;
744188417Sthompsa	}
745184610Salfred
746188417Sthompsa	RAL_UNLOCK(sc);
747188417Sthompsa	IEEE80211_LOCK(ic);
748188417Sthompsa	uvp->newstate(vap, sc->sc_state, sc->sc_arg);
749188417Sthompsa	if (vap->iv_newstate_cb != NULL)
750188417Sthompsa		vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg);
751188417Sthompsa	IEEE80211_UNLOCK(ic);
752188417Sthompsa	RAL_LOCK(sc);
753188417Sthompsa}
754184610Salfred
755188417Sthompsastatic void
756188417Sthompsaural_scantask(struct usb2_proc_msg *pm)
757188417Sthompsa{
758188417Sthompsa	struct ural_task *task = (struct ural_task *)pm;
759188417Sthompsa	struct ural_softc *sc = task->sc;
760188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
761188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
762184610Salfred
763188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
764184610Salfred
765188419Sthompsa	switch (sc->sc_scan_action) {
766188419Sthompsa	case URAL_SCAN_START:
767188417Sthompsa		/* abort TSF synchronization */
768188417Sthompsa		DPRINTF("starting scan\n");
769188417Sthompsa		ural_write(sc, RAL_TXRX_CSR19, 0);
770188417Sthompsa		ural_set_bssid(sc, ifp->if_broadcastaddr);
771188419Sthompsa		break;
772188419Sthompsa
773188419Sthompsa	case URAL_SET_CHANNEL:
774188417Sthompsa		ural_set_chan(sc, ic->ic_curchan);
775188419Sthompsa		break;
776188419Sthompsa
777188419Sthompsa	default: /* URAL_SCAN_END */
778188417Sthompsa		DPRINTF("stopping scan\n");
779188417Sthompsa		ural_enable_tsf_sync(sc);
780188419Sthompsa		ural_set_bssid(sc, sc->sc_bssid);
781188419Sthompsa		break;
782188419Sthompsa	}
783188417Sthompsa}
784184610Salfred
785188417Sthompsastatic int
786188417Sthompsaural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
787188417Sthompsa{
788188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
789188417Sthompsa	struct ieee80211com *ic = vap->iv_ic;
790188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
791184610Salfred
792188417Sthompsa	DPRINTF("%s -> %s\n",
793188417Sthompsa		ieee80211_state_name[vap->iv_state],
794188417Sthompsa		ieee80211_state_name[nstate]);
795184610Salfred
796188417Sthompsa	RAL_LOCK(sc);
797188417Sthompsa	usb2_callout_stop(&uvp->amrr_ch);
798184610Salfred
799188417Sthompsa	/* do it in a process context */
800188417Sthompsa	sc->sc_state = nstate;
801188417Sthompsa	sc->sc_arg = arg;
802188417Sthompsa	RAL_UNLOCK(sc);
803184610Salfred
804188417Sthompsa	if (nstate == IEEE80211_S_INIT) {
805188417Sthompsa		uvp->newstate(vap, nstate, arg);
806188417Sthompsa		return 0;
807188417Sthompsa	} else {
808188417Sthompsa		RAL_LOCK(sc);
809188417Sthompsa		ural_queue_command(sc, ural_task, &sc->sc_task[0].hdr,
810188417Sthompsa		    &sc->sc_task[1].hdr);
811188417Sthompsa		RAL_UNLOCK(sc);
812188417Sthompsa		return EINPROGRESS;
813184610Salfred	}
814188417Sthompsa}
815184610Salfred
816184610Salfred
817188417Sthompsastatic void
818188417Sthompsaural_bulk_write_callback(struct usb2_xfer *xfer)
819188417Sthompsa{
820188417Sthompsa	struct ural_softc *sc = xfer->priv_sc;
821188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
822188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
823188417Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
824188417Sthompsa	struct ural_tx_data *data;
825188417Sthompsa	struct mbuf *m;
826188419Sthompsa	unsigned int len;
827184610Salfred
828188417Sthompsa	switch (USB_GET_STATE(xfer)) {
829188417Sthompsa	case USB_ST_TRANSFERRED:
830188417Sthompsa		DPRINTFN(11, "transfer complete, %d bytes\n", xfer->actlen);
831184610Salfred
832188417Sthompsa		/* free resources */
833188417Sthompsa		data = xfer->priv_fifo;
834188417Sthompsa		ural_tx_free(data, 0);
835188417Sthompsa		xfer->priv_fifo = NULL;
836184610Salfred
837188417Sthompsa		ifp->if_opackets++;
838188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
839184610Salfred
840188417Sthompsa		/* FALLTHROUGH */
841188417Sthompsa	case USB_ST_SETUP:
842188417Sthompsatr_setup:
843188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
844188417Sthompsa		if (data) {
845188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
846188417Sthompsa			m = data->m;
847184610Salfred
848188417Sthompsa			if (m->m_pkthdr.len > (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) {
849188417Sthompsa				DPRINTFN(0, "data overflow, %u bytes\n",
850188417Sthompsa				    m->m_pkthdr.len);
851188417Sthompsa				m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE);
852188417Sthompsa			}
853188417Sthompsa			usb2_copy_in(xfer->frbuffers, 0, &data->desc,
854188417Sthompsa			    RAL_TX_DESC_SIZE);
855188417Sthompsa			usb2_m_copy_in(xfer->frbuffers, RAL_TX_DESC_SIZE, m, 0,
856188417Sthompsa			    m->m_pkthdr.len);
857184610Salfred
858188417Sthompsa			if (bpf_peers_present(ifp->if_bpf)) {
859188417Sthompsa				struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
860184610Salfred
861188417Sthompsa				tap->wt_flags = 0;
862188417Sthompsa				tap->wt_rate = data->rate;
863188417Sthompsa				tap->wt_chan_freq = htole16(c->ic_freq);
864188417Sthompsa				tap->wt_chan_flags = htole16(c->ic_flags);
865188417Sthompsa				tap->wt_antenna = sc->tx_ant;
866184610Salfred
867188417Sthompsa				bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m);
868188417Sthompsa			}
869184610Salfred
870188417Sthompsa			/* xfer length needs to be a multiple of two! */
871188417Sthompsa			len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1;
872188417Sthompsa			if ((len % 64) == 0)
873188417Sthompsa				len += 2;
874184610Salfred
875188417Sthompsa			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
876188417Sthompsa			    m->m_pkthdr.len, len);
877184610Salfred
878188417Sthompsa			xfer->frlengths[0] = len;
879188417Sthompsa			xfer->priv_fifo = data;
880184610Salfred
881188417Sthompsa			usb2_start_hardware(xfer);
882188417Sthompsa		}
883188417Sthompsa		break;
884184610Salfred
885188417Sthompsa	default:			/* Error */
886188417Sthompsa		DPRINTFN(11, "transfer error, %s\n",
887188417Sthompsa		    usb2_errstr(xfer->error));
888184610Salfred
889188419Sthompsa		ifp->if_oerrors++;
890188419Sthompsa		data = xfer->priv_fifo;
891188419Sthompsa		if (data != NULL) {
892188419Sthompsa			ural_tx_free(data, xfer->error);
893188419Sthompsa			xfer->priv_fifo = NULL;
894188419Sthompsa		}
895188419Sthompsa
896188417Sthompsa		if (xfer->error == USB_ERR_STALLED) {
897188417Sthompsa			/* try to clear stall first */
898188417Sthompsa			xfer->flags.stall_pipe = 1;
899188417Sthompsa			goto tr_setup;
900188417Sthompsa		}
901188417Sthompsa		if (xfer->error == USB_ERR_TIMEOUT)
902188417Sthompsa			device_printf(sc->sc_dev, "device timeout\n");
903188417Sthompsa		break;
904184610Salfred	}
905184610Salfred}
906184610Salfred
907184610Salfredstatic void
908184610Salfredural_bulk_read_callback(struct usb2_xfer *xfer)
909184610Salfred{
910184610Salfred	struct ural_softc *sc = xfer->priv_sc;
911184610Salfred	struct ifnet *ifp = sc->sc_ifp;
912184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
913184610Salfred	struct ieee80211_node *ni;
914184610Salfred	struct mbuf *m = NULL;
915184610Salfred	uint32_t flags;
916184610Salfred	uint8_t rssi = 0;
917188419Sthompsa	unsigned int len;
918184610Salfred
919184610Salfred	switch (USB_GET_STATE(xfer)) {
920184610Salfred	case USB_ST_TRANSFERRED:
921184610Salfred
922184610Salfred		DPRINTFN(15, "rx done, actlen=%d\n", xfer->actlen);
923184610Salfred
924188417Sthompsa		len = xfer->actlen;
925188417Sthompsa		if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
926188417Sthompsa			DPRINTF("%s: xfer too short %d\n",
927188417Sthompsa			    device_get_nameunit(sc->sc_dev), len);
928184610Salfred			ifp->if_ierrors++;
929184610Salfred			goto tr_setup;
930184610Salfred		}
931184610Salfred
932188417Sthompsa		len -= RAL_RX_DESC_SIZE;
933188417Sthompsa		/* rx descriptor is located at the end */
934188417Sthompsa		usb2_copy_out(xfer->frbuffers, len, &sc->sc_rx_desc,
935188417Sthompsa		    RAL_RX_DESC_SIZE);
936184610Salfred
937188417Sthompsa		rssi = URAL_RSSI(sc->sc_rx_desc.rssi);
938184610Salfred		flags = le32toh(sc->sc_rx_desc.flags);
939184610Salfred		if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
940184610Salfred			/*
941184610Salfred		         * This should not happen since we did not
942184610Salfred		         * request to receive those frames when we
943184610Salfred		         * filled RAL_TXRX_CSR2:
944184610Salfred		         */
945188417Sthompsa			DPRINTFN(5, "PHY or CRC error\n");
946184610Salfred			ifp->if_ierrors++;
947184610Salfred			goto tr_setup;
948184610Salfred		}
949184610Salfred
950184610Salfred		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
951184610Salfred		if (m == NULL) {
952184610Salfred			DPRINTF("could not allocate mbuf\n");
953184610Salfred			ifp->if_ierrors++;
954184610Salfred			goto tr_setup;
955184610Salfred		}
956188417Sthompsa		usb2_copy_out(xfer->frbuffers, 0, mtod(m, uint8_t *), len);
957184610Salfred
958184610Salfred		/* finalize mbuf */
959184610Salfred		m->m_pkthdr.rcvif = ifp;
960188417Sthompsa		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
961184610Salfred
962184610Salfred		if (bpf_peers_present(ifp->if_bpf)) {
963184610Salfred			struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
964184610Salfred
965184610Salfred			tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
966184610Salfred			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
967188417Sthompsa			    (flags & RAL_RX_OFDM) ?
968184610Salfred			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
969184610Salfred			tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
970184610Salfred			tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
971188417Sthompsa			tap->wr_antenna = sc->rx_ant;
972184610Salfred			tap->wr_antsignal = rssi;
973184610Salfred
974184610Salfred			bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
975184610Salfred		}
976184610Salfred		/* Strip trailing 802.11 MAC FCS. */
977184610Salfred		m_adj(m, -IEEE80211_CRC_LEN);
978184610Salfred
979188417Sthompsa		/* FALLTHROUGH */
980184610Salfred	case USB_ST_SETUP:
981184610Salfredtr_setup:
982188417Sthompsa		xfer->frlengths[0] = xfer->max_data_length;
983188417Sthompsa		usb2_start_hardware(xfer);
984184610Salfred
985184610Salfred		/*
986184610Salfred		 * At the end of a USB callback it is always safe to unlock
987184610Salfred		 * the private mutex of a device! That is why we do the
988184610Salfred		 * "ieee80211_input" here, and not some lines up!
989184610Salfred		 */
990184610Salfred		if (m) {
991188417Sthompsa			RAL_UNLOCK(sc);
992188417Sthompsa			ni = ieee80211_find_rxnode(ic,
993188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
994188417Sthompsa			if (ni != NULL) {
995188417Sthompsa				(void) ieee80211_input(ni, m, rssi,
996188417Sthompsa				    RAL_NOISE_FLOOR, 0);
997184610Salfred				ieee80211_free_node(ni);
998188417Sthompsa			} else
999188417Sthompsa				(void) ieee80211_input_all(ic, m, rssi,
1000188417Sthompsa				    RAL_NOISE_FLOOR, 0);
1001188417Sthompsa			RAL_LOCK(sc);
1002184610Salfred		}
1003184610Salfred		return;
1004184610Salfred
1005184610Salfred	default:			/* Error */
1006184610Salfred		if (xfer->error != USB_ERR_CANCELLED) {
1007184610Salfred			/* try to clear stall first */
1008188417Sthompsa			xfer->flags.stall_pipe = 1;
1009188417Sthompsa			goto tr_setup;
1010184610Salfred		}
1011184610Salfred		return;
1012184610Salfred	}
1013184610Salfred}
1014184610Salfred
1015184610Salfredstatic uint8_t
1016188417Sthompsaural_plcp_signal(int rate)
1017184610Salfred{
1018184610Salfred	switch (rate) {
1019188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1020188417Sthompsa	case 12:	return 0xb;
1021188417Sthompsa	case 18:	return 0xf;
1022188417Sthompsa	case 24:	return 0xa;
1023188417Sthompsa	case 36:	return 0xe;
1024188417Sthompsa	case 48:	return 0x9;
1025188417Sthompsa	case 72:	return 0xd;
1026188417Sthompsa	case 96:	return 0x8;
1027188417Sthompsa	case 108:	return 0xc;
1028184610Salfred
1029188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
1030188417Sthompsa	case 2:		return 0x0;
1031188417Sthompsa	case 4:		return 0x1;
1032188417Sthompsa	case 11:	return 0x2;
1033188417Sthompsa	case 22:	return 0x3;
1034184610Salfred	}
1035188417Sthompsa	return 0xff;		/* XXX unsupported/unknown rate */
1036184610Salfred}
1037184610Salfred
1038184610Salfredstatic void
1039188417Sthompsaural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1040188417Sthompsa    uint32_t flags, int len, int rate)
1041184610Salfred{
1042184610Salfred	struct ifnet *ifp = sc->sc_ifp;
1043184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1044184610Salfred	uint16_t plcp_length;
1045188417Sthompsa	int remainder;
1046184610Salfred
1047188417Sthompsa	desc->flags = htole32(flags);
1048188417Sthompsa	desc->flags |= htole32(RAL_TX_NEWSEQ);
1049188417Sthompsa	desc->flags |= htole32(len << 16);
1050184610Salfred
1051188417Sthompsa	desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1052188417Sthompsa	desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1053184610Salfred
1054188417Sthompsa	/* setup PLCP fields */
1055188417Sthompsa	desc->plcp_signal  = ural_plcp_signal(rate);
1056188417Sthompsa	desc->plcp_service = 4;
1057188417Sthompsa
1058188417Sthompsa	len += IEEE80211_CRC_LEN;
1059188417Sthompsa	if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
1060188417Sthompsa		desc->flags |= htole32(RAL_TX_OFDM);
1061188417Sthompsa
1062188417Sthompsa		plcp_length = len & 0xfff;
1063188417Sthompsa		desc->plcp_length_hi = plcp_length >> 6;
1064188417Sthompsa		desc->plcp_length_lo = plcp_length & 0x3f;
1065188417Sthompsa	} else {
1066188417Sthompsa		plcp_length = (16 * len + rate - 1) / rate;
1067188417Sthompsa		if (rate == 22) {
1068188417Sthompsa			remainder = (16 * len) % 22;
1069188417Sthompsa			if (remainder != 0 && remainder < 7)
1070188417Sthompsa				desc->plcp_service |= RAL_PLCP_LENGEXT;
1071188417Sthompsa		}
1072188417Sthompsa		desc->plcp_length_hi = plcp_length >> 8;
1073188417Sthompsa		desc->plcp_length_lo = plcp_length & 0xff;
1074188417Sthompsa
1075188417Sthompsa		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1076188417Sthompsa			desc->plcp_signal |= 0x08;
1077184610Salfred	}
1078184610Salfred
1079188417Sthompsa	desc->iv = 0;
1080188417Sthompsa	desc->eiv = 0;
1081188417Sthompsa}
1082184610Salfred
1083188417Sthompsa#define RAL_TX_TIMEOUT	5000
1084184610Salfred
1085188417Sthompsastatic int
1086188417Sthompsaural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1087188417Sthompsa{
1088188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1089188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1090188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1091188417Sthompsa	const struct ieee80211_txparam *tp;
1092188417Sthompsa	struct ural_tx_data *data;
1093188417Sthompsa
1094188417Sthompsa	if (sc->tx_nfree == 0) {
1095188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1096188417Sthompsa		m_freem(m0);
1097188417Sthompsa		ieee80211_free_node(ni);
1098188417Sthompsa		return EIO;
1099184610Salfred	}
1100188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1101188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1102188417Sthompsa	sc->tx_nfree--;
1103188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1104184610Salfred
1105188417Sthompsa	data->m = m0;
1106188417Sthompsa	data->ni = ni;
1107188417Sthompsa	data->rate = tp->mgmtrate;
1108184610Salfred
1109188417Sthompsa	ural_setup_tx_desc(sc, &data->desc,
1110188417Sthompsa	    RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len,
1111188417Sthompsa	    tp->mgmtrate);
1112184610Salfred
1113188417Sthompsa	DPRINTFN(10, "sending beacon frame len=%u rate=%u\n",
1114188417Sthompsa	    m0->m_pkthdr.len, tp->mgmtrate);
1115184610Salfred
1116188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1117188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1118184610Salfred
1119188417Sthompsa	return (0);
1120188417Sthompsa}
1121184610Salfred
1122188417Sthompsastatic int
1123188417Sthompsaural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1124188417Sthompsa{
1125188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1126188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1127188417Sthompsa	const struct ieee80211_txparam *tp;
1128188417Sthompsa	struct ural_tx_data *data;
1129188417Sthompsa	struct ieee80211_frame *wh;
1130188417Sthompsa	struct ieee80211_key *k;
1131188417Sthompsa	uint32_t flags;
1132188417Sthompsa	uint16_t dur;
1133184610Salfred
1134188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1135184610Salfred
1136188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1137188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1138188417Sthompsa	sc->tx_nfree--;
1139184610Salfred
1140188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1141188417Sthompsa
1142188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1143188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1144188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1145188417Sthompsa		if (k == NULL) {
1146188417Sthompsa			m_freem(m0);
1147188417Sthompsa			return ENOBUFS;
1148184610Salfred		}
1149188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1150184610Salfred	}
1151184610Salfred
1152188417Sthompsa	data->m = m0;
1153188417Sthompsa	data->ni = ni;
1154188417Sthompsa	data->rate = tp->mgmtrate;
1155184610Salfred
1156188417Sthompsa	flags = 0;
1157188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1158188417Sthompsa		flags |= RAL_TX_ACK;
1159188417Sthompsa
1160188417Sthompsa		dur = ieee80211_ack_duration(sc->sc_rates, tp->mgmtrate,
1161188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1162188417Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
1163188417Sthompsa
1164188417Sthompsa		/* tell hardware to add timestamp for probe responses */
1165188417Sthompsa		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1166188417Sthompsa		    IEEE80211_FC0_TYPE_MGT &&
1167188417Sthompsa		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1168188417Sthompsa		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1169188417Sthompsa			flags |= RAL_TX_TIMESTAMP;
1170184610Salfred	}
1171184610Salfred
1172188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate);
1173184610Salfred
1174188417Sthompsa	DPRINTFN(10, "sending mgt frame len=%u rate=%u\n",
1175188417Sthompsa	    m0->m_pkthdr.len, tp->mgmtrate);
1176184610Salfred
1177188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1178188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1179184610Salfred
1180188417Sthompsa	return 0;
1181184610Salfred}
1182184610Salfred
1183188417Sthompsastatic int
1184188417Sthompsaural_sendprot(struct ural_softc *sc,
1185188417Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1186184610Salfred{
1187188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1188188417Sthompsa	const struct ieee80211_frame *wh;
1189188417Sthompsa	struct ural_tx_data *data;
1190188417Sthompsa	struct mbuf *mprot;
1191188417Sthompsa	int protrate, ackrate, pktlen, flags, isshort;
1192188417Sthompsa	uint16_t dur;
1193184610Salfred
1194188417Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1195188417Sthompsa	    ("protection %d", prot));
1196184610Salfred
1197188417Sthompsa	wh = mtod(m, const struct ieee80211_frame *);
1198188417Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1199184610Salfred
1200188417Sthompsa	protrate = ieee80211_ctl_rate(sc->sc_rates, rate);
1201188417Sthompsa	ackrate = ieee80211_ack_rate(sc->sc_rates, rate);
1202188417Sthompsa
1203188417Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1204188417Sthompsa	dur = ieee80211_compute_duration(sc->sc_rates, pktlen, rate, isshort);
1205188417Sthompsa	    + ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1206188417Sthompsa	flags = RAL_TX_RETRY(7);
1207188417Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
1208188417Sthompsa		/* NB: CTS is the same size as an ACK */
1209188417Sthompsa		dur += ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1210188417Sthompsa		flags |= RAL_TX_ACK;
1211188417Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1212188417Sthompsa	} else {
1213188417Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1214188417Sthompsa	}
1215188417Sthompsa	if (mprot == NULL) {
1216188417Sthompsa		/* XXX stat + msg */
1217188417Sthompsa		return ENOBUFS;
1218188417Sthompsa	}
1219188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1220188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1221188417Sthompsa	sc->tx_nfree--;
1222188417Sthompsa
1223188417Sthompsa	data->m = mprot;
1224188417Sthompsa	data->ni = ieee80211_ref_node(ni);
1225188417Sthompsa	data->rate = protrate;
1226188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate);
1227188417Sthompsa
1228188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1229188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1230188417Sthompsa
1231188417Sthompsa	return 0;
1232188417Sthompsa}
1233188417Sthompsa
1234188417Sthompsastatic int
1235188417Sthompsaural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1236188417Sthompsa    const struct ieee80211_bpf_params *params)
1237188417Sthompsa{
1238188417Sthompsa	struct ural_tx_data *data;
1239188417Sthompsa	uint32_t flags;
1240188417Sthompsa	int error;
1241188417Sthompsa	int rate;
1242188417Sthompsa
1243188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1244188417Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
1245188417Sthompsa
1246188417Sthompsa	rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1247188417Sthompsa	/* XXX validate */
1248188417Sthompsa	if (rate == 0) {
1249188417Sthompsa		m_freem(m0);
1250188417Sthompsa		return EINVAL;
1251188417Sthompsa	}
1252188417Sthompsa	flags = 0;
1253188417Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1254188417Sthompsa		flags |= RAL_TX_ACK;
1255188417Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1256188417Sthompsa		error = ural_sendprot(sc, m0, ni,
1257188417Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
1258188417Sthompsa			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1259188417Sthompsa		    rate);
1260188969Sthompsa		if (error || sc->tx_nfree == 0) {
1261188417Sthompsa			m_freem(m0);
1262188969Sthompsa			return ENOBUFS;
1263184610Salfred		}
1264188417Sthompsa		flags |= RAL_TX_IFS_SIFS;
1265188417Sthompsa	}
1266184610Salfred
1267188969Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1268188969Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1269188969Sthompsa	sc->tx_nfree--;
1270188969Sthompsa
1271188417Sthompsa	data->m = m0;
1272188417Sthompsa	data->ni = ni;
1273188417Sthompsa	data->rate = rate;
1274184610Salfred
1275188417Sthompsa	/* XXX need to setup descriptor ourself */
1276188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1277184610Salfred
1278188417Sthompsa	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1279188417Sthompsa	    m0->m_pkthdr.len, rate);
1280184610Salfred
1281188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1282188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1283184610Salfred
1284188417Sthompsa	return 0;
1285188417Sthompsa}
1286184610Salfred
1287188417Sthompsastatic int
1288188417Sthompsaural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1289188417Sthompsa{
1290188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1291188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1292188417Sthompsa	struct ural_tx_data *data;
1293188417Sthompsa	struct ieee80211_frame *wh;
1294188417Sthompsa	const struct ieee80211_txparam *tp;
1295188417Sthompsa	struct ieee80211_key *k;
1296188417Sthompsa	uint32_t flags = 0;
1297188417Sthompsa	uint16_t dur;
1298188417Sthompsa	int error, rate;
1299184610Salfred
1300188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1301184610Salfred
1302188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1303184610Salfred
1304188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1305188417Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1306188417Sthompsa		rate = tp->mcastrate;
1307188417Sthompsa	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1308188417Sthompsa		rate = tp->ucastrate;
1309188417Sthompsa	else
1310188417Sthompsa		rate = ni->ni_txrate;
1311188417Sthompsa
1312188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1313188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1314188417Sthompsa		if (k == NULL) {
1315188417Sthompsa			m_freem(m0);
1316188417Sthompsa			return ENOBUFS;
1317184610Salfred		}
1318188417Sthompsa		/* packet header may have moved, reset our local pointer */
1319188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1320188417Sthompsa	}
1321184610Salfred
1322188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1323188417Sthompsa		int prot = IEEE80211_PROT_NONE;
1324188417Sthompsa		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1325188417Sthompsa			prot = IEEE80211_PROT_RTSCTS;
1326188417Sthompsa		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1327188417Sthompsa		    ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
1328188417Sthompsa			prot = ic->ic_protmode;
1329188417Sthompsa		if (prot != IEEE80211_PROT_NONE) {
1330188417Sthompsa			error = ural_sendprot(sc, m0, ni, prot, rate);
1331188969Sthompsa			if (error || sc->tx_nfree == 0) {
1332188417Sthompsa				m_freem(m0);
1333188969Sthompsa				return ENOBUFS;
1334188417Sthompsa			}
1335188417Sthompsa			flags |= RAL_TX_IFS_SIFS;
1336184610Salfred		}
1337184610Salfred	}
1338184610Salfred
1339188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1340188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1341188417Sthompsa	sc->tx_nfree--;
1342184610Salfred
1343188417Sthompsa	data->m = m0;
1344188417Sthompsa	data->ni = ni;
1345188417Sthompsa	data->rate = rate;
1346188417Sthompsa
1347188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1348188417Sthompsa		flags |= RAL_TX_ACK;
1349188417Sthompsa		flags |= RAL_TX_RETRY(7);
1350188417Sthompsa
1351188417Sthompsa		dur = ieee80211_ack_duration(sc->sc_rates, rate,
1352188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1353188417Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
1354184610Salfred	}
1355184610Salfred
1356188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1357184610Salfred
1358188417Sthompsa	DPRINTFN(10, "sending data frame len=%u rate=%u\n",
1359188417Sthompsa	    m0->m_pkthdr.len, rate);
1360184610Salfred
1361188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1362188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1363188417Sthompsa
1364188417Sthompsa	return 0;
1365184610Salfred}
1366184610Salfred
1367184610Salfredstatic void
1368188417Sthompsaural_start(struct ifnet *ifp)
1369184610Salfred{
1370188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
1371188417Sthompsa	struct ieee80211_node *ni;
1372188417Sthompsa	struct mbuf *m;
1373184610Salfred
1374188417Sthompsa	RAL_LOCK(sc);
1375188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1376188417Sthompsa		RAL_UNLOCK(sc);
1377188417Sthompsa		return;
1378188417Sthompsa	}
1379188417Sthompsa	for (;;) {
1380188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1381188417Sthompsa		if (m == NULL)
1382188417Sthompsa			break;
1383188969Sthompsa		if (sc->tx_nfree < RAL_TX_MINFREE) {
1384188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1385188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1386188417Sthompsa			break;
1387188417Sthompsa		}
1388188417Sthompsa		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1389188417Sthompsa		m = ieee80211_encap(ni, m);
1390188417Sthompsa		if (m == NULL) {
1391188417Sthompsa			ieee80211_free_node(ni);
1392188417Sthompsa			continue;
1393188417Sthompsa		}
1394188417Sthompsa		if (ural_tx_data(sc, m, ni) != 0) {
1395188417Sthompsa			ieee80211_free_node(ni);
1396188417Sthompsa			ifp->if_oerrors++;
1397188417Sthompsa			break;
1398188417Sthompsa		}
1399188417Sthompsa	}
1400188417Sthompsa	RAL_UNLOCK(sc);
1401184610Salfred}
1402184610Salfred
1403184610Salfredstatic int
1404188417Sthompsaural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1405184610Salfred{
1406184610Salfred	struct ural_softc *sc = ifp->if_softc;
1407184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1408188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
1409188417Sthompsa	int error = 0, startall = 0;
1410184610Salfred
1411184610Salfred	switch (cmd) {
1412184610Salfred	case SIOCSIFFLAGS:
1413188417Sthompsa		RAL_LOCK(sc);
1414184610Salfred		if (ifp->if_flags & IFF_UP) {
1415188417Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1416188419Sthompsa				ural_queue_command(sc, ural_init_task,
1417188419Sthompsa				    &sc->sc_synctask[0].hdr,
1418188419Sthompsa				    &sc->sc_synctask[1].hdr);
1419188417Sthompsa				startall = 1;
1420188417Sthompsa			} else
1421188417Sthompsa				ural_queue_command(sc, ural_promisctask,
1422188417Sthompsa				    &sc->sc_promisctask[0].hdr,
1423188417Sthompsa				    &sc->sc_promisctask[1].hdr);
1424184610Salfred		} else {
1425188419Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1426188419Sthompsa				ural_queue_command(sc, ural_stop_task,
1427188419Sthompsa				    &sc->sc_synctask[0].hdr,
1428188419Sthompsa				    &sc->sc_synctask[1].hdr);
1429188419Sthompsa			}
1430184610Salfred		}
1431188417Sthompsa		RAL_UNLOCK(sc);
1432188417Sthompsa		if (startall)
1433188417Sthompsa			ieee80211_start_all(ic);
1434184610Salfred		break;
1435184610Salfred	case SIOCGIFMEDIA:
1436184610Salfred	case SIOCSIFMEDIA:
1437188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1438184610Salfred		break;
1439184610Salfred	default:
1440184610Salfred		error = ether_ioctl(ifp, cmd, data);
1441188417Sthompsa		break;
1442184610Salfred	}
1443188417Sthompsa	return error;
1444184610Salfred}
1445184610Salfred
1446184610Salfredstatic void
1447188417Sthompsaural_set_testmode(struct ural_softc *sc)
1448184610Salfred{
1449188417Sthompsa	struct usb2_device_request req;
1450188417Sthompsa	usb2_error_t error;
1451184610Salfred
1452188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1453188417Sthompsa	req.bRequest = RAL_VENDOR_REQUEST;
1454188417Sthompsa	USETW(req.wValue, 4);
1455188417Sthompsa	USETW(req.wIndex, 1);
1456188417Sthompsa	USETW(req.wLength, 0);
1457188417Sthompsa
1458188419Sthompsa	error = ural_do_request(sc, &req, NULL);
1459188417Sthompsa	if (error != 0) {
1460188417Sthompsa		device_printf(sc->sc_dev, "could not set test mode: %s\n",
1461188417Sthompsa		    usb2_errstr(error));
1462188417Sthompsa	}
1463184610Salfred}
1464184610Salfred
1465184610Salfredstatic void
1466188417Sthompsaural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1467184610Salfred{
1468188417Sthompsa	struct usb2_device_request req;
1469188417Sthompsa	usb2_error_t error;
1470184610Salfred
1471188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1472188417Sthompsa	req.bRequest = RAL_READ_EEPROM;
1473188417Sthompsa	USETW(req.wValue, 0);
1474188417Sthompsa	USETW(req.wIndex, addr);
1475188417Sthompsa	USETW(req.wLength, len);
1476184610Salfred
1477188419Sthompsa	error = ural_do_request(sc, &req, buf);
1478188417Sthompsa	if (error != 0) {
1479188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1480188417Sthompsa		    usb2_errstr(error));
1481184610Salfred	}
1482188417Sthompsa}
1483184610Salfred
1484188417Sthompsastatic uint16_t
1485188417Sthompsaural_read(struct ural_softc *sc, uint16_t reg)
1486188417Sthompsa{
1487188417Sthompsa	struct usb2_device_request req;
1488188417Sthompsa	usb2_error_t error;
1489188417Sthompsa	uint16_t val;
1490184610Salfred
1491188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1492188417Sthompsa	req.bRequest = RAL_READ_MAC;
1493188417Sthompsa	USETW(req.wValue, 0);
1494188417Sthompsa	USETW(req.wIndex, reg);
1495188417Sthompsa	USETW(req.wLength, sizeof (uint16_t));
1496188417Sthompsa
1497188419Sthompsa	error = ural_do_request(sc, &req, &val);
1498188417Sthompsa	if (error != 0) {
1499188417Sthompsa		device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1500188417Sthompsa		    usb2_errstr(error));
1501188417Sthompsa		return 0;
1502184610Salfred	}
1503184610Salfred
1504188417Sthompsa	return le16toh(val);
1505184610Salfred}
1506184610Salfred
1507188417Sthompsastatic void
1508188417Sthompsaural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1509184610Salfred{
1510188417Sthompsa	struct usb2_device_request req;
1511188417Sthompsa	usb2_error_t error;
1512184610Salfred
1513188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1514188417Sthompsa	req.bRequest = RAL_READ_MULTI_MAC;
1515188417Sthompsa	USETW(req.wValue, 0);
1516188417Sthompsa	USETW(req.wIndex, reg);
1517188417Sthompsa	USETW(req.wLength, len);
1518184610Salfred
1519188419Sthompsa	error = ural_do_request(sc, &req, buf);
1520188417Sthompsa	if (error != 0) {
1521188417Sthompsa		device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1522188417Sthompsa		    usb2_errstr(error));
1523184610Salfred	}
1524188417Sthompsa}
1525188417Sthompsa
1526188417Sthompsastatic void
1527188417Sthompsaural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1528188417Sthompsa{
1529188417Sthompsa	struct usb2_device_request req;
1530188417Sthompsa	usb2_error_t error;
1531188417Sthompsa
1532188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1533188417Sthompsa	req.bRequest = RAL_WRITE_MAC;
1534188417Sthompsa	USETW(req.wValue, val);
1535188417Sthompsa	USETW(req.wIndex, reg);
1536188417Sthompsa	USETW(req.wLength, 0);
1537188417Sthompsa
1538188419Sthompsa	error = ural_do_request(sc, &req, NULL);
1539188417Sthompsa	if (error != 0) {
1540188417Sthompsa		device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1541188417Sthompsa		    usb2_errstr(error));
1542184610Salfred	}
1543188417Sthompsa}
1544184610Salfred
1545188417Sthompsastatic void
1546188417Sthompsaural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1547188417Sthompsa{
1548188417Sthompsa	struct usb2_device_request req;
1549188417Sthompsa	usb2_error_t error;
1550184610Salfred
1551188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1552188417Sthompsa	req.bRequest = RAL_WRITE_MULTI_MAC;
1553188417Sthompsa	USETW(req.wValue, 0);
1554188417Sthompsa	USETW(req.wIndex, reg);
1555188417Sthompsa	USETW(req.wLength, len);
1556184610Salfred
1557188419Sthompsa	error = ural_do_request(sc, &req, buf);
1558188417Sthompsa	if (error != 0) {
1559188417Sthompsa		device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1560188417Sthompsa		    usb2_errstr(error));
1561188417Sthompsa	}
1562184610Salfred}
1563184610Salfred
1564184610Salfredstatic void
1565188417Sthompsaural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1566184610Salfred{
1567188417Sthompsa	uint16_t tmp;
1568188417Sthompsa	int ntries;
1569184610Salfred
1570188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1571188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1572188417Sthompsa			break;
1573188619Sthompsa		if (ural_pause(sc, hz / 100))
1574188619Sthompsa			break;
1575188417Sthompsa	}
1576188619Sthompsa	if (ntries == 100) {
1577188417Sthompsa		device_printf(sc->sc_dev, "could not write to BBP\n");
1578188417Sthompsa		return;
1579188417Sthompsa	}
1580184610Salfred
1581188417Sthompsa	tmp = reg << 8 | val;
1582188417Sthompsa	ural_write(sc, RAL_PHY_CSR7, tmp);
1583188417Sthompsa}
1584184610Salfred
1585188417Sthompsastatic uint8_t
1586188417Sthompsaural_bbp_read(struct ural_softc *sc, uint8_t reg)
1587188417Sthompsa{
1588188417Sthompsa	uint16_t val;
1589188417Sthompsa	int ntries;
1590184610Salfred
1591188417Sthompsa	val = RAL_BBP_WRITE | reg << 8;
1592188417Sthompsa	ural_write(sc, RAL_PHY_CSR7, val);
1593188417Sthompsa
1594188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1595188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1596188417Sthompsa			break;
1597188619Sthompsa		if (ural_pause(sc, hz / 100))
1598188619Sthompsa			break;
1599188417Sthompsa	}
1600188619Sthompsa	if (ntries == 100) {
1601188417Sthompsa		device_printf(sc->sc_dev, "could not read BBP\n");
1602188417Sthompsa		return 0;
1603188417Sthompsa	}
1604188417Sthompsa
1605188417Sthompsa	return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1606184610Salfred}
1607184610Salfred
1608184610Salfredstatic void
1609188417Sthompsaural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1610184610Salfred{
1611188417Sthompsa	uint32_t tmp;
1612188417Sthompsa	int ntries;
1613188417Sthompsa
1614188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1615188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1616188417Sthompsa			break;
1617188619Sthompsa		if (ural_pause(sc, hz / 100))
1618188619Sthompsa			break;
1619188417Sthompsa	}
1620188619Sthompsa	if (ntries == 100) {
1621188417Sthompsa		device_printf(sc->sc_dev, "could not write to RF\n");
1622188417Sthompsa		return;
1623188417Sthompsa	}
1624188417Sthompsa
1625188417Sthompsa	tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1626188417Sthompsa	ural_write(sc, RAL_PHY_CSR9,  tmp & 0xffff);
1627188417Sthompsa	ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1628188417Sthompsa
1629188417Sthompsa	/* remember last written value in sc */
1630188417Sthompsa	sc->rf_regs[reg] = val;
1631188417Sthompsa
1632188417Sthompsa	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
1633184610Salfred}
1634184610Salfred
1635188417Sthompsa/* ARGUSED */
1636188417Sthompsastatic struct ieee80211_node *
1637188417Sthompsaural_node_alloc(struct ieee80211vap *vap __unused,
1638188417Sthompsa	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
1639188417Sthompsa{
1640188417Sthompsa	struct ural_node *un;
1641188417Sthompsa
1642188417Sthompsa	un = malloc(sizeof(struct ural_node), M_80211_NODE, M_NOWAIT | M_ZERO);
1643188417Sthompsa	return un != NULL ? &un->ni : NULL;
1644188417Sthompsa}
1645188417Sthompsa
1646184610Salfredstatic void
1647188417Sthompsaural_newassoc(struct ieee80211_node *ni, int isnew)
1648184610Salfred{
1649188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1650188417Sthompsa
1651188417Sthompsa	ieee80211_amrr_node_init(&URAL_VAP(vap)->amrr, &URAL_NODE(ni)->amn, ni);
1652184610Salfred}
1653184610Salfred
1654184610Salfredstatic void
1655188417Sthompsaural_scan_start(struct ieee80211com *ic)
1656184610Salfred{
1657188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
1658188417Sthompsa
1659188417Sthompsa	RAL_LOCK(sc);
1660188417Sthompsa	/* do it in a process context */
1661188417Sthompsa	sc->sc_scan_action = URAL_SCAN_START;
1662188417Sthompsa	ural_queue_command(sc, ural_scantask,
1663188417Sthompsa	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
1664188417Sthompsa	RAL_UNLOCK(sc);
1665188417Sthompsa
1666184610Salfred}
1667184610Salfred
1668184610Salfredstatic void
1669188417Sthompsaural_scan_end(struct ieee80211com *ic)
1670184610Salfred{
1671188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
1672188417Sthompsa
1673188417Sthompsa	RAL_LOCK(sc);
1674188417Sthompsa	/* do it in a process context */
1675188417Sthompsa	sc->sc_scan_action = URAL_SCAN_END;
1676188417Sthompsa	ural_queue_command(sc, ural_scantask,
1677188417Sthompsa	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
1678188417Sthompsa	RAL_UNLOCK(sc);
1679188417Sthompsa
1680184610Salfred}
1681184610Salfred
1682184610Salfredstatic void
1683188417Sthompsaural_set_channel(struct ieee80211com *ic)
1684184610Salfred{
1685188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
1686188417Sthompsa
1687188417Sthompsa	RAL_LOCK(sc);
1688188417Sthompsa	/* do it in a process context */
1689188417Sthompsa	sc->sc_scan_action = URAL_SET_CHANNEL;
1690188417Sthompsa	ural_queue_command(sc, ural_scantask,
1691188417Sthompsa	    &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr);
1692188417Sthompsa
1693188417Sthompsa	sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
1694188417Sthompsa	RAL_UNLOCK(sc);
1695184610Salfred}
1696184610Salfred
1697184610Salfredstatic void
1698188417Sthompsaural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1699184610Salfred{
1700188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1701188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1702188417Sthompsa	uint8_t power, tmp;
1703188619Sthompsa	int i, chan;
1704184610Salfred
1705188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
1706188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1707188417Sthompsa		return;
1708184610Salfred
1709188417Sthompsa	if (IEEE80211_IS_CHAN_2GHZ(c))
1710188417Sthompsa		power = min(sc->txpow[chan - 1], 31);
1711184610Salfred	else
1712184610Salfred		power = 31;
1713184610Salfred
1714184610Salfred	/* adjust txpower using ifconfig settings */
1715188417Sthompsa	power -= (100 - ic->ic_txpowlimit) / 8;
1716184610Salfred
1717188417Sthompsa	DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power);
1718184610Salfred
1719188417Sthompsa	switch (sc->rf_rev) {
1720184610Salfred	case RAL_RF_2522:
1721188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x00814);
1722188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1723188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1724184610Salfred		break;
1725184610Salfred
1726184610Salfred	case RAL_RF_2523:
1727188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08804);
1728188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1729188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1730188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1731184610Salfred		break;
1732184610Salfred
1733184610Salfred	case RAL_RF_2524:
1734188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x0c808);
1735188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1736188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1737188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1738184610Salfred		break;
1739184610Salfred
1740184610Salfred	case RAL_RF_2525:
1741188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1742188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1743188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1744188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1745184610Salfred
1746188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1747188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1748188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1749188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1750184610Salfred		break;
1751184610Salfred
1752184610Salfred	case RAL_RF_2525E:
1753188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1754188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1755188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1756188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1757184610Salfred		break;
1758184610Salfred
1759184610Salfred	case RAL_RF_2526:
1760188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1761188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1762188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08804);
1763184610Salfred
1764188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1765188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1766188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1767184610Salfred		break;
1768184610Salfred
1769188417Sthompsa	/* dual-band RF */
1770184610Salfred	case RAL_RF_5222:
1771188417Sthompsa		for (i = 0; ural_rf5222[i].chan != chan; i++);
1772188417Sthompsa
1773188417Sthompsa		ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1774188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1775188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1776188417Sthompsa		ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1777184610Salfred		break;
1778184610Salfred	}
1779184610Salfred
1780188417Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1781188417Sthompsa	    (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1782184610Salfred		/* set Japan filter bit for channel 14 */
1783188417Sthompsa		tmp = ural_bbp_read(sc, 70);
1784184610Salfred
1785188417Sthompsa		tmp &= ~RAL_JAPAN_FILTER;
1786188417Sthompsa		if (chan == 14)
1787184610Salfred			tmp |= RAL_JAPAN_FILTER;
1788184610Salfred
1789188417Sthompsa		ural_bbp_write(sc, 70, tmp);
1790184610Salfred
1791184610Salfred		/* clear CRC errors */
1792188417Sthompsa		ural_read(sc, RAL_STA_CSR0);
1793184610Salfred
1794188619Sthompsa		ural_pause(sc, hz / 100);
1795188417Sthompsa		ural_disable_rf_tune(sc);
1796184610Salfred	}
1797188417Sthompsa
1798188417Sthompsa	/* XXX doesn't belong here */
1799184610Salfred	/* update basic rate set */
1800188417Sthompsa	ural_set_basicrates(sc, c);
1801184610Salfred}
1802184610Salfred
1803188417Sthompsa/*
1804188417Sthompsa * Disable RF auto-tuning.
1805188417Sthompsa */
1806184610Salfredstatic void
1807188417Sthompsaural_disable_rf_tune(struct ural_softc *sc)
1808184610Salfred{
1809188417Sthompsa	uint32_t tmp;
1810184610Salfred
1811188417Sthompsa	if (sc->rf_rev != RAL_RF_2523) {
1812188417Sthompsa		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1813188417Sthompsa		ural_rf_write(sc, RAL_RF1, tmp);
1814184610Salfred	}
1815184610Salfred
1816188417Sthompsa	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1817188417Sthompsa	ural_rf_write(sc, RAL_RF3, tmp);
1818184610Salfred
1819188417Sthompsa	DPRINTFN(2, "disabling RF autotune\n");
1820184610Salfred}
1821184610Salfred
1822188417Sthompsa/*
1823188417Sthompsa * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1824188417Sthompsa * synchronization.
1825188417Sthompsa */
1826184610Salfredstatic void
1827188417Sthompsaural_enable_tsf_sync(struct ural_softc *sc)
1828184610Salfred{
1829188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1830188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1831188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1832188417Sthompsa	uint16_t logcwmin, preload, tmp;
1833184610Salfred
1834184610Salfred	/* first, disable TSF synchronization */
1835188417Sthompsa	ural_write(sc, RAL_TXRX_CSR19, 0);
1836184610Salfred
1837188417Sthompsa	tmp = (16 * vap->iv_bss->ni_intval) << 4;
1838188417Sthompsa	ural_write(sc, RAL_TXRX_CSR18, tmp);
1839184610Salfred
1840188417Sthompsa	logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1841188417Sthompsa	preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1842188417Sthompsa	tmp = logcwmin << 12 | preload;
1843188417Sthompsa	ural_write(sc, RAL_TXRX_CSR20, tmp);
1844184610Salfred
1845184610Salfred	/* finally, enable TSF synchronization */
1846184610Salfred	tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1847188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA)
1848184610Salfred		tmp |= RAL_ENABLE_TSF_SYNC(1);
1849184610Salfred	else
1850184610Salfred		tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1851188417Sthompsa	ural_write(sc, RAL_TXRX_CSR19, tmp);
1852184610Salfred
1853184610Salfred	DPRINTF("enabling TSF synchronization\n");
1854184610Salfred}
1855184610Salfred
1856188417Sthompsa#define RAL_RXTX_TURNAROUND	5	/* us */
1857184610Salfredstatic void
1858188417Sthompsaural_update_slot(struct ifnet *ifp)
1859184610Salfred{
1860188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
1861188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1862188417Sthompsa	uint16_t slottime, sifs, eifs;
1863184610Salfred
1864188417Sthompsa	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1865184610Salfred
1866184610Salfred	/*
1867184610Salfred	 * These settings may sound a bit inconsistent but this is what the
1868184610Salfred	 * reference driver does.
1869184610Salfred	 */
1870188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1871184610Salfred		sifs = 16 - RAL_RXTX_TURNAROUND;
1872184610Salfred		eifs = 364;
1873184610Salfred	} else {
1874184610Salfred		sifs = 10 - RAL_RXTX_TURNAROUND;
1875184610Salfred		eifs = 64;
1876184610Salfred	}
1877184610Salfred
1878188417Sthompsa	ural_write(sc, RAL_MAC_CSR10, slottime);
1879188417Sthompsa	ural_write(sc, RAL_MAC_CSR11, sifs);
1880188417Sthompsa	ural_write(sc, RAL_MAC_CSR12, eifs);
1881184610Salfred}
1882184610Salfred
1883184610Salfredstatic void
1884188417Sthompsaural_set_txpreamble(struct ural_softc *sc)
1885184610Salfred{
1886188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1887188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1888184610Salfred	uint16_t tmp;
1889184610Salfred
1890188417Sthompsa	tmp = ural_read(sc, RAL_TXRX_CSR10);
1891184610Salfred
1892188417Sthompsa	tmp &= ~RAL_SHORT_PREAMBLE;
1893188417Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1894184610Salfred		tmp |= RAL_SHORT_PREAMBLE;
1895184610Salfred
1896188417Sthompsa	ural_write(sc, RAL_TXRX_CSR10, tmp);
1897184610Salfred}
1898184610Salfred
1899184610Salfredstatic void
1900188417Sthompsaural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c)
1901184610Salfred{
1902188417Sthompsa	/* XXX wrong, take from rate set */
1903188417Sthompsa	/* update basic rate set */
1904188417Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1905188417Sthompsa		/* 11a basic rates: 6, 12, 24Mbps */
1906188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x150);
1907188417Sthompsa	} else if (IEEE80211_IS_CHAN_ANYG(c)) {
1908188417Sthompsa		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1909188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1910188417Sthompsa	} else {
1911188417Sthompsa		/* 11b basic rates: 1, 2Mbps */
1912188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x3);
1913188417Sthompsa	}
1914184610Salfred}
1915184610Salfred
1916184610Salfredstatic void
1917188417Sthompsaural_set_bssid(struct ural_softc *sc, const uint8_t *bssid)
1918184610Salfred{
1919188417Sthompsa	uint16_t tmp;
1920184610Salfred
1921188417Sthompsa	tmp = bssid[0] | bssid[1] << 8;
1922188417Sthompsa	ural_write(sc, RAL_MAC_CSR5, tmp);
1923188417Sthompsa
1924188417Sthompsa	tmp = bssid[2] | bssid[3] << 8;
1925188417Sthompsa	ural_write(sc, RAL_MAC_CSR6, tmp);
1926188417Sthompsa
1927188417Sthompsa	tmp = bssid[4] | bssid[5] << 8;
1928188417Sthompsa	ural_write(sc, RAL_MAC_CSR7, tmp);
1929188417Sthompsa
1930188417Sthompsa	DPRINTF("setting BSSID to %6D\n", bssid, ":");
1931184610Salfred}
1932184610Salfred
1933184610Salfredstatic void
1934188417Sthompsaural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1935184610Salfred{
1936184610Salfred	uint16_t tmp;
1937184610Salfred
1938188417Sthompsa	tmp = addr[0] | addr[1] << 8;
1939188417Sthompsa	ural_write(sc, RAL_MAC_CSR2, tmp);
1940184610Salfred
1941188417Sthompsa	tmp = addr[2] | addr[3] << 8;
1942188417Sthompsa	ural_write(sc, RAL_MAC_CSR3, tmp);
1943184610Salfred
1944188417Sthompsa	tmp = addr[4] | addr[5] << 8;
1945188417Sthompsa	ural_write(sc, RAL_MAC_CSR4, tmp);
1946184610Salfred
1947188417Sthompsa	DPRINTF("setting MAC address to %6D\n", addr, ":");
1948184610Salfred}
1949184610Salfred
1950184610Salfredstatic void
1951188417Sthompsaural_promisctask(struct usb2_proc_msg *pm)
1952184610Salfred{
1953188417Sthompsa	struct ural_task *task = (struct ural_task *)pm;
1954188417Sthompsa	struct ural_softc *sc = task->sc;
1955188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1956188417Sthompsa	uint32_t tmp;
1957184610Salfred
1958188417Sthompsa	tmp = ural_read(sc, RAL_TXRX_CSR2);
1959184610Salfred
1960188417Sthompsa	tmp &= ~RAL_DROP_NOT_TO_ME;
1961188417Sthompsa	if (!(ifp->if_flags & IFF_PROMISC))
1962188417Sthompsa		tmp |= RAL_DROP_NOT_TO_ME;
1963184610Salfred
1964188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, tmp);
1965184610Salfred
1966188417Sthompsa	DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1967188417Sthompsa	    "entering" : "leaving");
1968184610Salfred}
1969184610Salfred
1970188417Sthompsastatic const char *
1971188417Sthompsaural_get_rf(int rev)
1972184610Salfred{
1973188417Sthompsa	switch (rev) {
1974188417Sthompsa	case RAL_RF_2522:	return "RT2522";
1975188417Sthompsa	case RAL_RF_2523:	return "RT2523";
1976188417Sthompsa	case RAL_RF_2524:	return "RT2524";
1977188417Sthompsa	case RAL_RF_2525:	return "RT2525";
1978188417Sthompsa	case RAL_RF_2525E:	return "RT2525e";
1979188417Sthompsa	case RAL_RF_2526:	return "RT2526";
1980188417Sthompsa	case RAL_RF_5222:	return "RT5222";
1981188417Sthompsa	default:		return "unknown";
1982184610Salfred	}
1983184610Salfred}
1984184610Salfred
1985184610Salfredstatic void
1986188417Sthompsaural_read_eeprom(struct ural_softc *sc)
1987184610Salfred{
1988184610Salfred	uint16_t val;
1989184610Salfred
1990188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1991184610Salfred	val = le16toh(val);
1992188417Sthompsa	sc->rf_rev =   (val >> 11) & 0x7;
1993188417Sthompsa	sc->hw_radio = (val >> 10) & 0x1;
1994188417Sthompsa	sc->led_mode = (val >> 6)  & 0x7;
1995188417Sthompsa	sc->rx_ant =   (val >> 4)  & 0x3;
1996188417Sthompsa	sc->tx_ant =   (val >> 2)  & 0x3;
1997188417Sthompsa	sc->nb_ant =   val & 0x3;
1998184610Salfred
1999184610Salfred	/* read MAC address */
2000188419Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6);
2001184610Salfred
2002184610Salfred	/* read default values for BBP registers */
2003188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2004184610Salfred
2005184610Salfred	/* read Tx power for all b/g channels */
2006188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2007184610Salfred}
2008184610Salfred
2009188417Sthompsastatic int
2010188417Sthompsaural_bbp_init(struct ural_softc *sc)
2011184610Salfred{
2012188417Sthompsa#define N(a)	(sizeof (a) / sizeof ((a)[0]))
2013188417Sthompsa	int i, ntries;
2014184610Salfred
2015188417Sthompsa	/* wait for BBP to be ready */
2016188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
2017188417Sthompsa		if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2018188417Sthompsa			break;
2019188619Sthompsa		if (ural_pause(sc, hz / 100))
2020188619Sthompsa			break;
2021184610Salfred	}
2022188417Sthompsa	if (ntries == 100) {
2023188417Sthompsa		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2024188417Sthompsa		return EIO;
2025188417Sthompsa	}
2026184610Salfred
2027184610Salfred	/* initialize BBP registers to default values */
2028188417Sthompsa	for (i = 0; i < N(ural_def_bbp); i++)
2029188417Sthompsa		ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2030184610Salfred
2031184610Salfred#if 0
2032184610Salfred	/* initialize BBP registers to values stored in EEPROM */
2033184610Salfred	for (i = 0; i < 16; i++) {
2034188417Sthompsa		if (sc->bbp_prom[i].reg == 0xff)
2035184610Salfred			continue;
2036188417Sthompsa		ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2037184610Salfred	}
2038184610Salfred#endif
2039188417Sthompsa
2040188417Sthompsa	return 0;
2041188417Sthompsa#undef N
2042184610Salfred}
2043184610Salfred
2044184610Salfredstatic void
2045188417Sthompsaural_set_txantenna(struct ural_softc *sc, int antenna)
2046184610Salfred{
2047188417Sthompsa	uint16_t tmp;
2048188417Sthompsa	uint8_t tx;
2049184610Salfred
2050188417Sthompsa	tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2051188417Sthompsa	if (antenna == 1)
2052188417Sthompsa		tx |= RAL_BBP_ANTA;
2053188417Sthompsa	else if (antenna == 2)
2054188417Sthompsa		tx |= RAL_BBP_ANTB;
2055188417Sthompsa	else
2056188417Sthompsa		tx |= RAL_BBP_DIVERSITY;
2057184610Salfred
2058188417Sthompsa	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2059188417Sthompsa	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2060188417Sthompsa	    sc->rf_rev == RAL_RF_5222)
2061188417Sthompsa		tx |= RAL_BBP_FLIPIQ;
2062184610Salfred
2063188417Sthompsa	ural_bbp_write(sc, RAL_BBP_TX, tx);
2064184610Salfred
2065188417Sthompsa	/* update values in PHY_CSR5 and PHY_CSR6 */
2066188417Sthompsa	tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2067188417Sthompsa	ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2068184610Salfred
2069188417Sthompsa	tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2070188417Sthompsa	ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2071184610Salfred}
2072184610Salfred
2073184610Salfredstatic void
2074188417Sthompsaural_set_rxantenna(struct ural_softc *sc, int antenna)
2075184610Salfred{
2076188417Sthompsa	uint8_t rx;
2077188417Sthompsa
2078188417Sthompsa	rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2079188417Sthompsa	if (antenna == 1)
2080188417Sthompsa		rx |= RAL_BBP_ANTA;
2081188417Sthompsa	else if (antenna == 2)
2082188417Sthompsa		rx |= RAL_BBP_ANTB;
2083188417Sthompsa	else
2084188417Sthompsa		rx |= RAL_BBP_DIVERSITY;
2085188417Sthompsa
2086188417Sthompsa	/* need to force no I/Q flip for RF 2525e and 2526 */
2087188417Sthompsa	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2088188417Sthompsa		rx &= ~RAL_BBP_FLIPIQ;
2089188417Sthompsa
2090188417Sthompsa	ural_bbp_write(sc, RAL_BBP_RX, rx);
2091188417Sthompsa}
2092188417Sthompsa
2093188417Sthompsastatic void
2094188419Sthompsaural_init_task(struct usb2_proc_msg *pm)
2095188417Sthompsa{
2096188417Sthompsa#define N(a)	(sizeof (a) / sizeof ((a)[0]))
2097188419Sthompsa	struct ural_task *task = (struct ural_task *)pm;
2098188419Sthompsa	struct ural_softc *sc = task->sc;
2099188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2100188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2101184610Salfred	uint16_t tmp;
2102188417Sthompsa	int i, ntries;
2103184610Salfred
2104188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
2105184610Salfred
2106188417Sthompsa	ural_set_testmode(sc);
2107188417Sthompsa	ural_write(sc, 0x308, 0x00f0);	/* XXX magic */
2108184610Salfred
2109188419Sthompsa	ural_stop_task(pm);
2110184610Salfred
2111184610Salfred	/* initialize MAC registers to default values */
2112188417Sthompsa	for (i = 0; i < N(ural_def_mac); i++)
2113188417Sthompsa		ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2114184610Salfred
2115184610Salfred	/* wait for BBP and RF to wake up (this can take a long time!) */
2116188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
2117188417Sthompsa		tmp = ural_read(sc, RAL_MAC_CSR17);
2118188417Sthompsa		if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2119188417Sthompsa		    (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2120188417Sthompsa			break;
2121188619Sthompsa		if (ural_pause(sc, hz / 100))
2122188619Sthompsa			break;
2123184610Salfred	}
2124188417Sthompsa	if (ntries == 100) {
2125188417Sthompsa		device_printf(sc->sc_dev,
2126188417Sthompsa		    "timeout waiting for BBP/RF to wakeup\n");
2127188417Sthompsa		goto fail;
2128188417Sthompsa	}
2129184610Salfred
2130184610Salfred	/* we're ready! */
2131188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2132184610Salfred
2133184610Salfred	/* set basic rate set (will be updated later) */
2134188417Sthompsa	ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2135184610Salfred
2136188417Sthompsa	if (ural_bbp_init(sc) != 0)
2137184610Salfred		goto fail;
2138184610Salfred
2139188417Sthompsa	ural_set_chan(sc, ic->ic_curchan);
2140188417Sthompsa
2141184610Salfred	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
2142188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2143184610Salfred
2144188417Sthompsa	ural_set_txantenna(sc, sc->tx_ant);
2145188417Sthompsa	ural_set_rxantenna(sc, sc->rx_ant);
2146184610Salfred
2147188417Sthompsa	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2148188417Sthompsa	ural_set_macaddr(sc, ic->ic_myaddr);
2149184610Salfred
2150184610Salfred	/*
2151188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2152184610Salfred	 */
2153188419Sthompsa	ural_setup_tx_list(sc);
2154184610Salfred
2155188417Sthompsa	/* kick Rx */
2156184610Salfred	tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2157188417Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2158188417Sthompsa		tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2159188417Sthompsa		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2160184610Salfred			tmp |= RAL_DROP_TODS;
2161188417Sthompsa		if (!(ifp->if_flags & IFF_PROMISC))
2162184610Salfred			tmp |= RAL_DROP_NOT_TO_ME;
2163184610Salfred	}
2164188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, tmp);
2165184610Salfred
2166188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2167188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2168188417Sthompsa	usb2_transfer_start(sc->sc_xfer[URAL_BULK_RD]);
2169184610Salfred	return;
2170184610Salfred
2171188419Sthompsafail:	ural_stop_task(pm);
2172188417Sthompsa#undef N
2173184610Salfred}
2174184610Salfred
2175184610Salfredstatic void
2176188417Sthompsaural_init(void *priv)
2177184610Salfred{
2178188417Sthompsa	struct ural_softc *sc = priv;
2179184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2180188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2181184610Salfred
2182188417Sthompsa	RAL_LOCK(sc);
2183188419Sthompsa	ural_queue_command(sc, ural_init_task,
2184188419Sthompsa	    &sc->sc_synctask[0].hdr,
2185188419Sthompsa	    &sc->sc_synctask[1].hdr);
2186188417Sthompsa	RAL_UNLOCK(sc);
2187184610Salfred
2188188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2189188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2190184610Salfred}
2191184610Salfred
2192184610Salfredstatic void
2193188419Sthompsaural_stop_task(struct usb2_proc_msg *pm)
2194184610Salfred{
2195188419Sthompsa	struct ural_task *task = (struct ural_task *)pm;
2196188419Sthompsa	struct ural_softc *sc = task->sc;
2197184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2198184610Salfred
2199188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
2200184610Salfred
2201188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2202184610Salfred
2203184610Salfred	/*
2204188419Sthompsa	 * Drain all the transfers, if not already drained:
2205184610Salfred	 */
2206188419Sthompsa	RAL_UNLOCK(sc);
2207188419Sthompsa	usb2_transfer_drain(sc->sc_xfer[URAL_BULK_WR]);
2208188419Sthompsa	usb2_transfer_drain(sc->sc_xfer[URAL_BULK_RD]);
2209188419Sthompsa	RAL_LOCK(sc);
2210184610Salfred
2211188419Sthompsa	ural_unsetup_tx_list(sc);
2212184610Salfred
2213188417Sthompsa	/* disable Rx */
2214188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2215188417Sthompsa	/* reset ASIC and BBP (but won't reset MAC registers!) */
2216188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2217188619Sthompsa	/* wait a little */
2218188619Sthompsa	ural_pause(sc, hz / 10);
2219188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, 0);
2220184610Salfred}
2221184610Salfred
2222188417Sthompsastatic int
2223188417Sthompsaural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2224188417Sthompsa	const struct ieee80211_bpf_params *params)
2225184610Salfred{
2226188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2227188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2228188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
2229184610Salfred
2230188417Sthompsa	RAL_LOCK(sc);
2231188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2232188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2233188417Sthompsa		RAL_UNLOCK(sc);
2234188417Sthompsa		m_freem(m);
2235188417Sthompsa		ieee80211_free_node(ni);
2236188417Sthompsa		return ENETDOWN;
2237184610Salfred	}
2238188969Sthompsa	if (sc->tx_nfree < RAL_TX_MINFREE) {
2239188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2240188417Sthompsa		RAL_UNLOCK(sc);
2241184610Salfred		m_freem(m);
2242184610Salfred		ieee80211_free_node(ni);
2243188417Sthompsa		return EIO;
2244184610Salfred	}
2245184610Salfred
2246188417Sthompsa	ifp->if_opackets++;
2247184610Salfred
2248188417Sthompsa	if (params == NULL) {
2249188417Sthompsa		/*
2250188417Sthompsa		 * Legacy path; interpret frame contents to decide
2251188417Sthompsa		 * precisely how to send the frame.
2252188417Sthompsa		 */
2253188417Sthompsa		if (ural_tx_mgt(sc, m, ni) != 0)
2254188417Sthompsa			goto bad;
2255188417Sthompsa	} else {
2256188417Sthompsa		/*
2257188417Sthompsa		 * Caller supplied explicit parameters to use in
2258188417Sthompsa		 * sending the frame.
2259188417Sthompsa		 */
2260188417Sthompsa		if (ural_tx_raw(sc, m, ni, params) != 0)
2261188417Sthompsa			goto bad;
2262184610Salfred	}
2263188417Sthompsa	RAL_UNLOCK(sc);
2264188417Sthompsa	return 0;
2265188417Sthompsabad:
2266188417Sthompsa	ifp->if_oerrors++;
2267188417Sthompsa	RAL_UNLOCK(sc);
2268188417Sthompsa	ieee80211_free_node(ni);
2269188417Sthompsa	return EIO;		/* XXX */
2270184610Salfred}
2271184610Salfred
2272184610Salfredstatic void
2273188417Sthompsaural_amrr_start(struct ural_softc *sc, struct ieee80211_node *ni)
2274184610Salfred{
2275188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2276188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
2277184610Salfred
2278188417Sthompsa	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
2279188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2280184610Salfred
2281188417Sthompsa	ieee80211_amrr_node_init(&uvp->amrr, &URAL_NODE(ni)->amn, ni);
2282184610Salfred
2283188417Sthompsa	usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp);
2284184610Salfred}
2285184610Salfred
2286184610Salfredstatic void
2287188417Sthompsaural_amrr_timeout(void *arg)
2288184610Salfred{
2289188417Sthompsa	struct ural_vap *uvp = arg;
2290188417Sthompsa	struct ural_softc *sc = uvp->sc;
2291184610Salfred
2292188417Sthompsa	ural_queue_command(sc, ural_amrr_task,
2293188417Sthompsa	    &uvp->amrr_task[0].hdr, &uvp->amrr_task[1].hdr);
2294184610Salfred}
2295184610Salfred
2296184610Salfredstatic void
2297188417Sthompsaural_amrr_task(struct usb2_proc_msg *pm)
2298184610Salfred{
2299188417Sthompsa	struct ural_task *task = (struct ural_task *)pm;
2300188417Sthompsa	struct ural_softc *sc = task->sc;
2301188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2302188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2303188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2304188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
2305188417Sthompsa	struct ieee80211_node *ni = vap->iv_bss;
2306188417Sthompsa	int ok, fail;
2307184610Salfred
2308188417Sthompsa	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2309188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2310184610Salfred
2311188417Sthompsa	ok = sc->sta[7] +		/* TX ok w/o retry */
2312188417Sthompsa	     sc->sta[8];		/* TX ok w/ retry */
2313188417Sthompsa	fail = sc->sta[9];		/* TX retry-fail count */
2314184610Salfred
2315188417Sthompsa	ieee80211_amrr_tx_update(&URAL_NODE(ni)->amn,
2316188417Sthompsa	    ok+fail, ok, sc->sta[8] + fail);
2317188417Sthompsa	(void) ieee80211_amrr_choose(ni, &URAL_NODE(ni)->amn);
2318184610Salfred
2319188417Sthompsa	ifp->if_oerrors += fail;	/* count TX retry-fail as Tx errors */
2320184610Salfred
2321188417Sthompsa	usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp);
2322184610Salfred}
2323184610Salfred
2324188619Sthompsastatic int
2325188619Sthompsaural_pause(struct ural_softc *sc, int timeout)
2326188619Sthompsa{
2327188619Sthompsa	if (usb2_proc_is_gone(&sc->sc_tq))
2328188619Sthompsa		return (1);
2329188619Sthompsa
2330188619Sthompsa	usb2_pause_mtx(&sc->sc_mtx, timeout);
2331188619Sthompsa	return (0);
2332188619Sthompsa}
2333188619Sthompsa
2334184610Salfredstatic void
2335188417Sthompsaural_queue_command(struct ural_softc *sc, usb2_proc_callback_t *fn,
2336188417Sthompsa    struct usb2_proc_msg *t0, struct usb2_proc_msg *t1)
2337184610Salfred{
2338188417Sthompsa	struct ural_task *task;
2339184610Salfred
2340188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
2341184610Salfred
2342188417Sthompsa	if (usb2_proc_is_gone(&sc->sc_tq)) {
2343188417Sthompsa		DPRINTF("proc is gone\n");
2344188417Sthompsa		return;         /* nothing to do */
2345184610Salfred	}
2346188417Sthompsa	/*
2347188417Sthompsa	 * NOTE: The task cannot get executed before we drop the
2348188417Sthompsa	 * "sc_mtx" mutex. It is safe to update fields in the message
2349188417Sthompsa	 * structure after that the message got queued.
2350188417Sthompsa	 */
2351188417Sthompsa	task = (struct ural_task *)
2352188417Sthompsa	  usb2_proc_msignal(&sc->sc_tq, t0, t1);
2353184610Salfred
2354188417Sthompsa	/* Setup callback and softc pointers */
2355188417Sthompsa	task->hdr.pm_callback = fn;
2356188417Sthompsa	task->sc = sc;
2357184610Salfred
2358188419Sthompsa	/*
2359188419Sthompsa	 * Init and stop must be synchronous!
2360188419Sthompsa	 */
2361188419Sthompsa	if ((fn == ural_init_task) || (fn == ural_stop_task))
2362188419Sthompsa		usb2_proc_mwait(&sc->sc_tq, t0, t1);
2363184610Salfred}
2364184610Salfred
2365