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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 * Ralink Technology RT2501USB/RT2601USB chipset driver
26 * http://www.ralinktech.com.tw/
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
33 #include <sys/mutex.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <sys/endian.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_regdomain.h>
66 #include <net80211/ieee80211_radiotap.h>
67 #include <net80211/ieee80211_amrr.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
73 #define USB_DEBUG_VAR rum_debug
74 #include <dev/usb/usb_debug.h>
76 #include <dev/usb/wlan/if_rumreg.h>
77 #include <dev/usb/wlan/if_rumvar.h>
78 #include <dev/usb/wlan/if_rumfw.h>
81 static int rum_debug = 0;
83 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
84 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
88 static const struct usb_device_id rum_devs[] = {
89 { USB_VP(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM) },
90 { USB_VP(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2) },
91 { USB_VP(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3) },
92 { USB_VP(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4) },
93 { USB_VP(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700) },
94 { USB_VP(USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO) },
95 { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_1) },
96 { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_2) },
97 { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A) },
98 { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3) },
99 { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC) },
100 { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR) },
101 { USB_VP(USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU2) },
102 { USB_VP(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL) },
103 { USB_VP(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX) },
104 { USB_VP(USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F) },
105 { USB_VP(USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573) },
106 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1) },
107 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340) },
108 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA111) },
109 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA110) },
110 { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS) },
111 { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS) },
112 { USB_VP(USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573) },
113 { USB_VP(USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573) },
114 { USB_VP(USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB) },
115 { USB_VP(USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP) },
116 { USB_VP(USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_WUB320G) },
117 { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP) },
118 { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP) },
119 { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1) },
120 { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2) },
121 { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3) },
122 { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4) },
123 { USB_VP(USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573) },
124 { USB_VP(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP) },
125 { USB_VP(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2) },
126 { USB_VP(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM) },
127 { USB_VP(USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573) },
128 { USB_VP(USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2) },
129 { USB_VP(USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_3) },
130 { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573) },
131 { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2) },
132 { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671) },
133 { USB_VP(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2) },
134 { USB_VP(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172) },
135 { USB_VP(USB_VENDOR_SPARKLAN, USB_PRODUCT_SPARKLAN_RT2573) },
136 { USB_VP(USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573) },
139 MODULE_DEPEND(rum, wlan, 1, 1, 1);
140 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
141 MODULE_DEPEND(rum, usb, 1, 1, 1);
143 static device_probe_t rum_match;
144 static device_attach_t rum_attach;
145 static device_detach_t rum_detach;
147 static usb_callback_t rum_bulk_read_callback;
148 static usb_callback_t rum_bulk_write_callback;
150 static usb_error_t rum_do_request(struct rum_softc *sc,
151 struct usb_device_request *req, void *data);
152 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
153 const char name[IFNAMSIZ], int unit, int opmode,
154 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
155 const uint8_t mac[IEEE80211_ADDR_LEN]);
156 static void rum_vap_delete(struct ieee80211vap *);
157 static void rum_tx_free(struct rum_tx_data *, int);
158 static void rum_setup_tx_list(struct rum_softc *);
159 static void rum_unsetup_tx_list(struct rum_softc *);
160 static int rum_newstate(struct ieee80211vap *,
161 enum ieee80211_state, int);
162 static void rum_setup_tx_desc(struct rum_softc *,
163 struct rum_tx_desc *, uint32_t, uint16_t, int,
165 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
166 struct ieee80211_node *);
167 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
168 struct ieee80211_node *,
169 const struct ieee80211_bpf_params *);
170 static int rum_tx_data(struct rum_softc *, struct mbuf *,
171 struct ieee80211_node *);
172 static void rum_start(struct ifnet *);
173 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
174 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
176 static uint32_t rum_read(struct rum_softc *, uint16_t);
177 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
179 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
180 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
182 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
183 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
184 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
185 static void rum_select_antenna(struct rum_softc *);
186 static void rum_enable_mrr(struct rum_softc *);
187 static void rum_set_txpreamble(struct rum_softc *);
188 static void rum_set_basicrates(struct rum_softc *);
189 static void rum_select_band(struct rum_softc *,
190 struct ieee80211_channel *);
191 static void rum_set_chan(struct rum_softc *,
192 struct ieee80211_channel *);
193 static void rum_enable_tsf_sync(struct rum_softc *);
194 static void rum_enable_tsf(struct rum_softc *);
195 static void rum_update_slot(struct ifnet *);
196 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
197 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
198 static void rum_update_promisc(struct ifnet *);
199 static void rum_setpromisc(struct rum_softc *);
200 static const char *rum_get_rf(int);
201 static void rum_read_eeprom(struct rum_softc *);
202 static int rum_bbp_init(struct rum_softc *);
203 static void rum_init_locked(struct rum_softc *);
204 static void rum_init(void *);
205 static void rum_stop(struct rum_softc *);
206 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
208 static int rum_prepare_beacon(struct rum_softc *,
209 struct ieee80211vap *);
210 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
211 const struct ieee80211_bpf_params *);
212 static struct ieee80211_node *rum_node_alloc(struct ieee80211vap *,
213 const uint8_t mac[IEEE80211_ADDR_LEN]);
214 static void rum_newassoc(struct ieee80211_node *, int);
215 static void rum_scan_start(struct ieee80211com *);
216 static void rum_scan_end(struct ieee80211com *);
217 static void rum_set_channel(struct ieee80211com *);
218 static int rum_get_rssi(struct rum_softc *, uint8_t);
219 static void rum_amrr_start(struct rum_softc *,
220 struct ieee80211_node *);
221 static void rum_amrr_timeout(void *);
222 static void rum_amrr_task(void *, int);
223 static int rum_pause(struct rum_softc *, int);
225 static const struct {
229 { RT2573_TXRX_CSR0, 0x025fb032 },
230 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
231 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
232 { RT2573_TXRX_CSR3, 0x00858687 },
233 { RT2573_TXRX_CSR7, 0x2e31353b },
234 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
235 { RT2573_TXRX_CSR15, 0x0000000f },
236 { RT2573_MAC_CSR6, 0x00000fff },
237 { RT2573_MAC_CSR8, 0x016c030a },
238 { RT2573_MAC_CSR10, 0x00000718 },
239 { RT2573_MAC_CSR12, 0x00000004 },
240 { RT2573_MAC_CSR13, 0x00007f00 },
241 { RT2573_SEC_CSR0, 0x00000000 },
242 { RT2573_SEC_CSR1, 0x00000000 },
243 { RT2573_SEC_CSR5, 0x00000000 },
244 { RT2573_PHY_CSR1, 0x000023b0 },
245 { RT2573_PHY_CSR5, 0x00040a06 },
246 { RT2573_PHY_CSR6, 0x00080606 },
247 { RT2573_PHY_CSR7, 0x00000408 },
248 { RT2573_AIFSN_CSR, 0x00002273 },
249 { RT2573_CWMIN_CSR, 0x00002344 },
250 { RT2573_CWMAX_CSR, 0x000034aa }
253 static const struct {
285 static const struct rfprog {
287 uint32_t r1, r2, r3, r4;
289 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
290 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
291 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
292 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
293 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
294 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
295 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
296 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
297 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
298 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
299 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
300 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
301 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
302 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
304 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
305 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
306 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
307 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
309 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
310 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
311 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
312 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
313 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
314 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
315 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
316 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
318 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
319 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
320 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
321 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
322 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
323 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
324 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
325 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
326 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
327 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
328 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
330 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
331 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
332 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
333 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
334 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
336 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
337 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
338 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
339 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
340 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
341 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
342 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
343 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
344 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
345 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
346 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
347 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
348 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
349 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
351 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
352 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
353 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
354 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
356 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
357 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
358 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
359 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
360 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
361 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
362 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
363 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
365 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
366 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
367 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
368 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
369 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
370 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
371 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
372 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
373 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
374 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
375 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
377 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
378 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
379 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
380 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
381 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
384 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
387 .endpoint = UE_ADDR_ANY,
388 .direction = UE_DIR_OUT,
389 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
390 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
391 .callback = rum_bulk_write_callback,
392 .timeout = 5000, /* ms */
396 .endpoint = UE_ADDR_ANY,
397 .direction = UE_DIR_IN,
398 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
399 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
400 .callback = rum_bulk_read_callback,
405 rum_match(device_t self)
407 struct usb_attach_arg *uaa = device_get_ivars(self);
409 if (uaa->usb_mode != USB_MODE_HOST)
411 if (uaa->info.bConfigIndex != 0)
413 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
416 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
420 rum_attach(device_t self)
422 struct usb_attach_arg *uaa = device_get_ivars(self);
423 struct rum_softc *sc = device_get_softc(self);
424 struct ieee80211com *ic;
426 uint8_t iface_index, bands;
430 device_set_usb_desc(self);
431 sc->sc_udev = uaa->device;
434 mtx_init(&sc->sc_mtx, device_get_nameunit(self),
435 MTX_NETWORK_LOCK, MTX_DEF);
437 iface_index = RT2573_IFACE_INDEX;
438 error = usbd_transfer_setup(uaa->device, &iface_index,
439 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
441 device_printf(self, "could not allocate USB transfers, "
442 "err=%s\n", usbd_errstr(error));
447 /* retrieve RT2573 rev. no */
448 for (ntries = 0; ntries < 100; ntries++) {
449 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
451 if (rum_pause(sc, hz / 100))
455 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
460 /* retrieve MAC address and various other things from EEPROM */
463 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
464 tmp, rum_get_rf(sc->rf_rev));
466 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
469 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
471 device_printf(sc->sc_dev, "can not if_alloc()\n");
477 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
478 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
479 ifp->if_init = rum_init;
480 ifp->if_ioctl = rum_ioctl;
481 ifp->if_start = rum_start;
482 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
483 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
484 IFQ_SET_READY(&ifp->if_snd);
487 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
489 /* set device capabilities */
491 IEEE80211_C_STA /* station mode supported */
492 | IEEE80211_C_IBSS /* IBSS mode supported */
493 | IEEE80211_C_MONITOR /* monitor mode supported */
494 | IEEE80211_C_HOSTAP /* HostAp mode supported */
495 | IEEE80211_C_TXPMGT /* tx power management */
496 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
497 | IEEE80211_C_SHSLOT /* short slot time supported */
498 | IEEE80211_C_BGSCAN /* bg scanning supported */
499 | IEEE80211_C_WPA /* 802.11i */
503 setbit(&bands, IEEE80211_MODE_11B);
504 setbit(&bands, IEEE80211_MODE_11G);
505 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
506 setbit(&bands, IEEE80211_MODE_11A);
507 ieee80211_init_channels(ic, NULL, &bands);
509 ieee80211_ifattach(ic, sc->sc_bssid);
510 ic->ic_update_promisc = rum_update_promisc;
511 ic->ic_newassoc = rum_newassoc;
512 ic->ic_raw_xmit = rum_raw_xmit;
513 ic->ic_node_alloc = rum_node_alloc;
514 ic->ic_scan_start = rum_scan_start;
515 ic->ic_scan_end = rum_scan_end;
516 ic->ic_set_channel = rum_set_channel;
518 ic->ic_vap_create = rum_vap_create;
519 ic->ic_vap_delete = rum_vap_delete;
521 ieee80211_radiotap_attach(ic,
522 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
523 RT2573_TX_RADIOTAP_PRESENT,
524 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
525 RT2573_RX_RADIOTAP_PRESENT);
528 ieee80211_announce(ic);
534 return (ENXIO); /* failure */
538 rum_detach(device_t self)
540 struct rum_softc *sc = device_get_softc(self);
541 struct ifnet *ifp = sc->sc_ifp;
542 struct ieee80211com *ic;
544 /* stop all USB transfers */
545 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
547 /* free TX list, if any */
549 rum_unsetup_tx_list(sc);
554 ieee80211_ifdetach(ic);
557 mtx_destroy(&sc->sc_mtx);
563 rum_do_request(struct rum_softc *sc,
564 struct usb_device_request *req, void *data)
570 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
571 req, data, 0, NULL, 250 /* ms */);
575 DPRINTFN(1, "Control request failed, %s (retrying)\n",
577 if (rum_pause(sc, hz / 100))
583 static struct ieee80211vap *
584 rum_vap_create(struct ieee80211com *ic,
585 const char name[IFNAMSIZ], int unit, int opmode, int flags,
586 const uint8_t bssid[IEEE80211_ADDR_LEN],
587 const uint8_t mac[IEEE80211_ADDR_LEN])
589 struct rum_softc *sc = ic->ic_ifp->if_softc;
591 struct ieee80211vap *vap;
593 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
595 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
596 M_80211_VAP, M_NOWAIT | M_ZERO);
600 /* enable s/w bmiss handling for sta mode */
601 ieee80211_vap_setup(ic, vap, name, unit, opmode,
602 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
604 /* override state transition machine */
605 rvp->newstate = vap->iv_newstate;
606 vap->iv_newstate = rum_newstate;
608 usb_callout_init_mtx(&rvp->amrr_ch, &sc->sc_mtx, 0);
609 TASK_INIT(&rvp->amrr_task, 0, rum_amrr_task, rvp);
610 ieee80211_amrr_init(&rvp->amrr, vap,
611 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
612 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
616 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
617 ic->ic_opmode = opmode;
622 rum_vap_delete(struct ieee80211vap *vap)
624 struct rum_vap *rvp = RUM_VAP(vap);
625 struct ieee80211com *ic = vap->iv_ic;
627 usb_callout_drain(&rvp->amrr_ch);
628 ieee80211_draintask(ic, &rvp->amrr_task);
629 ieee80211_amrr_cleanup(&rvp->amrr);
630 ieee80211_vap_detach(vap);
631 free(rvp, M_80211_VAP);
635 rum_tx_free(struct rum_tx_data *data, int txerr)
637 struct rum_softc *sc = data->sc;
639 if (data->m != NULL) {
640 if (data->m->m_flags & M_TXCB)
641 ieee80211_process_callback(data->ni, data->m,
642 txerr ? ETIMEDOUT : 0);
646 ieee80211_free_node(data->ni);
649 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
654 rum_setup_tx_list(struct rum_softc *sc)
656 struct rum_tx_data *data;
660 STAILQ_INIT(&sc->tx_q);
661 STAILQ_INIT(&sc->tx_free);
663 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
664 data = &sc->tx_data[i];
667 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
673 rum_unsetup_tx_list(struct rum_softc *sc)
675 struct rum_tx_data *data;
678 /* make sure any subsequent use of the queues will fail */
680 STAILQ_INIT(&sc->tx_q);
681 STAILQ_INIT(&sc->tx_free);
683 /* free up all node references and mbufs */
684 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
685 data = &sc->tx_data[i];
687 if (data->m != NULL) {
691 if (data->ni != NULL) {
692 ieee80211_free_node(data->ni);
699 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
701 struct rum_vap *rvp = RUM_VAP(vap);
702 struct ieee80211com *ic = vap->iv_ic;
703 struct rum_softc *sc = ic->ic_ifp->if_softc;
704 const struct ieee80211_txparam *tp;
705 enum ieee80211_state ostate;
706 struct ieee80211_node *ni;
709 ostate = vap->iv_state;
710 DPRINTF("%s -> %s\n",
711 ieee80211_state_name[ostate],
712 ieee80211_state_name[nstate]);
714 IEEE80211_UNLOCK(ic);
716 usb_callout_stop(&rvp->amrr_ch);
719 case IEEE80211_S_INIT:
720 if (ostate == IEEE80211_S_RUN) {
721 /* abort TSF synchronization */
722 tmp = rum_read(sc, RT2573_TXRX_CSR9);
723 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
727 case IEEE80211_S_RUN:
730 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
731 rum_update_slot(ic->ic_ifp);
733 rum_set_txpreamble(sc);
734 rum_set_basicrates(sc);
735 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
736 rum_set_bssid(sc, sc->sc_bssid);
739 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
740 vap->iv_opmode == IEEE80211_M_IBSS)
741 rum_prepare_beacon(sc, vap);
743 if (vap->iv_opmode != IEEE80211_M_MONITOR)
744 rum_enable_tsf_sync(sc);
748 /* enable automatic rate adaptation */
749 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
750 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
751 rum_amrr_start(sc, ni);
758 return (rvp->newstate(vap, nstate, arg));
762 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
764 struct rum_softc *sc = usbd_xfer_softc(xfer);
765 struct ifnet *ifp = sc->sc_ifp;
766 struct ieee80211vap *vap;
767 struct rum_tx_data *data;
769 struct usb_page_cache *pc;
773 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
775 switch (USB_GET_STATE(xfer)) {
776 case USB_ST_TRANSFERRED:
777 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
780 data = usbd_xfer_get_priv(xfer);
781 rum_tx_free(data, 0);
782 usbd_xfer_set_priv(xfer, NULL);
785 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
790 data = STAILQ_FIRST(&sc->tx_q);
792 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
795 if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) {
796 DPRINTFN(0, "data overflow, %u bytes\n",
798 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
800 pc = usbd_xfer_get_frame(xfer, 0);
801 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
802 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
805 vap = data->ni->ni_vap;
806 if (ieee80211_radiotap_active_vap(vap)) {
807 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
810 tap->wt_rate = data->rate;
811 tap->wt_antenna = sc->tx_ant;
813 ieee80211_radiotap_tx(vap, m);
816 /* align end on a 4-bytes boundary */
817 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
821 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
822 m->m_pkthdr.len, len);
824 usbd_xfer_set_frame_len(xfer, 0, len);
825 usbd_xfer_set_priv(xfer, data);
827 usbd_transfer_submit(xfer);
835 DPRINTFN(11, "transfer error, %s\n",
839 data = usbd_xfer_get_priv(xfer);
841 rum_tx_free(data, error);
842 usbd_xfer_set_priv(xfer, NULL);
845 if (error == USB_ERR_STALLED) {
846 /* try to clear stall first */
847 usbd_xfer_set_stall(xfer);
850 if (error == USB_ERR_TIMEOUT)
851 device_printf(sc->sc_dev, "device timeout\n");
857 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
859 struct rum_softc *sc = usbd_xfer_softc(xfer);
860 struct ifnet *ifp = sc->sc_ifp;
861 struct ieee80211com *ic = ifp->if_l2com;
862 struct ieee80211_node *ni;
863 struct mbuf *m = NULL;
864 struct usb_page_cache *pc;
869 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
871 switch (USB_GET_STATE(xfer)) {
872 case USB_ST_TRANSFERRED:
874 DPRINTFN(15, "rx done, actlen=%d\n", len);
876 if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
877 DPRINTF("%s: xfer too short %d\n",
878 device_get_nameunit(sc->sc_dev), len);
883 len -= RT2573_RX_DESC_SIZE;
884 pc = usbd_xfer_get_frame(xfer, 0);
885 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
887 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
888 flags = le32toh(sc->sc_rx_desc.flags);
889 if (flags & RT2573_RX_CRC_ERROR) {
891 * This should not happen since we did not
892 * request to receive those frames when we
893 * filled RUM_TXRX_CSR2:
895 DPRINTFN(5, "PHY or CRC error\n");
900 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
902 DPRINTF("could not allocate mbuf\n");
906 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
907 mtod(m, uint8_t *), len);
910 m->m_pkthdr.rcvif = ifp;
911 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
913 if (ieee80211_radiotap_active(ic)) {
914 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
918 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
919 (flags & RT2573_RX_OFDM) ?
920 IEEE80211_T_OFDM : IEEE80211_T_CCK);
921 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
922 tap->wr_antnoise = RT2573_NOISE_FLOOR;
923 tap->wr_antenna = sc->rx_ant;
928 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
929 usbd_transfer_submit(xfer);
932 * At the end of a USB callback it is always safe to unlock
933 * the private mutex of a device! That is why we do the
934 * "ieee80211_input" here, and not some lines up!
938 ni = ieee80211_find_rxnode(ic,
939 mtod(m, struct ieee80211_frame_min *));
941 (void) ieee80211_input(ni, m, rssi,
943 ieee80211_free_node(ni);
945 (void) ieee80211_input_all(ic, m, rssi,
948 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
949 !IFQ_IS_EMPTY(&ifp->if_snd))
955 if (error != USB_ERR_CANCELLED) {
956 /* try to clear stall first */
957 usbd_xfer_set_stall(xfer);
965 rum_plcp_signal(int rate)
968 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
976 case 108: return 0xc;
978 /* CCK rates (NB: not IEEE std, device-specific) */
984 return 0xff; /* XXX unsupported/unknown rate */
988 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
989 uint32_t flags, uint16_t xflags, int len, int rate)
991 struct ifnet *ifp = sc->sc_ifp;
992 struct ieee80211com *ic = ifp->if_l2com;
993 uint16_t plcp_length;
996 desc->flags = htole32(flags);
997 desc->flags |= htole32(RT2573_TX_VALID);
998 desc->flags |= htole32(len << 16);
1000 desc->xflags = htole16(xflags);
1002 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1003 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1005 /* setup PLCP fields */
1006 desc->plcp_signal = rum_plcp_signal(rate);
1007 desc->plcp_service = 4;
1009 len += IEEE80211_CRC_LEN;
1010 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1011 desc->flags |= htole32(RT2573_TX_OFDM);
1013 plcp_length = len & 0xfff;
1014 desc->plcp_length_hi = plcp_length >> 6;
1015 desc->plcp_length_lo = plcp_length & 0x3f;
1017 plcp_length = (16 * len + rate - 1) / rate;
1019 remainder = (16 * len) % 22;
1020 if (remainder != 0 && remainder < 7)
1021 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1023 desc->plcp_length_hi = plcp_length >> 8;
1024 desc->plcp_length_lo = plcp_length & 0xff;
1026 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1027 desc->plcp_signal |= 0x08;
1032 rum_sendprot(struct rum_softc *sc,
1033 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1035 struct ieee80211com *ic = ni->ni_ic;
1036 const struct ieee80211_frame *wh;
1037 struct rum_tx_data *data;
1039 int protrate, ackrate, pktlen, flags, isshort;
1042 RUM_LOCK_ASSERT(sc, MA_OWNED);
1043 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1044 ("protection %d", prot));
1046 wh = mtod(m, const struct ieee80211_frame *);
1047 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1049 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1050 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1052 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1053 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort);
1054 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1055 flags = RT2573_TX_MORE_FRAG;
1056 if (prot == IEEE80211_PROT_RTSCTS) {
1057 /* NB: CTS is the same size as an ACK */
1058 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1059 flags |= RT2573_TX_NEED_ACK;
1060 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1062 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1064 if (mprot == NULL) {
1065 /* XXX stat + msg */
1068 data = STAILQ_FIRST(&sc->tx_free);
1069 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1073 data->ni = ieee80211_ref_node(ni);
1074 data->rate = protrate;
1075 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1077 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1078 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1084 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1086 struct ieee80211vap *vap = ni->ni_vap;
1087 struct ifnet *ifp = sc->sc_ifp;
1088 struct ieee80211com *ic = ifp->if_l2com;
1089 struct rum_tx_data *data;
1090 struct ieee80211_frame *wh;
1091 const struct ieee80211_txparam *tp;
1092 struct ieee80211_key *k;
1096 RUM_LOCK_ASSERT(sc, MA_OWNED);
1098 data = STAILQ_FIRST(&sc->tx_free);
1099 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1102 wh = mtod(m0, struct ieee80211_frame *);
1103 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1104 k = ieee80211_crypto_encap(ni, m0);
1109 wh = mtod(m0, struct ieee80211_frame *);
1112 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1114 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1115 flags |= RT2573_TX_NEED_ACK;
1117 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1118 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1119 *(uint16_t *)wh->i_dur = htole16(dur);
1121 /* tell hardware to add timestamp for probe responses */
1123 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1124 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1125 flags |= RT2573_TX_TIMESTAMP;
1130 data->rate = tp->mgmtrate;
1132 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1134 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1135 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1137 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1138 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1144 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1145 const struct ieee80211_bpf_params *params)
1147 struct ieee80211com *ic = ni->ni_ic;
1148 struct rum_tx_data *data;
1152 RUM_LOCK_ASSERT(sc, MA_OWNED);
1153 KASSERT(params != NULL, ("no raw xmit params"));
1155 rate = params->ibp_rate0;
1156 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1161 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1162 flags |= RT2573_TX_NEED_ACK;
1163 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1164 error = rum_sendprot(sc, m0, ni,
1165 params->ibp_flags & IEEE80211_BPF_RTS ?
1166 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1168 if (error || sc->tx_nfree == 0) {
1172 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1175 data = STAILQ_FIRST(&sc->tx_free);
1176 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1183 /* XXX need to setup descriptor ourself */
1184 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1186 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1187 m0->m_pkthdr.len, rate);
1189 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1190 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1196 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1198 struct ieee80211vap *vap = ni->ni_vap;
1199 struct ifnet *ifp = sc->sc_ifp;
1200 struct ieee80211com *ic = ifp->if_l2com;
1201 struct rum_tx_data *data;
1202 struct ieee80211_frame *wh;
1203 const struct ieee80211_txparam *tp;
1204 struct ieee80211_key *k;
1209 RUM_LOCK_ASSERT(sc, MA_OWNED);
1211 wh = mtod(m0, struct ieee80211_frame *);
1213 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1214 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1215 rate = tp->mcastrate;
1216 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1217 rate = tp->ucastrate;
1219 rate = ni->ni_txrate;
1221 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1222 k = ieee80211_crypto_encap(ni, m0);
1228 /* packet header may have moved, reset our local pointer */
1229 wh = mtod(m0, struct ieee80211_frame *);
1232 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1233 int prot = IEEE80211_PROT_NONE;
1234 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1235 prot = IEEE80211_PROT_RTSCTS;
1236 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1237 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1238 prot = ic->ic_protmode;
1239 if (prot != IEEE80211_PROT_NONE) {
1240 error = rum_sendprot(sc, m0, ni, prot, rate);
1241 if (error || sc->tx_nfree == 0) {
1245 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1249 data = STAILQ_FIRST(&sc->tx_free);
1250 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1257 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1258 flags |= RT2573_TX_NEED_ACK;
1259 flags |= RT2573_TX_MORE_FRAG;
1261 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1262 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1263 *(uint16_t *)wh->i_dur = htole16(dur);
1266 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1268 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1269 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1271 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1272 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1278 rum_start(struct ifnet *ifp)
1280 struct rum_softc *sc = ifp->if_softc;
1281 struct ieee80211_node *ni;
1285 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1290 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1293 if (sc->tx_nfree < RUM_TX_MINFREE) {
1294 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1295 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1298 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1299 if (rum_tx_data(sc, m, ni) != 0) {
1300 ieee80211_free_node(ni);
1309 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1311 struct rum_softc *sc = ifp->if_softc;
1312 struct ieee80211com *ic = ifp->if_l2com;
1313 struct ifreq *ifr = (struct ifreq *) data;
1314 int error = 0, startall = 0;
1319 if (ifp->if_flags & IFF_UP) {
1320 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1321 rum_init_locked(sc);
1326 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1331 ieee80211_start_all(ic);
1334 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1337 error = ether_ioctl(ifp, cmd, data);
1347 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1349 struct usb_device_request req;
1352 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1353 req.bRequest = RT2573_READ_EEPROM;
1354 USETW(req.wValue, 0);
1355 USETW(req.wIndex, addr);
1356 USETW(req.wLength, len);
1358 error = rum_do_request(sc, &req, buf);
1360 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1361 usbd_errstr(error));
1366 rum_read(struct rum_softc *sc, uint16_t reg)
1370 rum_read_multi(sc, reg, &val, sizeof val);
1372 return le32toh(val);
1376 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1378 struct usb_device_request req;
1381 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1382 req.bRequest = RT2573_READ_MULTI_MAC;
1383 USETW(req.wValue, 0);
1384 USETW(req.wIndex, reg);
1385 USETW(req.wLength, len);
1387 error = rum_do_request(sc, &req, buf);
1389 device_printf(sc->sc_dev,
1390 "could not multi read MAC register: %s\n",
1391 usbd_errstr(error));
1396 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1398 uint32_t tmp = htole32(val);
1400 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1404 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1406 struct usb_device_request req;
1409 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1410 req.bRequest = RT2573_WRITE_MULTI_MAC;
1411 USETW(req.wValue, 0);
1412 USETW(req.wIndex, reg);
1413 USETW(req.wLength, len);
1415 error = rum_do_request(sc, &req, buf);
1417 device_printf(sc->sc_dev,
1418 "could not multi write MAC register: %s\n",
1419 usbd_errstr(error));
1425 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1430 DPRINTFN(2, "reg=0x%08x\n", reg);
1432 for (ntries = 0; ntries < 100; ntries++) {
1433 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1435 if (rum_pause(sc, hz / 100))
1438 if (ntries == 100) {
1439 device_printf(sc->sc_dev, "could not write to BBP\n");
1443 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1444 rum_write(sc, RT2573_PHY_CSR3, tmp);
1448 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1453 DPRINTFN(2, "reg=0x%08x\n", reg);
1455 for (ntries = 0; ntries < 100; ntries++) {
1456 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1458 if (rum_pause(sc, hz / 100))
1461 if (ntries == 100) {
1462 device_printf(sc->sc_dev, "could not read BBP\n");
1466 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1467 rum_write(sc, RT2573_PHY_CSR3, val);
1469 for (ntries = 0; ntries < 100; ntries++) {
1470 val = rum_read(sc, RT2573_PHY_CSR3);
1471 if (!(val & RT2573_BBP_BUSY))
1473 if (rum_pause(sc, hz / 100))
1477 device_printf(sc->sc_dev, "could not read BBP\n");
1482 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1487 for (ntries = 0; ntries < 100; ntries++) {
1488 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1490 if (rum_pause(sc, hz / 100))
1493 if (ntries == 100) {
1494 device_printf(sc->sc_dev, "could not write to RF\n");
1498 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1500 rum_write(sc, RT2573_PHY_CSR4, tmp);
1502 /* remember last written value in sc */
1503 sc->rf_regs[reg] = val;
1505 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1509 rum_select_antenna(struct rum_softc *sc)
1511 uint8_t bbp4, bbp77;
1514 bbp4 = rum_bbp_read(sc, 4);
1515 bbp77 = rum_bbp_read(sc, 77);
1519 /* make sure Rx is disabled before switching antenna */
1520 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1521 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1523 rum_bbp_write(sc, 4, bbp4);
1524 rum_bbp_write(sc, 77, bbp77);
1526 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1530 * Enable multi-rate retries for frames sent at OFDM rates.
1531 * In 802.11b/g mode, allow fallback to CCK rates.
1534 rum_enable_mrr(struct rum_softc *sc)
1536 struct ifnet *ifp = sc->sc_ifp;
1537 struct ieee80211com *ic = ifp->if_l2com;
1540 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1542 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1543 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1544 tmp |= RT2573_MRR_CCK_FALLBACK;
1545 tmp |= RT2573_MRR_ENABLED;
1547 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1551 rum_set_txpreamble(struct rum_softc *sc)
1553 struct ifnet *ifp = sc->sc_ifp;
1554 struct ieee80211com *ic = ifp->if_l2com;
1557 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1559 tmp &= ~RT2573_SHORT_PREAMBLE;
1560 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1561 tmp |= RT2573_SHORT_PREAMBLE;
1563 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1567 rum_set_basicrates(struct rum_softc *sc)
1569 struct ifnet *ifp = sc->sc_ifp;
1570 struct ieee80211com *ic = ifp->if_l2com;
1572 /* update basic rate set */
1573 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1574 /* 11b basic rates: 1, 2Mbps */
1575 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1576 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1577 /* 11a basic rates: 6, 12, 24Mbps */
1578 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1580 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1581 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1586 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1590 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1592 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1595 /* update all BBP registers that depend on the band */
1596 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1597 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1598 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1599 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1600 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1602 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1603 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1604 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1608 rum_bbp_write(sc, 17, bbp17);
1609 rum_bbp_write(sc, 96, bbp96);
1610 rum_bbp_write(sc, 104, bbp104);
1612 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1613 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1614 rum_bbp_write(sc, 75, 0x80);
1615 rum_bbp_write(sc, 86, 0x80);
1616 rum_bbp_write(sc, 88, 0x80);
1619 rum_bbp_write(sc, 35, bbp35);
1620 rum_bbp_write(sc, 97, bbp97);
1621 rum_bbp_write(sc, 98, bbp98);
1623 tmp = rum_read(sc, RT2573_PHY_CSR0);
1624 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1625 if (IEEE80211_IS_CHAN_2GHZ(c))
1626 tmp |= RT2573_PA_PE_2GHZ;
1628 tmp |= RT2573_PA_PE_5GHZ;
1629 rum_write(sc, RT2573_PHY_CSR0, tmp);
1633 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1635 struct ifnet *ifp = sc->sc_ifp;
1636 struct ieee80211com *ic = ifp->if_l2com;
1637 const struct rfprog *rfprog;
1638 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1642 chan = ieee80211_chan2ieee(ic, c);
1643 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1646 /* select the appropriate RF settings based on what EEPROM says */
1647 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1648 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1650 /* find the settings for this channel (we know it exists) */
1651 for (i = 0; rfprog[i].chan != chan; i++);
1653 power = sc->txpow[i];
1657 } else if (power > 31) {
1658 bbp94 += power - 31;
1663 * If we are switching from the 2GHz band to the 5GHz band or
1664 * vice-versa, BBP registers need to be reprogrammed.
1666 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1667 rum_select_band(sc, c);
1668 rum_select_antenna(sc);
1672 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1673 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1674 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1675 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1677 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1678 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1679 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1680 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1682 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1683 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1684 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1685 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1687 rum_pause(sc, hz / 100);
1689 /* enable smart mode for MIMO-capable RFs */
1690 bbp3 = rum_bbp_read(sc, 3);
1692 bbp3 &= ~RT2573_SMART_MODE;
1693 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1694 bbp3 |= RT2573_SMART_MODE;
1696 rum_bbp_write(sc, 3, bbp3);
1698 if (bbp94 != RT2573_BBPR94_DEFAULT)
1699 rum_bbp_write(sc, 94, bbp94);
1701 /* give the chip some extra time to do the switchover */
1702 rum_pause(sc, hz / 100);
1706 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1707 * and HostAP operating modes.
1710 rum_enable_tsf_sync(struct rum_softc *sc)
1712 struct ifnet *ifp = sc->sc_ifp;
1713 struct ieee80211com *ic = ifp->if_l2com;
1714 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1717 if (vap->iv_opmode != IEEE80211_M_STA) {
1719 * Change default 16ms TBTT adjustment to 8ms.
1720 * Must be done before enabling beacon generation.
1722 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1725 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1727 /* set beacon interval (in 1/16ms unit) */
1728 tmp |= vap->iv_bss->ni_intval * 16;
1730 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1731 if (vap->iv_opmode == IEEE80211_M_STA)
1732 tmp |= RT2573_TSF_MODE(1);
1734 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1736 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1740 rum_enable_tsf(struct rum_softc *sc)
1742 rum_write(sc, RT2573_TXRX_CSR9,
1743 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1744 RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1748 rum_update_slot(struct ifnet *ifp)
1750 struct rum_softc *sc = ifp->if_softc;
1751 struct ieee80211com *ic = ifp->if_l2com;
1755 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1757 tmp = rum_read(sc, RT2573_MAC_CSR9);
1758 tmp = (tmp & ~0xff) | slottime;
1759 rum_write(sc, RT2573_MAC_CSR9, tmp);
1761 DPRINTF("setting slot time to %uus\n", slottime);
1765 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1769 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1770 rum_write(sc, RT2573_MAC_CSR4, tmp);
1772 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1773 rum_write(sc, RT2573_MAC_CSR5, tmp);
1777 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1781 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1782 rum_write(sc, RT2573_MAC_CSR2, tmp);
1784 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1785 rum_write(sc, RT2573_MAC_CSR3, tmp);
1789 rum_setpromisc(struct rum_softc *sc)
1791 struct ifnet *ifp = sc->sc_ifp;
1794 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1796 tmp &= ~RT2573_DROP_NOT_TO_ME;
1797 if (!(ifp->if_flags & IFF_PROMISC))
1798 tmp |= RT2573_DROP_NOT_TO_ME;
1800 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1802 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1803 "entering" : "leaving");
1807 rum_update_promisc(struct ifnet *ifp)
1809 struct rum_softc *sc = ifp->if_softc;
1811 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1823 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1824 case RT2573_RF_2528: return "RT2528";
1825 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1826 case RT2573_RF_5226: return "RT5226";
1827 default: return "unknown";
1832 rum_read_eeprom(struct rum_softc *sc)
1839 /* read MAC address */
1840 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1842 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1844 sc->rf_rev = (val >> 11) & 0x1f;
1845 sc->hw_radio = (val >> 10) & 0x1;
1846 sc->rx_ant = (val >> 4) & 0x3;
1847 sc->tx_ant = (val >> 2) & 0x3;
1848 sc->nb_ant = val & 0x3;
1850 DPRINTF("RF revision=%d\n", sc->rf_rev);
1852 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1854 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1855 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1857 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1858 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1860 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1862 if ((val & 0xff) != 0xff)
1863 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1865 /* Only [-10, 10] is valid */
1866 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1867 sc->rssi_2ghz_corr = 0;
1869 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1871 if ((val & 0xff) != 0xff)
1872 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1874 /* Only [-10, 10] is valid */
1875 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1876 sc->rssi_5ghz_corr = 0;
1878 if (sc->ext_2ghz_lna)
1879 sc->rssi_2ghz_corr -= 14;
1880 if (sc->ext_5ghz_lna)
1881 sc->rssi_5ghz_corr -= 14;
1883 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1884 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1886 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1888 if ((val & 0xff) != 0xff)
1889 sc->rffreq = val & 0xff;
1891 DPRINTF("RF freq=%d\n", sc->rffreq);
1893 /* read Tx power for all a/b/g channels */
1894 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1895 /* XXX default Tx power for 802.11a channels */
1896 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1898 for (i = 0; i < 14; i++)
1899 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
1902 /* read default values for BBP registers */
1903 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1905 for (i = 0; i < 14; i++) {
1906 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1908 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1909 sc->bbp_prom[i].val);
1915 rum_bbp_init(struct rum_softc *sc)
1917 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1920 /* wait for BBP to be ready */
1921 for (ntries = 0; ntries < 100; ntries++) {
1922 const uint8_t val = rum_bbp_read(sc, 0);
1923 if (val != 0 && val != 0xff)
1925 if (rum_pause(sc, hz / 100))
1928 if (ntries == 100) {
1929 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1933 /* initialize BBP registers to default values */
1934 for (i = 0; i < N(rum_def_bbp); i++)
1935 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1937 /* write vendor-specific BBP values (from EEPROM) */
1938 for (i = 0; i < 16; i++) {
1939 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1941 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1949 rum_init_locked(struct rum_softc *sc)
1951 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1952 struct ifnet *ifp = sc->sc_ifp;
1953 struct ieee80211com *ic = ifp->if_l2com;
1958 RUM_LOCK_ASSERT(sc, MA_OWNED);
1962 /* initialize MAC registers to default values */
1963 for (i = 0; i < N(rum_def_mac); i++)
1964 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1966 /* set host ready */
1967 rum_write(sc, RT2573_MAC_CSR1, 3);
1968 rum_write(sc, RT2573_MAC_CSR1, 0);
1970 /* wait for BBP/RF to wakeup */
1971 for (ntries = 0; ntries < 100; ntries++) {
1972 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1974 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1975 if (rum_pause(sc, hz / 100))
1978 if (ntries == 100) {
1979 device_printf(sc->sc_dev,
1980 "timeout waiting for BBP/RF to wakeup\n");
1984 if ((error = rum_bbp_init(sc)) != 0)
1987 /* select default channel */
1988 rum_select_band(sc, ic->ic_curchan);
1989 rum_select_antenna(sc);
1990 rum_set_chan(sc, ic->ic_curchan);
1992 /* clear STA registers */
1993 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1995 rum_set_macaddr(sc, IF_LLADDR(ifp));
1997 /* initialize ASIC */
1998 rum_write(sc, RT2573_MAC_CSR1, 4);
2001 * Allocate Tx and Rx xfer queues.
2003 rum_setup_tx_list(sc);
2005 /* update Rx filter */
2006 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2008 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2009 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2010 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2012 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2013 tmp |= RT2573_DROP_TODS;
2014 if (!(ifp->if_flags & IFF_PROMISC))
2015 tmp |= RT2573_DROP_NOT_TO_ME;
2017 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2019 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2020 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2021 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2022 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2030 rum_init(void *priv)
2032 struct rum_softc *sc = priv;
2033 struct ifnet *ifp = sc->sc_ifp;
2034 struct ieee80211com *ic = ifp->if_l2com;
2037 rum_init_locked(sc);
2040 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2041 ieee80211_start_all(ic); /* start all vap's */
2045 rum_stop(struct rum_softc *sc)
2047 struct ifnet *ifp = sc->sc_ifp;
2050 RUM_LOCK_ASSERT(sc, MA_OWNED);
2052 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2057 * Drain the USB transfers, if not already drained:
2059 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2060 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2064 rum_unsetup_tx_list(sc);
2067 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2068 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2071 rum_write(sc, RT2573_MAC_CSR1, 3);
2072 rum_write(sc, RT2573_MAC_CSR1, 0);
2076 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2078 struct usb_device_request req;
2079 uint16_t reg = RT2573_MCU_CODE_BASE;
2082 /* copy firmware image into NIC */
2083 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2084 err = rum_write(sc, reg, UGETDW(ucode));
2086 /* firmware already loaded ? */
2087 device_printf(sc->sc_dev, "Firmware load "
2088 "failure! (ignored)\n");
2093 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2094 req.bRequest = RT2573_MCU_CNTL;
2095 USETW(req.wValue, RT2573_MCU_RUN);
2096 USETW(req.wIndex, 0);
2097 USETW(req.wLength, 0);
2099 err = rum_do_request(sc, &req, NULL);
2101 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2105 /* give the chip some time to boot */
2106 rum_pause(sc, hz / 8);
2110 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2112 struct ieee80211com *ic = vap->iv_ic;
2113 const struct ieee80211_txparam *tp;
2114 struct rum_tx_desc desc;
2117 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2122 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2123 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2124 m0->m_pkthdr.len, tp->mgmtrate);
2126 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2127 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2129 /* copy beacon header and payload into NIC memory */
2130 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2139 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2140 const struct ieee80211_bpf_params *params)
2142 struct ifnet *ifp = ni->ni_ic->ic_ifp;
2143 struct rum_softc *sc = ifp->if_softc;
2146 /* prevent management frames from being sent if we're not ready */
2147 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2150 ieee80211_free_node(ni);
2153 if (sc->tx_nfree < RUM_TX_MINFREE) {
2154 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2157 ieee80211_free_node(ni);
2163 if (params == NULL) {
2165 * Legacy path; interpret frame contents to decide
2166 * precisely how to send the frame.
2168 if (rum_tx_mgt(sc, m, ni) != 0)
2172 * Caller supplied explicit parameters to use in
2173 * sending the frame.
2175 if (rum_tx_raw(sc, m, ni, params) != 0)
2184 ieee80211_free_node(ni);
2189 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2191 struct ieee80211vap *vap = ni->ni_vap;
2192 struct rum_vap *rvp = RUM_VAP(vap);
2194 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2195 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2197 ieee80211_amrr_node_init(&rvp->amrr, &RUM_NODE(ni)->amn, ni);
2199 usb_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp);
2203 rum_amrr_timeout(void *arg)
2205 struct rum_vap *rvp = arg;
2206 struct ieee80211vap *vap = &rvp->vap;
2207 struct ieee80211com *ic = vap->iv_ic;
2209 ieee80211_runtask(ic, &rvp->amrr_task);
2213 rum_amrr_task(void *arg, int pending)
2215 struct rum_vap *rvp = arg;
2216 struct ieee80211vap *vap = &rvp->vap;
2217 struct ieee80211com *ic = vap->iv_ic;
2218 struct ifnet *ifp = ic->ic_ifp;
2219 struct rum_softc *sc = ifp->if_softc;
2220 struct ieee80211_node *ni = vap->iv_bss;
2224 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2225 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2227 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */
2228 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */
2229 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2231 ieee80211_amrr_tx_update(&RUM_NODE(ni)->amn,
2232 ok+fail, ok, (le32toh(sc->sta[5]) & 0xffff) + fail);
2233 (void) ieee80211_amrr_choose(ni, &RUM_NODE(ni)->amn);
2235 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2237 usb_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp);
2242 static struct ieee80211_node *
2243 rum_node_alloc(struct ieee80211vap *vap __unused,
2244 const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
2246 struct rum_node *rn;
2248 rn = malloc(sizeof(struct rum_node), M_80211_NODE, M_NOWAIT | M_ZERO);
2249 return rn != NULL ? &rn->ni : NULL;
2253 rum_newassoc(struct ieee80211_node *ni, int isnew)
2255 struct ieee80211vap *vap = ni->ni_vap;
2257 ieee80211_amrr_node_init(&RUM_VAP(vap)->amrr, &RUM_NODE(ni)->amn, ni);
2261 rum_scan_start(struct ieee80211com *ic)
2263 struct ifnet *ifp = ic->ic_ifp;
2264 struct rum_softc *sc = ifp->if_softc;
2268 /* abort TSF synchronization */
2269 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2270 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2271 rum_set_bssid(sc, ifp->if_broadcastaddr);
2277 rum_scan_end(struct ieee80211com *ic)
2279 struct rum_softc *sc = ic->ic_ifp->if_softc;
2282 rum_enable_tsf_sync(sc);
2283 rum_set_bssid(sc, sc->sc_bssid);
2289 rum_set_channel(struct ieee80211com *ic)
2291 struct rum_softc *sc = ic->ic_ifp->if_softc;
2294 rum_set_chan(sc, ic->ic_curchan);
2299 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2301 struct ifnet *ifp = sc->sc_ifp;
2302 struct ieee80211com *ic = ifp->if_l2com;
2305 lna = (raw >> 5) & 0x3;
2312 * NB: Since RSSI is relative to noise floor, -1 is
2313 * adequate for caller to know error happened.
2318 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2320 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2321 rssi += sc->rssi_2ghz_corr;
2330 rssi += sc->rssi_5ghz_corr;
2332 if (!sc->ext_5ghz_lna && lna != 1)
2346 rum_pause(struct rum_softc *sc, int timeout)
2349 usb_pause_mtx(&sc->sc_mtx, timeout);
2353 static device_method_t rum_methods[] = {
2354 /* Device interface */
2355 DEVMETHOD(device_probe, rum_match),
2356 DEVMETHOD(device_attach, rum_attach),
2357 DEVMETHOD(device_detach, rum_detach),
2362 static driver_t rum_driver = {
2364 .methods = rum_methods,
2365 .size = sizeof(struct rum_softc),
2368 static devclass_t rum_devclass;
2370 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);