4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Ralink Technology RT2501USB/RT2601USB chipset driver
25 * http://www.ralinktech.com.tw/
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/endian.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_amrr.h>
54 #include <net80211/ieee80211_phy.h>
55 #include <net80211/ieee80211_radiotap.h>
56 #include <net80211/ieee80211_regdomain.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/if_rumreg.h>
64 #include <dev/usb/if_rumvar.h>
65 #include <dev/usb/rt2573_ucode.h>
68 #define DPRINTF(x) do { if (rumdebug > 0) printf x; } while (0)
69 #define DPRINTFN(n, x) do { if (rumdebug >= (n)) printf x; } while (0)
71 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
72 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rumdebug, 0,
76 #define DPRINTFN(n, x)
79 /* various supported device vendors/products */
80 static const struct usb_devno rum_devs[] = {
81 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
82 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
83 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 },
84 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 },
85 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
86 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
87 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_1 },
88 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_2 },
89 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
90 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
91 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
92 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
93 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
94 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL },
95 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX },
96 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
97 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
98 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
99 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
100 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA111 },
101 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
102 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
103 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
104 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
105 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
106 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
107 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_WUB320G },
108 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
109 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
110 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1 },
111 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
112 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
113 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
114 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
115 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
116 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
117 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
118 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
119 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
120 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
121 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2 },
122 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
123 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
124 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
125 { USB_VENDOR_SPARKLAN, USB_PRODUCT_SPARKLAN_RT2573 },
126 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
129 MODULE_DEPEND(rum, wlan, 1, 1, 1);
130 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
131 MODULE_DEPEND(rum, usb, 1, 1, 1);
133 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
134 const char name[IFNAMSIZ], int unit, int opmode,
135 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
136 const uint8_t mac[IEEE80211_ADDR_LEN]);
137 static void rum_vap_delete(struct ieee80211vap *);
138 static int rum_alloc_tx_list(struct rum_softc *);
139 static void rum_free_tx_list(struct rum_softc *);
140 static int rum_alloc_rx_list(struct rum_softc *);
141 static void rum_free_rx_list(struct rum_softc *);
142 static void rum_task(void *);
143 static void rum_scantask(void *);
144 static int rum_newstate(struct ieee80211vap *,
145 enum ieee80211_state, int);
146 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
148 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
150 static void rum_setup_tx_desc(struct rum_softc *,
151 struct rum_tx_desc *, uint32_t, uint16_t, int,
153 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
154 struct ieee80211_node *);
155 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
156 struct ieee80211_node *,
157 const struct ieee80211_bpf_params *);
158 static int rum_tx_data(struct rum_softc *, struct mbuf *,
159 struct ieee80211_node *);
160 static void rum_start(struct ifnet *);
161 static void rum_watchdog(void *);
162 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
163 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
165 static uint32_t rum_read(struct rum_softc *, uint16_t);
166 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
168 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
169 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
171 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
172 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
173 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
174 static void rum_select_antenna(struct rum_softc *);
175 static void rum_enable_mrr(struct rum_softc *);
176 static void rum_set_txpreamble(struct rum_softc *);
177 static void rum_set_basicrates(struct rum_softc *);
178 static void rum_select_band(struct rum_softc *,
179 struct ieee80211_channel *);
180 static void rum_set_chan(struct rum_softc *,
181 struct ieee80211_channel *);
182 static void rum_enable_tsf_sync(struct rum_softc *);
183 static void rum_update_slot(struct ifnet *);
184 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
185 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
186 static void rum_update_promisc(struct rum_softc *);
187 static const char *rum_get_rf(int);
188 static void rum_read_eeprom(struct rum_softc *);
189 static int rum_bbp_init(struct rum_softc *);
190 static void rum_init_locked(struct rum_softc *);
191 static void rum_init(void *);
192 static void rum_stop(void *);
193 static int rum_load_microcode(struct rum_softc *, const u_char *,
195 static int rum_prepare_beacon(struct rum_softc *,
196 struct ieee80211vap *);
197 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
198 const struct ieee80211_bpf_params *);
199 static struct ieee80211_node *rum_node_alloc(struct ieee80211vap *,
200 const uint8_t mac[IEEE80211_ADDR_LEN]);
201 static void rum_newassoc(struct ieee80211_node *, int);
202 static void rum_scan_start(struct ieee80211com *);
203 static void rum_scan_end(struct ieee80211com *);
204 static void rum_set_channel(struct ieee80211com *);
205 static int rum_get_rssi(struct rum_softc *, uint8_t);
206 static void rum_amrr_start(struct rum_softc *,
207 struct ieee80211_node *);
208 static void rum_amrr_timeout(void *);
209 static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
212 static const struct {
216 { RT2573_TXRX_CSR0, 0x025fb032 },
217 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
218 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
219 { RT2573_TXRX_CSR3, 0x00858687 },
220 { RT2573_TXRX_CSR7, 0x2e31353b },
221 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
222 { RT2573_TXRX_CSR15, 0x0000000f },
223 { RT2573_MAC_CSR6, 0x00000fff },
224 { RT2573_MAC_CSR8, 0x016c030a },
225 { RT2573_MAC_CSR10, 0x00000718 },
226 { RT2573_MAC_CSR12, 0x00000004 },
227 { RT2573_MAC_CSR13, 0x00007f00 },
228 { RT2573_SEC_CSR0, 0x00000000 },
229 { RT2573_SEC_CSR1, 0x00000000 },
230 { RT2573_SEC_CSR5, 0x00000000 },
231 { RT2573_PHY_CSR1, 0x000023b0 },
232 { RT2573_PHY_CSR5, 0x00040a06 },
233 { RT2573_PHY_CSR6, 0x00080606 },
234 { RT2573_PHY_CSR7, 0x00000408 },
235 { RT2573_AIFSN_CSR, 0x00002273 },
236 { RT2573_CWMIN_CSR, 0x00002344 },
237 { RT2573_CWMAX_CSR, 0x000034aa }
240 static const struct {
272 static const struct rfprog {
274 uint32_t r1, r2, r3, r4;
276 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
277 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
278 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
279 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
280 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
281 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
282 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
283 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
284 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
285 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
286 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
287 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
288 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
289 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
291 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
292 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
293 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
294 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
296 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
297 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
298 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
299 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
300 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
301 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
302 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
303 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
305 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
306 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
307 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
308 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
309 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
310 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
311 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
312 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
313 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
314 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
315 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
317 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
318 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
319 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
320 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
321 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
323 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
324 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
325 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
326 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
327 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
328 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
329 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
330 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
331 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
332 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
333 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
334 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
335 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
336 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
338 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
339 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
340 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
341 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
343 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
344 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
345 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
346 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
347 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
348 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
349 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
350 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
352 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
353 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
354 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
355 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
356 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
357 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
358 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
359 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
360 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
361 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
362 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
364 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
365 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
366 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
367 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
368 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
372 rum_match(device_t self)
374 struct usb_attach_arg *uaa = device_get_ivars(self);
376 if (uaa->iface != NULL)
379 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
380 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
384 rum_attach(device_t self)
386 struct rum_softc *sc = device_get_softc(self);
387 struct usb_attach_arg *uaa = device_get_ivars(self);
388 struct ieee80211com *ic;
390 const uint8_t *ucode = NULL;
391 usb_interface_descriptor_t *id;
392 usb_endpoint_descriptor_t *ed;
398 sc->sc_udev = uaa->device;
401 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
402 device_printf(self, "could not set configuration no\n");
406 /* get the first interface handle */
407 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
410 device_printf(self, "could not get interface handle\n");
417 id = usbd_get_interface_descriptor(sc->sc_iface);
419 sc->sc_rx_no = sc->sc_tx_no = -1;
420 for (i = 0; i < id->bNumEndpoints; i++) {
421 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
424 "no endpoint descriptor for iface %d\n", i);
428 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
429 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
430 sc->sc_rx_no = ed->bEndpointAddress;
431 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
432 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
433 sc->sc_tx_no = ed->bEndpointAddress;
435 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
436 device_printf(self, "missing endpoint\n");
440 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
442 device_printf(self, "can not if_alloc()\n");
447 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
448 MTX_DEF | MTX_RECURSE);
450 usb_init_task(&sc->sc_task, rum_task, sc);
451 usb_init_task(&sc->sc_scantask, rum_scantask, sc);
452 callout_init(&sc->watchdog_ch, 0);
454 /* retrieve RT2573 rev. no */
455 for (ntries = 0; ntries < 1000; ntries++) {
456 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
460 if (ntries == 1000) {
461 device_printf(self, "timeout waiting for chip to settle\n");
465 /* retrieve MAC address and various other things from EEPROM */
468 device_printf(self, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
469 tmp, rum_get_rf(sc->rf_rev));
471 ucode = rt2573_ucode;
472 size = sizeof rt2573_ucode;
473 error = rum_load_microcode(sc, ucode, size);
475 device_printf(self, "could not load 8051 microcode\n");
480 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
481 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
482 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
483 ifp->if_init = rum_init;
484 ifp->if_ioctl = rum_ioctl;
485 ifp->if_start = rum_start;
486 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
487 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
488 IFQ_SET_READY(&ifp->if_snd);
491 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
493 /* set device capabilities */
495 IEEE80211_C_STA /* station mode supported */
496 | IEEE80211_C_IBSS /* IBSS mode supported */
497 | IEEE80211_C_MONITOR /* monitor mode supported */
498 | IEEE80211_C_HOSTAP /* HostAp mode supported */
499 | IEEE80211_C_TXPMGT /* tx power management */
500 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
501 | IEEE80211_C_SHSLOT /* short slot time supported */
502 | IEEE80211_C_BGSCAN /* bg scanning supported */
503 | IEEE80211_C_WPA /* 802.11i */
507 setbit(&bands, IEEE80211_MODE_11B);
508 setbit(&bands, IEEE80211_MODE_11G);
509 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
510 setbit(&bands, IEEE80211_MODE_11A);
511 ieee80211_init_channels(ic, NULL, &bands);
513 ieee80211_ifattach(ic);
514 ic->ic_newassoc = rum_newassoc;
515 ic->ic_raw_xmit = rum_raw_xmit;
516 ic->ic_node_alloc = rum_node_alloc;
517 ic->ic_scan_start = rum_scan_start;
518 ic->ic_scan_end = rum_scan_end;
519 ic->ic_set_channel = rum_set_channel;
521 ic->ic_vap_create = rum_vap_create;
522 ic->ic_vap_delete = rum_vap_delete;
524 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
526 bpfattach(ifp, DLT_IEEE802_11_RADIO,
527 sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap));
529 sc->sc_rxtap_len = sizeof sc->sc_rxtap;
530 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
531 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
533 sc->sc_txtap_len = sizeof sc->sc_txtap;
534 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
535 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
538 ieee80211_announce(ic);
542 mtx_destroy(&sc->sc_mtx);
548 rum_detach(device_t self)
550 struct rum_softc *sc = device_get_softc(self);
551 struct ifnet *ifp = sc->sc_ifp;
552 struct ieee80211com *ic = ifp->if_l2com;
556 ieee80211_ifdetach(ic);
558 usb_rem_task(sc->sc_udev, &sc->sc_task);
559 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
560 callout_stop(&sc->watchdog_ch);
562 if (sc->amrr_xfer != NULL) {
563 usbd_free_xfer(sc->amrr_xfer);
564 sc->amrr_xfer = NULL;
567 if (sc->sc_rx_pipeh != NULL) {
568 usbd_abort_pipe(sc->sc_rx_pipeh);
569 usbd_close_pipe(sc->sc_rx_pipeh);
571 if (sc->sc_tx_pipeh != NULL) {
572 usbd_abort_pipe(sc->sc_tx_pipeh);
573 usbd_close_pipe(sc->sc_tx_pipeh);
576 rum_free_rx_list(sc);
577 rum_free_tx_list(sc);
580 mtx_destroy(&sc->sc_mtx);
585 static struct ieee80211vap *
586 rum_vap_create(struct ieee80211com *ic,
587 const char name[IFNAMSIZ], int unit, int opmode, int flags,
588 const uint8_t bssid[IEEE80211_ADDR_LEN],
589 const uint8_t mac[IEEE80211_ADDR_LEN])
592 struct ieee80211vap *vap;
594 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
596 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
597 M_80211_VAP, M_NOWAIT | M_ZERO);
601 /* enable s/w bmiss handling for sta mode */
602 ieee80211_vap_setup(ic, vap, name, unit, opmode,
603 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
605 /* override state transition machine */
606 rvp->newstate = vap->iv_newstate;
607 vap->iv_newstate = rum_newstate;
609 callout_init(&rvp->amrr_ch, 0);
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);
626 callout_stop(&rvp->amrr_ch);
627 ieee80211_amrr_cleanup(&rvp->amrr);
628 ieee80211_vap_detach(vap);
629 free(rvp, M_80211_VAP);
633 rum_alloc_tx_list(struct rum_softc *sc)
635 struct rum_tx_data *data;
638 sc->tx_queued = sc->tx_cur = 0;
640 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
641 data = &sc->tx_data[i];
645 data->xfer = usbd_alloc_xfer(sc->sc_udev);
646 if (data->xfer == NULL) {
647 device_printf(sc->sc_dev,
648 "could not allocate tx xfer\n");
652 data->buf = usbd_alloc_buffer(data->xfer,
653 RT2573_TX_DESC_SIZE + MCLBYTES);
654 if (data->buf == NULL) {
655 device_printf(sc->sc_dev,
656 "could not allocate tx buffer\n");
660 /* clean Tx descriptor */
661 bzero(data->buf, RT2573_TX_DESC_SIZE);
666 fail: rum_free_tx_list(sc);
671 rum_free_tx_list(struct rum_softc *sc)
673 struct rum_tx_data *data;
676 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
677 data = &sc->tx_data[i];
679 if (data->xfer != NULL) {
680 usbd_free_xfer(data->xfer);
684 if (data->ni != NULL) {
685 ieee80211_free_node(data->ni);
692 rum_alloc_rx_list(struct rum_softc *sc)
694 struct rum_rx_data *data;
697 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
698 data = &sc->rx_data[i];
702 data->xfer = usbd_alloc_xfer(sc->sc_udev);
703 if (data->xfer == NULL) {
704 device_printf(sc->sc_dev,
705 "could not allocate rx xfer\n");
709 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
710 device_printf(sc->sc_dev,
711 "could not allocate rx buffer\n");
716 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
717 if (data->m == NULL) {
718 device_printf(sc->sc_dev,
719 "could not allocate rx mbuf\n");
724 data->buf = mtod(data->m, uint8_t *);
729 fail: rum_free_tx_list(sc);
734 rum_free_rx_list(struct rum_softc *sc)
736 struct rum_rx_data *data;
739 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
740 data = &sc->rx_data[i];
742 if (data->xfer != NULL) {
743 usbd_free_xfer(data->xfer);
746 if (data->m != NULL) {
756 struct rum_softc *sc = arg;
757 struct ifnet *ifp = sc->sc_ifp;
758 struct ieee80211com *ic = ifp->if_l2com;
759 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
760 struct rum_vap *rvp = RUM_VAP(vap);
761 const struct ieee80211_txparam *tp;
762 enum ieee80211_state ostate;
763 struct ieee80211_node *ni;
766 ostate = vap->iv_state;
770 switch (sc->sc_state) {
771 case IEEE80211_S_INIT:
772 if (ostate == IEEE80211_S_RUN) {
773 /* abort TSF synchronization */
774 tmp = rum_read(sc, RT2573_TXRX_CSR9);
775 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
779 case IEEE80211_S_RUN:
782 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
783 rum_update_slot(ic->ic_ifp);
785 rum_set_txpreamble(sc);
786 rum_set_basicrates(sc);
787 rum_set_bssid(sc, ni->ni_bssid);
790 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
791 vap->iv_opmode == IEEE80211_M_IBSS)
792 rum_prepare_beacon(sc, vap);
794 if (vap->iv_opmode != IEEE80211_M_MONITOR)
795 rum_enable_tsf_sync(sc);
797 /* enable automatic rate adaptation */
798 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
799 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
800 rum_amrr_start(sc, ni);
809 rvp->newstate(vap, sc->sc_state, sc->sc_arg);
810 if (vap->iv_newstate_cb != NULL)
811 vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg);
812 IEEE80211_UNLOCK(ic);
816 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
818 struct rum_vap *rvp = RUM_VAP(vap);
819 struct ieee80211com *ic = vap->iv_ic;
820 struct rum_softc *sc = ic->ic_ifp->if_softc;
822 usb_rem_task(sc->sc_udev, &sc->sc_task);
823 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
824 callout_stop(&rvp->amrr_ch);
826 /* do it in a process context */
827 sc->sc_state = nstate;
830 if (nstate == IEEE80211_S_INIT) {
831 rvp->newstate(vap, nstate, arg);
834 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
840 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
842 struct rum_tx_data *data = priv;
843 struct rum_softc *sc = data->sc;
844 struct ifnet *ifp = sc->sc_ifp;
846 if (data->m != NULL && data->m->m_flags & M_TXCB)
847 ieee80211_process_callback(data->ni, data->m, 0/*XXX*/);
849 if (status != USBD_NORMAL_COMPLETION) {
850 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
853 device_printf(sc->sc_dev, "could not transmit buffer: %s\n",
854 usbd_errstr(status));
856 if (status == USBD_STALLED)
857 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
865 ieee80211_free_node(data->ni);
871 DPRINTFN(10, ("tx done\n"));
874 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
879 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
881 struct rum_rx_data *data = priv;
882 struct rum_softc *sc = data->sc;
883 struct ifnet *ifp = sc->sc_ifp;
884 struct ieee80211com *ic = ifp->if_l2com;
885 struct rum_rx_desc *desc;
886 struct ieee80211_node *ni;
887 struct mbuf *mnew, *m;
890 if (status != USBD_NORMAL_COMPLETION) {
891 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
894 if (status == USBD_STALLED)
895 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
899 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
901 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
902 DPRINTF(("%s: xfer too short %d\n",
903 device_get_nameunit(sc->sc_dev), len));
908 desc = (struct rum_rx_desc *)data->buf;
910 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
912 * This should not happen since we did not request to receive
913 * those frames when we filled RT2573_TXRX_CSR0.
915 DPRINTFN(5, ("CRC error\n"));
920 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
928 data->buf = mtod(data->m, uint8_t *);
931 m->m_pkthdr.rcvif = ifp;
932 m->m_data = (caddr_t)(desc + 1);
933 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
935 rssi = rum_get_rssi(sc, desc->rssi);
937 if (bpf_peers_present(ifp->if_bpf)) {
938 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
940 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
941 tap->wr_rate = ieee80211_plcp2rate(desc->rate,
942 (desc->flags & htole32(RT2573_RX_OFDM)) ?
943 IEEE80211_T_OFDM : IEEE80211_T_CCK);
944 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
945 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
946 tap->wr_antenna = sc->rx_ant;
947 tap->wr_antsignal = rssi;
949 bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
952 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
954 /* Error happened during RSSI conversion. */
956 rssi = -30; /* XXX ignored by net80211 */
957 (void) ieee80211_input(ni, m, rssi, RT2573_NOISE_FLOOR, 0);
958 ieee80211_free_node(ni);
960 (void) ieee80211_input_all(ic, m, rssi, RT2573_NOISE_FLOOR, 0);
962 DPRINTFN(15, ("rx done\n"));
964 skip: /* setup a new transfer */
965 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
966 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
971 rum_plcp_signal(int rate)
974 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
982 case 108: return 0xc;
984 /* CCK rates (NB: not IEEE std, device-specific) */
990 return 0xff; /* XXX unsupported/unknown rate */
994 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
995 uint32_t flags, uint16_t xflags, int len, int rate)
997 struct ifnet *ifp = sc->sc_ifp;
998 struct ieee80211com *ic = ifp->if_l2com;
999 uint16_t plcp_length;
1002 desc->flags = htole32(flags);
1003 desc->flags |= htole32(RT2573_TX_VALID);
1004 desc->flags |= htole32(len << 16);
1006 desc->xflags = htole16(xflags);
1008 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1009 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1011 /* setup PLCP fields */
1012 desc->plcp_signal = rum_plcp_signal(rate);
1013 desc->plcp_service = 4;
1015 len += IEEE80211_CRC_LEN;
1016 if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
1017 desc->flags |= htole32(RT2573_TX_OFDM);
1019 plcp_length = len & 0xfff;
1020 desc->plcp_length_hi = plcp_length >> 6;
1021 desc->plcp_length_lo = plcp_length & 0x3f;
1023 plcp_length = (16 * len + rate - 1) / rate;
1025 remainder = (16 * len) % 22;
1026 if (remainder != 0 && remainder < 7)
1027 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1029 desc->plcp_length_hi = plcp_length >> 8;
1030 desc->plcp_length_lo = plcp_length & 0xff;
1032 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1033 desc->plcp_signal |= 0x08;
1037 #define RUM_TX_TIMEOUT 5000
1040 rum_sendprot(struct rum_softc *sc,
1041 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1043 struct ieee80211com *ic = ni->ni_ic;
1044 const struct ieee80211_frame *wh;
1045 struct rum_tx_desc *desc;
1046 struct rum_tx_data *data;
1048 int protrate, ackrate, pktlen, flags, isshort;
1052 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1053 ("protection %d", prot));
1055 wh = mtod(m, const struct ieee80211_frame *);
1056 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1058 protrate = ieee80211_ctl_rate(sc->sc_rates, rate);
1059 ackrate = ieee80211_ack_rate(sc->sc_rates, rate);
1061 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1062 dur = ieee80211_compute_duration(sc->sc_rates, pktlen, rate, isshort);
1063 + ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1064 flags = RT2573_TX_MORE_FRAG;
1065 if (prot == IEEE80211_PROT_RTSCTS) {
1066 /* NB: CTS is the same size as an ACK */
1067 dur += ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1068 flags |= RT2573_TX_NEED_ACK;
1069 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1071 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1073 if (mprot == NULL) {
1074 /* XXX stat + msg */
1077 data = &sc->tx_data[sc->tx_cur];
1078 desc = (struct rum_tx_desc *)data->buf;
1081 data->ni = ieee80211_ref_node(ni);
1082 m_copydata(mprot, 0, mprot->m_pkthdr.len,
1083 data->buf + RT2573_TX_DESC_SIZE);
1084 rum_setup_tx_desc(sc, desc, flags, 0, mprot->m_pkthdr.len, protrate);
1086 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1087 /* NB: no roundup necessary */
1088 RT2573_TX_DESC_SIZE + mprot->m_pkthdr.len,
1089 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1091 error = usbd_transfer(data->xfer);
1092 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1099 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1105 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1107 struct ieee80211vap *vap = ni->ni_vap;
1108 struct ifnet *ifp = sc->sc_ifp;
1109 struct ieee80211com *ic = ifp->if_l2com;
1110 struct rum_tx_desc *desc;
1111 struct rum_tx_data *data;
1112 struct ieee80211_frame *wh;
1113 const struct ieee80211_txparam *tp;
1114 struct ieee80211_key *k;
1120 data = &sc->tx_data[sc->tx_cur];
1123 desc = (struct rum_tx_desc *)data->buf;
1125 wh = mtod(m0, struct ieee80211_frame *);
1126 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1127 k = ieee80211_crypto_encap(ni, m0);
1132 wh = mtod(m0, struct ieee80211_frame *);
1135 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1137 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1138 flags |= RT2573_TX_NEED_ACK;
1140 dur = ieee80211_ack_duration(sc->sc_rates, tp->mgmtrate,
1141 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1142 *(uint16_t *)wh->i_dur = htole16(dur);
1144 /* tell hardware to add timestamp for probe responses */
1146 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1147 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1148 flags |= RT2573_TX_TIMESTAMP;
1151 if (bpf_peers_present(ifp->if_bpf)) {
1152 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1155 tap->wt_rate = tp->mgmtrate;
1156 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1157 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1158 tap->wt_antenna = sc->tx_ant;
1160 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1163 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1164 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1166 /* align end on a 4-bytes boundary */
1167 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1170 * No space left in the last URB to store the extra 4 bytes, force
1171 * sending of another URB.
1173 if ((xferlen % 64) == 0)
1176 DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
1177 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate, xferlen));
1179 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1180 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1182 error = usbd_transfer(data->xfer);
1183 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1191 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1197 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1198 const struct ieee80211_bpf_params *params)
1200 struct ifnet *ifp = sc->sc_ifp;
1201 struct ieee80211com *ic = ifp->if_l2com;
1202 struct rum_tx_desc *desc;
1203 struct rum_tx_data *data;
1208 KASSERT(params != NULL, ("no raw xmit params"));
1210 data = &sc->tx_data[sc->tx_cur];
1211 desc = (struct rum_tx_desc *)data->buf;
1213 rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1220 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1221 flags |= RT2573_TX_NEED_ACK;
1222 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1223 error = rum_sendprot(sc, m0, ni,
1224 params->ibp_flags & IEEE80211_BPF_RTS ?
1225 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1231 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1234 if (bpf_peers_present(ifp->if_bpf)) {
1235 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1238 tap->wt_rate = rate;
1239 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1240 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1241 tap->wt_antenna = sc->tx_ant;
1243 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1249 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1250 /* XXX need to setup descriptor ourself */
1251 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1253 /* align end on a 4-bytes boundary */
1254 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1257 * No space left in the last URB to store the extra 4 bytes, force
1258 * sending of another URB.
1260 if ((xferlen % 64) == 0)
1263 DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
1264 m0->m_pkthdr.len, rate, xferlen));
1266 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1267 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
1270 error = usbd_transfer(data->xfer);
1271 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1275 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1281 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1283 struct ieee80211vap *vap = ni->ni_vap;
1284 struct ifnet *ifp = sc->sc_ifp;
1285 struct ieee80211com *ic = ifp->if_l2com;
1286 struct rum_tx_desc *desc;
1287 struct rum_tx_data *data;
1288 struct ieee80211_frame *wh;
1289 const struct ieee80211_txparam *tp;
1290 struct ieee80211_key *k;
1296 wh = mtod(m0, struct ieee80211_frame *);
1298 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1299 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1300 rate = tp->mcastrate;
1301 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1302 rate = tp->ucastrate;
1304 (void) ieee80211_amrr_choose(ni, &RUM_NODE(ni)->amn);
1305 rate = ni->ni_txrate;
1308 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1309 k = ieee80211_crypto_encap(ni, m0);
1315 /* packet header may have moved, reset our local pointer */
1316 wh = mtod(m0, struct ieee80211_frame *);
1319 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1320 int prot = IEEE80211_PROT_NONE;
1321 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1322 prot = IEEE80211_PROT_RTSCTS;
1323 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1324 ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
1325 prot = ic->ic_protmode;
1326 if (prot != IEEE80211_PROT_NONE) {
1327 error = rum_sendprot(sc, m0, ni, prot, rate);
1332 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1336 data = &sc->tx_data[sc->tx_cur];
1337 desc = (struct rum_tx_desc *)data->buf;
1342 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1343 flags |= RT2573_TX_NEED_ACK;
1344 flags |= RT2573_TX_MORE_FRAG;
1346 dur = ieee80211_ack_duration(sc->sc_rates, rate,
1347 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1348 *(uint16_t *)wh->i_dur = htole16(dur);
1351 if (bpf_peers_present(ifp->if_bpf)) {
1352 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1355 tap->wt_rate = rate;
1356 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1357 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1358 tap->wt_antenna = sc->tx_ant;
1360 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1363 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1364 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1366 /* align end on a 4-bytes boundary */
1367 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1370 * No space left in the last URB to store the extra 4 bytes, force
1371 * sending of another URB.
1373 if ((xferlen % 64) == 0)
1376 DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
1377 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1379 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1380 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1382 error = usbd_transfer(data->xfer);
1383 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1391 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1397 rum_start(struct ifnet *ifp)
1399 struct rum_softc *sc = ifp->if_softc;
1400 struct ieee80211_node *ni;
1404 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1407 if (sc->tx_queued >= RUM_TX_LIST_COUNT-1) {
1408 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1409 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1412 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1413 m = ieee80211_encap(ni, m);
1415 ieee80211_free_node(ni);
1418 if (rum_tx_data(sc, m, ni) != 0) {
1419 ieee80211_free_node(ni);
1423 sc->sc_tx_timer = 5;
1424 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1429 rum_watchdog(void *arg)
1431 struct rum_softc *sc = arg;
1435 if (sc->sc_tx_timer > 0) {
1436 if (--sc->sc_tx_timer == 0) {
1437 device_printf(sc->sc_dev, "device timeout\n");
1438 /*rum_init(ifp); XXX needs a process context! */
1439 sc->sc_ifp->if_oerrors++;
1443 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1450 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1452 struct rum_softc *sc = ifp->if_softc;
1453 struct ieee80211com *ic = ifp->if_l2com;
1454 struct ifreq *ifr = (struct ifreq *) data;
1455 int error = 0, startall = 0;
1460 if (ifp->if_flags & IFF_UP) {
1461 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1465 rum_update_promisc(sc);
1467 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1472 ieee80211_start_all(ic);
1475 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1478 error = ether_ioctl(ifp, cmd, data);
1488 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1490 usb_device_request_t req;
1493 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1494 req.bRequest = RT2573_READ_EEPROM;
1495 USETW(req.wValue, 0);
1496 USETW(req.wIndex, addr);
1497 USETW(req.wLength, len);
1499 error = usbd_do_request(sc->sc_udev, &req, buf);
1501 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1502 usbd_errstr(error));
1507 rum_read(struct rum_softc *sc, uint16_t reg)
1511 rum_read_multi(sc, reg, &val, sizeof val);
1513 return le32toh(val);
1517 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1519 usb_device_request_t req;
1522 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1523 req.bRequest = RT2573_READ_MULTI_MAC;
1524 USETW(req.wValue, 0);
1525 USETW(req.wIndex, reg);
1526 USETW(req.wLength, len);
1528 error = usbd_do_request(sc->sc_udev, &req, buf);
1530 device_printf(sc->sc_dev,
1531 "could not multi read MAC register: %s\n",
1532 usbd_errstr(error));
1537 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1539 uint32_t tmp = htole32(val);
1541 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1545 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1547 usb_device_request_t req;
1550 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1551 req.bRequest = RT2573_WRITE_MULTI_MAC;
1552 USETW(req.wValue, 0);
1553 USETW(req.wIndex, reg);
1554 USETW(req.wLength, len);
1556 error = usbd_do_request(sc->sc_udev, &req, buf);
1558 device_printf(sc->sc_dev,
1559 "could not multi write MAC register: %s\n",
1560 usbd_errstr(error));
1565 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1570 for (ntries = 0; ntries < 5; ntries++) {
1571 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1575 device_printf(sc->sc_dev, "could not write to BBP\n");
1579 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1580 rum_write(sc, RT2573_PHY_CSR3, tmp);
1584 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1589 for (ntries = 0; ntries < 5; ntries++) {
1590 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1594 device_printf(sc->sc_dev, "could not read BBP\n");
1598 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1599 rum_write(sc, RT2573_PHY_CSR3, val);
1601 for (ntries = 0; ntries < 100; ntries++) {
1602 val = rum_read(sc, RT2573_PHY_CSR3);
1603 if (!(val & RT2573_BBP_BUSY))
1608 device_printf(sc->sc_dev, "could not read BBP\n");
1613 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1618 for (ntries = 0; ntries < 5; ntries++) {
1619 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1623 device_printf(sc->sc_dev, "could not write to RF\n");
1627 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1629 rum_write(sc, RT2573_PHY_CSR4, tmp);
1631 /* remember last written value in sc */
1632 sc->rf_regs[reg] = val;
1634 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1638 rum_select_antenna(struct rum_softc *sc)
1640 uint8_t bbp4, bbp77;
1643 bbp4 = rum_bbp_read(sc, 4);
1644 bbp77 = rum_bbp_read(sc, 77);
1648 /* make sure Rx is disabled before switching antenna */
1649 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1650 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1652 rum_bbp_write(sc, 4, bbp4);
1653 rum_bbp_write(sc, 77, bbp77);
1655 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1659 * Enable multi-rate retries for frames sent at OFDM rates.
1660 * In 802.11b/g mode, allow fallback to CCK rates.
1663 rum_enable_mrr(struct rum_softc *sc)
1665 struct ifnet *ifp = sc->sc_ifp;
1666 struct ieee80211com *ic = ifp->if_l2com;
1669 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1671 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1672 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1673 tmp |= RT2573_MRR_CCK_FALLBACK;
1674 tmp |= RT2573_MRR_ENABLED;
1676 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1680 rum_set_txpreamble(struct rum_softc *sc)
1682 struct ifnet *ifp = sc->sc_ifp;
1683 struct ieee80211com *ic = ifp->if_l2com;
1686 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1688 tmp &= ~RT2573_SHORT_PREAMBLE;
1689 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1690 tmp |= RT2573_SHORT_PREAMBLE;
1692 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1696 rum_set_basicrates(struct rum_softc *sc)
1698 struct ifnet *ifp = sc->sc_ifp;
1699 struct ieee80211com *ic = ifp->if_l2com;
1701 /* update basic rate set */
1702 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1703 /* 11b basic rates: 1, 2Mbps */
1704 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1705 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1706 /* 11a basic rates: 6, 12, 24Mbps */
1707 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1709 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1710 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1715 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1719 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1721 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1724 /* update all BBP registers that depend on the band */
1725 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1726 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1727 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1728 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1729 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1731 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1732 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1733 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1737 rum_bbp_write(sc, 17, bbp17);
1738 rum_bbp_write(sc, 96, bbp96);
1739 rum_bbp_write(sc, 104, bbp104);
1741 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1742 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1743 rum_bbp_write(sc, 75, 0x80);
1744 rum_bbp_write(sc, 86, 0x80);
1745 rum_bbp_write(sc, 88, 0x80);
1748 rum_bbp_write(sc, 35, bbp35);
1749 rum_bbp_write(sc, 97, bbp97);
1750 rum_bbp_write(sc, 98, bbp98);
1752 tmp = rum_read(sc, RT2573_PHY_CSR0);
1753 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1754 if (IEEE80211_IS_CHAN_2GHZ(c))
1755 tmp |= RT2573_PA_PE_2GHZ;
1757 tmp |= RT2573_PA_PE_5GHZ;
1758 rum_write(sc, RT2573_PHY_CSR0, tmp);
1762 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1764 struct ifnet *ifp = sc->sc_ifp;
1765 struct ieee80211com *ic = ifp->if_l2com;
1766 const struct rfprog *rfprog;
1767 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1771 chan = ieee80211_chan2ieee(ic, c);
1772 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1775 /* select the appropriate RF settings based on what EEPROM says */
1776 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1777 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1779 /* find the settings for this channel (we know it exists) */
1780 for (i = 0; rfprog[i].chan != chan; i++);
1782 power = sc->txpow[i];
1786 } else if (power > 31) {
1787 bbp94 += power - 31;
1792 * If we are switching from the 2GHz band to the 5GHz band or
1793 * vice-versa, BBP registers need to be reprogrammed.
1795 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1796 rum_select_band(sc, c);
1797 rum_select_antenna(sc);
1801 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1802 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1803 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1804 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1806 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1807 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1808 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1809 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1811 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1812 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1813 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1814 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1818 /* enable smart mode for MIMO-capable RFs */
1819 bbp3 = rum_bbp_read(sc, 3);
1821 bbp3 &= ~RT2573_SMART_MODE;
1822 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1823 bbp3 |= RT2573_SMART_MODE;
1825 rum_bbp_write(sc, 3, bbp3);
1827 if (bbp94 != RT2573_BBPR94_DEFAULT)
1828 rum_bbp_write(sc, 94, bbp94);
1832 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1833 * and HostAP operating modes.
1836 rum_enable_tsf_sync(struct rum_softc *sc)
1838 struct ifnet *ifp = sc->sc_ifp;
1839 struct ieee80211com *ic = ifp->if_l2com;
1840 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1843 if (vap->iv_opmode != IEEE80211_M_STA) {
1845 * Change default 16ms TBTT adjustment to 8ms.
1846 * Must be done before enabling beacon generation.
1848 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1851 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1853 /* set beacon interval (in 1/16ms unit) */
1854 tmp |= vap->iv_bss->ni_intval * 16;
1856 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1857 if (vap->iv_opmode == IEEE80211_M_STA)
1858 tmp |= RT2573_TSF_MODE(1);
1860 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1862 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1866 rum_update_slot(struct ifnet *ifp)
1868 struct rum_softc *sc = ifp->if_softc;
1869 struct ieee80211com *ic = ifp->if_l2com;
1873 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1875 tmp = rum_read(sc, RT2573_MAC_CSR9);
1876 tmp = (tmp & ~0xff) | slottime;
1877 rum_write(sc, RT2573_MAC_CSR9, tmp);
1879 DPRINTF(("setting slot time to %uus\n", slottime));
1883 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1887 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1888 rum_write(sc, RT2573_MAC_CSR4, tmp);
1890 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1891 rum_write(sc, RT2573_MAC_CSR5, tmp);
1895 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1899 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1900 rum_write(sc, RT2573_MAC_CSR2, tmp);
1902 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1903 rum_write(sc, RT2573_MAC_CSR3, tmp);
1907 rum_update_promisc(struct rum_softc *sc)
1909 struct ifnet *ifp = sc->sc_ifp;
1912 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1914 tmp &= ~RT2573_DROP_NOT_TO_ME;
1915 if (!(ifp->if_flags & IFF_PROMISC))
1916 tmp |= RT2573_DROP_NOT_TO_ME;
1918 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1920 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1921 "entering" : "leaving"));
1928 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1929 case RT2573_RF_2528: return "RT2528";
1930 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1931 case RT2573_RF_5226: return "RT5226";
1932 default: return "unknown";
1937 rum_read_eeprom(struct rum_softc *sc)
1939 struct ifnet *ifp = sc->sc_ifp;
1940 struct ieee80211com *ic = ifp->if_l2com;
1946 /* read MAC address */
1947 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1949 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1951 sc->rf_rev = (val >> 11) & 0x1f;
1952 sc->hw_radio = (val >> 10) & 0x1;
1953 sc->rx_ant = (val >> 4) & 0x3;
1954 sc->tx_ant = (val >> 2) & 0x3;
1955 sc->nb_ant = val & 0x3;
1957 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1959 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1961 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1962 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1964 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1965 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1967 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1969 if ((val & 0xff) != 0xff)
1970 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1972 /* Only [-10, 10] is valid */
1973 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1974 sc->rssi_2ghz_corr = 0;
1976 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1978 if ((val & 0xff) != 0xff)
1979 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1981 /* Only [-10, 10] is valid */
1982 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1983 sc->rssi_5ghz_corr = 0;
1985 if (sc->ext_2ghz_lna)
1986 sc->rssi_2ghz_corr -= 14;
1987 if (sc->ext_5ghz_lna)
1988 sc->rssi_5ghz_corr -= 14;
1990 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1991 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1993 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1995 if ((val & 0xff) != 0xff)
1996 sc->rffreq = val & 0xff;
1998 DPRINTF(("RF freq=%d\n", sc->rffreq));
2000 /* read Tx power for all a/b/g channels */
2001 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2002 /* XXX default Tx power for 802.11a channels */
2003 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2005 for (i = 0; i < 14; i++)
2006 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
2009 /* read default values for BBP registers */
2010 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2012 for (i = 0; i < 14; i++) {
2013 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2015 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2016 sc->bbp_prom[i].val));
2022 rum_bbp_init(struct rum_softc *sc)
2024 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2027 /* wait for BBP to be ready */
2028 for (ntries = 0; ntries < 100; ntries++) {
2029 const uint8_t val = rum_bbp_read(sc, 0);
2030 if (val != 0 && val != 0xff)
2034 if (ntries == 100) {
2035 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2039 /* initialize BBP registers to default values */
2040 for (i = 0; i < N(rum_def_bbp); i++)
2041 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2043 /* write vendor-specific BBP values (from EEPROM) */
2044 for (i = 0; i < 16; i++) {
2045 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2047 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2055 rum_init_locked(struct rum_softc *sc)
2057 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2058 struct ifnet *ifp = sc->sc_ifp;
2059 struct ieee80211com *ic = ifp->if_l2com;
2060 struct rum_rx_data *data;
2067 /* initialize MAC registers to default values */
2068 for (i = 0; i < N(rum_def_mac); i++)
2069 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2071 /* set host ready */
2072 rum_write(sc, RT2573_MAC_CSR1, 3);
2073 rum_write(sc, RT2573_MAC_CSR1, 0);
2075 /* wait for BBP/RF to wakeup */
2076 for (ntries = 0; ntries < 1000; ntries++) {
2077 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2079 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2082 if (ntries == 1000) {
2083 device_printf(sc->sc_dev,
2084 "timeout waiting for BBP/RF to wakeup\n");
2088 if ((error = rum_bbp_init(sc)) != 0)
2091 /* select default channel */
2092 rum_select_band(sc, ic->ic_curchan);
2093 rum_select_antenna(sc);
2094 rum_set_chan(sc, ic->ic_curchan);
2096 /* clear STA registers */
2097 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2099 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2100 rum_set_macaddr(sc, ic->ic_myaddr);
2102 /* initialize ASIC */
2103 rum_write(sc, RT2573_MAC_CSR1, 4);
2106 * Allocate xfer for AMRR statistics requests.
2108 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2109 if (sc->amrr_xfer == NULL) {
2110 device_printf(sc->sc_dev, "could not allocate AMRR xfer\n");
2115 * Open Tx and Rx USB bulk pipes.
2117 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2120 device_printf(sc->sc_dev, "could not open Tx pipe: %s\n",
2121 usbd_errstr(error));
2124 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2127 device_printf(sc->sc_dev, "could not open Rx pipe: %s\n",
2128 usbd_errstr(error));
2133 * Allocate Tx and Rx xfer queues.
2135 error = rum_alloc_tx_list(sc);
2137 device_printf(sc->sc_dev, "could not allocate Tx list\n");
2140 error = rum_alloc_rx_list(sc);
2142 device_printf(sc->sc_dev, "could not allocate Rx list\n");
2147 * Start up the receive pipe.
2149 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2150 data = &sc->rx_data[i];
2152 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2153 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2154 usbd_transfer(data->xfer);
2157 /* update Rx filter */
2158 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2160 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2161 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2162 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2164 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2165 tmp |= RT2573_DROP_TODS;
2166 if (!(ifp->if_flags & IFF_PROMISC))
2167 tmp |= RT2573_DROP_NOT_TO_ME;
2169 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2171 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2172 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2180 rum_init(void *priv)
2182 struct rum_softc *sc = priv;
2183 struct ifnet *ifp = sc->sc_ifp;
2184 struct ieee80211com *ic = ifp->if_l2com;
2187 rum_init_locked(sc);
2190 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2191 ieee80211_start_all(ic); /* start all vap's */
2195 rum_stop(void *priv)
2197 struct rum_softc *sc = priv;
2198 struct ifnet *ifp = sc->sc_ifp;
2201 sc->sc_tx_timer = 0;
2202 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2205 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2206 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2209 rum_write(sc, RT2573_MAC_CSR1, 3);
2210 rum_write(sc, RT2573_MAC_CSR1, 0);
2212 if (sc->amrr_xfer != NULL) {
2213 usbd_free_xfer(sc->amrr_xfer);
2214 sc->amrr_xfer = NULL;
2217 if (sc->sc_rx_pipeh != NULL) {
2218 usbd_abort_pipe(sc->sc_rx_pipeh);
2219 usbd_close_pipe(sc->sc_rx_pipeh);
2220 sc->sc_rx_pipeh = NULL;
2222 if (sc->sc_tx_pipeh != NULL) {
2223 usbd_abort_pipe(sc->sc_tx_pipeh);
2224 usbd_close_pipe(sc->sc_tx_pipeh);
2225 sc->sc_tx_pipeh = NULL;
2228 rum_free_rx_list(sc);
2229 rum_free_tx_list(sc);
2233 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2235 usb_device_request_t req;
2236 uint16_t reg = RT2573_MCU_CODE_BASE;
2239 /* copy firmware image into NIC */
2240 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2241 rum_write(sc, reg, UGETDW(ucode));
2243 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2244 req.bRequest = RT2573_MCU_CNTL;
2245 USETW(req.wValue, RT2573_MCU_RUN);
2246 USETW(req.wIndex, 0);
2247 USETW(req.wLength, 0);
2249 error = usbd_do_request(sc->sc_udev, &req, NULL);
2251 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2252 usbd_errstr(error));
2258 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2260 struct ieee80211com *ic = vap->iv_ic;
2261 const struct ieee80211_txparam *tp;
2262 struct rum_tx_desc desc;
2265 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2270 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2271 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2272 m0->m_pkthdr.len, tp->mgmtrate);
2274 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2275 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2277 /* copy beacon header and payload into NIC memory */
2278 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2287 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2288 const struct ieee80211_bpf_params *params)
2290 struct ifnet *ifp = ni->ni_ic->ic_ifp;
2291 struct rum_softc *sc = ifp->if_softc;
2293 /* prevent management frames from being sent if we're not ready */
2294 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2296 ieee80211_free_node(ni);
2299 if (sc->tx_queued >= RUM_TX_LIST_COUNT-1) {
2300 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2302 ieee80211_free_node(ni);
2308 if (params == NULL) {
2310 * Legacy path; interpret frame contents to decide
2311 * precisely how to send the frame.
2313 if (rum_tx_mgt(sc, m, ni) != 0)
2317 * Caller supplied explicit parameters to use in
2318 * sending the frame.
2320 if (rum_tx_raw(sc, m, ni, params) != 0)
2323 sc->sc_tx_timer = 5;
2324 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
2329 ieee80211_free_node(ni);
2334 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2336 struct ieee80211vap *vap = ni->ni_vap;
2337 struct rum_vap *rvp = RUM_VAP(vap);
2339 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2340 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2342 ieee80211_amrr_node_init(&rvp->amrr, &RUM_NODE(ni)->amn, ni);
2344 callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, vap);
2348 rum_amrr_timeout(void *arg)
2350 struct ieee80211vap *vap = arg;
2351 struct rum_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2352 usb_device_request_t req;
2355 * Asynchronously read statistic registers (cleared by read).
2357 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2358 req.bRequest = RT2573_READ_MULTI_MAC;
2359 USETW(req.wValue, 0);
2360 USETW(req.wIndex, RT2573_STA_CSR0);
2361 USETW(req.wLength, sizeof sc->sta);
2363 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, vap,
2364 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2366 (void)usbd_transfer(sc->amrr_xfer);
2370 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2373 struct ieee80211vap *vap = priv;
2374 struct rum_vap *rvp = RUM_VAP(vap);
2375 struct ifnet *ifp = vap->iv_ic->ic_ifp;
2376 struct rum_softc *sc = ifp->if_softc;
2379 if (status != USBD_NORMAL_COMPLETION) {
2380 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2381 "cancelling automatic rate control\n");
2385 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */
2386 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */
2387 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2389 ieee80211_amrr_tx_update(&RUM_NODE(vap->iv_bss)->amn,
2390 ok+fail, ok, (le32toh(sc->sta[5]) & 0xffff) + fail);
2392 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2394 callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, vap);
2398 static struct ieee80211_node *
2399 rum_node_alloc(struct ieee80211vap *vap __unused,
2400 const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
2402 struct rum_node *rn;
2404 rn = malloc(sizeof(struct rum_node), M_80211_NODE, M_NOWAIT | M_ZERO);
2405 return rn != NULL ? &rn->ni : NULL;
2409 rum_newassoc(struct ieee80211_node *ni, int isnew)
2411 struct ieee80211vap *vap = ni->ni_vap;
2413 ieee80211_amrr_node_init(&RUM_VAP(vap)->amrr, &RUM_NODE(ni)->amn, ni);
2417 rum_scan_start(struct ieee80211com *ic)
2419 struct rum_softc *sc = ic->ic_ifp->if_softc;
2421 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2423 /* do it in a process context */
2424 sc->sc_scan_action = RUM_SCAN_START;
2425 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2429 rum_scan_end(struct ieee80211com *ic)
2431 struct rum_softc *sc = ic->ic_ifp->if_softc;
2433 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2435 /* do it in a process context */
2436 sc->sc_scan_action = RUM_SCAN_END;
2437 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2441 rum_set_channel(struct ieee80211com *ic)
2443 struct rum_softc *sc = ic->ic_ifp->if_softc;
2445 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2447 /* do it in a process context */
2448 sc->sc_scan_action = RUM_SET_CHANNEL;
2449 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2451 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
2455 rum_scantask(void *arg)
2457 struct rum_softc *sc = arg;
2458 struct ifnet *ifp = sc->sc_ifp;
2459 struct ieee80211com *ic = ifp->if_l2com;
2460 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2465 switch (sc->sc_scan_action) {
2466 case RUM_SCAN_START:
2467 /* abort TSF synchronization */
2468 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2469 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2470 rum_set_bssid(sc, ifp->if_broadcastaddr);
2474 rum_enable_tsf_sync(sc);
2475 /* XXX keep local copy */
2476 rum_set_bssid(sc, vap->iv_bss->ni_bssid);
2479 case RUM_SET_CHANNEL:
2481 rum_set_chan(sc, ic->ic_curchan);
2486 panic("unknown scan action %d\n", sc->sc_scan_action);
2495 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2497 struct ifnet *ifp = sc->sc_ifp;
2498 struct ieee80211com *ic = ifp->if_l2com;
2501 lna = (raw >> 5) & 0x3;
2508 * NB: Since RSSI is relative to noise floor, -1 is
2509 * adequate for caller to know error happened.
2514 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2516 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2517 rssi += sc->rssi_2ghz_corr;
2526 rssi += sc->rssi_5ghz_corr;
2528 if (!sc->ext_5ghz_lna && lna != 1)
2541 static device_method_t rum_methods[] = {
2542 /* Device interface */
2543 DEVMETHOD(device_probe, rum_match),
2544 DEVMETHOD(device_attach, rum_attach),
2545 DEVMETHOD(device_detach, rum_detach),
2550 static driver_t rum_driver = {
2553 sizeof(struct rum_softc)
2556 static devclass_t rum_devclass;
2558 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);