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