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