if_rum.c revision 288502
1/*	$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288502 2015-10-02 15:26:33Z adrian $	*/
2
3/*-
4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288502 2015-10-02 15:26:33Z adrian $");
23
24/*-
25 * Ralink Technology RT2501USB/RT2601USB chipset driver
26 * http://www.ralinktech.com.tw/
27 */
28
29#include <sys/param.h>
30#include <sys/sockio.h>
31#include <sys/sysctl.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/mbuf.h>
35#include <sys/kernel.h>
36#include <sys/socket.h>
37#include <sys/systm.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <sys/endian.h>
42#include <sys/kdb.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <sys/rman.h>
47
48#include <net/bpf.h>
49#include <net/if.h>
50#include <net/if_var.h>
51#include <net/if_arp.h>
52#include <net/ethernet.h>
53#include <net/if_dl.h>
54#include <net/if_media.h>
55#include <net/if_types.h>
56
57#ifdef INET
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62#include <netinet/ip.h>
63#endif
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_regdomain.h>
67#include <net80211/ieee80211_radiotap.h>
68#include <net80211/ieee80211_ratectl.h>
69
70#include <dev/usb/usb.h>
71#include <dev/usb/usbdi.h>
72#include "usbdevs.h"
73
74#define	USB_DEBUG_VAR rum_debug
75#include <dev/usb/usb_debug.h>
76
77#include <dev/usb/wlan/if_rumreg.h>
78#include <dev/usb/wlan/if_rumvar.h>
79#include <dev/usb/wlan/if_rumfw.h>
80
81#ifdef USB_DEBUG
82static int rum_debug = 0;
83
84static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
85SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
86    "Debug level");
87#endif
88
89#define N(a)	((int)(sizeof (a) / sizeof ((a)[0])))
90
91static const STRUCT_USB_HOST_ID rum_devs[] = {
92#define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
93    RUM_DEV(ABOCOM, HWU54DM),
94    RUM_DEV(ABOCOM, RT2573_2),
95    RUM_DEV(ABOCOM, RT2573_3),
96    RUM_DEV(ABOCOM, RT2573_4),
97    RUM_DEV(ABOCOM, WUG2700),
98    RUM_DEV(AMIT, CGWLUSB2GO),
99    RUM_DEV(ASUS, RT2573_1),
100    RUM_DEV(ASUS, RT2573_2),
101    RUM_DEV(BELKIN, F5D7050A),
102    RUM_DEV(BELKIN, F5D9050V3),
103    RUM_DEV(CISCOLINKSYS, WUSB54GC),
104    RUM_DEV(CISCOLINKSYS, WUSB54GR),
105    RUM_DEV(CONCEPTRONIC2, C54RU2),
106    RUM_DEV(COREGA, CGWLUSB2GL),
107    RUM_DEV(COREGA, CGWLUSB2GPX),
108    RUM_DEV(DICKSMITH, CWD854F),
109    RUM_DEV(DICKSMITH, RT2573),
110    RUM_DEV(EDIMAX, EW7318USG),
111    RUM_DEV(DLINK2, DWLG122C1),
112    RUM_DEV(DLINK2, WUA1340),
113    RUM_DEV(DLINK2, DWA111),
114    RUM_DEV(DLINK2, DWA110),
115    RUM_DEV(GIGABYTE, GNWB01GS),
116    RUM_DEV(GIGABYTE, GNWI05GS),
117    RUM_DEV(GIGASET, RT2573),
118    RUM_DEV(GOODWAY, RT2573),
119    RUM_DEV(GUILLEMOT, HWGUSB254LB),
120    RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
121    RUM_DEV(HUAWEI3COM, WUB320G),
122    RUM_DEV(MELCO, G54HP),
123    RUM_DEV(MELCO, SG54HP),
124    RUM_DEV(MELCO, SG54HG),
125    RUM_DEV(MELCO, WLIUCG),
126    RUM_DEV(MELCO, WLRUCG),
127    RUM_DEV(MELCO, WLRUCGAOSS),
128    RUM_DEV(MSI, RT2573_1),
129    RUM_DEV(MSI, RT2573_2),
130    RUM_DEV(MSI, RT2573_3),
131    RUM_DEV(MSI, RT2573_4),
132    RUM_DEV(NOVATECH, RT2573),
133    RUM_DEV(PLANEX2, GWUS54HP),
134    RUM_DEV(PLANEX2, GWUS54MINI2),
135    RUM_DEV(PLANEX2, GWUSMM),
136    RUM_DEV(QCOM, RT2573),
137    RUM_DEV(QCOM, RT2573_2),
138    RUM_DEV(QCOM, RT2573_3),
139    RUM_DEV(RALINK, RT2573),
140    RUM_DEV(RALINK, RT2573_2),
141    RUM_DEV(RALINK, RT2671),
142    RUM_DEV(SITECOMEU, WL113R2),
143    RUM_DEV(SITECOMEU, WL172),
144    RUM_DEV(SPARKLAN, RT2573),
145    RUM_DEV(SURECOM, RT2573),
146#undef RUM_DEV
147};
148
149static device_probe_t rum_match;
150static device_attach_t rum_attach;
151static device_detach_t rum_detach;
152
153static usb_callback_t rum_bulk_read_callback;
154static usb_callback_t rum_bulk_write_callback;
155
156static usb_error_t	rum_do_request(struct rum_softc *sc,
157			    struct usb_device_request *req, void *data);
158static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
159			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
160			    int, const uint8_t [IEEE80211_ADDR_LEN],
161			    const uint8_t [IEEE80211_ADDR_LEN]);
162static void		rum_vap_delete(struct ieee80211vap *);
163static void		rum_tx_free(struct rum_tx_data *, int);
164static void		rum_setup_tx_list(struct rum_softc *);
165static void		rum_unsetup_tx_list(struct rum_softc *);
166static int		rum_newstate(struct ieee80211vap *,
167			    enum ieee80211_state, int);
168static void		rum_setup_tx_desc(struct rum_softc *,
169			    struct rum_tx_desc *, uint32_t, uint16_t, int,
170			    int);
171static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
172			    struct ieee80211_node *);
173static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
174			    struct ieee80211_node *,
175			    const struct ieee80211_bpf_params *);
176static int		rum_tx_data(struct rum_softc *, struct mbuf *,
177			    struct ieee80211_node *);
178static int		rum_transmit(struct ieee80211com *, struct mbuf *);
179static void		rum_start(struct rum_softc *);
180static void		rum_parent(struct ieee80211com *);
181static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
182			    int);
183static uint32_t		rum_read(struct rum_softc *, uint16_t);
184static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
185			    int);
186static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
187static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
188			    size_t);
189static int		rum_bbp_busy(struct rum_softc *);
190static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
191static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
192static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
193static void		rum_select_antenna(struct rum_softc *);
194static void		rum_enable_mrr(struct rum_softc *);
195static void		rum_set_txpreamble(struct rum_softc *);
196static void		rum_set_basicrates(struct rum_softc *);
197static void		rum_select_band(struct rum_softc *,
198			    struct ieee80211_channel *);
199static void		rum_set_chan(struct rum_softc *,
200			    struct ieee80211_channel *);
201static void		rum_enable_tsf_sync(struct rum_softc *);
202static void		rum_enable_tsf(struct rum_softc *);
203static void		rum_abort_tsf_sync(struct rum_softc *);
204static void		rum_update_slot(struct rum_softc *);
205static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
206static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
207static void		rum_update_mcast(struct ieee80211com *);
208static void		rum_update_promisc(struct ieee80211com *);
209static void		rum_setpromisc(struct rum_softc *);
210static const char	*rum_get_rf(int);
211static void		rum_read_eeprom(struct rum_softc *);
212static int		rum_bbp_init(struct rum_softc *);
213static void		rum_init(struct rum_softc *);
214static void		rum_stop(struct rum_softc *);
215static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
216			    size_t);
217static void		rum_prepare_beacon(struct rum_softc *,
218			    struct ieee80211vap *);
219static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
220			    const struct ieee80211_bpf_params *);
221static void		rum_scan_start(struct ieee80211com *);
222static void		rum_scan_end(struct ieee80211com *);
223static void		rum_set_channel(struct ieee80211com *);
224static int		rum_get_rssi(struct rum_softc *, uint8_t);
225static void		rum_ratectl_start(struct rum_softc *,
226			    struct ieee80211_node *);
227static void		rum_ratectl_timeout(void *);
228static void		rum_ratectl_task(void *, int);
229static int		rum_pause(struct rum_softc *, int);
230
231static const struct {
232	uint32_t	reg;
233	uint32_t	val;
234} rum_def_mac[] = {
235	{ RT2573_TXRX_CSR0,  0x025fb032 },
236	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
237	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
238	{ RT2573_TXRX_CSR3,  0x00858687 },
239	{ RT2573_TXRX_CSR7,  0x2e31353b },
240	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
241	{ RT2573_TXRX_CSR15, 0x0000000f },
242	{ RT2573_MAC_CSR6,   0x00000fff },
243	{ RT2573_MAC_CSR8,   0x016c030a },
244	{ RT2573_MAC_CSR10,  0x00000718 },
245	{ RT2573_MAC_CSR12,  0x00000004 },
246	{ RT2573_MAC_CSR13,  0x00007f00 },
247	{ RT2573_SEC_CSR0,   0x00000000 },
248	{ RT2573_SEC_CSR1,   0x00000000 },
249	{ RT2573_SEC_CSR5,   0x00000000 },
250	{ RT2573_PHY_CSR1,   0x000023b0 },
251	{ RT2573_PHY_CSR5,   0x00040a06 },
252	{ RT2573_PHY_CSR6,   0x00080606 },
253	{ RT2573_PHY_CSR7,   0x00000408 },
254	{ RT2573_AIFSN_CSR,  0x00002273 },
255	{ RT2573_CWMIN_CSR,  0x00002344 },
256	{ RT2573_CWMAX_CSR,  0x000034aa }
257};
258
259static const struct {
260	uint8_t	reg;
261	uint8_t	val;
262} rum_def_bbp[] = {
263	{   3, 0x80 },
264	{  15, 0x30 },
265	{  17, 0x20 },
266	{  21, 0xc8 },
267	{  22, 0x38 },
268	{  23, 0x06 },
269	{  24, 0xfe },
270	{  25, 0x0a },
271	{  26, 0x0d },
272	{  32, 0x0b },
273	{  34, 0x12 },
274	{  37, 0x07 },
275	{  39, 0xf8 },
276	{  41, 0x60 },
277	{  53, 0x10 },
278	{  54, 0x18 },
279	{  60, 0x10 },
280	{  61, 0x04 },
281	{  62, 0x04 },
282	{  75, 0xfe },
283	{  86, 0xfe },
284	{  88, 0xfe },
285	{  90, 0x0f },
286	{  99, 0x00 },
287	{ 102, 0x16 },
288	{ 107, 0x04 }
289};
290
291static const struct rfprog {
292	uint8_t		chan;
293	uint32_t	r1, r2, r3, r4;
294}  rum_rf5226[] = {
295	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
296	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
297	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
298	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
299	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
300	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
301	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
302	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
303	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
304	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
305	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
306	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
307	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
308	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
309
310	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
311	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
312	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
313	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
314
315	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
316	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
317	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
318	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
319	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
320	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
321	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
322	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
323
324	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
325	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
326	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
327	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
328	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
329	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
330	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
331	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
332	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
333	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
334	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
335
336	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
337	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
338	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
339	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
340	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
341}, rum_rf5225[] = {
342	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
343	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
344	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
345	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
346	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
347	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
348	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
349	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
350	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
351	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
352	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
353	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
354	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
355	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
356
357	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
358	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
359	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
360	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
361
362	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
363	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
364	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
365	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
366	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
367	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
368	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
369	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
370
371	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
372	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
373	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
374	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
375	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
376	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
377	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
378	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
379	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
380	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
381	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
382
383	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
384	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
385	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
386	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
387	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
388};
389
390static const struct usb_config rum_config[RUM_N_TRANSFER] = {
391	[RUM_BULK_WR] = {
392		.type = UE_BULK,
393		.endpoint = UE_ADDR_ANY,
394		.direction = UE_DIR_OUT,
395		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
396		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
397		.callback = rum_bulk_write_callback,
398		.timeout = 5000,	/* ms */
399	},
400	[RUM_BULK_RD] = {
401		.type = UE_BULK,
402		.endpoint = UE_ADDR_ANY,
403		.direction = UE_DIR_IN,
404		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
405		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
406		.callback = rum_bulk_read_callback,
407	},
408};
409
410static int
411rum_match(device_t self)
412{
413	struct usb_attach_arg *uaa = device_get_ivars(self);
414
415	if (uaa->usb_mode != USB_MODE_HOST)
416		return (ENXIO);
417	if (uaa->info.bConfigIndex != 0)
418		return (ENXIO);
419	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
420		return (ENXIO);
421
422	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
423}
424
425static int
426rum_attach(device_t self)
427{
428	struct usb_attach_arg *uaa = device_get_ivars(self);
429	struct rum_softc *sc = device_get_softc(self);
430	struct ieee80211com *ic = &sc->sc_ic;
431	uint8_t iface_index, bands;
432	uint32_t tmp;
433	int error, ntries;
434
435	device_set_usb_desc(self);
436	sc->sc_udev = uaa->device;
437	sc->sc_dev = self;
438
439	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
440	    MTX_NETWORK_LOCK, MTX_DEF);
441	mbufq_init(&sc->sc_snd, ifqmaxlen);
442
443	iface_index = RT2573_IFACE_INDEX;
444	error = usbd_transfer_setup(uaa->device, &iface_index,
445	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
446	if (error) {
447		device_printf(self, "could not allocate USB transfers, "
448		    "err=%s\n", usbd_errstr(error));
449		goto detach;
450	}
451
452	RUM_LOCK(sc);
453	/* retrieve RT2573 rev. no */
454	for (ntries = 0; ntries < 100; ntries++) {
455		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
456			break;
457		if (rum_pause(sc, hz / 100))
458			break;
459	}
460	if (ntries == 100) {
461		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
462		RUM_UNLOCK(sc);
463		goto detach;
464	}
465
466	/* retrieve MAC address and various other things from EEPROM */
467	rum_read_eeprom(sc);
468
469	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
470	    tmp, rum_get_rf(sc->rf_rev));
471
472	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
473	RUM_UNLOCK(sc);
474
475	ic->ic_softc = sc;
476	ic->ic_name = device_get_nameunit(self);
477	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
478
479	/* set device capabilities */
480	ic->ic_caps =
481	      IEEE80211_C_STA		/* station mode supported */
482	    | IEEE80211_C_IBSS		/* IBSS mode supported */
483	    | IEEE80211_C_MONITOR	/* monitor mode supported */
484	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
485	    | IEEE80211_C_TXPMGT	/* tx power management */
486	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
487	    | IEEE80211_C_SHSLOT	/* short slot time supported */
488	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
489	    | IEEE80211_C_WPA		/* 802.11i */
490	    ;
491
492	bands = 0;
493	setbit(&bands, IEEE80211_MODE_11B);
494	setbit(&bands, IEEE80211_MODE_11G);
495	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
496		setbit(&bands, IEEE80211_MODE_11A);
497	ieee80211_init_channels(ic, NULL, &bands);
498
499	ieee80211_ifattach(ic);
500	ic->ic_update_promisc = rum_update_promisc;
501	ic->ic_raw_xmit = rum_raw_xmit;
502	ic->ic_scan_start = rum_scan_start;
503	ic->ic_scan_end = rum_scan_end;
504	ic->ic_set_channel = rum_set_channel;
505	ic->ic_transmit = rum_transmit;
506	ic->ic_parent = rum_parent;
507	ic->ic_vap_create = rum_vap_create;
508	ic->ic_vap_delete = rum_vap_delete;
509	ic->ic_update_mcast = rum_update_mcast;
510
511	ieee80211_radiotap_attach(ic,
512	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
513		RT2573_TX_RADIOTAP_PRESENT,
514	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
515		RT2573_RX_RADIOTAP_PRESENT);
516
517	if (bootverbose)
518		ieee80211_announce(ic);
519
520	return (0);
521
522detach:
523	rum_detach(self);
524	return (ENXIO);			/* failure */
525}
526
527static int
528rum_detach(device_t self)
529{
530	struct rum_softc *sc = device_get_softc(self);
531
532	/* Prevent further ioctls */
533	RUM_LOCK(sc);
534	sc->sc_detached = 1;
535	RUM_UNLOCK(sc);
536
537	/* stop all USB transfers */
538	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
539
540	/* free TX list, if any */
541	RUM_LOCK(sc);
542	rum_unsetup_tx_list(sc);
543	RUM_UNLOCK(sc);
544
545	if (sc->sc_ic.ic_softc == sc)
546		ieee80211_ifdetach(&sc->sc_ic);
547	mbufq_drain(&sc->sc_snd);
548	mtx_destroy(&sc->sc_mtx);
549	return (0);
550}
551
552static usb_error_t
553rum_do_request(struct rum_softc *sc,
554    struct usb_device_request *req, void *data)
555{
556	usb_error_t err;
557	int ntries = 10;
558
559	while (ntries--) {
560		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
561		    req, data, 0, NULL, 250 /* ms */);
562		if (err == 0)
563			break;
564
565		DPRINTFN(1, "Control request failed, %s (retrying)\n",
566		    usbd_errstr(err));
567		if (rum_pause(sc, hz / 100))
568			break;
569	}
570	return (err);
571}
572
573static struct ieee80211vap *
574rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
575    enum ieee80211_opmode opmode, int flags,
576    const uint8_t bssid[IEEE80211_ADDR_LEN],
577    const uint8_t mac[IEEE80211_ADDR_LEN])
578{
579	struct rum_softc *sc = ic->ic_softc;
580	struct rum_vap *rvp;
581	struct ieee80211vap *vap;
582
583	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
584		return NULL;
585	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
586	vap = &rvp->vap;
587	/* enable s/w bmiss handling for sta mode */
588
589	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
590	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
591		/* out of memory */
592		free(rvp, M_80211_VAP);
593		return (NULL);
594	}
595
596	/* override state transition machine */
597	rvp->newstate = vap->iv_newstate;
598	vap->iv_newstate = rum_newstate;
599
600	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
601	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
602	ieee80211_ratectl_init(vap);
603	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
604	/* complete setup */
605	ieee80211_vap_attach(vap, ieee80211_media_change,
606	    ieee80211_media_status, mac);
607	ic->ic_opmode = opmode;
608	return vap;
609}
610
611static void
612rum_vap_delete(struct ieee80211vap *vap)
613{
614	struct rum_vap *rvp = RUM_VAP(vap);
615	struct ieee80211com *ic = vap->iv_ic;
616
617	usb_callout_drain(&rvp->ratectl_ch);
618	ieee80211_draintask(ic, &rvp->ratectl_task);
619	ieee80211_ratectl_deinit(vap);
620	ieee80211_vap_detach(vap);
621	free(rvp, M_80211_VAP);
622}
623
624static void
625rum_tx_free(struct rum_tx_data *data, int txerr)
626{
627	struct rum_softc *sc = data->sc;
628
629	if (data->m != NULL) {
630		ieee80211_tx_complete(data->ni, data->m, txerr);
631		data->m = NULL;
632		data->ni = NULL;
633	}
634	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
635	sc->tx_nfree++;
636}
637
638static void
639rum_setup_tx_list(struct rum_softc *sc)
640{
641	struct rum_tx_data *data;
642	int i;
643
644	sc->tx_nfree = 0;
645	STAILQ_INIT(&sc->tx_q);
646	STAILQ_INIT(&sc->tx_free);
647
648	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
649		data = &sc->tx_data[i];
650
651		data->sc = sc;
652		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
653		sc->tx_nfree++;
654	}
655}
656
657static void
658rum_unsetup_tx_list(struct rum_softc *sc)
659{
660	struct rum_tx_data *data;
661	int i;
662
663	/* make sure any subsequent use of the queues will fail */
664	sc->tx_nfree = 0;
665	STAILQ_INIT(&sc->tx_q);
666	STAILQ_INIT(&sc->tx_free);
667
668	/* free up all node references and mbufs */
669	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
670		data = &sc->tx_data[i];
671
672		if (data->m != NULL) {
673			m_freem(data->m);
674			data->m = NULL;
675		}
676		if (data->ni != NULL) {
677			ieee80211_free_node(data->ni);
678			data->ni = NULL;
679		}
680	}
681}
682
683static int
684rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
685{
686	struct rum_vap *rvp = RUM_VAP(vap);
687	struct ieee80211com *ic = vap->iv_ic;
688	struct rum_softc *sc = ic->ic_softc;
689	const struct ieee80211_txparam *tp;
690	enum ieee80211_state ostate;
691	struct ieee80211_node *ni;
692
693	ostate = vap->iv_state;
694	DPRINTF("%s -> %s\n",
695		ieee80211_state_name[ostate],
696		ieee80211_state_name[nstate]);
697
698	IEEE80211_UNLOCK(ic);
699	RUM_LOCK(sc);
700	usb_callout_stop(&rvp->ratectl_ch);
701
702	switch (nstate) {
703	case IEEE80211_S_INIT:
704		if (ostate == IEEE80211_S_RUN)
705			rum_abort_tsf_sync(sc);
706
707		break;
708
709	case IEEE80211_S_RUN:
710		ni = ieee80211_ref_node(vap->iv_bss);
711
712		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
713			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
714				RUM_UNLOCK(sc);
715				IEEE80211_LOCK(ic);
716				ieee80211_free_node(ni);
717				return (-1);
718			}
719			rum_update_slot(sc);
720			rum_enable_mrr(sc);
721			rum_set_txpreamble(sc);
722			rum_set_basicrates(sc);
723			IEEE80211_ADDR_COPY(ic->ic_macaddr, ni->ni_bssid);
724			rum_set_bssid(sc, ic->ic_macaddr);
725		}
726
727		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
728		    vap->iv_opmode == IEEE80211_M_IBSS)
729			rum_prepare_beacon(sc, vap);
730
731		if (vap->iv_opmode != IEEE80211_M_MONITOR)
732			rum_enable_tsf_sync(sc);
733		else
734			rum_enable_tsf(sc);
735
736		/* enable automatic rate adaptation */
737		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
738		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
739			rum_ratectl_start(sc, ni);
740		ieee80211_free_node(ni);
741		break;
742	default:
743		break;
744	}
745	RUM_UNLOCK(sc);
746	IEEE80211_LOCK(ic);
747	return (rvp->newstate(vap, nstate, arg));
748}
749
750static void
751rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
752{
753	struct rum_softc *sc = usbd_xfer_softc(xfer);
754	struct ieee80211vap *vap;
755	struct rum_tx_data *data;
756	struct mbuf *m;
757	struct usb_page_cache *pc;
758	unsigned int len;
759	int actlen, sumlen;
760
761	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
762
763	switch (USB_GET_STATE(xfer)) {
764	case USB_ST_TRANSFERRED:
765		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
766
767		/* free resources */
768		data = usbd_xfer_get_priv(xfer);
769		rum_tx_free(data, 0);
770		usbd_xfer_set_priv(xfer, NULL);
771
772		/* FALLTHROUGH */
773	case USB_ST_SETUP:
774tr_setup:
775		data = STAILQ_FIRST(&sc->tx_q);
776		if (data) {
777			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
778			m = data->m;
779
780			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
781				DPRINTFN(0, "data overflow, %u bytes\n",
782				    m->m_pkthdr.len);
783				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
784			}
785			pc = usbd_xfer_get_frame(xfer, 0);
786			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
787			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
788			    m->m_pkthdr.len);
789
790			vap = data->ni->ni_vap;
791			if (ieee80211_radiotap_active_vap(vap)) {
792				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
793
794				tap->wt_flags = 0;
795				tap->wt_rate = data->rate;
796				tap->wt_antenna = sc->tx_ant;
797
798				ieee80211_radiotap_tx(vap, m);
799			}
800
801			/* align end on a 4-bytes boundary */
802			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
803			if ((len % 64) == 0)
804				len += 4;
805
806			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
807			    m->m_pkthdr.len, len);
808
809			usbd_xfer_set_frame_len(xfer, 0, len);
810			usbd_xfer_set_priv(xfer, data);
811
812			usbd_transfer_submit(xfer);
813		}
814		rum_start(sc);
815		break;
816
817	default:			/* Error */
818		DPRINTFN(11, "transfer error, %s\n",
819		    usbd_errstr(error));
820
821		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
822		data = usbd_xfer_get_priv(xfer);
823		if (data != NULL) {
824			rum_tx_free(data, error);
825			usbd_xfer_set_priv(xfer, NULL);
826		}
827
828		if (error != USB_ERR_CANCELLED) {
829			if (error == USB_ERR_TIMEOUT)
830				device_printf(sc->sc_dev, "device timeout\n");
831
832			/*
833			 * Try to clear stall first, also if other
834			 * errors occur, hence clearing stall
835			 * introduces a 50 ms delay:
836			 */
837			usbd_xfer_set_stall(xfer);
838			goto tr_setup;
839		}
840		break;
841	}
842}
843
844static void
845rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
846{
847	struct rum_softc *sc = usbd_xfer_softc(xfer);
848	struct ieee80211com *ic = &sc->sc_ic;
849	struct ieee80211_node *ni;
850	struct mbuf *m = NULL;
851	struct usb_page_cache *pc;
852	uint32_t flags;
853	uint8_t rssi = 0;
854	int len;
855
856	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
857
858	switch (USB_GET_STATE(xfer)) {
859	case USB_ST_TRANSFERRED:
860
861		DPRINTFN(15, "rx done, actlen=%d\n", len);
862
863		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
864			DPRINTF("%s: xfer too short %d\n",
865			    device_get_nameunit(sc->sc_dev), len);
866			counter_u64_add(ic->ic_ierrors, 1);
867			goto tr_setup;
868		}
869
870		len -= RT2573_RX_DESC_SIZE;
871		pc = usbd_xfer_get_frame(xfer, 0);
872		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
873
874		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
875		flags = le32toh(sc->sc_rx_desc.flags);
876		if (flags & RT2573_RX_CRC_ERROR) {
877			/*
878		         * This should not happen since we did not
879		         * request to receive those frames when we
880		         * filled RUM_TXRX_CSR2:
881		         */
882			DPRINTFN(5, "PHY or CRC error\n");
883			counter_u64_add(ic->ic_ierrors, 1);
884			goto tr_setup;
885		}
886
887		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
888		if (m == NULL) {
889			DPRINTF("could not allocate mbuf\n");
890			counter_u64_add(ic->ic_ierrors, 1);
891			goto tr_setup;
892		}
893		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
894		    mtod(m, uint8_t *), len);
895
896		/* finalize mbuf */
897		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
898
899		if (ieee80211_radiotap_active(ic)) {
900			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
901
902			/* XXX read tsf */
903			tap->wr_flags = 0;
904			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
905			    (flags & RT2573_RX_OFDM) ?
906			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
907			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
908			tap->wr_antnoise = RT2573_NOISE_FLOOR;
909			tap->wr_antenna = sc->rx_ant;
910		}
911		/* FALLTHROUGH */
912	case USB_ST_SETUP:
913tr_setup:
914		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
915		usbd_transfer_submit(xfer);
916
917		/*
918		 * At the end of a USB callback it is always safe to unlock
919		 * the private mutex of a device! That is why we do the
920		 * "ieee80211_input" here, and not some lines up!
921		 */
922		RUM_UNLOCK(sc);
923		if (m) {
924			ni = ieee80211_find_rxnode(ic,
925			    mtod(m, struct ieee80211_frame_min *));
926			if (ni != NULL) {
927				(void) ieee80211_input(ni, m, rssi,
928				    RT2573_NOISE_FLOOR);
929				ieee80211_free_node(ni);
930			} else
931				(void) ieee80211_input_all(ic, m, rssi,
932				    RT2573_NOISE_FLOOR);
933		}
934		RUM_LOCK(sc);
935		rum_start(sc);
936		return;
937
938	default:			/* Error */
939		if (error != USB_ERR_CANCELLED) {
940			/* try to clear stall first */
941			usbd_xfer_set_stall(xfer);
942			goto tr_setup;
943		}
944		return;
945	}
946}
947
948static uint8_t
949rum_plcp_signal(int rate)
950{
951	switch (rate) {
952	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
953	case 12:	return 0xb;
954	case 18:	return 0xf;
955	case 24:	return 0xa;
956	case 36:	return 0xe;
957	case 48:	return 0x9;
958	case 72:	return 0xd;
959	case 96:	return 0x8;
960	case 108:	return 0xc;
961
962	/* CCK rates (NB: not IEEE std, device-specific) */
963	case 2:		return 0x0;
964	case 4:		return 0x1;
965	case 11:	return 0x2;
966	case 22:	return 0x3;
967	}
968	return 0xff;		/* XXX unsupported/unknown rate */
969}
970
971static void
972rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
973    uint32_t flags, uint16_t xflags, int len, int rate)
974{
975	struct ieee80211com *ic = &sc->sc_ic;
976	uint16_t plcp_length;
977	int remainder;
978
979	desc->flags = htole32(flags);
980	desc->flags |= htole32(RT2573_TX_VALID);
981	desc->flags |= htole32(len << 16);
982
983	desc->xflags = htole16(xflags);
984
985	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
986	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
987
988	/* setup PLCP fields */
989	desc->plcp_signal  = rum_plcp_signal(rate);
990	desc->plcp_service = 4;
991
992	len += IEEE80211_CRC_LEN;
993	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
994		desc->flags |= htole32(RT2573_TX_OFDM);
995
996		plcp_length = len & 0xfff;
997		desc->plcp_length_hi = plcp_length >> 6;
998		desc->plcp_length_lo = plcp_length & 0x3f;
999	} else {
1000		if (rate == 0)
1001			rate = 2;	/* avoid division by zero */
1002		plcp_length = (16 * len + rate - 1) / rate;
1003		if (rate == 22) {
1004			remainder = (16 * len) % 22;
1005			if (remainder != 0 && remainder < 7)
1006				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1007		}
1008		desc->plcp_length_hi = plcp_length >> 8;
1009		desc->plcp_length_lo = plcp_length & 0xff;
1010
1011		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1012			desc->plcp_signal |= 0x08;
1013	}
1014}
1015
1016static int
1017rum_sendprot(struct rum_softc *sc,
1018    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1019{
1020	struct ieee80211com *ic = ni->ni_ic;
1021	const struct ieee80211_frame *wh;
1022	struct rum_tx_data *data;
1023	struct mbuf *mprot;
1024	int protrate, ackrate, pktlen, flags, isshort;
1025	uint16_t dur;
1026
1027	RUM_LOCK_ASSERT(sc, MA_OWNED);
1028	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1029	    ("protection %d", prot));
1030
1031	wh = mtod(m, const struct ieee80211_frame *);
1032	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1033
1034	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1035	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1036
1037	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1038	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1039	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1040	flags = RT2573_TX_MORE_FRAG;
1041	if (prot == IEEE80211_PROT_RTSCTS) {
1042		/* NB: CTS is the same size as an ACK */
1043		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1044		flags |= RT2573_TX_NEED_ACK;
1045		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1046	} else {
1047		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1048	}
1049	if (mprot == NULL) {
1050		/* XXX stat + msg */
1051		return (ENOBUFS);
1052	}
1053	data = STAILQ_FIRST(&sc->tx_free);
1054	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1055	sc->tx_nfree--;
1056
1057	data->m = mprot;
1058	data->ni = ieee80211_ref_node(ni);
1059	data->rate = protrate;
1060	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1061
1062	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1063	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1064
1065	return 0;
1066}
1067
1068static int
1069rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1070{
1071	struct ieee80211vap *vap = ni->ni_vap;
1072	struct ieee80211com *ic = &sc->sc_ic;
1073	struct rum_tx_data *data;
1074	struct ieee80211_frame *wh;
1075	const struct ieee80211_txparam *tp;
1076	struct ieee80211_key *k;
1077	uint32_t flags = 0;
1078	uint16_t dur;
1079
1080	RUM_LOCK_ASSERT(sc, MA_OWNED);
1081
1082	data = STAILQ_FIRST(&sc->tx_free);
1083	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1084	sc->tx_nfree--;
1085
1086	wh = mtod(m0, struct ieee80211_frame *);
1087	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1088		k = ieee80211_crypto_encap(ni, m0);
1089		if (k == NULL) {
1090			m_freem(m0);
1091			return ENOBUFS;
1092		}
1093		wh = mtod(m0, struct ieee80211_frame *);
1094	}
1095
1096	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1097
1098	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1099		flags |= RT2573_TX_NEED_ACK;
1100
1101		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1102		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1103		USETW(wh->i_dur, dur);
1104
1105		/* tell hardware to add timestamp for probe responses */
1106		if ((wh->i_fc[0] &
1107		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1108		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1109			flags |= RT2573_TX_TIMESTAMP;
1110	}
1111
1112	data->m = m0;
1113	data->ni = ni;
1114	data->rate = tp->mgmtrate;
1115
1116	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1117
1118	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1119	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1120
1121	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1122	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1123
1124	return (0);
1125}
1126
1127static int
1128rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1129    const struct ieee80211_bpf_params *params)
1130{
1131	struct ieee80211com *ic = ni->ni_ic;
1132	struct rum_tx_data *data;
1133	uint32_t flags;
1134	int rate, error;
1135
1136	RUM_LOCK_ASSERT(sc, MA_OWNED);
1137	KASSERT(params != NULL, ("no raw xmit params"));
1138
1139	rate = params->ibp_rate0;
1140	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1141		m_freem(m0);
1142		return EINVAL;
1143	}
1144	flags = 0;
1145	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1146		flags |= RT2573_TX_NEED_ACK;
1147	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1148		error = rum_sendprot(sc, m0, ni,
1149		    params->ibp_flags & IEEE80211_BPF_RTS ?
1150			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1151		    rate);
1152		if (error || sc->tx_nfree == 0) {
1153			m_freem(m0);
1154			return ENOBUFS;
1155		}
1156		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1157	}
1158
1159	data = STAILQ_FIRST(&sc->tx_free);
1160	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1161	sc->tx_nfree--;
1162
1163	data->m = m0;
1164	data->ni = ni;
1165	data->rate = rate;
1166
1167	/* XXX need to setup descriptor ourself */
1168	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1169
1170	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1171	    m0->m_pkthdr.len, rate);
1172
1173	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1174	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1175
1176	return 0;
1177}
1178
1179static int
1180rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1181{
1182	struct ieee80211vap *vap = ni->ni_vap;
1183	struct ieee80211com *ic = &sc->sc_ic;
1184	struct rum_tx_data *data;
1185	struct ieee80211_frame *wh;
1186	const struct ieee80211_txparam *tp;
1187	struct ieee80211_key *k;
1188	uint32_t flags = 0;
1189	uint16_t dur;
1190	int error, rate;
1191
1192	RUM_LOCK_ASSERT(sc, MA_OWNED);
1193
1194	wh = mtod(m0, struct ieee80211_frame *);
1195
1196	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1197	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1198		rate = tp->mcastrate;
1199	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1200		rate = tp->ucastrate;
1201	else
1202		rate = ni->ni_txrate;
1203
1204	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1205		k = ieee80211_crypto_encap(ni, m0);
1206		if (k == NULL) {
1207			m_freem(m0);
1208			return ENOBUFS;
1209		}
1210
1211		/* packet header may have moved, reset our local pointer */
1212		wh = mtod(m0, struct ieee80211_frame *);
1213	}
1214
1215	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1216		int prot = IEEE80211_PROT_NONE;
1217		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1218			prot = IEEE80211_PROT_RTSCTS;
1219		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1220		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1221			prot = ic->ic_protmode;
1222		if (prot != IEEE80211_PROT_NONE) {
1223			error = rum_sendprot(sc, m0, ni, prot, rate);
1224			if (error || sc->tx_nfree == 0) {
1225				m_freem(m0);
1226				return ENOBUFS;
1227			}
1228			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1229		}
1230	}
1231
1232	data = STAILQ_FIRST(&sc->tx_free);
1233	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1234	sc->tx_nfree--;
1235
1236	data->m = m0;
1237	data->ni = ni;
1238	data->rate = rate;
1239
1240	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1241		flags |= RT2573_TX_NEED_ACK;
1242		flags |= RT2573_TX_MORE_FRAG;
1243
1244		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1245		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1246		USETW(wh->i_dur, dur);
1247	}
1248
1249	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1250
1251	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1252	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1253
1254	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1255	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1256
1257	return 0;
1258}
1259
1260static int
1261rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1262{
1263	struct rum_softc *sc = ic->ic_softc;
1264	int error;
1265
1266	RUM_LOCK(sc);
1267	if (!sc->sc_running) {
1268		RUM_UNLOCK(sc);
1269		return (ENXIO);
1270	}
1271	error = mbufq_enqueue(&sc->sc_snd, m);
1272	if (error) {
1273		RUM_UNLOCK(sc);
1274		return (error);
1275	}
1276	rum_start(sc);
1277	RUM_UNLOCK(sc);
1278
1279	return (0);
1280}
1281
1282static void
1283rum_start(struct rum_softc *sc)
1284{
1285	struct ieee80211_node *ni;
1286	struct mbuf *m;
1287
1288	RUM_LOCK_ASSERT(sc, MA_OWNED);
1289
1290	if (!sc->sc_running)
1291		return;
1292
1293	while (sc->tx_nfree >= RUM_TX_MINFREE &&
1294	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1295		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1296		if (rum_tx_data(sc, m, ni) != 0) {
1297			if_inc_counter(ni->ni_vap->iv_ifp,
1298			    IFCOUNTER_OERRORS, 1);
1299			ieee80211_free_node(ni);
1300			break;
1301		}
1302	}
1303}
1304
1305static void
1306rum_parent(struct ieee80211com *ic)
1307{
1308	struct rum_softc *sc = ic->ic_softc;
1309	int startall = 0;
1310
1311	RUM_LOCK(sc);
1312	if (sc->sc_detached) {
1313		RUM_UNLOCK(sc);
1314		return;
1315	}
1316	if (ic->ic_nrunning > 0) {
1317		if (!sc->sc_running) {
1318			rum_init(sc);
1319			startall = 1;
1320		} else
1321			rum_setpromisc(sc);
1322	} else if (sc->sc_running)
1323		rum_stop(sc);
1324	RUM_UNLOCK(sc);
1325	if (startall)
1326		ieee80211_start_all(ic);
1327}
1328
1329static void
1330rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1331{
1332	struct usb_device_request req;
1333	usb_error_t error;
1334
1335	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1336	req.bRequest = RT2573_READ_EEPROM;
1337	USETW(req.wValue, 0);
1338	USETW(req.wIndex, addr);
1339	USETW(req.wLength, len);
1340
1341	error = rum_do_request(sc, &req, buf);
1342	if (error != 0) {
1343		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1344		    usbd_errstr(error));
1345	}
1346}
1347
1348static uint32_t
1349rum_read(struct rum_softc *sc, uint16_t reg)
1350{
1351	uint32_t val;
1352
1353	rum_read_multi(sc, reg, &val, sizeof val);
1354
1355	return le32toh(val);
1356}
1357
1358static void
1359rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1360{
1361	struct usb_device_request req;
1362	usb_error_t error;
1363
1364	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1365	req.bRequest = RT2573_READ_MULTI_MAC;
1366	USETW(req.wValue, 0);
1367	USETW(req.wIndex, reg);
1368	USETW(req.wLength, len);
1369
1370	error = rum_do_request(sc, &req, buf);
1371	if (error != 0) {
1372		device_printf(sc->sc_dev,
1373		    "could not multi read MAC register: %s\n",
1374		    usbd_errstr(error));
1375	}
1376}
1377
1378static usb_error_t
1379rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1380{
1381	uint32_t tmp = htole32(val);
1382
1383	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1384}
1385
1386static usb_error_t
1387rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1388{
1389	struct usb_device_request req;
1390	usb_error_t error;
1391	size_t offset;
1392
1393	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1394	req.bRequest = RT2573_WRITE_MULTI_MAC;
1395	USETW(req.wValue, 0);
1396
1397	/* write at most 64 bytes at a time */
1398	for (offset = 0; offset < len; offset += 64) {
1399		USETW(req.wIndex, reg + offset);
1400		USETW(req.wLength, MIN(len - offset, 64));
1401
1402		error = rum_do_request(sc, &req, (char *)buf + offset);
1403		if (error != 0) {
1404			device_printf(sc->sc_dev,
1405			    "could not multi write MAC register: %s\n",
1406			    usbd_errstr(error));
1407			return (error);
1408		}
1409	}
1410
1411	return (USB_ERR_NORMAL_COMPLETION);
1412}
1413
1414static int
1415rum_bbp_busy(struct rum_softc *sc)
1416{
1417	int ntries;
1418
1419	for (ntries = 0; ntries < 100; ntries++) {
1420		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1421			break;
1422		if (rum_pause(sc, hz / 100))
1423			break;
1424	}
1425	if (ntries == 100)
1426		return (ETIMEDOUT);
1427
1428	return (0);
1429}
1430
1431static void
1432rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1433{
1434	uint32_t tmp;
1435
1436	DPRINTFN(2, "reg=0x%08x\n", reg);
1437
1438	if (rum_bbp_busy(sc) != 0) {
1439		device_printf(sc->sc_dev, "could not write to BBP\n");
1440		return;
1441	}
1442
1443	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1444	rum_write(sc, RT2573_PHY_CSR3, tmp);
1445}
1446
1447static uint8_t
1448rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1449{
1450	uint32_t val;
1451	int ntries;
1452
1453	DPRINTFN(2, "reg=0x%08x\n", reg);
1454
1455	if (rum_bbp_busy(sc) != 0) {
1456		device_printf(sc->sc_dev, "could not read BBP\n");
1457		return 0;
1458	}
1459
1460	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1461	rum_write(sc, RT2573_PHY_CSR3, val);
1462
1463	for (ntries = 0; ntries < 100; ntries++) {
1464		val = rum_read(sc, RT2573_PHY_CSR3);
1465		if (!(val & RT2573_BBP_BUSY))
1466			return val & 0xff;
1467		if (rum_pause(sc, hz / 100))
1468			break;
1469	}
1470
1471	device_printf(sc->sc_dev, "could not read BBP\n");
1472	return 0;
1473}
1474
1475static void
1476rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1477{
1478	uint32_t tmp;
1479	int ntries;
1480
1481	for (ntries = 0; ntries < 100; ntries++) {
1482		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1483			break;
1484		if (rum_pause(sc, hz / 100))
1485			break;
1486	}
1487	if (ntries == 100) {
1488		device_printf(sc->sc_dev, "could not write to RF\n");
1489		return;
1490	}
1491
1492	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1493	    (reg & 3);
1494	rum_write(sc, RT2573_PHY_CSR4, tmp);
1495
1496	/* remember last written value in sc */
1497	sc->rf_regs[reg] = val;
1498
1499	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1500}
1501
1502static void
1503rum_select_antenna(struct rum_softc *sc)
1504{
1505	uint8_t bbp4, bbp77;
1506	uint32_t tmp;
1507
1508	bbp4  = rum_bbp_read(sc, 4);
1509	bbp77 = rum_bbp_read(sc, 77);
1510
1511	/* TBD */
1512
1513	/* make sure Rx is disabled before switching antenna */
1514	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1515	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1516
1517	rum_bbp_write(sc,  4, bbp4);
1518	rum_bbp_write(sc, 77, bbp77);
1519
1520	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1521}
1522
1523/*
1524 * Enable multi-rate retries for frames sent at OFDM rates.
1525 * In 802.11b/g mode, allow fallback to CCK rates.
1526 */
1527static void
1528rum_enable_mrr(struct rum_softc *sc)
1529{
1530	struct ieee80211com *ic = &sc->sc_ic;
1531	uint32_t tmp;
1532
1533	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1534
1535	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1536	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1537		tmp |= RT2573_MRR_CCK_FALLBACK;
1538	tmp |= RT2573_MRR_ENABLED;
1539
1540	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1541}
1542
1543static void
1544rum_set_txpreamble(struct rum_softc *sc)
1545{
1546	struct ieee80211com *ic = &sc->sc_ic;
1547	uint32_t tmp;
1548
1549	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1550
1551	tmp &= ~RT2573_SHORT_PREAMBLE;
1552	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1553		tmp |= RT2573_SHORT_PREAMBLE;
1554
1555	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1556}
1557
1558static void
1559rum_set_basicrates(struct rum_softc *sc)
1560{
1561	struct ieee80211com *ic = &sc->sc_ic;
1562
1563	/* update basic rate set */
1564	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1565		/* 11b basic rates: 1, 2Mbps */
1566		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1567	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1568		/* 11a basic rates: 6, 12, 24Mbps */
1569		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1570	} else {
1571		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1572		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1573	}
1574}
1575
1576/*
1577 * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1578 * driver.
1579 */
1580static void
1581rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1582{
1583	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1584	uint32_t tmp;
1585
1586	/* update all BBP registers that depend on the band */
1587	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1588	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1589	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1590		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1591		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1592	}
1593	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1594	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1595		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1596	}
1597
1598	sc->bbp17 = bbp17;
1599	rum_bbp_write(sc,  17, bbp17);
1600	rum_bbp_write(sc,  96, bbp96);
1601	rum_bbp_write(sc, 104, bbp104);
1602
1603	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1604	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1605		rum_bbp_write(sc, 75, 0x80);
1606		rum_bbp_write(sc, 86, 0x80);
1607		rum_bbp_write(sc, 88, 0x80);
1608	}
1609
1610	rum_bbp_write(sc, 35, bbp35);
1611	rum_bbp_write(sc, 97, bbp97);
1612	rum_bbp_write(sc, 98, bbp98);
1613
1614	tmp = rum_read(sc, RT2573_PHY_CSR0);
1615	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1616	if (IEEE80211_IS_CHAN_2GHZ(c))
1617		tmp |= RT2573_PA_PE_2GHZ;
1618	else
1619		tmp |= RT2573_PA_PE_5GHZ;
1620	rum_write(sc, RT2573_PHY_CSR0, tmp);
1621}
1622
1623static void
1624rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1625{
1626	struct ieee80211com *ic = &sc->sc_ic;
1627	const struct rfprog *rfprog;
1628	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1629	int8_t power;
1630	int i, chan;
1631
1632	chan = ieee80211_chan2ieee(ic, c);
1633	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1634		return;
1635
1636	/* select the appropriate RF settings based on what EEPROM says */
1637	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1638		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1639
1640	/* find the settings for this channel (we know it exists) */
1641	for (i = 0; rfprog[i].chan != chan; i++);
1642
1643	power = sc->txpow[i];
1644	if (power < 0) {
1645		bbp94 += power;
1646		power = 0;
1647	} else if (power > 31) {
1648		bbp94 += power - 31;
1649		power = 31;
1650	}
1651
1652	/*
1653	 * If we are switching from the 2GHz band to the 5GHz band or
1654	 * vice-versa, BBP registers need to be reprogrammed.
1655	 */
1656	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1657		rum_select_band(sc, c);
1658		rum_select_antenna(sc);
1659	}
1660	ic->ic_curchan = c;
1661
1662	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1663	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1664	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1665	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1666
1667	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1668	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1669	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1670	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1671
1672	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1673	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1674	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1675	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1676
1677	rum_pause(sc, hz / 100);
1678
1679	/* enable smart mode for MIMO-capable RFs */
1680	bbp3 = rum_bbp_read(sc, 3);
1681
1682	bbp3 &= ~RT2573_SMART_MODE;
1683	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1684		bbp3 |= RT2573_SMART_MODE;
1685
1686	rum_bbp_write(sc, 3, bbp3);
1687
1688	if (bbp94 != RT2573_BBPR94_DEFAULT)
1689		rum_bbp_write(sc, 94, bbp94);
1690
1691	/* give the chip some extra time to do the switchover */
1692	rum_pause(sc, hz / 100);
1693}
1694
1695/*
1696 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1697 * and HostAP operating modes.
1698 */
1699static void
1700rum_enable_tsf_sync(struct rum_softc *sc)
1701{
1702	struct ieee80211com *ic = &sc->sc_ic;
1703	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1704	uint32_t tmp;
1705
1706	if (vap->iv_opmode != IEEE80211_M_STA) {
1707		/*
1708		 * Change default 16ms TBTT adjustment to 8ms.
1709		 * Must be done before enabling beacon generation.
1710		 */
1711		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1712	}
1713
1714	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1715
1716	/* set beacon interval (in 1/16ms unit) */
1717	tmp |= vap->iv_bss->ni_intval * 16;
1718
1719	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1720	if (vap->iv_opmode == IEEE80211_M_STA)
1721		tmp |= RT2573_TSF_MODE(1);
1722	else
1723		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1724
1725	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1726}
1727
1728static void
1729rum_enable_tsf(struct rum_softc *sc)
1730{
1731	rum_write(sc, RT2573_TXRX_CSR9,
1732	    (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1733	    RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1734}
1735
1736static void
1737rum_abort_tsf_sync(struct rum_softc *sc)
1738{
1739	uint32_t tmp;
1740
1741	tmp = rum_read(sc, RT2573_TXRX_CSR9);
1742	rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
1743}
1744
1745static void
1746rum_update_slot(struct rum_softc *sc)
1747{
1748	struct ieee80211com *ic = &sc->sc_ic;
1749	uint8_t slottime;
1750	uint32_t tmp;
1751
1752	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1753
1754	tmp = rum_read(sc, RT2573_MAC_CSR9);
1755	tmp = (tmp & ~0xff) | slottime;
1756	rum_write(sc, RT2573_MAC_CSR9, tmp);
1757
1758	DPRINTF("setting slot time to %uus\n", slottime);
1759}
1760
1761static void
1762rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1763{
1764	uint32_t tmp;
1765
1766	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1767	rum_write(sc, RT2573_MAC_CSR4, tmp);
1768
1769	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1770	rum_write(sc, RT2573_MAC_CSR5, tmp);
1771}
1772
1773static void
1774rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1775{
1776	uint32_t tmp;
1777
1778	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1779	rum_write(sc, RT2573_MAC_CSR2, tmp);
1780
1781	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1782	rum_write(sc, RT2573_MAC_CSR3, tmp);
1783}
1784
1785static void
1786rum_setpromisc(struct rum_softc *sc)
1787{
1788	uint32_t tmp;
1789
1790	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1791
1792	tmp &= ~RT2573_DROP_NOT_TO_ME;
1793	if (sc->sc_ic.ic_promisc == 0)
1794		tmp |= RT2573_DROP_NOT_TO_ME;
1795
1796	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1797
1798	DPRINTF("%s promiscuous mode\n", sc->sc_ic.ic_promisc > 0 ?
1799	    "entering" : "leaving");
1800}
1801
1802static void
1803rum_update_promisc(struct ieee80211com *ic)
1804{
1805	struct rum_softc *sc = ic->ic_softc;
1806
1807	RUM_LOCK(sc);
1808	if (!sc->sc_running) {
1809		RUM_UNLOCK(sc);
1810		return;
1811	}
1812	rum_setpromisc(sc);
1813	RUM_UNLOCK(sc);
1814}
1815
1816static void
1817rum_update_mcast(struct ieee80211com *ic)
1818{
1819	static int warning_printed;
1820
1821	if (warning_printed == 0) {
1822		ic_printf(ic, "need to implement %s\n", __func__);
1823		warning_printed = 1;
1824	}
1825}
1826
1827static const char *
1828rum_get_rf(int rev)
1829{
1830	switch (rev) {
1831	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1832	case RT2573_RF_2528:	return "RT2528";
1833	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1834	case RT2573_RF_5226:	return "RT5226";
1835	default:		return "unknown";
1836	}
1837}
1838
1839static void
1840rum_read_eeprom(struct rum_softc *sc)
1841{
1842	uint16_t val;
1843#ifdef RUM_DEBUG
1844	int i;
1845#endif
1846
1847	/* read MAC address */
1848	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
1849
1850	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1851	val = le16toh(val);
1852	sc->rf_rev =   (val >> 11) & 0x1f;
1853	sc->hw_radio = (val >> 10) & 0x1;
1854	sc->rx_ant =   (val >> 4)  & 0x3;
1855	sc->tx_ant =   (val >> 2)  & 0x3;
1856	sc->nb_ant =   val & 0x3;
1857
1858	DPRINTF("RF revision=%d\n", sc->rf_rev);
1859
1860	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1861	val = le16toh(val);
1862	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1863	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1864
1865	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1866	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1867
1868	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1869	val = le16toh(val);
1870	if ((val & 0xff) != 0xff)
1871		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1872
1873	/* Only [-10, 10] is valid */
1874	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1875		sc->rssi_2ghz_corr = 0;
1876
1877	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1878	val = le16toh(val);
1879	if ((val & 0xff) != 0xff)
1880		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1881
1882	/* Only [-10, 10] is valid */
1883	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1884		sc->rssi_5ghz_corr = 0;
1885
1886	if (sc->ext_2ghz_lna)
1887		sc->rssi_2ghz_corr -= 14;
1888	if (sc->ext_5ghz_lna)
1889		sc->rssi_5ghz_corr -= 14;
1890
1891	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1892	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1893
1894	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1895	val = le16toh(val);
1896	if ((val & 0xff) != 0xff)
1897		sc->rffreq = val & 0xff;
1898
1899	DPRINTF("RF freq=%d\n", sc->rffreq);
1900
1901	/* read Tx power for all a/b/g channels */
1902	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1903	/* XXX default Tx power for 802.11a channels */
1904	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1905#ifdef RUM_DEBUG
1906	for (i = 0; i < 14; i++)
1907		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
1908#endif
1909
1910	/* read default values for BBP registers */
1911	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1912#ifdef RUM_DEBUG
1913	for (i = 0; i < 14; i++) {
1914		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1915			continue;
1916		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1917		    sc->bbp_prom[i].val);
1918	}
1919#endif
1920}
1921
1922static int
1923rum_bbp_init(struct rum_softc *sc)
1924{
1925	int i, ntries;
1926
1927	/* wait for BBP to be ready */
1928	for (ntries = 0; ntries < 100; ntries++) {
1929		const uint8_t val = rum_bbp_read(sc, 0);
1930		if (val != 0 && val != 0xff)
1931			break;
1932		if (rum_pause(sc, hz / 100))
1933			break;
1934	}
1935	if (ntries == 100) {
1936		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1937		return EIO;
1938	}
1939
1940	/* initialize BBP registers to default values */
1941	for (i = 0; i < N(rum_def_bbp); i++)
1942		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1943
1944	/* write vendor-specific BBP values (from EEPROM) */
1945	for (i = 0; i < 16; i++) {
1946		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1947			continue;
1948		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1949	}
1950
1951	return 0;
1952}
1953
1954static void
1955rum_init(struct rum_softc *sc)
1956{
1957	struct ieee80211com *ic = &sc->sc_ic;
1958	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1959	uint32_t tmp;
1960	usb_error_t error;
1961	int i, ntries;
1962
1963	RUM_LOCK_ASSERT(sc, MA_OWNED);
1964
1965	rum_stop(sc);
1966
1967	/* initialize MAC registers to default values */
1968	for (i = 0; i < N(rum_def_mac); i++)
1969		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1970
1971	/* set host ready */
1972	rum_write(sc, RT2573_MAC_CSR1, 3);
1973	rum_write(sc, RT2573_MAC_CSR1, 0);
1974
1975	/* wait for BBP/RF to wakeup */
1976	for (ntries = 0; ntries < 100; ntries++) {
1977		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1978			break;
1979		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
1980		if (rum_pause(sc, hz / 100))
1981			break;
1982	}
1983	if (ntries == 100) {
1984		device_printf(sc->sc_dev,
1985		    "timeout waiting for BBP/RF to wakeup\n");
1986		goto fail;
1987	}
1988
1989	if ((error = rum_bbp_init(sc)) != 0)
1990		goto fail;
1991
1992	/* select default channel */
1993	rum_select_band(sc, ic->ic_curchan);
1994	rum_select_antenna(sc);
1995	rum_set_chan(sc, ic->ic_curchan);
1996
1997	/* clear STA registers */
1998	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1999
2000	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2001
2002	/* initialize ASIC */
2003	rum_write(sc, RT2573_MAC_CSR1, 4);
2004
2005	/*
2006	 * Allocate Tx and Rx xfer queues.
2007	 */
2008	rum_setup_tx_list(sc);
2009
2010	/* update Rx filter */
2011	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2012
2013	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2014	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2015		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2016		       RT2573_DROP_ACKCTS;
2017		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2018			tmp |= RT2573_DROP_TODS;
2019		if (ic->ic_promisc == 0)
2020			tmp |= RT2573_DROP_NOT_TO_ME;
2021	}
2022	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2023
2024	sc->sc_running = 1;
2025	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2026	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2027	return;
2028
2029fail:	rum_stop(sc);
2030#undef N
2031}
2032
2033static void
2034rum_stop(struct rum_softc *sc)
2035{
2036	uint32_t tmp;
2037
2038	RUM_LOCK_ASSERT(sc, MA_OWNED);
2039
2040	sc->sc_running = 0;
2041
2042	RUM_UNLOCK(sc);
2043
2044	/*
2045	 * Drain the USB transfers, if not already drained:
2046	 */
2047	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2048	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2049
2050	RUM_LOCK(sc);
2051
2052	rum_unsetup_tx_list(sc);
2053
2054	/* disable Rx */
2055	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2056	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2057
2058	/* reset ASIC */
2059	rum_write(sc, RT2573_MAC_CSR1, 3);
2060	rum_write(sc, RT2573_MAC_CSR1, 0);
2061}
2062
2063static void
2064rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2065{
2066	struct usb_device_request req;
2067	uint16_t reg = RT2573_MCU_CODE_BASE;
2068	usb_error_t err;
2069
2070	/* copy firmware image into NIC */
2071	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2072		err = rum_write(sc, reg, UGETDW(ucode));
2073		if (err) {
2074			/* firmware already loaded ? */
2075			device_printf(sc->sc_dev, "Firmware load "
2076			    "failure! (ignored)\n");
2077			break;
2078		}
2079	}
2080
2081	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2082	req.bRequest = RT2573_MCU_CNTL;
2083	USETW(req.wValue, RT2573_MCU_RUN);
2084	USETW(req.wIndex, 0);
2085	USETW(req.wLength, 0);
2086
2087	err = rum_do_request(sc, &req, NULL);
2088	if (err != 0) {
2089		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2090		    usbd_errstr(err));
2091	}
2092
2093	/* give the chip some time to boot */
2094	rum_pause(sc, hz / 8);
2095}
2096
2097static void
2098rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2099{
2100	struct ieee80211com *ic = vap->iv_ic;
2101	const struct ieee80211_txparam *tp;
2102	struct rum_tx_desc desc;
2103	struct mbuf *m0;
2104
2105	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2106		return;
2107	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2108		return;
2109
2110	m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2111	if (m0 == NULL)
2112		return;
2113
2114	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2115	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2116	    m0->m_pkthdr.len, tp->mgmtrate);
2117
2118	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2119	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2120
2121	/* copy beacon header and payload into NIC memory */
2122	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2123	    m0->m_pkthdr.len);
2124
2125	m_freem(m0);
2126}
2127
2128static int
2129rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2130    const struct ieee80211_bpf_params *params)
2131{
2132	struct rum_softc *sc = ni->ni_ic->ic_softc;
2133
2134	RUM_LOCK(sc);
2135	/* prevent management frames from being sent if we're not ready */
2136	if (!sc->sc_running) {
2137		RUM_UNLOCK(sc);
2138		m_freem(m);
2139		ieee80211_free_node(ni);
2140		return ENETDOWN;
2141	}
2142	if (sc->tx_nfree < RUM_TX_MINFREE) {
2143		RUM_UNLOCK(sc);
2144		m_freem(m);
2145		ieee80211_free_node(ni);
2146		return EIO;
2147	}
2148
2149	if (params == NULL) {
2150		/*
2151		 * Legacy path; interpret frame contents to decide
2152		 * precisely how to send the frame.
2153		 */
2154		if (rum_tx_mgt(sc, m, ni) != 0)
2155			goto bad;
2156	} else {
2157		/*
2158		 * Caller supplied explicit parameters to use in
2159		 * sending the frame.
2160		 */
2161		if (rum_tx_raw(sc, m, ni, params) != 0)
2162			goto bad;
2163	}
2164	RUM_UNLOCK(sc);
2165
2166	return 0;
2167bad:
2168	RUM_UNLOCK(sc);
2169	ieee80211_free_node(ni);
2170	return EIO;
2171}
2172
2173static void
2174rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2175{
2176	struct ieee80211vap *vap = ni->ni_vap;
2177	struct rum_vap *rvp = RUM_VAP(vap);
2178
2179	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2180	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2181
2182	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2183}
2184
2185static void
2186rum_ratectl_timeout(void *arg)
2187{
2188	struct rum_vap *rvp = arg;
2189	struct ieee80211vap *vap = &rvp->vap;
2190	struct ieee80211com *ic = vap->iv_ic;
2191
2192	ieee80211_runtask(ic, &rvp->ratectl_task);
2193}
2194
2195static void
2196rum_ratectl_task(void *arg, int pending)
2197{
2198	struct rum_vap *rvp = arg;
2199	struct ieee80211vap *vap = &rvp->vap;
2200	struct ieee80211com *ic = vap->iv_ic;
2201	struct rum_softc *sc = ic->ic_softc;
2202	struct ieee80211_node *ni;
2203	int ok, fail;
2204	int sum, retrycnt;
2205
2206	RUM_LOCK(sc);
2207	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2208	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2209
2210	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
2211	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
2212	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2213	sum = ok+fail;
2214	retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2215
2216	ni = ieee80211_ref_node(vap->iv_bss);
2217	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2218	(void) ieee80211_ratectl_rate(ni, NULL, 0);
2219	ieee80211_free_node(ni);
2220
2221	/* count TX retry-fail as Tx errors */
2222	if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, fail);
2223
2224	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2225	RUM_UNLOCK(sc);
2226}
2227
2228static void
2229rum_scan_start(struct ieee80211com *ic)
2230{
2231	struct rum_softc *sc = ic->ic_softc;
2232
2233	RUM_LOCK(sc);
2234	rum_abort_tsf_sync(sc);
2235	rum_set_bssid(sc, ieee80211broadcastaddr);
2236	RUM_UNLOCK(sc);
2237
2238}
2239
2240static void
2241rum_scan_end(struct ieee80211com *ic)
2242{
2243	struct rum_softc *sc = ic->ic_softc;
2244
2245	RUM_LOCK(sc);
2246	rum_enable_tsf_sync(sc);
2247	rum_set_bssid(sc, ic->ic_macaddr);
2248	RUM_UNLOCK(sc);
2249
2250}
2251
2252static void
2253rum_set_channel(struct ieee80211com *ic)
2254{
2255	struct rum_softc *sc = ic->ic_softc;
2256
2257	RUM_LOCK(sc);
2258	rum_set_chan(sc, ic->ic_curchan);
2259	RUM_UNLOCK(sc);
2260}
2261
2262static int
2263rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2264{
2265	struct ieee80211com *ic = &sc->sc_ic;
2266	int lna, agc, rssi;
2267
2268	lna = (raw >> 5) & 0x3;
2269	agc = raw & 0x1f;
2270
2271	if (lna == 0) {
2272		/*
2273		 * No RSSI mapping
2274		 *
2275		 * NB: Since RSSI is relative to noise floor, -1 is
2276		 *     adequate for caller to know error happened.
2277		 */
2278		return -1;
2279	}
2280
2281	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2282
2283	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2284		rssi += sc->rssi_2ghz_corr;
2285
2286		if (lna == 1)
2287			rssi -= 64;
2288		else if (lna == 2)
2289			rssi -= 74;
2290		else if (lna == 3)
2291			rssi -= 90;
2292	} else {
2293		rssi += sc->rssi_5ghz_corr;
2294
2295		if (!sc->ext_5ghz_lna && lna != 1)
2296			rssi += 4;
2297
2298		if (lna == 1)
2299			rssi -= 64;
2300		else if (lna == 2)
2301			rssi -= 86;
2302		else if (lna == 3)
2303			rssi -= 100;
2304	}
2305	return rssi;
2306}
2307
2308static int
2309rum_pause(struct rum_softc *sc, int timeout)
2310{
2311
2312	usb_pause_mtx(&sc->sc_mtx, timeout);
2313	return (0);
2314}
2315
2316static device_method_t rum_methods[] = {
2317	/* Device interface */
2318	DEVMETHOD(device_probe,		rum_match),
2319	DEVMETHOD(device_attach,	rum_attach),
2320	DEVMETHOD(device_detach,	rum_detach),
2321	DEVMETHOD_END
2322};
2323
2324static driver_t rum_driver = {
2325	.name = "rum",
2326	.methods = rum_methods,
2327	.size = sizeof(struct rum_softc),
2328};
2329
2330static devclass_t rum_devclass;
2331
2332DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2333MODULE_DEPEND(rum, wlan, 1, 1, 1);
2334MODULE_DEPEND(rum, usb, 1, 1, 1);
2335MODULE_VERSION(rum, 1);
2336