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_radiotap.h>
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/if_rumreg.h>
62 #include <dev/usb/if_rumvar.h>
63 #include <dev/usb/rt2573_ucode.h>
66 #define DPRINTF(x) do { if (rumdebug > 0) logprintf x; } while (0)
67 #define DPRINTFN(n, x) do { if (rumdebug >= (n)) logprintf x; } while (0)
69 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
70 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rumdebug, 0,
74 #define DPRINTFN(n, x)
77 /* various supported device vendors/products */
78 static const struct usb_devno rum_devs[] = {
79 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
80 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
81 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 },
82 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 },
83 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
84 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
85 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_1 },
86 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_2 },
87 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
88 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
89 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
90 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
91 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
92 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
93 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
94 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
95 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
96 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
97 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
98 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
99 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
100 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
101 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
102 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_WUB320G },
103 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
104 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
105 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1 },
106 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
107 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
108 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
109 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
110 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
111 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
112 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
113 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
114 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
115 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
116 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2 },
117 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
118 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
119 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
120 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
123 MODULE_DEPEND(rum, wlan, 1, 1, 1);
124 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
126 static int rum_alloc_tx_list(struct rum_softc *);
127 static void rum_free_tx_list(struct rum_softc *);
128 static int rum_alloc_rx_list(struct rum_softc *);
129 static void rum_free_rx_list(struct rum_softc *);
130 static int rum_media_change(struct ifnet *);
131 static void rum_next_scan(void *);
132 static void rum_task(void *);
133 static int rum_newstate(struct ieee80211com *,
134 enum ieee80211_state, int);
135 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
137 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
139 static int rum_rxrate(struct rum_rx_desc *);
140 static int rum_ack_rate(struct ieee80211com *, int);
141 static uint16_t rum_txtime(int, int, uint32_t);
142 static uint8_t rum_plcp_signal(int);
143 static void rum_setup_tx_desc(struct rum_softc *,
144 struct rum_tx_desc *, uint32_t, uint16_t, int,
146 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
147 struct ieee80211_node *);
148 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
149 struct ieee80211_node *,
150 const struct ieee80211_bpf_params *);
151 static int rum_tx_data(struct rum_softc *, struct mbuf *,
152 struct ieee80211_node *);
153 static void rum_start(struct ifnet *);
154 static void rum_watchdog(void *);
155 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
156 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
158 static uint32_t rum_read(struct rum_softc *, uint16_t);
159 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
161 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
162 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
164 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
165 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
166 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
167 static void rum_select_antenna(struct rum_softc *);
168 static void rum_enable_mrr(struct rum_softc *);
169 static void rum_set_txpreamble(struct rum_softc *);
170 static void rum_set_basicrates(struct rum_softc *);
171 static void rum_select_band(struct rum_softc *,
172 struct ieee80211_channel *);
173 static void rum_set_chan(struct rum_softc *,
174 struct ieee80211_channel *);
175 static void rum_enable_tsf_sync(struct rum_softc *);
176 static void rum_update_slot(struct ifnet *);
177 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
178 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
179 static void rum_update_promisc(struct rum_softc *);
180 static const char *rum_get_rf(int);
181 static void rum_read_eeprom(struct rum_softc *);
182 static int rum_bbp_init(struct rum_softc *);
183 static void rum_init(void *);
184 static void rum_stop(void *);
185 static int rum_load_microcode(struct rum_softc *, const u_char *,
187 static int rum_prepare_beacon(struct rum_softc *);
188 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
189 const struct ieee80211_bpf_params *);
190 static void rum_amrr_start(struct rum_softc *,
191 struct ieee80211_node *);
192 static void rum_amrr_timeout(void *);
193 static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
196 static const struct {
200 { RT2573_TXRX_CSR0, 0x025fb032 },
201 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
202 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
203 { RT2573_TXRX_CSR3, 0x00858687 },
204 { RT2573_TXRX_CSR7, 0x2e31353b },
205 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
206 { RT2573_TXRX_CSR15, 0x0000000f },
207 { RT2573_MAC_CSR6, 0x00000fff },
208 { RT2573_MAC_CSR8, 0x016c030a },
209 { RT2573_MAC_CSR10, 0x00000718 },
210 { RT2573_MAC_CSR12, 0x00000004 },
211 { RT2573_MAC_CSR13, 0x00007f00 },
212 { RT2573_SEC_CSR0, 0x00000000 },
213 { RT2573_SEC_CSR1, 0x00000000 },
214 { RT2573_SEC_CSR5, 0x00000000 },
215 { RT2573_PHY_CSR1, 0x000023b0 },
216 { RT2573_PHY_CSR5, 0x00040a06 },
217 { RT2573_PHY_CSR6, 0x00080606 },
218 { RT2573_PHY_CSR7, 0x00000408 },
219 { RT2573_AIFSN_CSR, 0x00002273 },
220 { RT2573_CWMIN_CSR, 0x00002344 },
221 { RT2573_CWMAX_CSR, 0x000034aa }
224 static const struct {
256 static const struct rfprog {
258 uint32_t r1, r2, r3, r4;
260 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
261 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
262 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
263 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
264 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
265 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
266 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
267 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
268 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
269 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
270 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
271 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
272 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
273 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
275 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
276 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
277 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
278 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
280 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
281 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
282 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
283 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
284 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
285 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
286 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
287 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
289 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
290 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
291 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
292 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
293 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
294 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
295 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
296 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
297 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
298 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
299 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
301 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
302 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
303 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
304 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
305 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
307 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
308 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
309 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
310 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
311 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
312 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
313 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
314 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
315 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
316 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
317 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
318 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
319 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
320 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
322 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
323 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
324 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
325 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
327 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
328 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
329 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
330 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
331 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
332 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
333 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
334 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
336 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
337 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
338 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
339 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
340 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
341 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
342 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
343 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
344 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
345 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
346 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
348 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
349 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
350 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
351 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
352 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
355 USB_DECLARE_DRIVER(rum);
359 USB_MATCH_START(rum, uaa);
361 if (uaa->iface != NULL)
364 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
365 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
370 USB_ATTACH_START(rum, sc, uaa);
371 struct ieee80211com *ic = &sc->sc_ic;
373 const uint8_t *ucode = NULL;
374 usb_interface_descriptor_t *id;
375 usb_endpoint_descriptor_t *ed;
381 sc->sc_udev = uaa->device;
383 usbd_devinfo(sc->sc_udev, 0, devinfo);
386 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
387 printf("%s: could not set configuration no\n",
388 device_get_nameunit(sc->sc_dev));
389 USB_ATTACH_ERROR_RETURN;
392 /* get the first interface handle */
393 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
396 printf("%s: could not get interface handle\n",
397 device_get_nameunit(sc->sc_dev));
398 USB_ATTACH_ERROR_RETURN;
404 id = usbd_get_interface_descriptor(sc->sc_iface);
406 sc->sc_rx_no = sc->sc_tx_no = -1;
407 for (i = 0; i < id->bNumEndpoints; i++) {
408 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
410 printf("%s: no endpoint descriptor for iface %d\n",
411 device_get_nameunit(sc->sc_dev), i);
412 USB_ATTACH_ERROR_RETURN;
415 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
416 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
417 sc->sc_rx_no = ed->bEndpointAddress;
418 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
419 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
420 sc->sc_tx_no = ed->bEndpointAddress;
422 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
423 printf("%s: missing endpoint\n",
424 device_get_nameunit(sc->sc_dev));
425 USB_ATTACH_ERROR_RETURN;
428 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
429 MTX_DEF | MTX_RECURSE);
431 usb_init_task(&sc->sc_task, rum_task, sc);
432 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
433 callout_init(&sc->scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
435 callout_init(&sc->amrr_ch, 0);
437 /* retrieve RT2573 rev. no */
438 for (ntries = 0; ntries < 1000; ntries++) {
439 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
443 if (ntries == 1000) {
444 printf("%s: timeout waiting for chip to settle\n",
445 device_get_nameunit(sc->sc_dev));
446 USB_ATTACH_ERROR_RETURN;
449 /* retrieve MAC address and various other things from EEPROM */
452 printf("%s: MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
453 device_get_nameunit(sc->sc_dev), tmp, rum_get_rf(sc->rf_rev));
455 ucode = rt2573_ucode;
456 size = sizeof rt2573_ucode;
457 error = rum_load_microcode(sc, ucode, size);
459 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
460 mtx_destroy(&sc->sc_mtx);
461 USB_ATTACH_ERROR_RETURN;
464 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
466 printf("%s: can not if_alloc()\n",
467 device_get_nameunit(sc->sc_dev));
468 mtx_destroy(&sc->sc_mtx);
469 USB_ATTACH_ERROR_RETURN;
473 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
474 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
475 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
476 ifp->if_init = rum_init;
477 ifp->if_ioctl = rum_ioctl;
478 ifp->if_start = rum_start;
479 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
480 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
481 IFQ_SET_READY(&ifp->if_snd);
484 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
485 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
486 ic->ic_state = IEEE80211_S_INIT;
488 /* set device capabilities */
490 IEEE80211_C_IBSS | /* IBSS mode supported */
491 IEEE80211_C_MONITOR | /* monitor mode supported */
492 IEEE80211_C_HOSTAP | /* HostAp mode supported */
493 IEEE80211_C_TXPMGT | /* tx power management */
494 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
495 IEEE80211_C_SHSLOT | /* short slot time supported */
496 IEEE80211_C_WPA; /* 802.11i */
498 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
499 /* set supported .11a channels */
500 for (i = 34; i <= 46; i += 4) {
501 ic->ic_channels[i].ic_freq =
502 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
503 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
505 for (i = 36; i <= 64; i += 4) {
506 ic->ic_channels[i].ic_freq =
507 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
508 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
510 for (i = 100; i <= 140; i += 4) {
511 ic->ic_channels[i].ic_freq =
512 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
513 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
515 for (i = 149; i <= 165; i += 4) {
516 ic->ic_channels[i].ic_freq =
517 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
518 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
522 /* set supported .11b and .11g channels (1 through 14) */
523 for (i = 1; i <= 14; i++) {
524 ic->ic_channels[i].ic_freq =
525 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
526 ic->ic_channels[i].ic_flags =
527 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
528 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
531 ieee80211_ifattach(ic);
532 /* enable s/w bmiss handling in sta mode */
533 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
535 /* override state transition machine */
536 sc->sc_newstate = ic->ic_newstate;
537 ic->ic_newstate = rum_newstate;
538 ic->ic_raw_xmit = rum_raw_xmit;
539 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
541 ieee80211_amrr_init(&sc->amrr, ic, 1, 10);
543 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
544 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
547 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
548 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
549 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
551 sc->sc_txtap_len = sizeof sc->sc_txtapu;
552 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
553 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
556 ieee80211_announce(ic);
558 USB_ATTACH_SUCCESS_RETURN;
563 USB_DETACH_START(rum, sc);
564 struct ieee80211com *ic = &sc->sc_ic;
565 struct ifnet *ifp = ic->ic_ifp;
568 usb_rem_task(sc->sc_udev, &sc->sc_task);
569 callout_stop(&sc->watchdog_ch);
570 callout_stop(&sc->scan_ch);
571 callout_stop(&sc->amrr_ch);
573 if (sc->amrr_xfer != NULL) {
574 usbd_free_xfer(sc->amrr_xfer);
575 sc->amrr_xfer = NULL;
578 if (sc->sc_rx_pipeh != NULL) {
579 usbd_abort_pipe(sc->sc_rx_pipeh);
580 usbd_close_pipe(sc->sc_rx_pipeh);
582 if (sc->sc_tx_pipeh != NULL) {
583 usbd_abort_pipe(sc->sc_tx_pipeh);
584 usbd_close_pipe(sc->sc_tx_pipeh);
587 rum_free_rx_list(sc);
588 rum_free_tx_list(sc);
591 ieee80211_ifdetach(ic);
594 mtx_destroy(&sc->sc_mtx);
600 rum_alloc_tx_list(struct rum_softc *sc)
602 struct rum_tx_data *data;
607 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
608 data = &sc->tx_data[i];
612 data->xfer = usbd_alloc_xfer(sc->sc_udev);
613 if (data->xfer == NULL) {
614 printf("%s: could not allocate tx xfer\n",
615 device_get_nameunit(sc->sc_dev));
619 data->buf = usbd_alloc_buffer(data->xfer,
620 RT2573_TX_DESC_SIZE + MCLBYTES);
621 if (data->buf == NULL) {
622 printf("%s: could not allocate tx buffer\n",
623 device_get_nameunit(sc->sc_dev));
627 /* clean Tx descriptor */
628 bzero(data->buf, RT2573_TX_DESC_SIZE);
633 fail: rum_free_tx_list(sc);
638 rum_free_tx_list(struct rum_softc *sc)
640 struct rum_tx_data *data;
643 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
644 data = &sc->tx_data[i];
646 if (data->xfer != NULL) {
647 usbd_free_xfer(data->xfer);
651 if (data->ni != NULL) {
652 ieee80211_free_node(data->ni);
659 rum_alloc_rx_list(struct rum_softc *sc)
661 struct rum_rx_data *data;
664 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
665 data = &sc->rx_data[i];
669 data->xfer = usbd_alloc_xfer(sc->sc_udev);
670 if (data->xfer == NULL) {
671 printf("%s: could not allocate rx xfer\n",
672 device_get_nameunit(sc->sc_dev));
676 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
677 printf("%s: could not allocate rx buffer\n",
678 device_get_nameunit(sc->sc_dev));
683 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
684 if (data->m == NULL) {
685 printf("%s: could not allocate rx mbuf\n",
686 device_get_nameunit(sc->sc_dev));
691 data->buf = mtod(data->m, uint8_t *);
696 fail: rum_free_tx_list(sc);
701 rum_free_rx_list(struct rum_softc *sc)
703 struct rum_rx_data *data;
706 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
707 data = &sc->rx_data[i];
709 if (data->xfer != NULL) {
710 usbd_free_xfer(data->xfer);
713 if (data->m != NULL) {
721 rum_media_change(struct ifnet *ifp)
723 struct rum_softc *sc = ifp->if_softc;
728 error = ieee80211_media_change(ifp);
729 if (error != ENETRESET) {
734 if ((ifp->if_flags & IFF_UP) &&
735 (ifp->if_drv_flags & IFF_DRV_RUNNING))
744 * This function is called periodically (every 200ms) during scanning to
745 * switch from one channel to another.
748 rum_next_scan(void *arg)
750 struct rum_softc *sc = arg;
751 struct ieee80211com *ic = &sc->sc_ic;
753 if (ic->ic_state == IEEE80211_S_SCAN)
754 ieee80211_next_scan(ic);
760 struct rum_softc *sc = arg;
761 struct ieee80211com *ic = &sc->sc_ic;
762 enum ieee80211_state ostate;
763 struct ieee80211_node *ni;
766 ostate = ic->ic_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_SCAN:
780 rum_set_chan(sc, ic->ic_curchan);
781 callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
784 case IEEE80211_S_AUTH:
785 rum_set_chan(sc, ic->ic_curchan);
788 case IEEE80211_S_ASSOC:
789 rum_set_chan(sc, ic->ic_curchan);
792 case IEEE80211_S_RUN:
793 rum_set_chan(sc, ic->ic_curchan);
797 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
798 rum_update_slot(ic->ic_ifp);
800 rum_set_txpreamble(sc);
801 rum_set_basicrates(sc);
802 rum_set_bssid(sc, ni->ni_bssid);
805 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
806 ic->ic_opmode == IEEE80211_M_IBSS)
807 rum_prepare_beacon(sc);
809 if (ic->ic_opmode != IEEE80211_M_MONITOR)
810 rum_enable_tsf_sync(sc);
812 /* enable automatic rate adaptation in STA mode */
813 if (ic->ic_opmode == IEEE80211_M_STA &&
814 ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
815 rum_amrr_start(sc, ni);
821 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
825 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
827 struct rum_softc *sc = ic->ic_ifp->if_softc;
829 callout_stop(&sc->scan_ch);
830 callout_stop(&sc->amrr_ch);
832 /* do it in a process context */
833 sc->sc_state = nstate;
836 usb_rem_task(sc->sc_udev, &sc->sc_task);
837 if (nstate == IEEE80211_S_INIT)
838 sc->sc_newstate(ic, nstate, arg);
840 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
844 /* quickly determine if a given rate is CCK or OFDM */
845 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
847 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
848 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
851 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
853 struct rum_tx_data *data = priv;
854 struct rum_softc *sc = data->sc;
855 struct ifnet *ifp = sc->sc_ic.ic_ifp;
857 if (status != USBD_NORMAL_COMPLETION) {
858 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
861 printf("%s: could not transmit buffer: %s\n",
862 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
864 if (status == USBD_STALLED)
865 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
873 ieee80211_free_node(data->ni);
879 DPRINTFN(10, ("tx done\n"));
882 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
887 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
889 struct rum_rx_data *data = priv;
890 struct rum_softc *sc = data->sc;
891 struct ieee80211com *ic = &sc->sc_ic;
892 struct ifnet *ifp = ic->ic_ifp;
893 struct rum_rx_desc *desc;
894 struct ieee80211_frame *wh;
895 struct ieee80211_node *ni;
896 struct mbuf *mnew, *m;
899 if (status != USBD_NORMAL_COMPLETION) {
900 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
903 if (status == USBD_STALLED)
904 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
908 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
910 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
911 DPRINTF(("%s: xfer too short %d\n",
912 device_get_nameunit(sc->sc_dev), len));
917 desc = (struct rum_rx_desc *)data->buf;
919 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
921 * This should not happen since we did not request to receive
922 * those frames when we filled RT2573_TXRX_CSR0.
924 DPRINTFN(5, ("CRC error\n"));
929 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
937 data->buf = mtod(data->m, uint8_t *);
940 m->m_pkthdr.rcvif = ifp;
941 m->m_data = (caddr_t)(desc + 1);
942 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
944 if (bpf_peers_present(sc->sc_drvbpf)) {
945 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
947 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
948 tap->wr_rate = rum_rxrate(desc);
949 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
950 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
951 tap->wr_antenna = sc->rx_ant;
952 tap->wr_antsignal = desc->rssi;
954 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
957 wh = mtod(m, struct ieee80211_frame *);
958 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
960 /* send the frame to the 802.11 layer */
961 ieee80211_input(ic, m, ni, desc->rssi, 0);
963 /* node is no longer needed */
964 ieee80211_free_node(ni);
966 DPRINTFN(15, ("rx done\n"));
968 skip: /* setup a new transfer */
969 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
970 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
975 * This function is only used by the Rx radiotap code.
978 rum_rxrate(struct rum_rx_desc *desc)
980 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
981 /* reverse function of rum_plcp_signal */
982 switch (desc->rate) {
990 case 0xc: return 108;
993 if (desc->rate == 10)
995 if (desc->rate == 20)
997 if (desc->rate == 55)
999 if (desc->rate == 110)
1002 return 2; /* should not get there */
1006 * Return the expected ack rate for a frame transmitted at rate `rate'.
1009 rum_ack_rate(struct ieee80211com *ic, int rate)
1018 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1034 /* default to 1Mbps */
1039 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1040 * The function automatically determines the operating mode depending on the
1041 * given rate. `flags' indicates whether short preamble is in use or not.
1044 rum_txtime(int len, int rate, uint32_t flags)
1048 if (RUM_RATE_IS_OFDM(rate)) {
1049 /* IEEE Std 802.11a-1999, pp. 37 */
1050 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1051 txtime = 16 + 4 + 4 * txtime + 6;
1053 /* IEEE Std 802.11b-1999, pp. 28 */
1054 txtime = (16 * len + rate - 1) / rate;
1055 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1064 rum_plcp_signal(int rate)
1067 /* CCK rates (returned values are device-dependent) */
1070 case 11: return 0x2;
1071 case 22: return 0x3;
1073 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1074 case 12: return 0xb;
1075 case 18: return 0xf;
1076 case 24: return 0xa;
1077 case 36: return 0xe;
1078 case 48: return 0x9;
1079 case 72: return 0xd;
1080 case 96: return 0x8;
1081 case 108: return 0xc;
1083 /* unsupported rates (should not get there) */
1084 default: return 0xff;
1089 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1090 uint32_t flags, uint16_t xflags, int len, int rate)
1092 struct ieee80211com *ic = &sc->sc_ic;
1093 uint16_t plcp_length;
1096 desc->flags = htole32(flags);
1097 desc->flags |= htole32(RT2573_TX_VALID);
1098 desc->flags |= htole32(len << 16);
1100 desc->xflags = htole16(xflags);
1102 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1103 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1105 /* setup PLCP fields */
1106 desc->plcp_signal = rum_plcp_signal(rate);
1107 desc->plcp_service = 4;
1109 len += IEEE80211_CRC_LEN;
1110 if (RUM_RATE_IS_OFDM(rate)) {
1111 desc->flags |= htole32(RT2573_TX_OFDM);
1113 plcp_length = len & 0xfff;
1114 desc->plcp_length_hi = plcp_length >> 6;
1115 desc->plcp_length_lo = plcp_length & 0x3f;
1117 plcp_length = (16 * len + rate - 1) / rate;
1119 remainder = (16 * len) % 22;
1120 if (remainder != 0 && remainder < 7)
1121 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1123 desc->plcp_length_hi = plcp_length >> 8;
1124 desc->plcp_length_lo = plcp_length & 0xff;
1126 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1127 desc->plcp_signal |= 0x08;
1131 #define RUM_TX_TIMEOUT 5000
1134 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1136 struct ieee80211com *ic = &sc->sc_ic;
1137 struct rum_tx_desc *desc;
1138 struct rum_tx_data *data;
1139 struct ieee80211_frame *wh;
1145 data = &sc->tx_data[0];
1146 desc = (struct rum_tx_desc *)data->buf;
1148 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1153 wh = mtod(m0, struct ieee80211_frame *);
1155 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1156 flags |= RT2573_TX_NEED_ACK;
1158 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1159 ic->ic_flags) + sc->sifs;
1160 *(uint16_t *)wh->i_dur = htole16(dur);
1162 /* tell hardware to add timestamp for probe responses */
1164 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1165 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1166 flags |= RT2573_TX_TIMESTAMP;
1169 if (bpf_peers_present(sc->sc_drvbpf)) {
1170 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1173 tap->wt_rate = rate;
1174 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1175 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1176 tap->wt_antenna = sc->tx_ant;
1178 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1181 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1182 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1184 /* align end on a 4-bytes boundary */
1185 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1188 * No space left in the last URB to store the extra 4 bytes, force
1189 * sending of another URB.
1191 if ((xferlen % 64) == 0)
1194 DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
1195 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1197 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1198 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1200 error = usbd_transfer(data->xfer);
1201 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1214 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1215 const struct ieee80211_bpf_params *params)
1217 struct ieee80211com *ic = &sc->sc_ic;
1218 struct rum_tx_desc *desc;
1219 struct rum_tx_data *data;
1224 data = &sc->tx_data[0];
1225 desc = (struct rum_tx_desc *)data->buf;
1227 rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1234 if (bpf_peers_present(sc->sc_drvbpf)) {
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(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1250 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1251 flags |= RT2573_TX_NEED_ACK;
1253 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1254 /* XXX need to setup descriptor ourself */
1255 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1257 /* align end on a 4-bytes boundary */
1258 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1261 * No space left in the last URB to store the extra 4 bytes, force
1262 * sending of another URB.
1264 if ((xferlen % 64) == 0)
1267 DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
1268 m0->m_pkthdr.len, rate, xferlen));
1270 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1271 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
1274 error = usbd_transfer(data->xfer);
1275 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1284 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1286 struct ieee80211com *ic = &sc->sc_ic;
1287 struct rum_tx_desc *desc;
1288 struct rum_tx_data *data;
1289 struct ieee80211_frame *wh;
1290 struct ieee80211_key *k;
1296 wh = mtod(m0, struct ieee80211_frame *);
1298 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1299 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
1301 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1303 rate &= IEEE80211_RATE_VAL;
1305 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1306 k = ieee80211_crypto_encap(ic, ni, m0);
1312 /* packet header may have moved, reset our local pointer */
1313 wh = mtod(m0, struct ieee80211_frame *);
1316 data = &sc->tx_data[0];
1317 desc = (struct rum_tx_desc *)data->buf;
1322 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1323 flags |= RT2573_TX_NEED_ACK;
1324 flags |= RT2573_TX_MORE_FRAG;
1326 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1327 ic->ic_flags) + sc->sifs;
1328 *(uint16_t *)wh->i_dur = htole16(dur);
1331 if (bpf_peers_present(sc->sc_drvbpf)) {
1332 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1335 tap->wt_rate = rate;
1336 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1337 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1338 tap->wt_antenna = sc->tx_ant;
1340 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1343 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1344 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1346 /* align end on a 4-bytes boundary */
1347 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1350 * No space left in the last URB to store the extra 4 bytes, force
1351 * sending of another URB.
1353 if ((xferlen % 64) == 0)
1356 DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
1357 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1359 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1360 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1362 error = usbd_transfer(data->xfer);
1363 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1376 rum_start(struct ifnet *ifp)
1378 struct rum_softc *sc = ifp->if_softc;
1379 struct ieee80211com *ic = &sc->sc_ic;
1380 struct ieee80211_node *ni;
1382 struct ether_header *eh;
1387 IF_POLL(&ic->ic_mgtq, m0);
1389 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1390 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1393 IF_DEQUEUE(&ic->ic_mgtq, m0);
1395 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1396 m0->m_pkthdr.rcvif = NULL;
1398 if (bpf_peers_present(ic->ic_rawbpf))
1399 bpf_mtap(ic->ic_rawbpf, m0);
1401 if (rum_tx_mgt(sc, m0, ni) != 0) {
1402 ieee80211_free_node(ni);
1406 if (ic->ic_state != IEEE80211_S_RUN)
1408 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1411 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1412 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1413 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1417 if (m0->m_len < sizeof (struct ether_header) &&
1418 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1421 eh = mtod(m0, struct ether_header *);
1422 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1429 m0 = ieee80211_encap(ic, m0, ni);
1431 ieee80211_free_node(ni);
1435 if (bpf_peers_present(ic->ic_rawbpf))
1436 bpf_mtap(ic->ic_rawbpf, m0);
1438 if (rum_tx_data(sc, m0, ni) != 0) {
1439 ieee80211_free_node(ni);
1445 sc->sc_tx_timer = 5;
1446 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1453 rum_watchdog(void *arg)
1455 struct rum_softc *sc = (struct rum_softc *)arg;
1456 struct ieee80211com *ic = &sc->sc_ic;
1458 if (sc->sc_tx_timer > 0) {
1459 if (--sc->sc_tx_timer == 0) {
1460 device_printf(sc->sc_dev, "device timeout\n");
1461 /*rum_init(ifp); XXX needs a process context! */
1462 sc->sc_ifp->if_oerrors++;
1465 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1468 ieee80211_watchdog(ic);
1472 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1474 struct rum_softc *sc = ifp->if_softc;
1475 struct ieee80211com *ic = &sc->sc_ic;
1482 if (ifp->if_flags & IFF_UP) {
1483 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1484 rum_update_promisc(sc);
1488 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1493 error = ieee80211_ioctl(ic, cmd, data);
1496 if (error == ENETRESET) {
1497 if ((ifp->if_flags & IFF_UP) &&
1498 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1499 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1510 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1512 usb_device_request_t req;
1515 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1516 req.bRequest = RT2573_READ_EEPROM;
1517 USETW(req.wValue, 0);
1518 USETW(req.wIndex, addr);
1519 USETW(req.wLength, len);
1521 error = usbd_do_request(sc->sc_udev, &req, buf);
1523 printf("%s: could not read EEPROM: %s\n",
1524 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1529 rum_read(struct rum_softc *sc, uint16_t reg)
1533 rum_read_multi(sc, reg, &val, sizeof val);
1535 return le32toh(val);
1539 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1541 usb_device_request_t req;
1544 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1545 req.bRequest = RT2573_READ_MULTI_MAC;
1546 USETW(req.wValue, 0);
1547 USETW(req.wIndex, reg);
1548 USETW(req.wLength, len);
1550 error = usbd_do_request(sc->sc_udev, &req, buf);
1552 printf("%s: could not multi read MAC register: %s\n",
1553 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1558 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1560 uint32_t tmp = htole32(val);
1562 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1566 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1568 usb_device_request_t req;
1571 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1572 req.bRequest = RT2573_WRITE_MULTI_MAC;
1573 USETW(req.wValue, 0);
1574 USETW(req.wIndex, reg);
1575 USETW(req.wLength, len);
1577 error = usbd_do_request(sc->sc_udev, &req, buf);
1579 printf("%s: could not multi write MAC register: %s\n",
1580 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1585 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1590 for (ntries = 0; ntries < 5; ntries++) {
1591 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1595 printf("%s: could not write to BBP\n",
1596 device_get_nameunit(sc->sc_dev));
1600 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1601 rum_write(sc, RT2573_PHY_CSR3, tmp);
1605 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1610 for (ntries = 0; ntries < 5; ntries++) {
1611 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1615 printf("%s: could not read BBP\n",
1616 device_get_nameunit(sc->sc_dev));
1620 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1621 rum_write(sc, RT2573_PHY_CSR3, val);
1623 for (ntries = 0; ntries < 100; ntries++) {
1624 val = rum_read(sc, RT2573_PHY_CSR3);
1625 if (!(val & RT2573_BBP_BUSY))
1630 printf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1635 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1640 for (ntries = 0; ntries < 5; ntries++) {
1641 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1645 printf("%s: could not write to RF\n",
1646 device_get_nameunit(sc->sc_dev));
1650 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1652 rum_write(sc, RT2573_PHY_CSR4, tmp);
1654 /* remember last written value in sc */
1655 sc->rf_regs[reg] = val;
1657 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1661 rum_select_antenna(struct rum_softc *sc)
1663 uint8_t bbp4, bbp77;
1666 bbp4 = rum_bbp_read(sc, 4);
1667 bbp77 = rum_bbp_read(sc, 77);
1671 /* make sure Rx is disabled before switching antenna */
1672 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1673 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1675 rum_bbp_write(sc, 4, bbp4);
1676 rum_bbp_write(sc, 77, bbp77);
1678 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1682 * Enable multi-rate retries for frames sent at OFDM rates.
1683 * In 802.11b/g mode, allow fallback to CCK rates.
1686 rum_enable_mrr(struct rum_softc *sc)
1688 struct ieee80211com *ic = &sc->sc_ic;
1691 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1693 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1694 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1695 tmp |= RT2573_MRR_CCK_FALLBACK;
1696 tmp |= RT2573_MRR_ENABLED;
1698 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1702 rum_set_txpreamble(struct rum_softc *sc)
1706 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1708 tmp &= ~RT2573_SHORT_PREAMBLE;
1709 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1710 tmp |= RT2573_SHORT_PREAMBLE;
1712 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1716 rum_set_basicrates(struct rum_softc *sc)
1718 struct ieee80211com *ic = &sc->sc_ic;
1720 /* update basic rate set */
1721 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1722 /* 11b basic rates: 1, 2Mbps */
1723 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1724 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1725 /* 11a basic rates: 6, 12, 24Mbps */
1726 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1728 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1729 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1734 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1738 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1740 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1743 /* update all BBP registers that depend on the band */
1744 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1745 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1746 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1747 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1748 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1750 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1751 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1752 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1756 rum_bbp_write(sc, 17, bbp17);
1757 rum_bbp_write(sc, 96, bbp96);
1758 rum_bbp_write(sc, 104, bbp104);
1760 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1761 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1762 rum_bbp_write(sc, 75, 0x80);
1763 rum_bbp_write(sc, 86, 0x80);
1764 rum_bbp_write(sc, 88, 0x80);
1767 rum_bbp_write(sc, 35, bbp35);
1768 rum_bbp_write(sc, 97, bbp97);
1769 rum_bbp_write(sc, 98, bbp98);
1771 tmp = rum_read(sc, RT2573_PHY_CSR0);
1772 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1773 if (IEEE80211_IS_CHAN_2GHZ(c))
1774 tmp |= RT2573_PA_PE_2GHZ;
1776 tmp |= RT2573_PA_PE_5GHZ;
1777 rum_write(sc, RT2573_PHY_CSR0, tmp);
1779 /* 802.11a uses a 16 microseconds short interframe space */
1780 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1784 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1786 struct ieee80211com *ic = &sc->sc_ic;
1787 const struct rfprog *rfprog;
1788 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1792 chan = ieee80211_chan2ieee(ic, c);
1793 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1796 /* select the appropriate RF settings based on what EEPROM says */
1797 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1798 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1800 /* find the settings for this channel (we know it exists) */
1801 for (i = 0; rfprog[i].chan != chan; i++);
1803 power = sc->txpow[i];
1807 } else if (power > 31) {
1808 bbp94 += power - 31;
1813 * If we are switching from the 2GHz band to the 5GHz band or
1814 * vice-versa, BBP registers need to be reprogrammed.
1816 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1817 rum_select_band(sc, c);
1818 rum_select_antenna(sc);
1822 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1823 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1824 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1825 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1827 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1828 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1829 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1830 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1832 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1833 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1834 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1835 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1839 /* enable smart mode for MIMO-capable RFs */
1840 bbp3 = rum_bbp_read(sc, 3);
1842 bbp3 &= ~RT2573_SMART_MODE;
1843 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1844 bbp3 |= RT2573_SMART_MODE;
1846 rum_bbp_write(sc, 3, bbp3);
1848 if (bbp94 != RT2573_BBPR94_DEFAULT)
1849 rum_bbp_write(sc, 94, bbp94);
1853 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1854 * and HostAP operating modes.
1857 rum_enable_tsf_sync(struct rum_softc *sc)
1859 struct ieee80211com *ic = &sc->sc_ic;
1862 if (ic->ic_opmode != IEEE80211_M_STA) {
1864 * Change default 16ms TBTT adjustment to 8ms.
1865 * Must be done before enabling beacon generation.
1867 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1870 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1872 /* set beacon interval (in 1/16ms unit) */
1873 tmp |= ic->ic_bss->ni_intval * 16;
1875 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1876 if (ic->ic_opmode == IEEE80211_M_STA)
1877 tmp |= RT2573_TSF_MODE(1);
1879 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1881 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1885 rum_update_slot(struct ifnet *ifp)
1887 struct rum_softc *sc = ifp->if_softc;
1888 struct ieee80211com *ic = &sc->sc_ic;
1892 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1894 tmp = rum_read(sc, RT2573_MAC_CSR9);
1895 tmp = (tmp & ~0xff) | slottime;
1896 rum_write(sc, RT2573_MAC_CSR9, tmp);
1898 DPRINTF(("setting slot time to %uus\n", slottime));
1902 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1906 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1907 rum_write(sc, RT2573_MAC_CSR4, tmp);
1909 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1910 rum_write(sc, RT2573_MAC_CSR5, tmp);
1914 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1918 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1919 rum_write(sc, RT2573_MAC_CSR2, tmp);
1921 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1922 rum_write(sc, RT2573_MAC_CSR3, tmp);
1926 rum_update_promisc(struct rum_softc *sc)
1928 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1931 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1933 tmp &= ~RT2573_DROP_NOT_TO_ME;
1934 if (!(ifp->if_flags & IFF_PROMISC))
1935 tmp |= RT2573_DROP_NOT_TO_ME;
1937 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1939 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1940 "entering" : "leaving"));
1947 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1948 case RT2573_RF_2528: return "RT2528";
1949 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1950 case RT2573_RF_5226: return "RT5226";
1951 default: return "unknown";
1956 rum_read_eeprom(struct rum_softc *sc)
1958 struct ieee80211com *ic = &sc->sc_ic;
1964 /* read MAC address */
1965 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1967 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1969 sc->rf_rev = (val >> 11) & 0x1f;
1970 sc->hw_radio = (val >> 10) & 0x1;
1971 sc->rx_ant = (val >> 4) & 0x3;
1972 sc->tx_ant = (val >> 2) & 0x3;
1973 sc->nb_ant = val & 0x3;
1975 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1977 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1979 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1980 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1982 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1983 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1985 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1987 if ((val & 0xff) != 0xff)
1988 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1990 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1992 if ((val & 0xff) != 0xff)
1993 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1995 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1996 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1998 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2000 if ((val & 0xff) != 0xff)
2001 sc->rffreq = val & 0xff;
2003 DPRINTF(("RF freq=%d\n", sc->rffreq));
2005 /* read Tx power for all a/b/g channels */
2006 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2007 /* XXX default Tx power for 802.11a channels */
2008 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2010 for (i = 0; i < 14; i++)
2011 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
2014 /* read default values for BBP registers */
2015 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2017 for (i = 0; i < 14; i++) {
2018 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2020 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2021 sc->bbp_prom[i].val));
2027 rum_bbp_init(struct rum_softc *sc)
2029 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2032 /* wait for BBP to be ready */
2033 for (ntries = 0; ntries < 100; ntries++) {
2034 const uint8_t val = rum_bbp_read(sc, 0);
2035 if (val != 0 && val != 0xff)
2039 if (ntries == 100) {
2040 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2044 /* initialize BBP registers to default values */
2045 for (i = 0; i < N(rum_def_bbp); i++)
2046 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2048 /* write vendor-specific BBP values (from EEPROM) */
2049 for (i = 0; i < 16; i++) {
2050 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2052 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2060 rum_init(void *priv)
2062 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2063 struct rum_softc *sc = priv;
2064 struct ieee80211com *ic = &sc->sc_ic;
2065 struct ifnet *ifp = ic->ic_ifp;
2066 struct rum_rx_data *data;
2073 /* initialize MAC registers to default values */
2074 for (i = 0; i < N(rum_def_mac); i++)
2075 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2077 /* set host ready */
2078 rum_write(sc, RT2573_MAC_CSR1, 3);
2079 rum_write(sc, RT2573_MAC_CSR1, 0);
2081 /* wait for BBP/RF to wakeup */
2082 for (ntries = 0; ntries < 1000; ntries++) {
2083 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2085 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2088 if (ntries == 1000) {
2089 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2090 device_get_nameunit(sc->sc_dev));
2094 if ((error = rum_bbp_init(sc)) != 0)
2097 /* select default channel */
2098 rum_select_band(sc, ic->ic_curchan);
2099 rum_select_antenna(sc);
2100 rum_set_chan(sc, ic->ic_curchan);
2102 /* clear STA registers */
2103 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2105 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2106 rum_set_macaddr(sc, ic->ic_myaddr);
2108 /* initialize ASIC */
2109 rum_write(sc, RT2573_MAC_CSR1, 4);
2112 * Allocate xfer for AMRR statistics requests.
2114 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2115 if (sc->amrr_xfer == NULL) {
2116 printf("%s: could not allocate AMRR xfer\n",
2117 device_get_nameunit(sc->sc_dev));
2122 * Open Tx and Rx USB bulk pipes.
2124 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2127 printf("%s: could not open Tx pipe: %s\n",
2128 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2131 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2134 printf("%s: could not open Rx pipe: %s\n",
2135 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2140 * Allocate Tx and Rx xfer queues.
2142 error = rum_alloc_tx_list(sc);
2144 printf("%s: could not allocate Tx list\n",
2145 device_get_nameunit(sc->sc_dev));
2148 error = rum_alloc_rx_list(sc);
2150 printf("%s: could not allocate Rx list\n",
2151 device_get_nameunit(sc->sc_dev));
2156 * Start up the receive pipe.
2158 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2159 data = &sc->rx_data[i];
2161 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2162 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2163 usbd_transfer(data->xfer);
2166 /* update Rx filter */
2167 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2169 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2170 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2171 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2173 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2174 tmp |= RT2573_DROP_TODS;
2175 if (!(ifp->if_flags & IFF_PROMISC))
2176 tmp |= RT2573_DROP_NOT_TO_ME;
2178 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2180 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2181 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2183 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2184 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2185 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2187 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2196 rum_stop(void *priv)
2198 struct rum_softc *sc = priv;
2199 struct ieee80211com *ic = &sc->sc_ic;
2200 struct ifnet *ifp = ic->ic_ifp;
2203 sc->sc_tx_timer = 0;
2204 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2206 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2209 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2210 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2213 rum_write(sc, RT2573_MAC_CSR1, 3);
2214 rum_write(sc, RT2573_MAC_CSR1, 0);
2216 if (sc->amrr_xfer != NULL) {
2217 usbd_free_xfer(sc->amrr_xfer);
2218 sc->amrr_xfer = NULL;
2221 if (sc->sc_rx_pipeh != NULL) {
2222 usbd_abort_pipe(sc->sc_rx_pipeh);
2223 usbd_close_pipe(sc->sc_rx_pipeh);
2224 sc->sc_rx_pipeh = NULL;
2226 if (sc->sc_tx_pipeh != NULL) {
2227 usbd_abort_pipe(sc->sc_tx_pipeh);
2228 usbd_close_pipe(sc->sc_tx_pipeh);
2229 sc->sc_tx_pipeh = NULL;
2232 rum_free_rx_list(sc);
2233 rum_free_tx_list(sc);
2237 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2239 usb_device_request_t req;
2240 uint16_t reg = RT2573_MCU_CODE_BASE;
2243 /* copy firmware image into NIC */
2244 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2245 rum_write(sc, reg, UGETDW(ucode));
2247 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2248 req.bRequest = RT2573_MCU_CNTL;
2249 USETW(req.wValue, RT2573_MCU_RUN);
2250 USETW(req.wIndex, 0);
2251 USETW(req.wLength, 0);
2253 error = usbd_do_request(sc->sc_udev, &req, NULL);
2255 printf("%s: could not run firmware: %s\n",
2256 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2262 rum_prepare_beacon(struct rum_softc *sc)
2264 struct ieee80211com *ic = &sc->sc_ic;
2265 struct rum_tx_desc desc;
2269 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2274 /* send beacons at the lowest available rate */
2275 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2277 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2278 m0->m_pkthdr.len, rate);
2280 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2281 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2283 /* copy beacon header and payload into NIC memory */
2284 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2293 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2294 const struct ieee80211_bpf_params *params)
2296 struct ieee80211com *ic = ni->ni_ic;
2297 struct ifnet *ifp = ic->ic_ifp;
2298 struct rum_softc *sc = ifp->if_softc;
2300 /* prevent management frames from being sent if we're not ready */
2301 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2303 ieee80211_free_node(ni);
2306 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
2307 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2309 ieee80211_free_node(ni);
2313 if (bpf_peers_present(ic->ic_rawbpf))
2314 bpf_mtap(ic->ic_rawbpf, m);
2318 if (params == NULL) {
2320 * Legacy path; interpret frame contents to decide
2321 * precisely how to send the frame.
2323 if (rum_tx_mgt(sc, m, ni) != 0)
2327 * Caller supplied explicit parameters to use in
2328 * sending the frame.
2330 if (rum_tx_raw(sc, m, ni, params) != 0)
2333 sc->sc_tx_timer = 5;
2334 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
2339 ieee80211_free_node(ni);
2344 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2348 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2349 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2351 ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2353 /* set rate to some reasonable initial value */
2354 for (i = ni->ni_rates.rs_nrates - 1;
2355 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2359 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2363 rum_amrr_timeout(void *arg)
2365 struct rum_softc *sc = (struct rum_softc *)arg;
2366 usb_device_request_t req;
2369 * Asynchronously read statistic registers (cleared by read).
2371 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2372 req.bRequest = RT2573_READ_MULTI_MAC;
2373 USETW(req.wValue, 0);
2374 USETW(req.wIndex, RT2573_STA_CSR0);
2375 USETW(req.wLength, sizeof sc->sta);
2377 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2378 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2380 (void)usbd_transfer(sc->amrr_xfer);
2384 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2387 struct rum_softc *sc = (struct rum_softc *)priv;
2388 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2390 if (status != USBD_NORMAL_COMPLETION) {
2391 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2392 "cancelling automatic rate control\n");
2396 /* count TX retry-fail as Tx errors */
2397 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2399 sc->amn.amn_retrycnt =
2400 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2401 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2402 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2405 sc->amn.amn_retrycnt +
2406 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2408 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2410 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2413 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);