if_ural.c revision 206358
1184610Salfred/*	$FreeBSD: head/sys/dev/usb/wlan/if_ural.c 206358 2010-04-07 15:29:13Z rpaulo $	*/
2184610Salfred
3184610Salfred/*-
4184610Salfred * Copyright (c) 2005, 2006
5184610Salfred *	Damien Bergamini <damien.bergamini@free.fr>
6184610Salfred *
7184610Salfred * Copyright (c) 2006, 2008
8189002Sed *	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 206358 2010-04-07 15:29:13Z rpaulo $");
25184610Salfred
26184610Salfred/*-
27184610Salfred * Ralink Technology RT2500USB chipset driver
28184610Salfred * http://www.ralinktech.com/
29184610Salfred */
30184610Salfred
31191746Sthompsa#include <sys/param.h>
32191746Sthompsa#include <sys/sockio.h>
33191746Sthompsa#include <sys/sysctl.h>
34191746Sthompsa#include <sys/lock.h>
35191746Sthompsa#include <sys/mutex.h>
36191746Sthompsa#include <sys/mbuf.h>
37191746Sthompsa#include <sys/kernel.h>
38191746Sthompsa#include <sys/socket.h>
39191746Sthompsa#include <sys/systm.h>
40191746Sthompsa#include <sys/malloc.h>
41191746Sthompsa#include <sys/module.h>
42191746Sthompsa#include <sys/bus.h>
43191746Sthompsa#include <sys/endian.h>
44191746Sthompsa#include <sys/kdb.h>
45184610Salfred
46191746Sthompsa#include <machine/bus.h>
47191746Sthompsa#include <machine/resource.h>
48191746Sthompsa#include <sys/rman.h>
49191746Sthompsa
50191746Sthompsa#include <net/bpf.h>
51191746Sthompsa#include <net/if.h>
52191746Sthompsa#include <net/if_arp.h>
53191746Sthompsa#include <net/ethernet.h>
54191746Sthompsa#include <net/if_dl.h>
55191746Sthompsa#include <net/if_media.h>
56191746Sthompsa#include <net/if_types.h>
57191746Sthompsa
58191746Sthompsa#ifdef INET
59191746Sthompsa#include <netinet/in.h>
60191746Sthompsa#include <netinet/in_systm.h>
61191746Sthompsa#include <netinet/in_var.h>
62191746Sthompsa#include <netinet/if_ether.h>
63191746Sthompsa#include <netinet/ip.h>
64191746Sthompsa#endif
65191746Sthompsa
66191746Sthompsa#include <net80211/ieee80211_var.h>
67191746Sthompsa#include <net80211/ieee80211_regdomain.h>
68191746Sthompsa#include <net80211/ieee80211_radiotap.h>
69206358Srpaulo#include <net80211/ieee80211_ratectl.h>
70191746Sthompsa
71191746Sthompsa#include <dev/usb/usb.h>
72194677Sthompsa#include <dev/usb/usbdi.h>
73191746Sthompsa#include "usbdevs.h"
74184610Salfred
75194677Sthompsa#define	USB_DEBUG_VAR ural_debug
76194677Sthompsa#include <dev/usb/usb_debug.h>
77191746Sthompsa
78188942Sthompsa#include <dev/usb/wlan/if_uralreg.h>
79188942Sthompsa#include <dev/usb/wlan/if_uralvar.h>
80184610Salfred
81184610Salfred#if USB_DEBUG
82184610Salfredstatic int ural_debug = 0;
83184610Salfred
84192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
85192502SthompsaSYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0,
86184610Salfred    "Debug level");
87184610Salfred#endif
88184610Salfred
89188417Sthompsa#define URAL_RSSI(rssi)					\
90188417Sthompsa	((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ?	\
91188417Sthompsa	 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
92184610Salfred
93188417Sthompsa/* various supported device vendors/products */
94192984Sthompsastatic const struct usb_device_id ural_devs[] = {
95201028Sthompsa#define	URAL_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
96201028Sthompsa	URAL_DEV(ASUS, WL167G),
97201028Sthompsa	URAL_DEV(ASUS, RT2570),
98201028Sthompsa	URAL_DEV(BELKIN, F5D7050),
99201028Sthompsa	URAL_DEV(BELKIN, F5D7051),
100201028Sthompsa	URAL_DEV(CISCOLINKSYS, HU200TS),
101201028Sthompsa	URAL_DEV(CISCOLINKSYS, WUSB54G),
102201028Sthompsa	URAL_DEV(CISCOLINKSYS, WUSB54GP),
103201028Sthompsa	URAL_DEV(CONCEPTRONIC2, C54RU),
104201028Sthompsa	URAL_DEV(DLINK, DWLG122),
105201028Sthompsa	URAL_DEV(GIGABYTE, GN54G),
106201028Sthompsa	URAL_DEV(GIGABYTE, GNWBKG),
107201028Sthompsa	URAL_DEV(GUILLEMOT, HWGUSB254),
108201028Sthompsa	URAL_DEV(MELCO, KG54),
109201028Sthompsa	URAL_DEV(MELCO, KG54AI),
110201028Sthompsa	URAL_DEV(MELCO, KG54YB),
111201028Sthompsa	URAL_DEV(MELCO, NINWIFI),
112201028Sthompsa	URAL_DEV(MSI, RT2570),
113201028Sthompsa	URAL_DEV(MSI, RT2570_2),
114201028Sthompsa	URAL_DEV(MSI, RT2570_3),
115201028Sthompsa	URAL_DEV(NOVATECH, NV902),
116201028Sthompsa	URAL_DEV(RALINK, RT2570),
117201028Sthompsa	URAL_DEV(RALINK, RT2570_2),
118201028Sthompsa	URAL_DEV(RALINK, RT2570_3),
119201028Sthompsa	URAL_DEV(SIEMENS2, WL54G),
120201028Sthompsa	URAL_DEV(SMC, 2862WG),
121201028Sthompsa	URAL_DEV(SPHAIRON, UB801R),
122201028Sthompsa	URAL_DEV(SURECOM, RT2570),
123201028Sthompsa	URAL_DEV(VTECH, RT2570),
124201028Sthompsa	URAL_DEV(ZINWELL, RT2570),
125201028Sthompsa#undef URAL_DEV
126188417Sthompsa};
127184610Salfred
128193045Sthompsastatic usb_callback_t ural_bulk_read_callback;
129193045Sthompsastatic usb_callback_t ural_bulk_write_callback;
130184610Salfred
131193045Sthompsastatic usb_error_t	ural_do_request(struct ural_softc *sc,
132192984Sthompsa			    struct usb_device_request *req, void *data);
133185948Sthompsastatic struct ieee80211vap *ural_vap_create(struct ieee80211com *,
134188417Sthompsa			    const char name[IFNAMSIZ], int unit, int opmode,
135188417Sthompsa			    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
136188417Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
137188417Sthompsastatic void		ural_vap_delete(struct ieee80211vap *);
138188417Sthompsastatic void		ural_tx_free(struct ural_tx_data *, int);
139188419Sthompsastatic void		ural_setup_tx_list(struct ural_softc *);
140188419Sthompsastatic void		ural_unsetup_tx_list(struct ural_softc *);
141188417Sthompsastatic int		ural_newstate(struct ieee80211vap *,
142188417Sthompsa			    enum ieee80211_state, int);
143188417Sthompsastatic void		ural_setup_tx_desc(struct ural_softc *,
144188417Sthompsa			    struct ural_tx_desc *, uint32_t, int, int);
145188417Sthompsastatic int		ural_tx_bcn(struct ural_softc *, struct mbuf *,
146188417Sthompsa			    struct ieee80211_node *);
147188417Sthompsastatic int		ural_tx_mgt(struct ural_softc *, struct mbuf *,
148188417Sthompsa			    struct ieee80211_node *);
149188417Sthompsastatic int		ural_tx_data(struct ural_softc *, struct mbuf *,
150188417Sthompsa			    struct ieee80211_node *);
151188417Sthompsastatic void		ural_start(struct ifnet *);
152188417Sthompsastatic int		ural_ioctl(struct ifnet *, u_long, caddr_t);
153188417Sthompsastatic void		ural_set_testmode(struct ural_softc *);
154188417Sthompsastatic void		ural_eeprom_read(struct ural_softc *, uint16_t, void *,
155188417Sthompsa			    int);
156188417Sthompsastatic uint16_t		ural_read(struct ural_softc *, uint16_t);
157188417Sthompsastatic void		ural_read_multi(struct ural_softc *, uint16_t, void *,
158188417Sthompsa			    int);
159188417Sthompsastatic void		ural_write(struct ural_softc *, uint16_t, uint16_t);
160188417Sthompsastatic void		ural_write_multi(struct ural_softc *, uint16_t, void *,
161188417Sthompsa			    int) __unused;
162188417Sthompsastatic void		ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
163188417Sthompsastatic uint8_t		ural_bbp_read(struct ural_softc *, uint8_t);
164188417Sthompsastatic void		ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
165188417Sthompsastatic void		ural_scan_start(struct ieee80211com *);
166188417Sthompsastatic void		ural_scan_end(struct ieee80211com *);
167188417Sthompsastatic void		ural_set_channel(struct ieee80211com *);
168188417Sthompsastatic void		ural_set_chan(struct ural_softc *,
169188417Sthompsa			    struct ieee80211_channel *);
170188417Sthompsastatic void		ural_disable_rf_tune(struct ural_softc *);
171188417Sthompsastatic void		ural_enable_tsf_sync(struct ural_softc *);
172192468Ssamstatic void 		ural_enable_tsf(struct ural_softc *);
173188417Sthompsastatic void		ural_update_slot(struct ifnet *);
174188417Sthompsastatic void		ural_set_txpreamble(struct ural_softc *);
175188417Sthompsastatic void		ural_set_basicrates(struct ural_softc *,
176188417Sthompsa			    const struct ieee80211_channel *);
177188417Sthompsastatic void		ural_set_bssid(struct ural_softc *, const uint8_t *);
178188417Sthompsastatic void		ural_set_macaddr(struct ural_softc *, uint8_t *);
179189123Sthompsastatic void		ural_update_promisc(struct ifnet *);
180191746Sthompsastatic void		ural_setpromisc(struct ural_softc *);
181188417Sthompsastatic const char	*ural_get_rf(int);
182188417Sthompsastatic void		ural_read_eeprom(struct ural_softc *);
183188417Sthompsastatic int		ural_bbp_init(struct ural_softc *);
184188417Sthompsastatic void		ural_set_txantenna(struct ural_softc *, int);
185188417Sthompsastatic void		ural_set_rxantenna(struct ural_softc *, int);
186191746Sthompsastatic void		ural_init_locked(struct ural_softc *);
187188417Sthompsastatic void		ural_init(void *);
188191746Sthompsastatic void		ural_stop(struct ural_softc *);
189188417Sthompsastatic int		ural_raw_xmit(struct ieee80211_node *, struct mbuf *,
190188417Sthompsa			    const struct ieee80211_bpf_params *);
191206358Srpaulostatic void		ural_ratectl_start(struct ural_softc *,
192188417Sthompsa			    struct ieee80211_node *);
193206358Srpaulostatic void		ural_ratectl_timeout(void *);
194206358Srpaulostatic void		ural_ratectl_task(void *, int);
195188619Sthompsastatic int		ural_pause(struct ural_softc *sc, int timeout);
196184610Salfred
197184610Salfred/*
198188417Sthompsa * Default values for MAC registers; values taken from the reference driver.
199184610Salfred */
200188417Sthompsastatic const struct {
201188417Sthompsa	uint16_t	reg;
202188417Sthompsa	uint16_t	val;
203188417Sthompsa} ural_def_mac[] = {
204188417Sthompsa	{ RAL_TXRX_CSR5,  0x8c8d },
205188417Sthompsa	{ RAL_TXRX_CSR6,  0x8b8a },
206188417Sthompsa	{ RAL_TXRX_CSR7,  0x8687 },
207188417Sthompsa	{ RAL_TXRX_CSR8,  0x0085 },
208188417Sthompsa	{ RAL_MAC_CSR13,  0x1111 },
209188417Sthompsa	{ RAL_MAC_CSR14,  0x1e11 },
210188417Sthompsa	{ RAL_TXRX_CSR21, 0xe78f },
211188417Sthompsa	{ RAL_MAC_CSR9,   0xff1d },
212188417Sthompsa	{ RAL_MAC_CSR11,  0x0002 },
213188417Sthompsa	{ RAL_MAC_CSR22,  0x0053 },
214188417Sthompsa	{ RAL_MAC_CSR15,  0x0000 },
215188417Sthompsa	{ RAL_MAC_CSR8,   RAL_FRAME_SIZE },
216188417Sthompsa	{ RAL_TXRX_CSR19, 0x0000 },
217188417Sthompsa	{ RAL_TXRX_CSR18, 0x005a },
218188417Sthompsa	{ RAL_PHY_CSR2,   0x0000 },
219188417Sthompsa	{ RAL_TXRX_CSR0,  0x1ec0 },
220188417Sthompsa	{ RAL_PHY_CSR4,   0x000f }
221184610Salfred};
222184610Salfred
223184610Salfred/*
224184610Salfred * Default values for BBP registers; values taken from the reference driver.
225184610Salfred */
226188417Sthompsastatic const struct {
227184610Salfred	uint8_t	reg;
228184610Salfred	uint8_t	val;
229188417Sthompsa} ural_def_bbp[] = {
230188417Sthompsa	{  3, 0x02 },
231188417Sthompsa	{  4, 0x19 },
232188417Sthompsa	{ 14, 0x1c },
233188417Sthompsa	{ 15, 0x30 },
234188417Sthompsa	{ 16, 0xac },
235188417Sthompsa	{ 17, 0x48 },
236188417Sthompsa	{ 18, 0x18 },
237188417Sthompsa	{ 19, 0xff },
238188417Sthompsa	{ 20, 0x1e },
239188417Sthompsa	{ 21, 0x08 },
240188417Sthompsa	{ 22, 0x08 },
241188417Sthompsa	{ 23, 0x08 },
242188417Sthompsa	{ 24, 0x80 },
243188417Sthompsa	{ 25, 0x50 },
244188417Sthompsa	{ 26, 0x08 },
245188417Sthompsa	{ 27, 0x23 },
246188417Sthompsa	{ 30, 0x10 },
247188417Sthompsa	{ 31, 0x2b },
248188417Sthompsa	{ 32, 0xb9 },
249188417Sthompsa	{ 34, 0x12 },
250188417Sthompsa	{ 35, 0x50 },
251188417Sthompsa	{ 39, 0xc4 },
252188417Sthompsa	{ 40, 0x02 },
253188417Sthompsa	{ 41, 0x60 },
254188417Sthompsa	{ 53, 0x10 },
255188417Sthompsa	{ 54, 0x18 },
256188417Sthompsa	{ 56, 0x08 },
257188417Sthompsa	{ 57, 0x10 },
258188417Sthompsa	{ 58, 0x08 },
259188417Sthompsa	{ 61, 0x60 },
260188417Sthompsa	{ 62, 0x10 },
261188417Sthompsa	{ 75, 0xff }
262184610Salfred};
263184610Salfred
264184610Salfred/*
265184610Salfred * Default values for RF register R2 indexed by channel numbers.
266184610Salfred */
267184610Salfredstatic const uint32_t ural_rf2522_r2[] = {
268184610Salfred	0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
269184610Salfred	0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
270184610Salfred};
271184610Salfred
272184610Salfredstatic const uint32_t ural_rf2523_r2[] = {
273184610Salfred	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
274184610Salfred	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
275184610Salfred};
276184610Salfred
277184610Salfredstatic const uint32_t ural_rf2524_r2[] = {
278184610Salfred	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
279184610Salfred	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
280184610Salfred};
281184610Salfred
282184610Salfredstatic const uint32_t ural_rf2525_r2[] = {
283184610Salfred	0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
284184610Salfred	0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
285184610Salfred};
286184610Salfred
287184610Salfredstatic const uint32_t ural_rf2525_hi_r2[] = {
288184610Salfred	0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
289184610Salfred	0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
290184610Salfred};
291184610Salfred
292184610Salfredstatic const uint32_t ural_rf2525e_r2[] = {
293184610Salfred	0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
294184610Salfred	0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
295184610Salfred};
296184610Salfred
297184610Salfredstatic const uint32_t ural_rf2526_hi_r2[] = {
298184610Salfred	0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
299184610Salfred	0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
300184610Salfred};
301184610Salfred
302184610Salfredstatic const uint32_t ural_rf2526_r2[] = {
303184610Salfred	0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
304184610Salfred	0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
305184610Salfred};
306184610Salfred
307184610Salfred/*
308184610Salfred * For dual-band RF, RF registers R1 and R4 also depend on channel number;
309184610Salfred * values taken from the reference driver.
310184610Salfred */
311188417Sthompsastatic const struct {
312188417Sthompsa	uint8_t		chan;
313188417Sthompsa	uint32_t	r1;
314188417Sthompsa	uint32_t	r2;
315188417Sthompsa	uint32_t	r4;
316188417Sthompsa} ural_rf5222[] = {
317188417Sthompsa	{   1, 0x08808, 0x0044d, 0x00282 },
318188417Sthompsa	{   2, 0x08808, 0x0044e, 0x00282 },
319188417Sthompsa	{   3, 0x08808, 0x0044f, 0x00282 },
320188417Sthompsa	{   4, 0x08808, 0x00460, 0x00282 },
321188417Sthompsa	{   5, 0x08808, 0x00461, 0x00282 },
322188417Sthompsa	{   6, 0x08808, 0x00462, 0x00282 },
323188417Sthompsa	{   7, 0x08808, 0x00463, 0x00282 },
324188417Sthompsa	{   8, 0x08808, 0x00464, 0x00282 },
325188417Sthompsa	{   9, 0x08808, 0x00465, 0x00282 },
326188417Sthompsa	{  10, 0x08808, 0x00466, 0x00282 },
327188417Sthompsa	{  11, 0x08808, 0x00467, 0x00282 },
328188417Sthompsa	{  12, 0x08808, 0x00468, 0x00282 },
329188417Sthompsa	{  13, 0x08808, 0x00469, 0x00282 },
330188417Sthompsa	{  14, 0x08808, 0x0046b, 0x00286 },
331184610Salfred
332188417Sthompsa	{  36, 0x08804, 0x06225, 0x00287 },
333188417Sthompsa	{  40, 0x08804, 0x06226, 0x00287 },
334188417Sthompsa	{  44, 0x08804, 0x06227, 0x00287 },
335188417Sthompsa	{  48, 0x08804, 0x06228, 0x00287 },
336188417Sthompsa	{  52, 0x08804, 0x06229, 0x00287 },
337188417Sthompsa	{  56, 0x08804, 0x0622a, 0x00287 },
338188417Sthompsa	{  60, 0x08804, 0x0622b, 0x00287 },
339188417Sthompsa	{  64, 0x08804, 0x0622c, 0x00287 },
340184610Salfred
341188417Sthompsa	{ 100, 0x08804, 0x02200, 0x00283 },
342188417Sthompsa	{ 104, 0x08804, 0x02201, 0x00283 },
343188417Sthompsa	{ 108, 0x08804, 0x02202, 0x00283 },
344188417Sthompsa	{ 112, 0x08804, 0x02203, 0x00283 },
345188417Sthompsa	{ 116, 0x08804, 0x02204, 0x00283 },
346188417Sthompsa	{ 120, 0x08804, 0x02205, 0x00283 },
347188417Sthompsa	{ 124, 0x08804, 0x02206, 0x00283 },
348188417Sthompsa	{ 128, 0x08804, 0x02207, 0x00283 },
349188417Sthompsa	{ 132, 0x08804, 0x02208, 0x00283 },
350188417Sthompsa	{ 136, 0x08804, 0x02209, 0x00283 },
351188417Sthompsa	{ 140, 0x08804, 0x0220a, 0x00283 },
352184610Salfred
353188417Sthompsa	{ 149, 0x08808, 0x02429, 0x00281 },
354188417Sthompsa	{ 153, 0x08808, 0x0242b, 0x00281 },
355188417Sthompsa	{ 157, 0x08808, 0x0242d, 0x00281 },
356188417Sthompsa	{ 161, 0x08808, 0x0242f, 0x00281 }
357184610Salfred};
358184610Salfred
359192984Sthompsastatic const struct usb_config ural_config[URAL_N_TRANSFER] = {
360188417Sthompsa	[URAL_BULK_WR] = {
361184610Salfred		.type = UE_BULK,
362184610Salfred		.endpoint = UE_ADDR_ANY,
363184610Salfred		.direction = UE_DIR_OUT,
364190734Sthompsa		.bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4),
365190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
366190734Sthompsa		.callback = ural_bulk_write_callback,
367190734Sthompsa		.timeout = 5000,	/* ms */
368184610Salfred	},
369188417Sthompsa	[URAL_BULK_RD] = {
370184610Salfred		.type = UE_BULK,
371184610Salfred		.endpoint = UE_ADDR_ANY,
372184610Salfred		.direction = UE_DIR_IN,
373190734Sthompsa		.bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE),
374190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
375190734Sthompsa		.callback = ural_bulk_read_callback,
376184610Salfred	},
377184610Salfred};
378184610Salfred
379188417Sthompsastatic device_probe_t ural_match;
380188417Sthompsastatic device_attach_t ural_attach;
381188417Sthompsastatic device_detach_t ural_detach;
382184610Salfred
383184610Salfredstatic device_method_t ural_methods[] = {
384188417Sthompsa	/* Device interface */
385188417Sthompsa	DEVMETHOD(device_probe,		ural_match),
386188417Sthompsa	DEVMETHOD(device_attach,	ural_attach),
387188417Sthompsa	DEVMETHOD(device_detach,	ural_detach),
388188417Sthompsa
389188417Sthompsa	{ 0, 0 }
390184610Salfred};
391184610Salfred
392184610Salfredstatic driver_t ural_driver = {
393184610Salfred	.name = "ural",
394184610Salfred	.methods = ural_methods,
395184610Salfred	.size = sizeof(struct ural_softc),
396184610Salfred};
397184610Salfred
398188417Sthompsastatic devclass_t ural_devclass;
399188417Sthompsa
400189275SthompsaDRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, NULL, 0);
401188942SthompsaMODULE_DEPEND(ural, usb, 1, 1, 1);
402184610SalfredMODULE_DEPEND(ural, wlan, 1, 1, 1);
403184610Salfred
404184610Salfredstatic int
405188417Sthompsaural_match(device_t self)
406184610Salfred{
407192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
408184610Salfred
409192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
410184610Salfred		return (ENXIO);
411188417Sthompsa	if (uaa->info.bConfigIndex != 0)
412184610Salfred		return (ENXIO);
413188417Sthompsa	if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX)
414184610Salfred		return (ENXIO);
415188417Sthompsa
416194228Sthompsa	return (usbd_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa));
417184610Salfred}
418184610Salfred
419184610Salfredstatic int
420188417Sthompsaural_attach(device_t self)
421184610Salfred{
422192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
423188417Sthompsa	struct ural_softc *sc = device_get_softc(self);
424191746Sthompsa	struct ifnet *ifp;
425191746Sthompsa	struct ieee80211com *ic;
426191746Sthompsa	uint8_t iface_index, bands;
427184610Salfred	int error;
428184610Salfred
429194228Sthompsa	device_set_usb_desc(self);
430184610Salfred	sc->sc_udev = uaa->device;
431188417Sthompsa	sc->sc_dev = self;
432184610Salfred
433188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
434188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
435184610Salfred
436184610Salfred	iface_index = RAL_IFACE_INDEX;
437194228Sthompsa	error = usbd_transfer_setup(uaa->device,
438184610Salfred	    &iface_index, sc->sc_xfer, ural_config,
439184610Salfred	    URAL_N_TRANSFER, sc, &sc->sc_mtx);
440184610Salfred	if (error) {
441188417Sthompsa		device_printf(self, "could not allocate USB transfers, "
442194228Sthompsa		    "err=%s\n", usbd_errstr(error));
443184610Salfred		goto detach;
444184610Salfred	}
445184610Salfred
446188419Sthompsa	RAL_LOCK(sc);
447188417Sthompsa	/* retrieve RT2570 rev. no */
448188417Sthompsa	sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
449184610Salfred
450188417Sthompsa	/* retrieve MAC address and various other things from EEPROM */
451188417Sthompsa	ural_read_eeprom(sc);
452188417Sthompsa	RAL_UNLOCK(sc);
453184610Salfred
454191746Sthompsa	device_printf(self, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
455188417Sthompsa	    sc->asic_rev, ural_get_rf(sc->rf_rev));
456184610Salfred
457188419Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
458188419Sthompsa	if (ifp == NULL) {
459188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
460191746Sthompsa		goto detach;
461188419Sthompsa	}
462188419Sthompsa	ic = ifp->if_l2com;
463188419Sthompsa
464188417Sthompsa	ifp->if_softc = sc;
465188417Sthompsa	if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
466188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
467188417Sthompsa	ifp->if_init = ural_init;
468188417Sthompsa	ifp->if_ioctl = ural_ioctl;
469188417Sthompsa	ifp->if_start = ural_start;
470188417Sthompsa	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
471188417Sthompsa	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
472188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
473184610Salfred
474188417Sthompsa	ic->ic_ifp = ifp;
475188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
476184610Salfred
477188417Sthompsa	/* set device capabilities */
478188417Sthompsa	ic->ic_caps =
479188417Sthompsa	      IEEE80211_C_STA		/* station mode supported */
480188417Sthompsa	    | IEEE80211_C_IBSS		/* IBSS mode supported */
481188417Sthompsa	    | IEEE80211_C_MONITOR	/* monitor mode supported */
482188417Sthompsa	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
483188417Sthompsa	    | IEEE80211_C_TXPMGT	/* tx power management */
484188417Sthompsa	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
485188417Sthompsa	    | IEEE80211_C_SHSLOT	/* short slot time supported */
486188417Sthompsa	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
487188417Sthompsa	    | IEEE80211_C_WPA		/* 802.11i */
488188417Sthompsa	    ;
489184610Salfred
490188417Sthompsa	bands = 0;
491188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
492188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
493188417Sthompsa	if (sc->rf_rev == RAL_RF_5222)
494188417Sthompsa		setbit(&bands, IEEE80211_MODE_11A);
495188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
496184610Salfred
497190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
498189123Sthompsa	ic->ic_update_promisc = ural_update_promisc;
499188417Sthompsa	ic->ic_raw_xmit = ural_raw_xmit;
500188417Sthompsa	ic->ic_scan_start = ural_scan_start;
501188417Sthompsa	ic->ic_scan_end = ural_scan_end;
502188417Sthompsa	ic->ic_set_channel = ural_set_channel;
503184610Salfred
504188417Sthompsa	ic->ic_vap_create = ural_vap_create;
505188417Sthompsa	ic->ic_vap_delete = ural_vap_delete;
506184610Salfred
507192468Ssam	ieee80211_radiotap_attach(ic,
508192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
509192468Ssam		RAL_TX_RADIOTAP_PRESENT,
510192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
511192468Ssam		RAL_RX_RADIOTAP_PRESENT);
512184610Salfred
513188417Sthompsa	if (bootverbose)
514188417Sthompsa		ieee80211_announce(ic);
515184610Salfred
516191746Sthompsa	return (0);
517191746Sthompsa
518191746Sthompsadetach:
519191746Sthompsa	ural_detach(self);
520191746Sthompsa	return (ENXIO);			/* failure */
521188417Sthompsa}
522184610Salfred
523188417Sthompsastatic int
524188417Sthompsaural_detach(device_t self)
525188417Sthompsa{
526188417Sthompsa	struct ural_softc *sc = device_get_softc(self);
527188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
528188619Sthompsa	struct ieee80211com *ic;
529184610Salfred
530188419Sthompsa	/* stop all USB transfers */
531194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER);
532184610Salfred
533188419Sthompsa	/* free TX list, if any */
534188419Sthompsa	RAL_LOCK(sc);
535188419Sthompsa	ural_unsetup_tx_list(sc);
536188419Sthompsa	RAL_UNLOCK(sc);
537188419Sthompsa
538188417Sthompsa	if (ifp) {
539188619Sthompsa		ic = ifp->if_l2com;
540188417Sthompsa		ieee80211_ifdetach(ic);
541188417Sthompsa		if_free(ifp);
542184610Salfred	}
543188417Sthompsa	mtx_destroy(&sc->sc_mtx);
544184610Salfred
545188417Sthompsa	return (0);
546184610Salfred}
547184610Salfred
548193045Sthompsastatic usb_error_t
549189123Sthompsaural_do_request(struct ural_softc *sc,
550192984Sthompsa    struct usb_device_request *req, void *data)
551189123Sthompsa{
552193045Sthompsa	usb_error_t err;
553189123Sthompsa	int ntries = 10;
554189123Sthompsa
555189123Sthompsa	while (ntries--) {
556194228Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
557189123Sthompsa		    req, data, 0, NULL, 250 /* ms */);
558189123Sthompsa		if (err == 0)
559189123Sthompsa			break;
560189123Sthompsa
561189123Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
562194228Sthompsa		    usbd_errstr(err));
563189123Sthompsa		if (ural_pause(sc, hz / 100))
564189123Sthompsa			break;
565189123Sthompsa	}
566189123Sthompsa	return (err);
567189123Sthompsa}
568189123Sthompsa
569188417Sthompsastatic struct ieee80211vap *
570188417Sthompsaural_vap_create(struct ieee80211com *ic,
571188417Sthompsa	const char name[IFNAMSIZ], int unit, int opmode, int flags,
572188417Sthompsa	const uint8_t bssid[IEEE80211_ADDR_LEN],
573188417Sthompsa	const uint8_t mac[IEEE80211_ADDR_LEN])
574184610Salfred{
575188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
576188417Sthompsa	struct ural_vap *uvp;
577188417Sthompsa	struct ieee80211vap *vap;
578184610Salfred
579188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
580188417Sthompsa		return NULL;
581188417Sthompsa	uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap),
582188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
583188417Sthompsa	if (uvp == NULL)
584188417Sthompsa		return NULL;
585188417Sthompsa	vap = &uvp->vap;
586188417Sthompsa	/* enable s/w bmiss handling for sta mode */
587188417Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode,
588188417Sthompsa	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
589184610Salfred
590188417Sthompsa	/* override state transition machine */
591188417Sthompsa	uvp->newstate = vap->iv_newstate;
592188417Sthompsa	vap->iv_newstate = ural_newstate;
593184610Salfred
594206358Srpaulo	usb_callout_init_mtx(&uvp->ratectl_ch, &sc->sc_mtx, 0);
595206358Srpaulo	TASK_INIT(&uvp->ratectl_task, 0, ural_ratectl_task, uvp);
596206358Srpaulo	ieee80211_ratectl_init(vap);
597206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
598184610Salfred
599188417Sthompsa	/* complete setup */
600188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
601188417Sthompsa	ic->ic_opmode = opmode;
602188417Sthompsa	return vap;
603184610Salfred}
604184610Salfred
605184610Salfredstatic void
606188417Sthompsaural_vap_delete(struct ieee80211vap *vap)
607184610Salfred{
608188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
609191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
610184610Salfred
611206358Srpaulo	usb_callout_drain(&uvp->ratectl_ch);
612206358Srpaulo	ieee80211_draintask(ic, &uvp->ratectl_task);
613206358Srpaulo	ieee80211_ratectl_deinit(vap);
614188417Sthompsa	ieee80211_vap_detach(vap);
615188417Sthompsa	free(uvp, M_80211_VAP);
616184610Salfred}
617184610Salfred
618184610Salfredstatic void
619188417Sthompsaural_tx_free(struct ural_tx_data *data, int txerr)
620184610Salfred{
621188417Sthompsa	struct ural_softc *sc = data->sc;
622184610Salfred
623188417Sthompsa	if (data->m != NULL) {
624188417Sthompsa		if (data->m->m_flags & M_TXCB)
625188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
626188417Sthompsa			    txerr ? ETIMEDOUT : 0);
627188417Sthompsa		m_freem(data->m);
628188417Sthompsa		data->m = NULL;
629184610Salfred
630188417Sthompsa		ieee80211_free_node(data->ni);
631188417Sthompsa		data->ni = NULL;
632188417Sthompsa	}
633188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
634188417Sthompsa	sc->tx_nfree++;
635184610Salfred}
636184610Salfred
637188419Sthompsastatic void
638188419Sthompsaural_setup_tx_list(struct ural_softc *sc)
639184610Salfred{
640188417Sthompsa	struct ural_tx_data *data;
641188417Sthompsa	int i;
642184610Salfred
643188417Sthompsa	sc->tx_nfree = 0;
644188417Sthompsa	STAILQ_INIT(&sc->tx_q);
645188417Sthompsa	STAILQ_INIT(&sc->tx_free);
646184610Salfred
647188417Sthompsa	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
648188417Sthompsa		data = &sc->tx_data[i];
649184610Salfred
650188417Sthompsa		data->sc = sc;
651188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
652188417Sthompsa		sc->tx_nfree++;
653184610Salfred	}
654184610Salfred}
655184610Salfred
656184610Salfredstatic void
657188419Sthompsaural_unsetup_tx_list(struct ural_softc *sc)
658184610Salfred{
659188417Sthompsa	struct ural_tx_data *data;
660188417Sthompsa	int i;
661184610Salfred
662188419Sthompsa	/* make sure any subsequent use of the queues will fail */
663188419Sthompsa	sc->tx_nfree = 0;
664188419Sthompsa	STAILQ_INIT(&sc->tx_q);
665188419Sthompsa	STAILQ_INIT(&sc->tx_free);
666184610Salfred
667188419Sthompsa	/* free up all node references and mbufs */
668188417Sthompsa	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
669188417Sthompsa		data = &sc->tx_data[i];
670184610Salfred
671188417Sthompsa		if (data->m != NULL) {
672188417Sthompsa			m_freem(data->m);
673188417Sthompsa			data->m = NULL;
674188417Sthompsa		}
675188417Sthompsa		if (data->ni != NULL) {
676188417Sthompsa			ieee80211_free_node(data->ni);
677188417Sthompsa			data->ni = NULL;
678188417Sthompsa		}
679184610Salfred	}
680184610Salfred}
681184610Salfred
682191746Sthompsastatic int
683191746Sthompsaural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
684184610Salfred{
685188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
686191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
687191746Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
688188417Sthompsa	const struct ieee80211_txparam *tp;
689188417Sthompsa	struct ieee80211_node *ni;
690188417Sthompsa	struct mbuf *m;
691184610Salfred
692191746Sthompsa	DPRINTF("%s -> %s\n",
693191746Sthompsa		ieee80211_state_name[vap->iv_state],
694191746Sthompsa		ieee80211_state_name[nstate]);
695184610Salfred
696191746Sthompsa	IEEE80211_UNLOCK(ic);
697191746Sthompsa	RAL_LOCK(sc);
698206358Srpaulo	usb_callout_stop(&uvp->ratectl_ch);
699191746Sthompsa
700191746Sthompsa	switch (nstate) {
701188417Sthompsa	case IEEE80211_S_INIT:
702191746Sthompsa		if (vap->iv_state == IEEE80211_S_RUN) {
703188417Sthompsa			/* abort TSF synchronization */
704188417Sthompsa			ural_write(sc, RAL_TXRX_CSR19, 0);
705184610Salfred
706188417Sthompsa			/* force tx led to stop blinking */
707188417Sthompsa			ural_write(sc, RAL_MAC_CSR20, 0);
708188417Sthompsa		}
709188417Sthompsa		break;
710184610Salfred
711188417Sthompsa	case IEEE80211_S_RUN:
712188417Sthompsa		ni = vap->iv_bss;
713188417Sthompsa
714188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
715188417Sthompsa			ural_update_slot(ic->ic_ifp);
716188417Sthompsa			ural_set_txpreamble(sc);
717188417Sthompsa			ural_set_basicrates(sc, ic->ic_bsschan);
718188419Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
719188419Sthompsa			ural_set_bssid(sc, sc->sc_bssid);
720188417Sthompsa		}
721188417Sthompsa
722188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
723188417Sthompsa		    vap->iv_opmode == IEEE80211_M_IBSS) {
724188417Sthompsa			m = ieee80211_beacon_alloc(ni, &uvp->bo);
725188417Sthompsa			if (m == NULL) {
726188417Sthompsa				device_printf(sc->sc_dev,
727188417Sthompsa				    "could not allocate beacon\n");
728191746Sthompsa				RAL_UNLOCK(sc);
729191746Sthompsa				IEEE80211_LOCK(ic);
730191746Sthompsa				return (-1);
731184610Salfred			}
732191710Sthompsa			ieee80211_ref_node(ni);
733188417Sthompsa			if (ural_tx_bcn(sc, m, ni) != 0) {
734188417Sthompsa				device_printf(sc->sc_dev,
735188417Sthompsa				    "could not send beacon\n");
736191746Sthompsa				RAL_UNLOCK(sc);
737191746Sthompsa				IEEE80211_LOCK(ic);
738191746Sthompsa				return (-1);
739184610Salfred			}
740184610Salfred		}
741184610Salfred
742188417Sthompsa		/* make tx led blink on tx (controlled by ASIC) */
743188417Sthompsa		ural_write(sc, RAL_MAC_CSR20, 1);
744184610Salfred
745188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR)
746188417Sthompsa			ural_enable_tsf_sync(sc);
747192468Ssam		else
748192468Ssam			ural_enable_tsf(sc);
749184610Salfred
750188417Sthompsa		/* enable automatic rate adaptation */
751192468Ssam		/* XXX should use ic_bsschan but not valid until after newstate call below */
752192468Ssam		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
753188417Sthompsa		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
754206358Srpaulo			ural_ratectl_start(sc, ni);
755184610Salfred
756188417Sthompsa		break;
757184610Salfred
758188417Sthompsa	default:
759188417Sthompsa		break;
760188417Sthompsa	}
761188417Sthompsa	RAL_UNLOCK(sc);
762188417Sthompsa	IEEE80211_LOCK(ic);
763191746Sthompsa	return (uvp->newstate(vap, nstate, arg));
764188417Sthompsa}
765184610Salfred
766184610Salfred
767188417Sthompsastatic void
768194677Sthompsaural_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
769188417Sthompsa{
770194677Sthompsa	struct ural_softc *sc = usbd_xfer_softc(xfer);
771188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
772192468Ssam	struct ieee80211vap *vap;
773188417Sthompsa	struct ural_tx_data *data;
774188417Sthompsa	struct mbuf *m;
775194677Sthompsa	struct usb_page_cache *pc;
776194677Sthompsa	int len;
777184610Salfred
778194677Sthompsa	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
779194677Sthompsa
780188417Sthompsa	switch (USB_GET_STATE(xfer)) {
781188417Sthompsa	case USB_ST_TRANSFERRED:
782194677Sthompsa		DPRINTFN(11, "transfer complete, %d bytes\n", len);
783184610Salfred
784188417Sthompsa		/* free resources */
785194677Sthompsa		data = usbd_xfer_get_priv(xfer);
786188417Sthompsa		ural_tx_free(data, 0);
787194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
788184610Salfred
789188417Sthompsa		ifp->if_opackets++;
790188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
791184610Salfred
792188417Sthompsa		/* FALLTHROUGH */
793188417Sthompsa	case USB_ST_SETUP:
794188417Sthompsatr_setup:
795188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
796188417Sthompsa		if (data) {
797188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
798188417Sthompsa			m = data->m;
799184610Salfred
800188417Sthompsa			if (m->m_pkthdr.len > (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) {
801188417Sthompsa				DPRINTFN(0, "data overflow, %u bytes\n",
802188417Sthompsa				    m->m_pkthdr.len);
803188417Sthompsa				m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE);
804188417Sthompsa			}
805194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
806194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, RAL_TX_DESC_SIZE);
807194677Sthompsa			usbd_m_copy_in(pc, RAL_TX_DESC_SIZE, m, 0,
808188417Sthompsa			    m->m_pkthdr.len);
809184610Salfred
810192468Ssam			vap = data->ni->ni_vap;
811192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
812188417Sthompsa				struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
813184610Salfred
814188417Sthompsa				tap->wt_flags = 0;
815188417Sthompsa				tap->wt_rate = data->rate;
816188417Sthompsa				tap->wt_antenna = sc->tx_ant;
817184610Salfred
818192468Ssam				ieee80211_radiotap_tx(vap, m);
819188417Sthompsa			}
820184610Salfred
821188417Sthompsa			/* xfer length needs to be a multiple of two! */
822188417Sthompsa			len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1;
823188417Sthompsa			if ((len % 64) == 0)
824188417Sthompsa				len += 2;
825184610Salfred
826188417Sthompsa			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
827188417Sthompsa			    m->m_pkthdr.len, len);
828184610Salfred
829194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, len);
830194677Sthompsa			usbd_xfer_set_priv(xfer, data);
831184610Salfred
832194228Sthompsa			usbd_transfer_submit(xfer);
833188417Sthompsa		}
834198099Sweongyo		RAL_UNLOCK(sc);
835198099Sweongyo		ural_start(ifp);
836198099Sweongyo		RAL_LOCK(sc);
837188417Sthompsa		break;
838184610Salfred
839188417Sthompsa	default:			/* Error */
840188417Sthompsa		DPRINTFN(11, "transfer error, %s\n",
841194677Sthompsa		    usbd_errstr(error));
842184610Salfred
843188419Sthompsa		ifp->if_oerrors++;
844194677Sthompsa		data = usbd_xfer_get_priv(xfer);
845188419Sthompsa		if (data != NULL) {
846194677Sthompsa			ural_tx_free(data, error);
847194677Sthompsa			usbd_xfer_set_priv(xfer, NULL);
848188419Sthompsa		}
849188419Sthompsa
850194677Sthompsa		if (error == USB_ERR_STALLED) {
851188417Sthompsa			/* try to clear stall first */
852194677Sthompsa			usbd_xfer_set_stall(xfer);
853188417Sthompsa			goto tr_setup;
854188417Sthompsa		}
855194677Sthompsa		if (error == USB_ERR_TIMEOUT)
856188417Sthompsa			device_printf(sc->sc_dev, "device timeout\n");
857188417Sthompsa		break;
858184610Salfred	}
859184610Salfred}
860184610Salfred
861184610Salfredstatic void
862194677Sthompsaural_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
863184610Salfred{
864194677Sthompsa	struct ural_softc *sc = usbd_xfer_softc(xfer);
865184610Salfred	struct ifnet *ifp = sc->sc_ifp;
866184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
867184610Salfred	struct ieee80211_node *ni;
868184610Salfred	struct mbuf *m = NULL;
869194677Sthompsa	struct usb_page_cache *pc;
870184610Salfred	uint32_t flags;
871192468Ssam	int8_t rssi = 0, nf = 0;
872194677Sthompsa	int len;
873184610Salfred
874194677Sthompsa	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
875194677Sthompsa
876184610Salfred	switch (USB_GET_STATE(xfer)) {
877184610Salfred	case USB_ST_TRANSFERRED:
878184610Salfred
879194677Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", len);
880184610Salfred
881188417Sthompsa		if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
882188417Sthompsa			DPRINTF("%s: xfer too short %d\n",
883188417Sthompsa			    device_get_nameunit(sc->sc_dev), len);
884184610Salfred			ifp->if_ierrors++;
885184610Salfred			goto tr_setup;
886184610Salfred		}
887184610Salfred
888188417Sthompsa		len -= RAL_RX_DESC_SIZE;
889188417Sthompsa		/* rx descriptor is located at the end */
890194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
891194677Sthompsa		usbd_copy_out(pc, len, &sc->sc_rx_desc, RAL_RX_DESC_SIZE);
892184610Salfred
893188417Sthompsa		rssi = URAL_RSSI(sc->sc_rx_desc.rssi);
894192468Ssam		nf = RAL_NOISE_FLOOR;
895184610Salfred		flags = le32toh(sc->sc_rx_desc.flags);
896184610Salfred		if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
897184610Salfred			/*
898184610Salfred		         * This should not happen since we did not
899184610Salfred		         * request to receive those frames when we
900184610Salfred		         * filled RAL_TXRX_CSR2:
901184610Salfred		         */
902188417Sthompsa			DPRINTFN(5, "PHY or CRC error\n");
903184610Salfred			ifp->if_ierrors++;
904184610Salfred			goto tr_setup;
905184610Salfred		}
906184610Salfred
907184610Salfred		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
908184610Salfred		if (m == NULL) {
909184610Salfred			DPRINTF("could not allocate mbuf\n");
910184610Salfred			ifp->if_ierrors++;
911184610Salfred			goto tr_setup;
912184610Salfred		}
913194677Sthompsa		usbd_copy_out(pc, 0, mtod(m, uint8_t *), len);
914184610Salfred
915184610Salfred		/* finalize mbuf */
916184610Salfred		m->m_pkthdr.rcvif = ifp;
917188417Sthompsa		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
918184610Salfred
919192468Ssam		if (ieee80211_radiotap_active(ic)) {
920184610Salfred			struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
921184610Salfred
922192468Ssam			/* XXX set once */
923192468Ssam			tap->wr_flags = 0;
924184610Salfred			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
925188417Sthompsa			    (flags & RAL_RX_OFDM) ?
926184610Salfred			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
927188417Sthompsa			tap->wr_antenna = sc->rx_ant;
928192468Ssam			tap->wr_antsignal = nf + rssi;
929192468Ssam			tap->wr_antnoise = nf;
930184610Salfred		}
931184610Salfred		/* Strip trailing 802.11 MAC FCS. */
932184610Salfred		m_adj(m, -IEEE80211_CRC_LEN);
933184610Salfred
934188417Sthompsa		/* FALLTHROUGH */
935184610Salfred	case USB_ST_SETUP:
936184610Salfredtr_setup:
937194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
938194228Sthompsa		usbd_transfer_submit(xfer);
939184610Salfred
940184610Salfred		/*
941184610Salfred		 * At the end of a USB callback it is always safe to unlock
942184610Salfred		 * the private mutex of a device! That is why we do the
943184610Salfred		 * "ieee80211_input" here, and not some lines up!
944184610Salfred		 */
945198099Sweongyo		RAL_UNLOCK(sc);
946184610Salfred		if (m) {
947188417Sthompsa			ni = ieee80211_find_rxnode(ic,
948188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
949188417Sthompsa			if (ni != NULL) {
950192468Ssam				(void) ieee80211_input(ni, m, rssi, nf);
951184610Salfred				ieee80211_free_node(ni);
952188417Sthompsa			} else
953192468Ssam				(void) ieee80211_input_all(ic, m, rssi, nf);
954184610Salfred		}
955198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
956198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
957198099Sweongyo			ural_start(ifp);
958198099Sweongyo		RAL_LOCK(sc);
959184610Salfred		return;
960184610Salfred
961184610Salfred	default:			/* Error */
962194677Sthompsa		if (error != USB_ERR_CANCELLED) {
963184610Salfred			/* try to clear stall first */
964194677Sthompsa			usbd_xfer_set_stall(xfer);
965188417Sthompsa			goto tr_setup;
966184610Salfred		}
967184610Salfred		return;
968184610Salfred	}
969184610Salfred}
970184610Salfred
971184610Salfredstatic uint8_t
972188417Sthompsaural_plcp_signal(int rate)
973184610Salfred{
974184610Salfred	switch (rate) {
975188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
976188417Sthompsa	case 12:	return 0xb;
977188417Sthompsa	case 18:	return 0xf;
978188417Sthompsa	case 24:	return 0xa;
979188417Sthompsa	case 36:	return 0xe;
980188417Sthompsa	case 48:	return 0x9;
981188417Sthompsa	case 72:	return 0xd;
982188417Sthompsa	case 96:	return 0x8;
983188417Sthompsa	case 108:	return 0xc;
984184610Salfred
985188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
986188417Sthompsa	case 2:		return 0x0;
987188417Sthompsa	case 4:		return 0x1;
988188417Sthompsa	case 11:	return 0x2;
989188417Sthompsa	case 22:	return 0x3;
990184610Salfred	}
991188417Sthompsa	return 0xff;		/* XXX unsupported/unknown rate */
992184610Salfred}
993184610Salfred
994184610Salfredstatic void
995188417Sthompsaural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
996188417Sthompsa    uint32_t flags, int len, int rate)
997184610Salfred{
998184610Salfred	struct ifnet *ifp = sc->sc_ifp;
999184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1000184610Salfred	uint16_t plcp_length;
1001188417Sthompsa	int remainder;
1002184610Salfred
1003188417Sthompsa	desc->flags = htole32(flags);
1004188417Sthompsa	desc->flags |= htole32(RAL_TX_NEWSEQ);
1005188417Sthompsa	desc->flags |= htole32(len << 16);
1006184610Salfred
1007188417Sthompsa	desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1008188417Sthompsa	desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1009184610Salfred
1010188417Sthompsa	/* setup PLCP fields */
1011188417Sthompsa	desc->plcp_signal  = ural_plcp_signal(rate);
1012188417Sthompsa	desc->plcp_service = 4;
1013188417Sthompsa
1014188417Sthompsa	len += IEEE80211_CRC_LEN;
1015190532Ssam	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1016188417Sthompsa		desc->flags |= htole32(RAL_TX_OFDM);
1017188417Sthompsa
1018188417Sthompsa		plcp_length = len & 0xfff;
1019188417Sthompsa		desc->plcp_length_hi = plcp_length >> 6;
1020188417Sthompsa		desc->plcp_length_lo = plcp_length & 0x3f;
1021188417Sthompsa	} else {
1022188417Sthompsa		plcp_length = (16 * len + rate - 1) / rate;
1023188417Sthompsa		if (rate == 22) {
1024188417Sthompsa			remainder = (16 * len) % 22;
1025188417Sthompsa			if (remainder != 0 && remainder < 7)
1026188417Sthompsa				desc->plcp_service |= RAL_PLCP_LENGEXT;
1027188417Sthompsa		}
1028188417Sthompsa		desc->plcp_length_hi = plcp_length >> 8;
1029188417Sthompsa		desc->plcp_length_lo = plcp_length & 0xff;
1030188417Sthompsa
1031188417Sthompsa		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1032188417Sthompsa			desc->plcp_signal |= 0x08;
1033184610Salfred	}
1034184610Salfred
1035188417Sthompsa	desc->iv = 0;
1036188417Sthompsa	desc->eiv = 0;
1037188417Sthompsa}
1038184610Salfred
1039188417Sthompsa#define RAL_TX_TIMEOUT	5000
1040184610Salfred
1041188417Sthompsastatic int
1042188417Sthompsaural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1043188417Sthompsa{
1044188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1045188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1046188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1047188417Sthompsa	const struct ieee80211_txparam *tp;
1048188417Sthompsa	struct ural_tx_data *data;
1049188417Sthompsa
1050188417Sthompsa	if (sc->tx_nfree == 0) {
1051188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1052188417Sthompsa		m_freem(m0);
1053188417Sthompsa		ieee80211_free_node(ni);
1054188417Sthompsa		return EIO;
1055184610Salfred	}
1056188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1057188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1058188417Sthompsa	sc->tx_nfree--;
1059188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1060184610Salfred
1061188417Sthompsa	data->m = m0;
1062188417Sthompsa	data->ni = ni;
1063188417Sthompsa	data->rate = tp->mgmtrate;
1064184610Salfred
1065188417Sthompsa	ural_setup_tx_desc(sc, &data->desc,
1066188417Sthompsa	    RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len,
1067188417Sthompsa	    tp->mgmtrate);
1068184610Salfred
1069188417Sthompsa	DPRINTFN(10, "sending beacon frame len=%u rate=%u\n",
1070188417Sthompsa	    m0->m_pkthdr.len, tp->mgmtrate);
1071184610Salfred
1072188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1073194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1074184610Salfred
1075188417Sthompsa	return (0);
1076188417Sthompsa}
1077184610Salfred
1078188417Sthompsastatic int
1079188417Sthompsaural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1080188417Sthompsa{
1081188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1082188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1083188417Sthompsa	const struct ieee80211_txparam *tp;
1084188417Sthompsa	struct ural_tx_data *data;
1085188417Sthompsa	struct ieee80211_frame *wh;
1086188417Sthompsa	struct ieee80211_key *k;
1087188417Sthompsa	uint32_t flags;
1088188417Sthompsa	uint16_t dur;
1089184610Salfred
1090188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1091184610Salfred
1092188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1093188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1094188417Sthompsa	sc->tx_nfree--;
1095184610Salfred
1096188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1097188417Sthompsa
1098188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1099188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1100188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1101188417Sthompsa		if (k == NULL) {
1102188417Sthompsa			m_freem(m0);
1103188417Sthompsa			return ENOBUFS;
1104184610Salfred		}
1105188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1106184610Salfred	}
1107184610Salfred
1108188417Sthompsa	data->m = m0;
1109188417Sthompsa	data->ni = ni;
1110188417Sthompsa	data->rate = tp->mgmtrate;
1111184610Salfred
1112188417Sthompsa	flags = 0;
1113188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1114188417Sthompsa		flags |= RAL_TX_ACK;
1115188417Sthompsa
1116190532Ssam		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1117188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1118188417Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
1119188417Sthompsa
1120188417Sthompsa		/* tell hardware to add timestamp for probe responses */
1121188417Sthompsa		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1122188417Sthompsa		    IEEE80211_FC0_TYPE_MGT &&
1123188417Sthompsa		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1124188417Sthompsa		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1125188417Sthompsa			flags |= RAL_TX_TIMESTAMP;
1126184610Salfred	}
1127184610Salfred
1128188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate);
1129184610Salfred
1130188417Sthompsa	DPRINTFN(10, "sending mgt frame len=%u rate=%u\n",
1131188417Sthompsa	    m0->m_pkthdr.len, tp->mgmtrate);
1132184610Salfred
1133188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1134194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1135184610Salfred
1136188417Sthompsa	return 0;
1137184610Salfred}
1138184610Salfred
1139188417Sthompsastatic int
1140188417Sthompsaural_sendprot(struct ural_softc *sc,
1141188417Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1142184610Salfred{
1143188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1144188417Sthompsa	const struct ieee80211_frame *wh;
1145188417Sthompsa	struct ural_tx_data *data;
1146188417Sthompsa	struct mbuf *mprot;
1147188417Sthompsa	int protrate, ackrate, pktlen, flags, isshort;
1148188417Sthompsa	uint16_t dur;
1149184610Salfred
1150188417Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1151188417Sthompsa	    ("protection %d", prot));
1152184610Salfred
1153188417Sthompsa	wh = mtod(m, const struct ieee80211_frame *);
1154188417Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1155184610Salfred
1156190532Ssam	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1157190532Ssam	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1158188417Sthompsa
1159188417Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1160190532Ssam	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort);
1161190532Ssam	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1162188417Sthompsa	flags = RAL_TX_RETRY(7);
1163188417Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
1164188417Sthompsa		/* NB: CTS is the same size as an ACK */
1165190532Ssam		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1166188417Sthompsa		flags |= RAL_TX_ACK;
1167188417Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1168188417Sthompsa	} else {
1169188417Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1170188417Sthompsa	}
1171188417Sthompsa	if (mprot == NULL) {
1172188417Sthompsa		/* XXX stat + msg */
1173188417Sthompsa		return ENOBUFS;
1174188417Sthompsa	}
1175188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1176188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1177188417Sthompsa	sc->tx_nfree--;
1178188417Sthompsa
1179188417Sthompsa	data->m = mprot;
1180188417Sthompsa	data->ni = ieee80211_ref_node(ni);
1181188417Sthompsa	data->rate = protrate;
1182188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate);
1183188417Sthompsa
1184188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1185194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1186188417Sthompsa
1187188417Sthompsa	return 0;
1188188417Sthompsa}
1189188417Sthompsa
1190188417Sthompsastatic int
1191188417Sthompsaural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1192188417Sthompsa    const struct ieee80211_bpf_params *params)
1193188417Sthompsa{
1194193073Ssam	struct ieee80211com *ic = ni->ni_ic;
1195188417Sthompsa	struct ural_tx_data *data;
1196188417Sthompsa	uint32_t flags;
1197188417Sthompsa	int error;
1198188417Sthompsa	int rate;
1199188417Sthompsa
1200188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1201188417Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
1202188417Sthompsa
1203193073Ssam	rate = params->ibp_rate0;
1204193073Ssam	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1205188417Sthompsa		m_freem(m0);
1206188417Sthompsa		return EINVAL;
1207188417Sthompsa	}
1208188417Sthompsa	flags = 0;
1209188417Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1210188417Sthompsa		flags |= RAL_TX_ACK;
1211188417Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1212188417Sthompsa		error = ural_sendprot(sc, m0, ni,
1213188417Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
1214188417Sthompsa			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1215188417Sthompsa		    rate);
1216188969Sthompsa		if (error || sc->tx_nfree == 0) {
1217188417Sthompsa			m_freem(m0);
1218188969Sthompsa			return ENOBUFS;
1219184610Salfred		}
1220188417Sthompsa		flags |= RAL_TX_IFS_SIFS;
1221188417Sthompsa	}
1222184610Salfred
1223188969Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1224188969Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1225188969Sthompsa	sc->tx_nfree--;
1226188969Sthompsa
1227188417Sthompsa	data->m = m0;
1228188417Sthompsa	data->ni = ni;
1229188417Sthompsa	data->rate = rate;
1230184610Salfred
1231188417Sthompsa	/* XXX need to setup descriptor ourself */
1232188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1233184610Salfred
1234188417Sthompsa	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1235188417Sthompsa	    m0->m_pkthdr.len, rate);
1236184610Salfred
1237188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1238194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1239184610Salfred
1240188417Sthompsa	return 0;
1241188417Sthompsa}
1242184610Salfred
1243188417Sthompsastatic int
1244188417Sthompsaural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1245188417Sthompsa{
1246188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1247188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1248188417Sthompsa	struct ural_tx_data *data;
1249188417Sthompsa	struct ieee80211_frame *wh;
1250188417Sthompsa	const struct ieee80211_txparam *tp;
1251188417Sthompsa	struct ieee80211_key *k;
1252188417Sthompsa	uint32_t flags = 0;
1253188417Sthompsa	uint16_t dur;
1254188417Sthompsa	int error, rate;
1255184610Salfred
1256188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
1257184610Salfred
1258188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1259184610Salfred
1260188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1261188417Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1262188417Sthompsa		rate = tp->mcastrate;
1263188417Sthompsa	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1264188417Sthompsa		rate = tp->ucastrate;
1265188417Sthompsa	else
1266188417Sthompsa		rate = ni->ni_txrate;
1267188417Sthompsa
1268188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1269188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1270188417Sthompsa		if (k == NULL) {
1271188417Sthompsa			m_freem(m0);
1272188417Sthompsa			return ENOBUFS;
1273184610Salfred		}
1274188417Sthompsa		/* packet header may have moved, reset our local pointer */
1275188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1276188417Sthompsa	}
1277184610Salfred
1278188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1279188417Sthompsa		int prot = IEEE80211_PROT_NONE;
1280188417Sthompsa		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1281188417Sthompsa			prot = IEEE80211_PROT_RTSCTS;
1282188417Sthompsa		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1283190532Ssam		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1284188417Sthompsa			prot = ic->ic_protmode;
1285188417Sthompsa		if (prot != IEEE80211_PROT_NONE) {
1286188417Sthompsa			error = ural_sendprot(sc, m0, ni, prot, rate);
1287188969Sthompsa			if (error || sc->tx_nfree == 0) {
1288188417Sthompsa				m_freem(m0);
1289188969Sthompsa				return ENOBUFS;
1290188417Sthompsa			}
1291188417Sthompsa			flags |= RAL_TX_IFS_SIFS;
1292184610Salfred		}
1293184610Salfred	}
1294184610Salfred
1295188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1296188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1297188417Sthompsa	sc->tx_nfree--;
1298184610Salfred
1299188417Sthompsa	data->m = m0;
1300188417Sthompsa	data->ni = ni;
1301188417Sthompsa	data->rate = rate;
1302188417Sthompsa
1303188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1304188417Sthompsa		flags |= RAL_TX_ACK;
1305188417Sthompsa		flags |= RAL_TX_RETRY(7);
1306188417Sthompsa
1307190532Ssam		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1308188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1309188417Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
1310184610Salfred	}
1311184610Salfred
1312188417Sthompsa	ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1313184610Salfred
1314188417Sthompsa	DPRINTFN(10, "sending data frame len=%u rate=%u\n",
1315188417Sthompsa	    m0->m_pkthdr.len, rate);
1316184610Salfred
1317188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1318194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1319188417Sthompsa
1320188417Sthompsa	return 0;
1321184610Salfred}
1322184610Salfred
1323184610Salfredstatic void
1324188417Sthompsaural_start(struct ifnet *ifp)
1325184610Salfred{
1326188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
1327188417Sthompsa	struct ieee80211_node *ni;
1328188417Sthompsa	struct mbuf *m;
1329184610Salfred
1330188417Sthompsa	RAL_LOCK(sc);
1331188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1332188417Sthompsa		RAL_UNLOCK(sc);
1333188417Sthompsa		return;
1334188417Sthompsa	}
1335188417Sthompsa	for (;;) {
1336188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1337188417Sthompsa		if (m == NULL)
1338188417Sthompsa			break;
1339188969Sthompsa		if (sc->tx_nfree < RAL_TX_MINFREE) {
1340188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1341188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1342188417Sthompsa			break;
1343188417Sthompsa		}
1344188417Sthompsa		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1345188417Sthompsa		if (ural_tx_data(sc, m, ni) != 0) {
1346188417Sthompsa			ieee80211_free_node(ni);
1347188417Sthompsa			ifp->if_oerrors++;
1348188417Sthompsa			break;
1349188417Sthompsa		}
1350188417Sthompsa	}
1351188417Sthompsa	RAL_UNLOCK(sc);
1352184610Salfred}
1353184610Salfred
1354184610Salfredstatic int
1355188417Sthompsaural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1356184610Salfred{
1357184610Salfred	struct ural_softc *sc = ifp->if_softc;
1358184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1359188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
1360188417Sthompsa	int error = 0, startall = 0;
1361184610Salfred
1362184610Salfred	switch (cmd) {
1363184610Salfred	case SIOCSIFFLAGS:
1364188417Sthompsa		RAL_LOCK(sc);
1365184610Salfred		if (ifp->if_flags & IFF_UP) {
1366188417Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1367191746Sthompsa				ural_init_locked(sc);
1368188417Sthompsa				startall = 1;
1369188417Sthompsa			} else
1370191746Sthompsa				ural_setpromisc(sc);
1371184610Salfred		} else {
1372191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1373191746Sthompsa				ural_stop(sc);
1374184610Salfred		}
1375188417Sthompsa		RAL_UNLOCK(sc);
1376188417Sthompsa		if (startall)
1377188417Sthompsa			ieee80211_start_all(ic);
1378184610Salfred		break;
1379184610Salfred	case SIOCGIFMEDIA:
1380184610Salfred	case SIOCSIFMEDIA:
1381188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1382184610Salfred		break;
1383184610Salfred	default:
1384184610Salfred		error = ether_ioctl(ifp, cmd, data);
1385188417Sthompsa		break;
1386184610Salfred	}
1387188417Sthompsa	return error;
1388184610Salfred}
1389184610Salfred
1390184610Salfredstatic void
1391188417Sthompsaural_set_testmode(struct ural_softc *sc)
1392184610Salfred{
1393192984Sthompsa	struct usb_device_request req;
1394193045Sthompsa	usb_error_t error;
1395184610Salfred
1396188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1397188417Sthompsa	req.bRequest = RAL_VENDOR_REQUEST;
1398188417Sthompsa	USETW(req.wValue, 4);
1399188417Sthompsa	USETW(req.wIndex, 1);
1400188417Sthompsa	USETW(req.wLength, 0);
1401188417Sthompsa
1402188419Sthompsa	error = ural_do_request(sc, &req, NULL);
1403188417Sthompsa	if (error != 0) {
1404188417Sthompsa		device_printf(sc->sc_dev, "could not set test mode: %s\n",
1405194228Sthompsa		    usbd_errstr(error));
1406188417Sthompsa	}
1407184610Salfred}
1408184610Salfred
1409184610Salfredstatic void
1410188417Sthompsaural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1411184610Salfred{
1412192984Sthompsa	struct usb_device_request req;
1413193045Sthompsa	usb_error_t error;
1414184610Salfred
1415188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1416188417Sthompsa	req.bRequest = RAL_READ_EEPROM;
1417188417Sthompsa	USETW(req.wValue, 0);
1418188417Sthompsa	USETW(req.wIndex, addr);
1419188417Sthompsa	USETW(req.wLength, len);
1420184610Salfred
1421188419Sthompsa	error = ural_do_request(sc, &req, buf);
1422188417Sthompsa	if (error != 0) {
1423188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1424194228Sthompsa		    usbd_errstr(error));
1425184610Salfred	}
1426188417Sthompsa}
1427184610Salfred
1428188417Sthompsastatic uint16_t
1429188417Sthompsaural_read(struct ural_softc *sc, uint16_t reg)
1430188417Sthompsa{
1431192984Sthompsa	struct usb_device_request req;
1432193045Sthompsa	usb_error_t error;
1433188417Sthompsa	uint16_t val;
1434184610Salfred
1435188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1436188417Sthompsa	req.bRequest = RAL_READ_MAC;
1437188417Sthompsa	USETW(req.wValue, 0);
1438188417Sthompsa	USETW(req.wIndex, reg);
1439188417Sthompsa	USETW(req.wLength, sizeof (uint16_t));
1440188417Sthompsa
1441188419Sthompsa	error = ural_do_request(sc, &req, &val);
1442188417Sthompsa	if (error != 0) {
1443188417Sthompsa		device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1444194228Sthompsa		    usbd_errstr(error));
1445188417Sthompsa		return 0;
1446184610Salfred	}
1447184610Salfred
1448188417Sthompsa	return le16toh(val);
1449184610Salfred}
1450184610Salfred
1451188417Sthompsastatic void
1452188417Sthompsaural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1453184610Salfred{
1454192984Sthompsa	struct usb_device_request req;
1455193045Sthompsa	usb_error_t error;
1456184610Salfred
1457188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1458188417Sthompsa	req.bRequest = RAL_READ_MULTI_MAC;
1459188417Sthompsa	USETW(req.wValue, 0);
1460188417Sthompsa	USETW(req.wIndex, reg);
1461188417Sthompsa	USETW(req.wLength, len);
1462184610Salfred
1463188419Sthompsa	error = ural_do_request(sc, &req, buf);
1464188417Sthompsa	if (error != 0) {
1465188417Sthompsa		device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1466194228Sthompsa		    usbd_errstr(error));
1467184610Salfred	}
1468188417Sthompsa}
1469188417Sthompsa
1470188417Sthompsastatic void
1471188417Sthompsaural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1472188417Sthompsa{
1473192984Sthompsa	struct usb_device_request req;
1474193045Sthompsa	usb_error_t error;
1475188417Sthompsa
1476188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1477188417Sthompsa	req.bRequest = RAL_WRITE_MAC;
1478188417Sthompsa	USETW(req.wValue, val);
1479188417Sthompsa	USETW(req.wIndex, reg);
1480188417Sthompsa	USETW(req.wLength, 0);
1481188417Sthompsa
1482188419Sthompsa	error = ural_do_request(sc, &req, NULL);
1483188417Sthompsa	if (error != 0) {
1484188417Sthompsa		device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1485194228Sthompsa		    usbd_errstr(error));
1486184610Salfred	}
1487188417Sthompsa}
1488184610Salfred
1489188417Sthompsastatic void
1490188417Sthompsaural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1491188417Sthompsa{
1492192984Sthompsa	struct usb_device_request req;
1493193045Sthompsa	usb_error_t error;
1494184610Salfred
1495188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1496188417Sthompsa	req.bRequest = RAL_WRITE_MULTI_MAC;
1497188417Sthompsa	USETW(req.wValue, 0);
1498188417Sthompsa	USETW(req.wIndex, reg);
1499188417Sthompsa	USETW(req.wLength, len);
1500184610Salfred
1501188419Sthompsa	error = ural_do_request(sc, &req, buf);
1502188417Sthompsa	if (error != 0) {
1503188417Sthompsa		device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1504194228Sthompsa		    usbd_errstr(error));
1505188417Sthompsa	}
1506184610Salfred}
1507184610Salfred
1508184610Salfredstatic void
1509188417Sthompsaural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1510184610Salfred{
1511188417Sthompsa	uint16_t tmp;
1512188417Sthompsa	int ntries;
1513184610Salfred
1514188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1515188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1516188417Sthompsa			break;
1517188619Sthompsa		if (ural_pause(sc, hz / 100))
1518188619Sthompsa			break;
1519188417Sthompsa	}
1520188619Sthompsa	if (ntries == 100) {
1521188417Sthompsa		device_printf(sc->sc_dev, "could not write to BBP\n");
1522188417Sthompsa		return;
1523188417Sthompsa	}
1524184610Salfred
1525188417Sthompsa	tmp = reg << 8 | val;
1526188417Sthompsa	ural_write(sc, RAL_PHY_CSR7, tmp);
1527188417Sthompsa}
1528184610Salfred
1529188417Sthompsastatic uint8_t
1530188417Sthompsaural_bbp_read(struct ural_softc *sc, uint8_t reg)
1531188417Sthompsa{
1532188417Sthompsa	uint16_t val;
1533188417Sthompsa	int ntries;
1534184610Salfred
1535188417Sthompsa	val = RAL_BBP_WRITE | reg << 8;
1536188417Sthompsa	ural_write(sc, RAL_PHY_CSR7, val);
1537188417Sthompsa
1538188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1539188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1540188417Sthompsa			break;
1541188619Sthompsa		if (ural_pause(sc, hz / 100))
1542188619Sthompsa			break;
1543188417Sthompsa	}
1544188619Sthompsa	if (ntries == 100) {
1545188417Sthompsa		device_printf(sc->sc_dev, "could not read BBP\n");
1546188417Sthompsa		return 0;
1547188417Sthompsa	}
1548188417Sthompsa
1549188417Sthompsa	return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1550184610Salfred}
1551184610Salfred
1552184610Salfredstatic void
1553188417Sthompsaural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1554184610Salfred{
1555188417Sthompsa	uint32_t tmp;
1556188417Sthompsa	int ntries;
1557188417Sthompsa
1558188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1559188417Sthompsa		if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1560188417Sthompsa			break;
1561188619Sthompsa		if (ural_pause(sc, hz / 100))
1562188619Sthompsa			break;
1563188417Sthompsa	}
1564188619Sthompsa	if (ntries == 100) {
1565188417Sthompsa		device_printf(sc->sc_dev, "could not write to RF\n");
1566188417Sthompsa		return;
1567188417Sthompsa	}
1568188417Sthompsa
1569188417Sthompsa	tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1570188417Sthompsa	ural_write(sc, RAL_PHY_CSR9,  tmp & 0xffff);
1571188417Sthompsa	ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1572188417Sthompsa
1573188417Sthompsa	/* remember last written value in sc */
1574188417Sthompsa	sc->rf_regs[reg] = val;
1575188417Sthompsa
1576188417Sthompsa	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
1577184610Salfred}
1578184610Salfred
1579184610Salfredstatic void
1580188417Sthompsaural_scan_start(struct ieee80211com *ic)
1581184610Salfred{
1582191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
1583191746Sthompsa	struct ural_softc *sc = ifp->if_softc;
1584188417Sthompsa
1585188417Sthompsa	RAL_LOCK(sc);
1586191746Sthompsa	ural_write(sc, RAL_TXRX_CSR19, 0);
1587191746Sthompsa	ural_set_bssid(sc, ifp->if_broadcastaddr);
1588188417Sthompsa	RAL_UNLOCK(sc);
1589184610Salfred}
1590184610Salfred
1591184610Salfredstatic void
1592188417Sthompsaural_scan_end(struct ieee80211com *ic)
1593184610Salfred{
1594188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
1595188417Sthompsa
1596188417Sthompsa	RAL_LOCK(sc);
1597191746Sthompsa	ural_enable_tsf_sync(sc);
1598191746Sthompsa	ural_set_bssid(sc, sc->sc_bssid);
1599188417Sthompsa	RAL_UNLOCK(sc);
1600188417Sthompsa
1601184610Salfred}
1602184610Salfred
1603184610Salfredstatic void
1604188417Sthompsaural_set_channel(struct ieee80211com *ic)
1605184610Salfred{
1606188417Sthompsa	struct ural_softc *sc = ic->ic_ifp->if_softc;
1607188417Sthompsa
1608188417Sthompsa	RAL_LOCK(sc);
1609191746Sthompsa	ural_set_chan(sc, ic->ic_curchan);
1610188417Sthompsa	RAL_UNLOCK(sc);
1611184610Salfred}
1612184610Salfred
1613184610Salfredstatic void
1614188417Sthompsaural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1615184610Salfred{
1616188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1617188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1618188417Sthompsa	uint8_t power, tmp;
1619188619Sthompsa	int i, chan;
1620184610Salfred
1621188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
1622188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1623188417Sthompsa		return;
1624184610Salfred
1625188417Sthompsa	if (IEEE80211_IS_CHAN_2GHZ(c))
1626188417Sthompsa		power = min(sc->txpow[chan - 1], 31);
1627184610Salfred	else
1628184610Salfred		power = 31;
1629184610Salfred
1630184610Salfred	/* adjust txpower using ifconfig settings */
1631188417Sthompsa	power -= (100 - ic->ic_txpowlimit) / 8;
1632184610Salfred
1633188417Sthompsa	DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power);
1634184610Salfred
1635188417Sthompsa	switch (sc->rf_rev) {
1636184610Salfred	case RAL_RF_2522:
1637188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x00814);
1638188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1639188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1640184610Salfred		break;
1641184610Salfred
1642184610Salfred	case RAL_RF_2523:
1643188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08804);
1644188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1645188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1646188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1647184610Salfred		break;
1648184610Salfred
1649184610Salfred	case RAL_RF_2524:
1650188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x0c808);
1651188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1652188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1653188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1654184610Salfred		break;
1655184610Salfred
1656184610Salfred	case RAL_RF_2525:
1657188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1658188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1659188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1660188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1661184610Salfred
1662188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1663188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1664188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1665188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1666184610Salfred		break;
1667184610Salfred
1668184610Salfred	case RAL_RF_2525E:
1669188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08808);
1670188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1671188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1672188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1673184610Salfred		break;
1674184610Salfred
1675184610Salfred	case RAL_RF_2526:
1676188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1677188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1678188417Sthompsa		ural_rf_write(sc, RAL_RF1, 0x08804);
1679184610Salfred
1680188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1681188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1682188417Sthompsa		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1683184610Salfred		break;
1684184610Salfred
1685188417Sthompsa	/* dual-band RF */
1686184610Salfred	case RAL_RF_5222:
1687188417Sthompsa		for (i = 0; ural_rf5222[i].chan != chan; i++);
1688188417Sthompsa
1689188417Sthompsa		ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1690188417Sthompsa		ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1691188417Sthompsa		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1692188417Sthompsa		ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1693184610Salfred		break;
1694184610Salfred	}
1695184610Salfred
1696188417Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1697188417Sthompsa	    (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1698184610Salfred		/* set Japan filter bit for channel 14 */
1699188417Sthompsa		tmp = ural_bbp_read(sc, 70);
1700184610Salfred
1701188417Sthompsa		tmp &= ~RAL_JAPAN_FILTER;
1702188417Sthompsa		if (chan == 14)
1703184610Salfred			tmp |= RAL_JAPAN_FILTER;
1704184610Salfred
1705188417Sthompsa		ural_bbp_write(sc, 70, tmp);
1706184610Salfred
1707184610Salfred		/* clear CRC errors */
1708188417Sthompsa		ural_read(sc, RAL_STA_CSR0);
1709184610Salfred
1710188619Sthompsa		ural_pause(sc, hz / 100);
1711188417Sthompsa		ural_disable_rf_tune(sc);
1712184610Salfred	}
1713188417Sthompsa
1714188417Sthompsa	/* XXX doesn't belong here */
1715184610Salfred	/* update basic rate set */
1716188417Sthompsa	ural_set_basicrates(sc, c);
1717189123Sthompsa
1718189123Sthompsa	/* give the hardware some time to do the switchover */
1719189123Sthompsa	ural_pause(sc, hz / 100);
1720184610Salfred}
1721184610Salfred
1722188417Sthompsa/*
1723188417Sthompsa * Disable RF auto-tuning.
1724188417Sthompsa */
1725184610Salfredstatic void
1726188417Sthompsaural_disable_rf_tune(struct ural_softc *sc)
1727184610Salfred{
1728188417Sthompsa	uint32_t tmp;
1729184610Salfred
1730188417Sthompsa	if (sc->rf_rev != RAL_RF_2523) {
1731188417Sthompsa		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1732188417Sthompsa		ural_rf_write(sc, RAL_RF1, tmp);
1733184610Salfred	}
1734184610Salfred
1735188417Sthompsa	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1736188417Sthompsa	ural_rf_write(sc, RAL_RF3, tmp);
1737184610Salfred
1738188417Sthompsa	DPRINTFN(2, "disabling RF autotune\n");
1739184610Salfred}
1740184610Salfred
1741188417Sthompsa/*
1742188417Sthompsa * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1743188417Sthompsa * synchronization.
1744188417Sthompsa */
1745184610Salfredstatic void
1746188417Sthompsaural_enable_tsf_sync(struct ural_softc *sc)
1747184610Salfred{
1748188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1749188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1750188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1751188417Sthompsa	uint16_t logcwmin, preload, tmp;
1752184610Salfred
1753184610Salfred	/* first, disable TSF synchronization */
1754188417Sthompsa	ural_write(sc, RAL_TXRX_CSR19, 0);
1755184610Salfred
1756188417Sthompsa	tmp = (16 * vap->iv_bss->ni_intval) << 4;
1757188417Sthompsa	ural_write(sc, RAL_TXRX_CSR18, tmp);
1758184610Salfred
1759188417Sthompsa	logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1760188417Sthompsa	preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1761188417Sthompsa	tmp = logcwmin << 12 | preload;
1762188417Sthompsa	ural_write(sc, RAL_TXRX_CSR20, tmp);
1763184610Salfred
1764184610Salfred	/* finally, enable TSF synchronization */
1765184610Salfred	tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1766188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA)
1767184610Salfred		tmp |= RAL_ENABLE_TSF_SYNC(1);
1768184610Salfred	else
1769184610Salfred		tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1770188417Sthompsa	ural_write(sc, RAL_TXRX_CSR19, tmp);
1771184610Salfred
1772184610Salfred	DPRINTF("enabling TSF synchronization\n");
1773184610Salfred}
1774184610Salfred
1775192468Ssamstatic void
1776192468Ssamural_enable_tsf(struct ural_softc *sc)
1777192468Ssam{
1778192468Ssam	/* first, disable TSF synchronization */
1779192468Ssam	ural_write(sc, RAL_TXRX_CSR19, 0);
1780192468Ssam	ural_write(sc, RAL_TXRX_CSR19, RAL_ENABLE_TSF | RAL_ENABLE_TSF_SYNC(2));
1781192468Ssam}
1782192468Ssam
1783188417Sthompsa#define RAL_RXTX_TURNAROUND	5	/* us */
1784184610Salfredstatic void
1785188417Sthompsaural_update_slot(struct ifnet *ifp)
1786184610Salfred{
1787188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
1788188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1789188417Sthompsa	uint16_t slottime, sifs, eifs;
1790184610Salfred
1791188417Sthompsa	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1792184610Salfred
1793184610Salfred	/*
1794184610Salfred	 * These settings may sound a bit inconsistent but this is what the
1795184610Salfred	 * reference driver does.
1796184610Salfred	 */
1797188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1798184610Salfred		sifs = 16 - RAL_RXTX_TURNAROUND;
1799184610Salfred		eifs = 364;
1800184610Salfred	} else {
1801184610Salfred		sifs = 10 - RAL_RXTX_TURNAROUND;
1802184610Salfred		eifs = 64;
1803184610Salfred	}
1804184610Salfred
1805188417Sthompsa	ural_write(sc, RAL_MAC_CSR10, slottime);
1806188417Sthompsa	ural_write(sc, RAL_MAC_CSR11, sifs);
1807188417Sthompsa	ural_write(sc, RAL_MAC_CSR12, eifs);
1808184610Salfred}
1809184610Salfred
1810184610Salfredstatic void
1811188417Sthompsaural_set_txpreamble(struct ural_softc *sc)
1812184610Salfred{
1813188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1814188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1815184610Salfred	uint16_t tmp;
1816184610Salfred
1817188417Sthompsa	tmp = ural_read(sc, RAL_TXRX_CSR10);
1818184610Salfred
1819188417Sthompsa	tmp &= ~RAL_SHORT_PREAMBLE;
1820188417Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1821184610Salfred		tmp |= RAL_SHORT_PREAMBLE;
1822184610Salfred
1823188417Sthompsa	ural_write(sc, RAL_TXRX_CSR10, tmp);
1824184610Salfred}
1825184610Salfred
1826184610Salfredstatic void
1827188417Sthompsaural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c)
1828184610Salfred{
1829188417Sthompsa	/* XXX wrong, take from rate set */
1830188417Sthompsa	/* update basic rate set */
1831188417Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1832188417Sthompsa		/* 11a basic rates: 6, 12, 24Mbps */
1833188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x150);
1834188417Sthompsa	} else if (IEEE80211_IS_CHAN_ANYG(c)) {
1835188417Sthompsa		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1836188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1837188417Sthompsa	} else {
1838188417Sthompsa		/* 11b basic rates: 1, 2Mbps */
1839188417Sthompsa		ural_write(sc, RAL_TXRX_CSR11, 0x3);
1840188417Sthompsa	}
1841184610Salfred}
1842184610Salfred
1843184610Salfredstatic void
1844188417Sthompsaural_set_bssid(struct ural_softc *sc, const uint8_t *bssid)
1845184610Salfred{
1846188417Sthompsa	uint16_t tmp;
1847184610Salfred
1848188417Sthompsa	tmp = bssid[0] | bssid[1] << 8;
1849188417Sthompsa	ural_write(sc, RAL_MAC_CSR5, tmp);
1850188417Sthompsa
1851188417Sthompsa	tmp = bssid[2] | bssid[3] << 8;
1852188417Sthompsa	ural_write(sc, RAL_MAC_CSR6, tmp);
1853188417Sthompsa
1854188417Sthompsa	tmp = bssid[4] | bssid[5] << 8;
1855188417Sthompsa	ural_write(sc, RAL_MAC_CSR7, tmp);
1856188417Sthompsa
1857188417Sthompsa	DPRINTF("setting BSSID to %6D\n", bssid, ":");
1858184610Salfred}
1859184610Salfred
1860184610Salfredstatic void
1861188417Sthompsaural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1862184610Salfred{
1863184610Salfred	uint16_t tmp;
1864184610Salfred
1865188417Sthompsa	tmp = addr[0] | addr[1] << 8;
1866188417Sthompsa	ural_write(sc, RAL_MAC_CSR2, tmp);
1867184610Salfred
1868188417Sthompsa	tmp = addr[2] | addr[3] << 8;
1869188417Sthompsa	ural_write(sc, RAL_MAC_CSR3, tmp);
1870184610Salfred
1871188417Sthompsa	tmp = addr[4] | addr[5] << 8;
1872188417Sthompsa	ural_write(sc, RAL_MAC_CSR4, tmp);
1873184610Salfred
1874188417Sthompsa	DPRINTF("setting MAC address to %6D\n", addr, ":");
1875184610Salfred}
1876184610Salfred
1877184610Salfredstatic void
1878191746Sthompsaural_setpromisc(struct ural_softc *sc)
1879184610Salfred{
1880188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1881188417Sthompsa	uint32_t tmp;
1882184610Salfred
1883188417Sthompsa	tmp = ural_read(sc, RAL_TXRX_CSR2);
1884184610Salfred
1885188417Sthompsa	tmp &= ~RAL_DROP_NOT_TO_ME;
1886188417Sthompsa	if (!(ifp->if_flags & IFF_PROMISC))
1887188417Sthompsa		tmp |= RAL_DROP_NOT_TO_ME;
1888184610Salfred
1889188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, tmp);
1890184610Salfred
1891188417Sthompsa	DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1892188417Sthompsa	    "entering" : "leaving");
1893184610Salfred}
1894184610Salfred
1895189123Sthompsastatic void
1896189123Sthompsaural_update_promisc(struct ifnet *ifp)
1897189123Sthompsa{
1898189123Sthompsa	struct ural_softc *sc = ifp->if_softc;
1899189123Sthompsa
1900189123Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1901189123Sthompsa		return;
1902189123Sthompsa
1903189123Sthompsa	RAL_LOCK(sc);
1904191746Sthompsa	ural_setpromisc(sc);
1905189123Sthompsa	RAL_UNLOCK(sc);
1906189123Sthompsa}
1907189123Sthompsa
1908188417Sthompsastatic const char *
1909188417Sthompsaural_get_rf(int rev)
1910184610Salfred{
1911188417Sthompsa	switch (rev) {
1912188417Sthompsa	case RAL_RF_2522:	return "RT2522";
1913188417Sthompsa	case RAL_RF_2523:	return "RT2523";
1914188417Sthompsa	case RAL_RF_2524:	return "RT2524";
1915188417Sthompsa	case RAL_RF_2525:	return "RT2525";
1916188417Sthompsa	case RAL_RF_2525E:	return "RT2525e";
1917188417Sthompsa	case RAL_RF_2526:	return "RT2526";
1918188417Sthompsa	case RAL_RF_5222:	return "RT5222";
1919188417Sthompsa	default:		return "unknown";
1920184610Salfred	}
1921184610Salfred}
1922184610Salfred
1923184610Salfredstatic void
1924188417Sthompsaural_read_eeprom(struct ural_softc *sc)
1925184610Salfred{
1926184610Salfred	uint16_t val;
1927184610Salfred
1928188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1929184610Salfred	val = le16toh(val);
1930188417Sthompsa	sc->rf_rev =   (val >> 11) & 0x7;
1931188417Sthompsa	sc->hw_radio = (val >> 10) & 0x1;
1932188417Sthompsa	sc->led_mode = (val >> 6)  & 0x7;
1933188417Sthompsa	sc->rx_ant =   (val >> 4)  & 0x3;
1934188417Sthompsa	sc->tx_ant =   (val >> 2)  & 0x3;
1935188417Sthompsa	sc->nb_ant =   val & 0x3;
1936184610Salfred
1937184610Salfred	/* read MAC address */
1938188419Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6);
1939184610Salfred
1940184610Salfred	/* read default values for BBP registers */
1941188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1942184610Salfred
1943184610Salfred	/* read Tx power for all b/g channels */
1944188417Sthompsa	ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
1945184610Salfred}
1946184610Salfred
1947188417Sthompsastatic int
1948188417Sthompsaural_bbp_init(struct ural_softc *sc)
1949184610Salfred{
1950188417Sthompsa#define N(a)	(sizeof (a) / sizeof ((a)[0]))
1951188417Sthompsa	int i, ntries;
1952184610Salfred
1953188417Sthompsa	/* wait for BBP to be ready */
1954188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1955188417Sthompsa		if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
1956188417Sthompsa			break;
1957188619Sthompsa		if (ural_pause(sc, hz / 100))
1958188619Sthompsa			break;
1959184610Salfred	}
1960188417Sthompsa	if (ntries == 100) {
1961188417Sthompsa		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1962188417Sthompsa		return EIO;
1963188417Sthompsa	}
1964184610Salfred
1965184610Salfred	/* initialize BBP registers to default values */
1966188417Sthompsa	for (i = 0; i < N(ural_def_bbp); i++)
1967188417Sthompsa		ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
1968184610Salfred
1969184610Salfred#if 0
1970184610Salfred	/* initialize BBP registers to values stored in EEPROM */
1971184610Salfred	for (i = 0; i < 16; i++) {
1972188417Sthompsa		if (sc->bbp_prom[i].reg == 0xff)
1973184610Salfred			continue;
1974188417Sthompsa		ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1975184610Salfred	}
1976184610Salfred#endif
1977188417Sthompsa
1978188417Sthompsa	return 0;
1979188417Sthompsa#undef N
1980184610Salfred}
1981184610Salfred
1982184610Salfredstatic void
1983188417Sthompsaural_set_txantenna(struct ural_softc *sc, int antenna)
1984184610Salfred{
1985188417Sthompsa	uint16_t tmp;
1986188417Sthompsa	uint8_t tx;
1987184610Salfred
1988188417Sthompsa	tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
1989188417Sthompsa	if (antenna == 1)
1990188417Sthompsa		tx |= RAL_BBP_ANTA;
1991188417Sthompsa	else if (antenna == 2)
1992188417Sthompsa		tx |= RAL_BBP_ANTB;
1993188417Sthompsa	else
1994188417Sthompsa		tx |= RAL_BBP_DIVERSITY;
1995184610Salfred
1996188417Sthompsa	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
1997188417Sthompsa	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
1998188417Sthompsa	    sc->rf_rev == RAL_RF_5222)
1999188417Sthompsa		tx |= RAL_BBP_FLIPIQ;
2000184610Salfred
2001188417Sthompsa	ural_bbp_write(sc, RAL_BBP_TX, tx);
2002184610Salfred
2003188417Sthompsa	/* update values in PHY_CSR5 and PHY_CSR6 */
2004188417Sthompsa	tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2005188417Sthompsa	ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2006184610Salfred
2007188417Sthompsa	tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2008188417Sthompsa	ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2009184610Salfred}
2010184610Salfred
2011184610Salfredstatic void
2012188417Sthompsaural_set_rxantenna(struct ural_softc *sc, int antenna)
2013184610Salfred{
2014188417Sthompsa	uint8_t rx;
2015188417Sthompsa
2016188417Sthompsa	rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2017188417Sthompsa	if (antenna == 1)
2018188417Sthompsa		rx |= RAL_BBP_ANTA;
2019188417Sthompsa	else if (antenna == 2)
2020188417Sthompsa		rx |= RAL_BBP_ANTB;
2021188417Sthompsa	else
2022188417Sthompsa		rx |= RAL_BBP_DIVERSITY;
2023188417Sthompsa
2024188417Sthompsa	/* need to force no I/Q flip for RF 2525e and 2526 */
2025188417Sthompsa	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2026188417Sthompsa		rx &= ~RAL_BBP_FLIPIQ;
2027188417Sthompsa
2028188417Sthompsa	ural_bbp_write(sc, RAL_BBP_RX, rx);
2029188417Sthompsa}
2030188417Sthompsa
2031188417Sthompsastatic void
2032191746Sthompsaural_init_locked(struct ural_softc *sc)
2033188417Sthompsa{
2034188417Sthompsa#define N(a)	(sizeof (a) / sizeof ((a)[0]))
2035188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2036188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2037184610Salfred	uint16_t tmp;
2038188417Sthompsa	int i, ntries;
2039184610Salfred
2040188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
2041184610Salfred
2042188417Sthompsa	ural_set_testmode(sc);
2043188417Sthompsa	ural_write(sc, 0x308, 0x00f0);	/* XXX magic */
2044184610Salfred
2045191746Sthompsa	ural_stop(sc);
2046184610Salfred
2047184610Salfred	/* initialize MAC registers to default values */
2048188417Sthompsa	for (i = 0; i < N(ural_def_mac); i++)
2049188417Sthompsa		ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2050184610Salfred
2051184610Salfred	/* wait for BBP and RF to wake up (this can take a long time!) */
2052188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
2053188417Sthompsa		tmp = ural_read(sc, RAL_MAC_CSR17);
2054188417Sthompsa		if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2055188417Sthompsa		    (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2056188417Sthompsa			break;
2057188619Sthompsa		if (ural_pause(sc, hz / 100))
2058188619Sthompsa			break;
2059184610Salfred	}
2060188417Sthompsa	if (ntries == 100) {
2061188417Sthompsa		device_printf(sc->sc_dev,
2062188417Sthompsa		    "timeout waiting for BBP/RF to wakeup\n");
2063188417Sthompsa		goto fail;
2064188417Sthompsa	}
2065184610Salfred
2066184610Salfred	/* we're ready! */
2067188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2068184610Salfred
2069184610Salfred	/* set basic rate set (will be updated later) */
2070188417Sthompsa	ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2071184610Salfred
2072188417Sthompsa	if (ural_bbp_init(sc) != 0)
2073184610Salfred		goto fail;
2074184610Salfred
2075188417Sthompsa	ural_set_chan(sc, ic->ic_curchan);
2076188417Sthompsa
2077184610Salfred	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
2078188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2079184610Salfred
2080188417Sthompsa	ural_set_txantenna(sc, sc->tx_ant);
2081188417Sthompsa	ural_set_rxantenna(sc, sc->rx_ant);
2082184610Salfred
2083190526Ssam	ural_set_macaddr(sc, IF_LLADDR(ifp));
2084184610Salfred
2085184610Salfred	/*
2086188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2087184610Salfred	 */
2088188419Sthompsa	ural_setup_tx_list(sc);
2089184610Salfred
2090188417Sthompsa	/* kick Rx */
2091184610Salfred	tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2092188417Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2093188417Sthompsa		tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2094188417Sthompsa		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2095184610Salfred			tmp |= RAL_DROP_TODS;
2096188417Sthompsa		if (!(ifp->if_flags & IFF_PROMISC))
2097184610Salfred			tmp |= RAL_DROP_NOT_TO_ME;
2098184610Salfred	}
2099188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, tmp);
2100184610Salfred
2101188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2102188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2103194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[URAL_BULK_WR]);
2104194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URAL_BULK_RD]);
2105184610Salfred	return;
2106184610Salfred
2107191746Sthompsafail:	ural_stop(sc);
2108188417Sthompsa#undef N
2109184610Salfred}
2110184610Salfred
2111184610Salfredstatic void
2112188417Sthompsaural_init(void *priv)
2113184610Salfred{
2114188417Sthompsa	struct ural_softc *sc = priv;
2115184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2116188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2117184610Salfred
2118188417Sthompsa	RAL_LOCK(sc);
2119191746Sthompsa	ural_init_locked(sc);
2120188417Sthompsa	RAL_UNLOCK(sc);
2121184610Salfred
2122188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2123188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2124184610Salfred}
2125184610Salfred
2126184610Salfredstatic void
2127191746Sthompsaural_stop(struct ural_softc *sc)
2128184610Salfred{
2129184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2130184610Salfred
2131188417Sthompsa	RAL_LOCK_ASSERT(sc, MA_OWNED);
2132184610Salfred
2133188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2134184610Salfred
2135184610Salfred	/*
2136188419Sthompsa	 * Drain all the transfers, if not already drained:
2137184610Salfred	 */
2138188419Sthompsa	RAL_UNLOCK(sc);
2139194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[URAL_BULK_WR]);
2140194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[URAL_BULK_RD]);
2141188419Sthompsa	RAL_LOCK(sc);
2142184610Salfred
2143188419Sthompsa	ural_unsetup_tx_list(sc);
2144184610Salfred
2145188417Sthompsa	/* disable Rx */
2146188417Sthompsa	ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2147188417Sthompsa	/* reset ASIC and BBP (but won't reset MAC registers!) */
2148188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2149188619Sthompsa	/* wait a little */
2150188619Sthompsa	ural_pause(sc, hz / 10);
2151188417Sthompsa	ural_write(sc, RAL_MAC_CSR1, 0);
2152189123Sthompsa	/* wait a little */
2153189123Sthompsa	ural_pause(sc, hz / 10);
2154184610Salfred}
2155184610Salfred
2156188417Sthompsastatic int
2157188417Sthompsaural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2158188417Sthompsa	const struct ieee80211_bpf_params *params)
2159184610Salfred{
2160188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2161188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2162188417Sthompsa	struct ural_softc *sc = ifp->if_softc;
2163184610Salfred
2164188417Sthompsa	RAL_LOCK(sc);
2165188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2166188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2167188417Sthompsa		RAL_UNLOCK(sc);
2168188417Sthompsa		m_freem(m);
2169188417Sthompsa		ieee80211_free_node(ni);
2170188417Sthompsa		return ENETDOWN;
2171184610Salfred	}
2172188969Sthompsa	if (sc->tx_nfree < RAL_TX_MINFREE) {
2173188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2174188417Sthompsa		RAL_UNLOCK(sc);
2175184610Salfred		m_freem(m);
2176184610Salfred		ieee80211_free_node(ni);
2177188417Sthompsa		return EIO;
2178184610Salfred	}
2179184610Salfred
2180188417Sthompsa	ifp->if_opackets++;
2181184610Salfred
2182188417Sthompsa	if (params == NULL) {
2183188417Sthompsa		/*
2184188417Sthompsa		 * Legacy path; interpret frame contents to decide
2185188417Sthompsa		 * precisely how to send the frame.
2186188417Sthompsa		 */
2187188417Sthompsa		if (ural_tx_mgt(sc, m, ni) != 0)
2188188417Sthompsa			goto bad;
2189188417Sthompsa	} else {
2190188417Sthompsa		/*
2191188417Sthompsa		 * Caller supplied explicit parameters to use in
2192188417Sthompsa		 * sending the frame.
2193188417Sthompsa		 */
2194188417Sthompsa		if (ural_tx_raw(sc, m, ni, params) != 0)
2195188417Sthompsa			goto bad;
2196184610Salfred	}
2197188417Sthompsa	RAL_UNLOCK(sc);
2198188417Sthompsa	return 0;
2199188417Sthompsabad:
2200188417Sthompsa	ifp->if_oerrors++;
2201188417Sthompsa	RAL_UNLOCK(sc);
2202188417Sthompsa	ieee80211_free_node(ni);
2203188417Sthompsa	return EIO;		/* XXX */
2204184610Salfred}
2205184610Salfred
2206184610Salfredstatic void
2207206358Srpauloural_ratectl_start(struct ural_softc *sc, struct ieee80211_node *ni)
2208184610Salfred{
2209188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2210188417Sthompsa	struct ural_vap *uvp = URAL_VAP(vap);
2211184610Salfred
2212188417Sthompsa	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
2213188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2214184610Salfred
2215206358Srpaulo	ieee80211_ratectl_node_init(ni);
2216184610Salfred
2217206358Srpaulo	usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
2218184610Salfred}
2219184610Salfred
2220184610Salfredstatic void
2221206358Srpauloural_ratectl_timeout(void *arg)
2222184610Salfred{
2223188417Sthompsa	struct ural_vap *uvp = arg;
2224191746Sthompsa	struct ieee80211vap *vap = &uvp->vap;
2225191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2226184610Salfred
2227206358Srpaulo	ieee80211_runtask(ic, &uvp->ratectl_task);
2228184610Salfred}
2229184610Salfred
2230184610Salfredstatic void
2231206358Srpauloural_ratectl_task(void *arg, int pending)
2232184610Salfred{
2233191746Sthompsa	struct ural_vap *uvp = arg;
2234191746Sthompsa	struct ieee80211vap *vap = &uvp->vap;
2235191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2236191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2237191746Sthompsa	struct ural_softc *sc = ifp->if_softc;
2238188417Sthompsa	struct ieee80211_node *ni = vap->iv_bss;
2239188417Sthompsa	int ok, fail;
2240206358Srpaulo	int sum, retrycnt;
2241184610Salfred
2242191746Sthompsa	RAL_LOCK(sc);
2243188417Sthompsa	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2244188417Sthompsa	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2245184610Salfred
2246188417Sthompsa	ok = sc->sta[7] +		/* TX ok w/o retry */
2247188417Sthompsa	     sc->sta[8];		/* TX ok w/ retry */
2248188417Sthompsa	fail = sc->sta[9];		/* TX retry-fail count */
2249206358Srpaulo	sum = ok+fail;
2250206358Srpaulo	retrycnt = sc->sta[8] + fail;
2251184610Salfred
2252206358Srpaulo	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2253206358Srpaulo	(void) ieee80211_ratectl_rate(ni, NULL, 0);
2254184610Salfred
2255188417Sthompsa	ifp->if_oerrors += fail;	/* count TX retry-fail as Tx errors */
2256184610Salfred
2257206358Srpaulo	usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
2258191746Sthompsa	RAL_UNLOCK(sc);
2259184610Salfred}
2260184610Salfred
2261188619Sthompsastatic int
2262188619Sthompsaural_pause(struct ural_softc *sc, int timeout)
2263188619Sthompsa{
2264188619Sthompsa
2265194228Sthompsa	usb_pause_mtx(&sc->sc_mtx, timeout);
2266188619Sthompsa	return (0);
2267188619Sthompsa}
2268