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