Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / usb / if_rum.c
blob190e6f966eadbe0fd20e04315ae030a96fd3d59e
1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 /* $NetBSD: if_rum.c,v 1.29 2009/09/28 10:22:07 pooka Exp $ */
4 /*-
5 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
6 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 /*-
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com.tw/
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: if_rum.c,v 1.29 2009/09/28 10:22:07 pooka Exp $");
29 #include "bpfilter.h"
31 #include <sys/param.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/mbuf.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
42 #include <sys/bus.h>
43 #include <machine/endian.h>
44 #include <sys/intr.h>
46 #if NBPFILTER > 0
47 #include <net/bpf.h>
48 #endif
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
61 #include <net80211/ieee80211_netbsd.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_amrr.h>
64 #include <net80211/ieee80211_radiotap.h>
66 #include <dev/firmload.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
73 #include <dev/usb/if_rumreg.h>
74 #include <dev/usb/if_rumvar.h>
76 #ifdef USB_DEBUG
77 #define RUM_DEBUG
78 #endif
80 #ifdef RUM_DEBUG
81 #define DPRINTF(x) do { if (rum_debug) logprintf x; } while (0)
82 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) logprintf x; } while (0)
83 int rum_debug = 1;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n, x)
87 #endif
89 /* various supported device vendors/products */
90 static const struct usb_devno rum_devs[] = {
91 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
92 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
93 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 },
94 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 },
95 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
96 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
97 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_2 },
98 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_3 },
99 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
100 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
101 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
102 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
103 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
104 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL },
105 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX },
106 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
107 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
108 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
109 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
110 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA111 },
111 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
112 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
113 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
114 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
115 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
116 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
117 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
118 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
119 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
120 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_WLIUCG },
121 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
122 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
123 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
124 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
125 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
126 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
127 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
128 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
129 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
130 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
131 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_3 },
132 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
133 { USB_VENDOR_RALINK_2, USB_PRODUCT_RALINK_2_RT2573 },
134 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
135 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
136 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
137 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
140 Static int rum_attachhook(void *);
141 Static int rum_alloc_tx_list(struct rum_softc *);
142 Static void rum_free_tx_list(struct rum_softc *);
143 Static int rum_alloc_rx_list(struct rum_softc *);
144 Static void rum_free_rx_list(struct rum_softc *);
145 Static int rum_media_change(struct ifnet *);
146 Static void rum_next_scan(void *);
147 Static void rum_task(void *);
148 Static int rum_newstate(struct ieee80211com *,
149 enum ieee80211_state, int);
150 Static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
151 usbd_status);
152 Static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
153 usbd_status);
154 #if NBPFILTER > 0
155 Static uint8_t rum_rxrate(const struct rum_rx_desc *);
156 #endif
157 Static int rum_ack_rate(struct ieee80211com *, int);
158 Static uint16_t rum_txtime(int, int, uint32_t);
159 Static uint8_t rum_plcp_signal(int);
160 Static void rum_setup_tx_desc(struct rum_softc *,
161 struct rum_tx_desc *, uint32_t, uint16_t, int,
162 int);
163 Static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
164 struct ieee80211_node *);
165 Static int rum_tx_data(struct rum_softc *, struct mbuf *,
166 struct ieee80211_node *);
167 Static void rum_start(struct ifnet *);
168 Static void rum_watchdog(struct ifnet *);
169 Static int rum_ioctl(struct ifnet *, u_long, void *);
170 Static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
171 int);
172 Static uint32_t rum_read(struct rum_softc *, uint16_t);
173 Static void rum_read_multi(struct rum_softc *, uint16_t, void *,
174 int);
175 Static void rum_write(struct rum_softc *, uint16_t, uint32_t);
176 Static void rum_write_multi(struct rum_softc *, uint16_t, void *,
177 size_t);
178 Static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
179 Static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
180 Static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
181 Static void rum_select_antenna(struct rum_softc *);
182 Static void rum_enable_mrr(struct rum_softc *);
183 Static void rum_set_txpreamble(struct rum_softc *);
184 Static void rum_set_basicrates(struct rum_softc *);
185 Static void rum_select_band(struct rum_softc *,
186 struct ieee80211_channel *);
187 Static void rum_set_chan(struct rum_softc *,
188 struct ieee80211_channel *);
189 Static void rum_enable_tsf_sync(struct rum_softc *);
190 Static void rum_update_slot(struct rum_softc *);
191 Static void rum_set_bssid(struct rum_softc *, const uint8_t *);
192 Static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
193 Static void rum_update_promisc(struct rum_softc *);
194 Static const char *rum_get_rf(int);
195 Static void rum_read_eeprom(struct rum_softc *);
196 Static int rum_bbp_init(struct rum_softc *);
197 Static int rum_init(struct ifnet *);
198 Static void rum_stop(struct ifnet *, int);
199 Static int rum_load_microcode(struct rum_softc *, const u_char *,
200 size_t);
201 Static int rum_prepare_beacon(struct rum_softc *);
202 Static void rum_newassoc(struct ieee80211_node *, int);
203 Static void rum_amrr_start(struct rum_softc *,
204 struct ieee80211_node *);
205 Static void rum_amrr_timeout(void *);
206 Static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
207 usbd_status status);
210 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
212 static const struct ieee80211_rateset rum_rateset_11a =
213 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
215 static const struct ieee80211_rateset rum_rateset_11b =
216 { 4, { 2, 4, 11, 22 } };
218 static const struct ieee80211_rateset rum_rateset_11g =
219 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
221 static const struct {
222 uint32_t reg;
223 uint32_t val;
224 } rum_def_mac[] = {
225 RT2573_DEF_MAC
228 static const struct {
229 uint8_t reg;
230 uint8_t val;
231 } rum_def_bbp[] = {
232 RT2573_DEF_BBP
235 static const struct rfprog {
236 uint8_t chan;
237 uint32_t r1, r2, r3, r4;
238 } rum_rf5226[] = {
239 RT2573_RF5226
240 }, rum_rf5225[] = {
241 RT2573_RF5225
244 USB_DECLARE_DRIVER(rum);
246 USB_MATCH(rum)
248 USB_MATCH_START(rum, uaa);
250 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
251 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
254 Static int
255 rum_attachhook(void *xsc)
257 struct rum_softc *sc = xsc;
258 firmware_handle_t fwh;
259 const char *name = "rum-rt2573";
260 u_char *ucode;
261 size_t size;
262 int error;
264 if ((error = firmware_open("rum", name, &fwh)) != 0) {
265 printf("%s: failed loadfirmware of file %s (error %d)\n",
266 USBDEVNAME(sc->sc_dev), name, error);
267 return error;
269 size = firmware_get_size(fwh);
270 ucode = firmware_malloc(size);
271 if (ucode == NULL) {
272 printf("%s: failed to allocate firmware memory\n",
273 USBDEVNAME(sc->sc_dev));
274 firmware_close(fwh);
275 return ENOMEM;
277 error = firmware_read(fwh, 0, ucode, size);
278 firmware_close(fwh);
279 if (error != 0) {
280 printf("%s: failed to read firmware (error %d)\n",
281 USBDEVNAME(sc->sc_dev), error);
282 firmware_free(ucode, 0);
283 return error;
286 if (rum_load_microcode(sc, ucode, size) != 0) {
287 printf("%s: could not load 8051 microcode\n",
288 USBDEVNAME(sc->sc_dev));
289 firmware_free(ucode, 0);
290 return ENXIO;
293 firmware_free(ucode, 0);
294 sc->sc_flags |= RT2573_FWLOADED;
296 return 0;
299 USB_ATTACH(rum)
301 USB_ATTACH_START(rum, sc, uaa);
302 struct ieee80211com *ic = &sc->sc_ic;
303 struct ifnet *ifp = &sc->sc_if;
304 usb_interface_descriptor_t *id;
305 usb_endpoint_descriptor_t *ed;
306 usbd_status error;
307 char *devinfop;
308 int i, ntries;
309 uint32_t tmp;
311 sc->sc_dev = self;
312 sc->sc_udev = uaa->device;
313 sc->sc_flags = 0;
315 aprint_naive("\n");
316 aprint_normal("\n");
318 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
319 aprint_normal_dev(self, "%s\n", devinfop);
320 usbd_devinfo_free(devinfop);
322 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
323 aprint_error_dev(self, "could not set configuration no\n");
324 USB_ATTACH_ERROR_RETURN;
327 /* get the first interface handle */
328 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
329 &sc->sc_iface);
330 if (error != 0) {
331 aprint_error_dev(self, "could not get interface handle\n");
332 USB_ATTACH_ERROR_RETURN;
336 * Find endpoints.
338 id = usbd_get_interface_descriptor(sc->sc_iface);
340 sc->sc_rx_no = sc->sc_tx_no = -1;
341 for (i = 0; i < id->bNumEndpoints; i++) {
342 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
343 if (ed == NULL) {
344 aprint_error_dev(self,
345 "no endpoint descriptor for iface %d\n", i);
346 USB_ATTACH_ERROR_RETURN;
349 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
350 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
351 sc->sc_rx_no = ed->bEndpointAddress;
352 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
353 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
354 sc->sc_tx_no = ed->bEndpointAddress;
356 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
357 aprint_error_dev(self, "missing endpoint\n");
358 USB_ATTACH_ERROR_RETURN;
361 usb_init_task(&sc->sc_task, rum_task, sc);
362 usb_callout_init(sc->sc_scan_ch);
364 sc->amrr.amrr_min_success_threshold = 1;
365 sc->amrr.amrr_max_success_threshold = 10;
366 usb_callout_init(sc->sc_amrr_ch);
368 /* retrieve RT2573 rev. no */
369 for (ntries = 0; ntries < 1000; ntries++) {
370 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
371 break;
372 DELAY(1000);
374 if (ntries == 1000) {
375 aprint_error_dev(self, "timeout waiting for chip to settle\n");
376 USB_ATTACH_ERROR_RETURN;
379 /* retrieve MAC address and various other things from EEPROM */
380 rum_read_eeprom(sc);
382 aprint_normal_dev(self,
383 "MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
384 sc->macbbp_rev, tmp,
385 rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr));
387 ic->ic_ifp = ifp;
388 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
389 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
390 ic->ic_state = IEEE80211_S_INIT;
392 /* set device capabilities */
393 ic->ic_caps =
394 IEEE80211_C_IBSS | /* IBSS mode supported */
395 IEEE80211_C_MONITOR | /* monitor mode supported */
396 IEEE80211_C_HOSTAP | /* HostAp mode supported */
397 IEEE80211_C_TXPMGT | /* tx power management */
398 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
399 IEEE80211_C_SHSLOT | /* short slot time supported */
400 IEEE80211_C_WPA; /* 802.11i */
402 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
403 /* set supported .11a rates */
404 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
406 /* set supported .11a channels */
407 for (i = 34; i <= 46; i += 4) {
408 ic->ic_channels[i].ic_freq =
409 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
410 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
412 for (i = 36; i <= 64; i += 4) {
413 ic->ic_channels[i].ic_freq =
414 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
415 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
417 for (i = 100; i <= 140; i += 4) {
418 ic->ic_channels[i].ic_freq =
419 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
420 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
422 for (i = 149; i <= 165; i += 4) {
423 ic->ic_channels[i].ic_freq =
424 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
425 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
429 /* set supported .11b and .11g rates */
430 ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
431 ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
433 /* set supported .11b and .11g channels (1 through 14) */
434 for (i = 1; i <= 14; i++) {
435 ic->ic_channels[i].ic_freq =
436 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
437 ic->ic_channels[i].ic_flags =
438 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
439 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
442 ifp->if_softc = sc;
443 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
444 ifp->if_init = rum_init;
445 ifp->if_ioctl = rum_ioctl;
446 ifp->if_start = rum_start;
447 ifp->if_watchdog = rum_watchdog;
448 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
449 IFQ_SET_READY(&ifp->if_snd);
450 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
452 if_attach(ifp);
453 ieee80211_ifattach(ic);
454 ic->ic_newassoc = rum_newassoc;
456 /* override state transition machine */
457 sc->sc_newstate = ic->ic_newstate;
458 ic->ic_newstate = rum_newstate;
459 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
461 #if NBPFILTER > 0
462 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
463 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, &sc->sc_drvbpf);
465 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
466 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
467 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
469 sc->sc_txtap_len = sizeof sc->sc_txtapu;
470 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
471 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
472 #endif
474 ieee80211_announce(ic);
476 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
477 USBDEV(sc->sc_dev));
479 USB_ATTACH_SUCCESS_RETURN;
482 USB_DETACH(rum)
484 USB_DETACH_START(rum, sc);
485 struct ieee80211com *ic = &sc->sc_ic;
486 struct ifnet *ifp = &sc->sc_if;
487 int s;
489 if (!ifp->if_softc)
490 return 0;
492 s = splusb();
494 rum_stop(ifp, 1);
495 usb_rem_task(sc->sc_udev, &sc->sc_task);
496 usb_uncallout(sc->sc_scan_ch, rum_next_scan, sc);
497 usb_uncallout(sc->sc_amrr_ch, rum_amrr_timeout, sc);
499 if (sc->amrr_xfer != NULL) {
500 usbd_free_xfer(sc->amrr_xfer);
501 sc->amrr_xfer = NULL;
504 if (sc->sc_rx_pipeh != NULL) {
505 usbd_abort_pipe(sc->sc_rx_pipeh);
506 usbd_close_pipe(sc->sc_rx_pipeh);
509 if (sc->sc_tx_pipeh != NULL) {
510 usbd_abort_pipe(sc->sc_tx_pipeh);
511 usbd_close_pipe(sc->sc_tx_pipeh);
514 #if NBPFILTER > 0
515 bpfdetach(ifp);
516 #endif
517 ieee80211_ifdetach(ic); /* free all nodes */
518 if_detach(ifp);
520 splx(s);
522 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
523 USBDEV(sc->sc_dev));
525 return 0;
528 Static int
529 rum_alloc_tx_list(struct rum_softc *sc)
531 struct rum_tx_data *data;
532 int i, error;
534 sc->tx_queued = 0;
536 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
537 data = &sc->tx_data[i];
539 data->sc = sc;
541 data->xfer = usbd_alloc_xfer(sc->sc_udev);
542 if (data->xfer == NULL) {
543 printf("%s: could not allocate tx xfer\n",
544 USBDEVNAME(sc->sc_dev));
545 error = ENOMEM;
546 goto fail;
549 data->buf = usbd_alloc_buffer(data->xfer,
550 RT2573_TX_DESC_SIZE + MCLBYTES);
551 if (data->buf == NULL) {
552 printf("%s: could not allocate tx buffer\n",
553 USBDEVNAME(sc->sc_dev));
554 error = ENOMEM;
555 goto fail;
558 /* clean Tx descriptor */
559 memset(data->buf, 0, RT2573_TX_DESC_SIZE);
562 return 0;
564 fail: rum_free_tx_list(sc);
565 return error;
568 Static void
569 rum_free_tx_list(struct rum_softc *sc)
571 struct rum_tx_data *data;
572 int i;
574 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
575 data = &sc->tx_data[i];
577 if (data->xfer != NULL) {
578 usbd_free_xfer(data->xfer);
579 data->xfer = NULL;
582 if (data->ni != NULL) {
583 ieee80211_free_node(data->ni);
584 data->ni = NULL;
589 Static int
590 rum_alloc_rx_list(struct rum_softc *sc)
592 struct rum_rx_data *data;
593 int i, error;
595 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
596 data = &sc->rx_data[i];
598 data->sc = sc;
600 data->xfer = usbd_alloc_xfer(sc->sc_udev);
601 if (data->xfer == NULL) {
602 printf("%s: could not allocate rx xfer\n",
603 USBDEVNAME(sc->sc_dev));
604 error = ENOMEM;
605 goto fail;
608 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
609 printf("%s: could not allocate rx buffer\n",
610 USBDEVNAME(sc->sc_dev));
611 error = ENOMEM;
612 goto fail;
615 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
616 if (data->m == NULL) {
617 printf("%s: could not allocate rx mbuf\n",
618 USBDEVNAME(sc->sc_dev));
619 error = ENOMEM;
620 goto fail;
623 MCLGET(data->m, M_DONTWAIT);
624 if (!(data->m->m_flags & M_EXT)) {
625 printf("%s: could not allocate rx mbuf cluster\n",
626 USBDEVNAME(sc->sc_dev));
627 error = ENOMEM;
628 goto fail;
631 data->buf = mtod(data->m, uint8_t *);
634 return 0;
636 fail: rum_free_tx_list(sc);
637 return error;
640 Static void
641 rum_free_rx_list(struct rum_softc *sc)
643 struct rum_rx_data *data;
644 int i;
646 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
647 data = &sc->rx_data[i];
649 if (data->xfer != NULL) {
650 usbd_free_xfer(data->xfer);
651 data->xfer = NULL;
654 if (data->m != NULL) {
655 m_freem(data->m);
656 data->m = NULL;
661 Static int
662 rum_media_change(struct ifnet *ifp)
664 int error;
666 error = ieee80211_media_change(ifp);
667 if (error != ENETRESET)
668 return error;
670 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
671 rum_init(ifp);
673 return 0;
677 * This function is called periodically (every 200ms) during scanning to
678 * switch from one channel to another.
680 Static void
681 rum_next_scan(void *arg)
683 struct rum_softc *sc = arg;
684 struct ieee80211com *ic = &sc->sc_ic;
686 if (ic->ic_state == IEEE80211_S_SCAN)
687 ieee80211_next_scan(ic);
690 Static void
691 rum_task(void *arg)
693 struct rum_softc *sc = arg;
694 struct ieee80211com *ic = &sc->sc_ic;
695 enum ieee80211_state ostate;
696 struct ieee80211_node *ni;
697 uint32_t tmp;
699 ostate = ic->ic_state;
701 switch (sc->sc_state) {
702 case IEEE80211_S_INIT:
703 if (ostate == IEEE80211_S_RUN) {
704 /* abort TSF synchronization */
705 tmp = rum_read(sc, RT2573_TXRX_CSR9);
706 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
708 break;
710 case IEEE80211_S_SCAN:
711 rum_set_chan(sc, ic->ic_curchan);
712 usb_callout(sc->sc_scan_ch, hz / 5, rum_next_scan, sc);
713 break;
715 case IEEE80211_S_AUTH:
716 rum_set_chan(sc, ic->ic_curchan);
717 break;
719 case IEEE80211_S_ASSOC:
720 rum_set_chan(sc, ic->ic_curchan);
721 break;
723 case IEEE80211_S_RUN:
724 rum_set_chan(sc, ic->ic_curchan);
726 ni = ic->ic_bss;
728 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
729 rum_update_slot(sc);
730 rum_enable_mrr(sc);
731 rum_set_txpreamble(sc);
732 rum_set_basicrates(sc);
733 rum_set_bssid(sc, ni->ni_bssid);
736 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
737 ic->ic_opmode == IEEE80211_M_IBSS)
738 rum_prepare_beacon(sc);
740 if (ic->ic_opmode != IEEE80211_M_MONITOR)
741 rum_enable_tsf_sync(sc);
743 if (ic->ic_opmode == IEEE80211_M_STA) {
744 /* fake a join to init the tx rate */
745 rum_newassoc(ic->ic_bss, 1);
747 /* enable automatic rate adaptation in STA mode */
748 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
749 rum_amrr_start(sc, ni);
752 break;
755 sc->sc_newstate(ic, sc->sc_state, -1);
758 Static int
759 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
761 struct rum_softc *sc = ic->ic_ifp->if_softc;
763 usb_rem_task(sc->sc_udev, &sc->sc_task);
764 usb_uncallout(sc->sc_scan_ch, rum_next_scan, sc);
765 usb_uncallout(sc->sc_amrr_ch, rum_amrr_timeout, sc);
767 /* do it in a process context */
768 sc->sc_state = nstate;
769 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
771 return 0;
774 /* quickly determine if a given rate is CCK or OFDM */
775 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
777 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
778 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
780 Static void
781 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
783 struct rum_tx_data *data = priv;
784 struct rum_softc *sc = data->sc;
785 struct ifnet *ifp = &sc->sc_if;
786 int s;
788 if (status != USBD_NORMAL_COMPLETION) {
789 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
790 return;
792 printf("%s: could not transmit buffer: %s\n",
793 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
795 if (status == USBD_STALLED)
796 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
798 ifp->if_oerrors++;
799 return;
802 s = splnet();
804 ieee80211_free_node(data->ni);
805 data->ni = NULL;
807 sc->tx_queued--;
808 ifp->if_opackets++;
810 DPRINTFN(10, ("tx done\n"));
812 sc->sc_tx_timer = 0;
813 ifp->if_flags &= ~IFF_OACTIVE;
814 rum_start(ifp);
816 splx(s);
819 Static void
820 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
822 struct rum_rx_data *data = priv;
823 struct rum_softc *sc = data->sc;
824 struct ieee80211com *ic = &sc->sc_ic;
825 struct ifnet *ifp = &sc->sc_if;
826 struct rum_rx_desc *desc;
827 struct ieee80211_frame *wh;
828 struct ieee80211_node *ni;
829 struct mbuf *mnew, *m;
830 int s, len;
832 if (status != USBD_NORMAL_COMPLETION) {
833 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
834 return;
836 if (status == USBD_STALLED)
837 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
838 goto skip;
841 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
843 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
844 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
845 len));
846 ifp->if_ierrors++;
847 goto skip;
850 desc = (struct rum_rx_desc *)data->buf;
852 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
854 * This should not happen since we did not request to receive
855 * those frames when we filled RT2573_TXRX_CSR0.
857 DPRINTFN(5, ("CRC error\n"));
858 ifp->if_ierrors++;
859 goto skip;
862 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
863 if (mnew == NULL) {
864 printf("%s: could not allocate rx mbuf\n",
865 USBDEVNAME(sc->sc_dev));
866 ifp->if_ierrors++;
867 goto skip;
870 MCLGET(mnew, M_DONTWAIT);
871 if (!(mnew->m_flags & M_EXT)) {
872 printf("%s: could not allocate rx mbuf cluster\n",
873 USBDEVNAME(sc->sc_dev));
874 m_freem(mnew);
875 ifp->if_ierrors++;
876 goto skip;
879 m = data->m;
880 data->m = mnew;
881 data->buf = mtod(data->m, uint8_t *);
883 /* finalize mbuf */
884 m->m_pkthdr.rcvif = ifp;
885 m->m_data = (void *)(desc + 1);
886 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
888 s = splnet();
890 #if NBPFILTER > 0
891 if (sc->sc_drvbpf != NULL) {
892 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
894 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
895 tap->wr_rate = rum_rxrate(desc);
896 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
897 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
898 tap->wr_antenna = sc->rx_ant;
899 tap->wr_antsignal = desc->rssi;
901 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
903 #endif
905 wh = mtod(m, struct ieee80211_frame *);
906 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
908 /* send the frame to the 802.11 layer */
909 ieee80211_input(ic, m, ni, desc->rssi, 0);
911 /* node is no longer needed */
912 ieee80211_free_node(ni);
914 splx(s);
916 DPRINTFN(15, ("rx done\n"));
918 skip: /* setup a new transfer */
919 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
920 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
921 usbd_transfer(xfer);
925 * This function is only used by the Rx radiotap code. It returns the rate at
926 * which a given frame was received.
928 #if NBPFILTER > 0
929 Static uint8_t
930 rum_rxrate(const struct rum_rx_desc *desc)
932 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
933 /* reverse function of rum_plcp_signal */
934 switch (desc->rate) {
935 case 0xb: return 12;
936 case 0xf: return 18;
937 case 0xa: return 24;
938 case 0xe: return 36;
939 case 0x9: return 48;
940 case 0xd: return 72;
941 case 0x8: return 96;
942 case 0xc: return 108;
944 } else {
945 if (desc->rate == 10)
946 return 2;
947 if (desc->rate == 20)
948 return 4;
949 if (desc->rate == 55)
950 return 11;
951 if (desc->rate == 110)
952 return 22;
954 return 2; /* should not get there */
956 #endif
959 * Return the expected ack rate for a frame transmitted at rate `rate'.
960 * XXX: this should depend on the destination node basic rate set.
962 Static int
963 rum_ack_rate(struct ieee80211com *ic, int rate)
965 switch (rate) {
966 /* CCK rates */
967 case 2:
968 return 2;
969 case 4:
970 case 11:
971 case 22:
972 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
974 /* OFDM rates */
975 case 12:
976 case 18:
977 return 12;
978 case 24:
979 case 36:
980 return 24;
981 case 48:
982 case 72:
983 case 96:
984 case 108:
985 return 48;
988 /* default to 1Mbps */
989 return 2;
993 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
994 * The function automatically determines the operating mode depending on the
995 * given rate. `flags' indicates whether short preamble is in use or not.
997 Static uint16_t
998 rum_txtime(int len, int rate, uint32_t flags)
1000 uint16_t txtime;
1002 if (RUM_RATE_IS_OFDM(rate)) {
1003 /* IEEE Std 802.11a-1999, pp. 37 */
1004 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1005 txtime = 16 + 4 + 4 * txtime + 6;
1006 } else {
1007 /* IEEE Std 802.11b-1999, pp. 28 */
1008 txtime = (16 * len + rate - 1) / rate;
1009 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1010 txtime += 72 + 24;
1011 else
1012 txtime += 144 + 48;
1014 return txtime;
1017 Static uint8_t
1018 rum_plcp_signal(int rate)
1020 switch (rate) {
1021 /* CCK rates (returned values are device-dependent) */
1022 case 2: return 0x0;
1023 case 4: return 0x1;
1024 case 11: return 0x2;
1025 case 22: return 0x3;
1027 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1028 case 12: return 0xb;
1029 case 18: return 0xf;
1030 case 24: return 0xa;
1031 case 36: return 0xe;
1032 case 48: return 0x9;
1033 case 72: return 0xd;
1034 case 96: return 0x8;
1035 case 108: return 0xc;
1037 /* unsupported rates (should not get there) */
1038 default: return 0xff;
1042 Static void
1043 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1044 uint32_t flags, uint16_t xflags, int len, int rate)
1046 struct ieee80211com *ic = &sc->sc_ic;
1047 uint16_t plcp_length;
1048 int remainder;
1050 desc->flags = htole32(flags);
1051 desc->flags |= htole32(RT2573_TX_VALID);
1052 desc->flags |= htole32(len << 16);
1054 desc->xflags = htole16(xflags);
1056 desc->wme = htole16(
1057 RT2573_QID(0) |
1058 RT2573_AIFSN(2) |
1059 RT2573_LOGCWMIN(4) |
1060 RT2573_LOGCWMAX(10));
1062 /* setup PLCP fields */
1063 desc->plcp_signal = rum_plcp_signal(rate);
1064 desc->plcp_service = 4;
1066 len += IEEE80211_CRC_LEN;
1067 if (RUM_RATE_IS_OFDM(rate)) {
1068 desc->flags |= htole32(RT2573_TX_OFDM);
1070 plcp_length = len & 0xfff;
1071 desc->plcp_length_hi = plcp_length >> 6;
1072 desc->plcp_length_lo = plcp_length & 0x3f;
1073 } else {
1074 plcp_length = (16 * len + rate - 1) / rate;
1075 if (rate == 22) {
1076 remainder = (16 * len) % 22;
1077 if (remainder != 0 && remainder < 7)
1078 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1080 desc->plcp_length_hi = plcp_length >> 8;
1081 desc->plcp_length_lo = plcp_length & 0xff;
1083 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1084 desc->plcp_signal |= 0x08;
1088 #define RUM_TX_TIMEOUT 5000
1090 Static int
1091 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1093 struct ieee80211com *ic = &sc->sc_ic;
1094 struct rum_tx_desc *desc;
1095 struct rum_tx_data *data;
1096 struct ieee80211_frame *wh;
1097 struct ieee80211_key *k;
1098 uint32_t flags = 0;
1099 uint16_t dur;
1100 usbd_status error;
1101 int xferlen, rate;
1103 data = &sc->tx_data[0];
1104 desc = (struct rum_tx_desc *)data->buf;
1106 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1108 data->m = m0;
1109 data->ni = ni;
1111 wh = mtod(m0, struct ieee80211_frame *);
1113 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1114 k = ieee80211_crypto_encap(ic, ni, m0);
1115 if (k == NULL) {
1116 m_freem(m0);
1117 return ENOBUFS;
1121 wh = mtod(m0, struct ieee80211_frame *);
1123 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1124 flags |= RT2573_TX_NEED_ACK;
1126 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1127 ic->ic_flags) + sc->sifs;
1128 *(uint16_t *)wh->i_dur = htole16(dur);
1130 /* tell hardware to set timestamp in probe responses */
1131 if ((wh->i_fc[0] &
1132 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1133 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1134 flags |= RT2573_TX_TIMESTAMP;
1137 #if NBPFILTER > 0
1138 if (sc->sc_drvbpf != NULL) {
1139 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1141 tap->wt_flags = 0;
1142 tap->wt_rate = rate;
1143 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1144 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1145 tap->wt_antenna = sc->tx_ant;
1147 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1149 #endif
1151 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1152 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1154 /* align end on a 4-bytes boundary */
1155 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1158 * No space left in the last URB to store the extra 4 bytes, force
1159 * sending of another URB.
1161 if ((xferlen % 64) == 0)
1162 xferlen += 4;
1164 DPRINTFN(10, ("sending msg frame len=%zu rate=%u xfer len=%u\n",
1165 (size_t)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE,
1166 rate, xferlen));
1168 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1169 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1171 error = usbd_transfer(data->xfer);
1172 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1173 m_freem(m0);
1174 return error;
1177 sc->tx_queued++;
1179 return 0;
1182 Static int
1183 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1185 struct ieee80211com *ic = &sc->sc_ic;
1186 struct rum_tx_desc *desc;
1187 struct rum_tx_data *data;
1188 struct ieee80211_frame *wh;
1189 struct ieee80211_key *k;
1190 uint32_t flags = 0;
1191 uint16_t dur;
1192 usbd_status error;
1193 int xferlen, rate;
1195 wh = mtod(m0, struct ieee80211_frame *);
1197 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1198 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
1199 else
1200 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1201 if (rate == 0)
1202 rate = 2; /* XXX should not happen */
1203 rate &= IEEE80211_RATE_VAL;
1205 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1206 k = ieee80211_crypto_encap(ic, ni, m0);
1207 if (k == NULL) {
1208 m_freem(m0);
1209 return ENOBUFS;
1212 /* packet header may have moved, reset our local pointer */
1213 wh = mtod(m0, struct ieee80211_frame *);
1216 data = &sc->tx_data[0];
1217 desc = (struct rum_tx_desc *)data->buf;
1219 data->ni = ni;
1221 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1222 flags |= RT2573_TX_NEED_ACK;
1224 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1225 ic->ic_flags) + sc->sifs;
1226 *(uint16_t *)wh->i_dur = htole16(dur);
1229 #if NBPFILTER > 0
1230 if (sc->sc_drvbpf != NULL) {
1231 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1233 tap->wt_flags = 0;
1234 tap->wt_rate = rate;
1235 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1236 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1237 tap->wt_antenna = sc->tx_ant;
1239 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1241 #endif
1243 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1244 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1246 /* align end on a 4-bytes boundary */
1247 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1250 * No space left in the last URB to store the extra 4 bytes, force
1251 * sending of another URB.
1253 if ((xferlen % 64) == 0)
1254 xferlen += 4;
1256 DPRINTFN(10, ("sending data frame len=%zu rate=%u xfer len=%u\n",
1257 (size_t)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE,
1258 rate, xferlen));
1260 /* mbuf is no longer needed */
1261 m_freem(m0);
1263 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1264 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1266 error = usbd_transfer(data->xfer);
1267 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1268 return error;
1270 sc->tx_queued++;
1272 return 0;
1275 Static void
1276 rum_start(struct ifnet *ifp)
1278 struct rum_softc *sc = ifp->if_softc;
1279 struct ieee80211com *ic = &sc->sc_ic;
1280 struct ether_header *eh;
1281 struct ieee80211_node *ni;
1282 struct mbuf *m0;
1284 for (;;) {
1285 IF_POLL(&ic->ic_mgtq, m0);
1286 if (m0 != NULL) {
1287 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1288 ifp->if_flags |= IFF_OACTIVE;
1289 break;
1291 IF_DEQUEUE(&ic->ic_mgtq, m0);
1293 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1294 m0->m_pkthdr.rcvif = NULL;
1295 #if NBPFILTER > 0
1296 if (ic->ic_rawbpf != NULL)
1297 bpf_mtap(ic->ic_rawbpf, m0);
1298 #endif
1299 if (rum_tx_mgt(sc, m0, ni) != 0)
1300 break;
1302 } else {
1303 if (ic->ic_state != IEEE80211_S_RUN)
1304 break;
1305 IFQ_POLL(&ifp->if_snd, m0);
1306 if (m0 == NULL)
1307 break;
1308 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1309 ifp->if_flags |= IFF_OACTIVE;
1310 break;
1312 IFQ_DEQUEUE(&ifp->if_snd, m0);
1313 if (m0->m_len < sizeof(struct ether_header) &&
1314 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
1315 continue;
1317 eh = mtod(m0, struct ether_header *);
1318 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1319 if (ni == NULL) {
1320 m_freem(m0);
1321 continue;
1323 #if NBPFILTER > 0
1324 if (ifp->if_bpf != NULL)
1325 bpf_mtap(ifp->if_bpf, m0);
1326 #endif
1327 m0 = ieee80211_encap(ic, m0, ni);
1328 if (m0 == NULL) {
1329 ieee80211_free_node(ni);
1330 continue;
1332 #if NBPFILTER > 0
1333 if (ic->ic_rawbpf != NULL)
1334 bpf_mtap(ic->ic_rawbpf, m0);
1335 #endif
1336 if (rum_tx_data(sc, m0, ni) != 0) {
1337 ieee80211_free_node(ni);
1338 ifp->if_oerrors++;
1339 break;
1343 sc->sc_tx_timer = 5;
1344 ifp->if_timer = 1;
1348 Static void
1349 rum_watchdog(struct ifnet *ifp)
1351 struct rum_softc *sc = ifp->if_softc;
1352 struct ieee80211com *ic = &sc->sc_ic;
1354 ifp->if_timer = 0;
1356 if (sc->sc_tx_timer > 0) {
1357 if (--sc->sc_tx_timer == 0) {
1358 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1359 /*rum_init(ifp); XXX needs a process context! */
1360 ifp->if_oerrors++;
1361 return;
1363 ifp->if_timer = 1;
1366 ieee80211_watchdog(ic);
1369 Static int
1370 rum_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1372 struct rum_softc *sc = ifp->if_softc;
1373 struct ieee80211com *ic = &sc->sc_ic;
1374 int s, error = 0;
1376 s = splnet();
1378 switch (cmd) {
1379 case SIOCSIFFLAGS:
1380 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1381 break;
1382 /* XXX re-use ether_ioctl() */
1383 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1384 case IFF_UP|IFF_RUNNING:
1385 rum_update_promisc(sc);
1386 break;
1387 case IFF_UP:
1388 rum_init(ifp);
1389 break;
1390 case IFF_RUNNING:
1391 rum_stop(ifp, 1);
1392 break;
1393 case 0:
1394 break;
1396 break;
1398 default:
1399 error = ieee80211_ioctl(ic, cmd, data);
1402 if (error == ENETRESET) {
1403 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1404 (IFF_UP | IFF_RUNNING))
1405 rum_init(ifp);
1406 error = 0;
1409 splx(s);
1411 return error;
1414 Static void
1415 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1417 usb_device_request_t req;
1418 usbd_status error;
1420 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1421 req.bRequest = RT2573_READ_EEPROM;
1422 USETW(req.wValue, 0);
1423 USETW(req.wIndex, addr);
1424 USETW(req.wLength, len);
1426 error = usbd_do_request(sc->sc_udev, &req, buf);
1427 if (error != 0) {
1428 printf("%s: could not read EEPROM: %s\n",
1429 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1433 Static uint32_t
1434 rum_read(struct rum_softc *sc, uint16_t reg)
1436 uint32_t val;
1438 rum_read_multi(sc, reg, &val, sizeof val);
1440 return le32toh(val);
1443 Static void
1444 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1446 usb_device_request_t req;
1447 usbd_status error;
1449 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1450 req.bRequest = RT2573_READ_MULTI_MAC;
1451 USETW(req.wValue, 0);
1452 USETW(req.wIndex, reg);
1453 USETW(req.wLength, len);
1455 error = usbd_do_request(sc->sc_udev, &req, buf);
1456 if (error != 0) {
1457 printf("%s: could not multi read MAC register: %s\n",
1458 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1462 Static void
1463 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1465 uint32_t tmp = htole32(val);
1467 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1470 Static void
1471 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1473 usb_device_request_t req;
1474 usbd_status error;
1476 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1477 req.bRequest = RT2573_WRITE_MULTI_MAC;
1478 USETW(req.wValue, 0);
1479 USETW(req.wIndex, reg);
1480 USETW(req.wLength, len);
1482 error = usbd_do_request(sc->sc_udev, &req, buf);
1483 if (error != 0) {
1484 printf("%s: could not multi write MAC register: %s\n",
1485 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1489 Static void
1490 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1492 uint32_t tmp;
1493 int ntries;
1495 for (ntries = 0; ntries < 5; ntries++) {
1496 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1497 break;
1499 if (ntries == 5) {
1500 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1501 return;
1504 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1505 rum_write(sc, RT2573_PHY_CSR3, tmp);
1508 Static uint8_t
1509 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1511 uint32_t val;
1512 int ntries;
1514 for (ntries = 0; ntries < 5; ntries++) {
1515 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1516 break;
1518 if (ntries == 5) {
1519 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1520 return 0;
1523 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1524 rum_write(sc, RT2573_PHY_CSR3, val);
1526 for (ntries = 0; ntries < 100; ntries++) {
1527 val = rum_read(sc, RT2573_PHY_CSR3);
1528 if (!(val & RT2573_BBP_BUSY))
1529 return val & 0xff;
1530 DELAY(1);
1533 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1534 return 0;
1537 Static void
1538 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1540 uint32_t tmp;
1541 int ntries;
1543 for (ntries = 0; ntries < 5; ntries++) {
1544 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1545 break;
1547 if (ntries == 5) {
1548 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1549 return;
1552 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1553 (reg & 3);
1554 rum_write(sc, RT2573_PHY_CSR4, tmp);
1556 /* remember last written value in sc */
1557 sc->rf_regs[reg] = val;
1559 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1562 Static void
1563 rum_select_antenna(struct rum_softc *sc)
1565 uint8_t bbp4, bbp77;
1566 uint32_t tmp;
1568 bbp4 = rum_bbp_read(sc, 4);
1569 bbp77 = rum_bbp_read(sc, 77);
1571 /* TBD */
1573 /* make sure Rx is disabled before switching antenna */
1574 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1575 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1577 rum_bbp_write(sc, 4, bbp4);
1578 rum_bbp_write(sc, 77, bbp77);
1580 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1584 * Enable multi-rate retries for frames sent at OFDM rates.
1585 * In 802.11b/g mode, allow fallback to CCK rates.
1587 Static void
1588 rum_enable_mrr(struct rum_softc *sc)
1590 struct ieee80211com *ic = &sc->sc_ic;
1591 uint32_t tmp;
1593 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1595 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1596 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1597 tmp |= RT2573_MRR_CCK_FALLBACK;
1598 tmp |= RT2573_MRR_ENABLED;
1600 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1603 Static void
1604 rum_set_txpreamble(struct rum_softc *sc)
1606 uint32_t tmp;
1608 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1610 tmp &= ~RT2573_SHORT_PREAMBLE;
1611 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1612 tmp |= RT2573_SHORT_PREAMBLE;
1614 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1617 Static void
1618 rum_set_basicrates(struct rum_softc *sc)
1620 struct ieee80211com *ic = &sc->sc_ic;
1622 /* update basic rate set */
1623 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1624 /* 11b basic rates: 1, 2Mbps */
1625 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1626 } else if (ic->ic_curmode == IEEE80211_MODE_11A) {
1627 /* 11a basic rates: 6, 12, 24Mbps */
1628 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1629 } else {
1630 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1631 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1636 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1637 * driver.
1639 Static void
1640 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1642 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1643 uint32_t tmp;
1645 /* update all BBP registers that depend on the band */
1646 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1647 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1648 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1649 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1650 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1652 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1653 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1654 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1657 sc->bbp17 = bbp17;
1658 rum_bbp_write(sc, 17, bbp17);
1659 rum_bbp_write(sc, 96, bbp96);
1660 rum_bbp_write(sc, 104, bbp104);
1662 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1663 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1664 rum_bbp_write(sc, 75, 0x80);
1665 rum_bbp_write(sc, 86, 0x80);
1666 rum_bbp_write(sc, 88, 0x80);
1669 rum_bbp_write(sc, 35, bbp35);
1670 rum_bbp_write(sc, 97, bbp97);
1671 rum_bbp_write(sc, 98, bbp98);
1673 tmp = rum_read(sc, RT2573_PHY_CSR0);
1674 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1675 if (IEEE80211_IS_CHAN_2GHZ(c))
1676 tmp |= RT2573_PA_PE_2GHZ;
1677 else
1678 tmp |= RT2573_PA_PE_5GHZ;
1679 rum_write(sc, RT2573_PHY_CSR0, tmp);
1681 /* 802.11a uses a 16 microseconds short interframe space */
1682 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1685 Static void
1686 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1688 struct ieee80211com *ic = &sc->sc_ic;
1689 const struct rfprog *rfprog;
1690 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1691 int8_t power;
1692 u_int i, chan;
1694 chan = ieee80211_chan2ieee(ic, c);
1695 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1696 return;
1698 /* select the appropriate RF settings based on what EEPROM says */
1699 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1700 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1702 /* find the settings for this channel (we know it exists) */
1703 for (i = 0; rfprog[i].chan != chan; i++);
1705 power = sc->txpow[i];
1706 if (power < 0) {
1707 bbp94 += power;
1708 power = 0;
1709 } else if (power > 31) {
1710 bbp94 += power - 31;
1711 power = 31;
1715 * If we are switching from the 2GHz band to the 5GHz band or
1716 * vice-versa, BBP registers need to be reprogrammed.
1718 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1719 rum_select_band(sc, c);
1720 rum_select_antenna(sc);
1722 ic->ic_curchan = c;
1724 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1725 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1726 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1727 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1729 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1730 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1731 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1732 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1734 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1735 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1736 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1737 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1739 DELAY(10);
1741 /* enable smart mode for MIMO-capable RFs */
1742 bbp3 = rum_bbp_read(sc, 3);
1744 bbp3 &= ~RT2573_SMART_MODE;
1745 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1746 bbp3 |= RT2573_SMART_MODE;
1748 rum_bbp_write(sc, 3, bbp3);
1750 if (bbp94 != RT2573_BBPR94_DEFAULT)
1751 rum_bbp_write(sc, 94, bbp94);
1755 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1756 * and HostAP operating modes.
1758 Static void
1759 rum_enable_tsf_sync(struct rum_softc *sc)
1761 struct ieee80211com *ic = &sc->sc_ic;
1762 uint32_t tmp;
1764 if (ic->ic_opmode != IEEE80211_M_STA) {
1766 * Change default 16ms TBTT adjustment to 8ms.
1767 * Must be done before enabling beacon generation.
1769 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1772 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1774 /* set beacon interval (in 1/16ms unit) */
1775 tmp |= ic->ic_bss->ni_intval * 16;
1777 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1778 if (ic->ic_opmode == IEEE80211_M_STA)
1779 tmp |= RT2573_TSF_MODE(1);
1780 else
1781 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1783 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1786 Static void
1787 rum_update_slot(struct rum_softc *sc)
1789 struct ieee80211com *ic = &sc->sc_ic;
1790 uint8_t slottime;
1791 uint32_t tmp;
1793 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1795 tmp = rum_read(sc, RT2573_MAC_CSR9);
1796 tmp = (tmp & ~0xff) | slottime;
1797 rum_write(sc, RT2573_MAC_CSR9, tmp);
1799 DPRINTF(("setting slot time to %uus\n", slottime));
1802 Static void
1803 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1805 uint32_t tmp;
1807 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1808 rum_write(sc, RT2573_MAC_CSR4, tmp);
1810 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1811 rum_write(sc, RT2573_MAC_CSR5, tmp);
1814 Static void
1815 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1817 uint32_t tmp;
1819 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1820 rum_write(sc, RT2573_MAC_CSR2, tmp);
1822 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1823 rum_write(sc, RT2573_MAC_CSR3, tmp);
1826 Static void
1827 rum_update_promisc(struct rum_softc *sc)
1829 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1830 uint32_t tmp;
1832 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1834 tmp &= ~RT2573_DROP_NOT_TO_ME;
1835 if (!(ifp->if_flags & IFF_PROMISC))
1836 tmp |= RT2573_DROP_NOT_TO_ME;
1838 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1840 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1841 "entering" : "leaving"));
1844 Static const char *
1845 rum_get_rf(int rev)
1847 switch (rev) {
1848 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1849 case RT2573_RF_2528: return "RT2528";
1850 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1851 case RT2573_RF_5226: return "RT5226";
1852 default: return "unknown";
1856 Static void
1857 rum_read_eeprom(struct rum_softc *sc)
1859 struct ieee80211com *ic = &sc->sc_ic;
1860 uint16_t val;
1861 #ifdef RUM_DEBUG
1862 int i;
1863 #endif
1865 /* read MAC/BBP type */
1866 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1867 sc->macbbp_rev = le16toh(val);
1869 /* read MAC address */
1870 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1872 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1873 val = le16toh(val);
1874 sc->rf_rev = (val >> 11) & 0x1f;
1875 sc->hw_radio = (val >> 10) & 0x1;
1876 sc->rx_ant = (val >> 4) & 0x3;
1877 sc->tx_ant = (val >> 2) & 0x3;
1878 sc->nb_ant = val & 0x3;
1880 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1882 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1883 val = le16toh(val);
1884 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1885 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1887 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1888 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1890 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1891 val = le16toh(val);
1892 if ((val & 0xff) != 0xff)
1893 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1895 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1896 val = le16toh(val);
1897 if ((val & 0xff) != 0xff)
1898 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1900 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1901 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1903 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1904 val = le16toh(val);
1905 if ((val & 0xff) != 0xff)
1906 sc->rffreq = val & 0xff;
1908 DPRINTF(("RF freq=%d\n", sc->rffreq));
1910 /* read Tx power for all a/b/g channels */
1911 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1912 /* XXX default Tx power for 802.11a channels */
1913 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1914 #ifdef RUM_DEBUG
1915 for (i = 0; i < 14; i++)
1916 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1917 #endif
1919 /* read default values for BBP registers */
1920 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1921 #ifdef RUM_DEBUG
1922 for (i = 0; i < 14; i++) {
1923 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1924 continue;
1925 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1926 sc->bbp_prom[i].val));
1928 #endif
1931 Static int
1932 rum_bbp_init(struct rum_softc *sc)
1934 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1935 int i, ntries;
1936 uint8_t val;
1938 /* wait for BBP to be ready */
1939 for (ntries = 0; ntries < 100; ntries++) {
1940 val = rum_bbp_read(sc, 0);
1941 if (val != 0 && val != 0xff)
1942 break;
1943 DELAY(1000);
1945 if (ntries == 100) {
1946 printf("%s: timeout waiting for BBP\n",
1947 USBDEVNAME(sc->sc_dev));
1948 return EIO;
1951 /* initialize BBP registers to default values */
1952 for (i = 0; i < N(rum_def_bbp); i++)
1953 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1955 /* write vendor-specific BBP values (from EEPROM) */
1956 for (i = 0; i < 16; i++) {
1957 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1958 continue;
1959 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1962 return 0;
1963 #undef N
1966 Static int
1967 rum_init(struct ifnet *ifp)
1969 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1970 struct rum_softc *sc = ifp->if_softc;
1971 struct ieee80211com *ic = &sc->sc_ic;
1972 struct rum_rx_data *data;
1973 uint32_t tmp;
1974 usbd_status error = 0;
1975 int i, ntries;
1977 if ((sc->sc_flags & RT2573_FWLOADED) == 0) {
1978 if (rum_attachhook(sc))
1979 goto fail;
1982 rum_stop(ifp, 0);
1984 /* initialize MAC registers to default values */
1985 for (i = 0; i < N(rum_def_mac); i++)
1986 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1988 /* set host ready */
1989 rum_write(sc, RT2573_MAC_CSR1, 3);
1990 rum_write(sc, RT2573_MAC_CSR1, 0);
1992 /* wait for BBP/RF to wakeup */
1993 for (ntries = 0; ntries < 1000; ntries++) {
1994 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1995 break;
1996 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1997 DELAY(1000);
1999 if (ntries == 1000) {
2000 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2001 USBDEVNAME(sc->sc_dev));
2002 goto fail;
2005 if ((error = rum_bbp_init(sc)) != 0)
2006 goto fail;
2008 /* select default channel */
2009 rum_select_band(sc, ic->ic_curchan);
2010 rum_select_antenna(sc);
2011 rum_set_chan(sc, ic->ic_curchan);
2013 /* clear STA registers */
2014 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2016 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2017 rum_set_macaddr(sc, ic->ic_myaddr);
2019 /* initialize ASIC */
2020 rum_write(sc, RT2573_MAC_CSR1, 4);
2023 * Allocate xfer for AMRR statistics requests.
2025 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2026 if (sc->amrr_xfer == NULL) {
2027 printf("%s: could not allocate AMRR xfer\n",
2028 USBDEVNAME(sc->sc_dev));
2029 goto fail;
2033 * Open Tx and Rx USB bulk pipes.
2035 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2036 &sc->sc_tx_pipeh);
2037 if (error != 0) {
2038 printf("%s: could not open Tx pipe: %s\n",
2039 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2040 goto fail;
2043 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2044 &sc->sc_rx_pipeh);
2045 if (error != 0) {
2046 printf("%s: could not open Rx pipe: %s\n",
2047 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2048 goto fail;
2052 * Allocate Tx and Rx xfer queues.
2054 error = rum_alloc_tx_list(sc);
2055 if (error != 0) {
2056 printf("%s: could not allocate Tx list\n",
2057 USBDEVNAME(sc->sc_dev));
2058 goto fail;
2061 error = rum_alloc_rx_list(sc);
2062 if (error != 0) {
2063 printf("%s: could not allocate Rx list\n",
2064 USBDEVNAME(sc->sc_dev));
2065 goto fail;
2069 * Start up the receive pipe.
2071 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2072 data = &sc->rx_data[i];
2074 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2075 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2076 error = usbd_transfer(data->xfer);
2077 if (error != USBD_NORMAL_COMPLETION &&
2078 error != USBD_IN_PROGRESS) {
2079 printf("%s: could not queue Rx transfer\n",
2080 USBDEVNAME(sc->sc_dev));
2081 goto fail;
2085 /* update Rx filter */
2086 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2088 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2089 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2090 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2091 RT2573_DROP_ACKCTS;
2092 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2093 tmp |= RT2573_DROP_TODS;
2094 if (!(ifp->if_flags & IFF_PROMISC))
2095 tmp |= RT2573_DROP_NOT_TO_ME;
2097 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2099 ifp->if_flags &= ~IFF_OACTIVE;
2100 ifp->if_flags |= IFF_RUNNING;
2102 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2103 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2104 else
2105 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2107 return 0;
2109 fail: rum_stop(ifp, 1);
2110 return error;
2111 #undef N
2114 Static void
2115 rum_stop(struct ifnet *ifp, int disable)
2117 struct rum_softc *sc = ifp->if_softc;
2118 struct ieee80211com *ic = &sc->sc_ic;
2119 uint32_t tmp;
2121 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2123 sc->sc_tx_timer = 0;
2124 ifp->if_timer = 0;
2125 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2127 /* disable Rx */
2128 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2129 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2131 /* reset ASIC */
2132 rum_write(sc, RT2573_MAC_CSR1, 3);
2133 rum_write(sc, RT2573_MAC_CSR1, 0);
2135 if (sc->sc_rx_pipeh != NULL) {
2136 usbd_abort_pipe(sc->sc_rx_pipeh);
2137 usbd_close_pipe(sc->sc_rx_pipeh);
2138 sc->sc_rx_pipeh = NULL;
2141 if (sc->sc_tx_pipeh != NULL) {
2142 usbd_abort_pipe(sc->sc_tx_pipeh);
2143 usbd_close_pipe(sc->sc_tx_pipeh);
2144 sc->sc_tx_pipeh = NULL;
2147 rum_free_rx_list(sc);
2148 rum_free_tx_list(sc);
2151 Static int
2152 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2154 usb_device_request_t req;
2155 uint16_t reg = RT2573_MCU_CODE_BASE;
2156 usbd_status error;
2158 /* copy firmware image into NIC */
2159 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2160 rum_write(sc, reg, UGETDW(ucode));
2162 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2163 req.bRequest = RT2573_MCU_CNTL;
2164 USETW(req.wValue, RT2573_MCU_RUN);
2165 USETW(req.wIndex, 0);
2166 USETW(req.wLength, 0);
2168 error = usbd_do_request(sc->sc_udev, &req, NULL);
2169 if (error != 0) {
2170 printf("%s: could not run firmware: %s\n",
2171 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2173 return error;
2176 Static int
2177 rum_prepare_beacon(struct rum_softc *sc)
2179 struct ieee80211com *ic = &sc->sc_ic;
2180 struct rum_tx_desc desc;
2181 struct mbuf *m0;
2182 int rate;
2184 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2185 if (m0 == NULL) {
2186 aprint_error_dev(sc->sc_dev,
2187 "could not allocate beacon frame\n");
2188 return ENOBUFS;
2191 /* send beacons at the lowest available rate */
2192 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2194 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2195 m0->m_pkthdr.len, rate);
2197 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2198 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2200 /* copy beacon header and payload into NIC memory */
2201 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2202 m0->m_pkthdr.len);
2204 m_freem(m0);
2206 return 0;
2209 Static void
2210 rum_newassoc(struct ieee80211_node *ni, int isnew)
2212 /* start with lowest Tx rate */
2213 ni->ni_txrate = 0;
2216 Static void
2217 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2219 int i;
2221 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2222 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2224 ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2226 /* set rate to some reasonable initial value */
2227 for (i = ni->ni_rates.rs_nrates - 1;
2228 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2229 i--);
2230 ni->ni_txrate = i;
2232 usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc);
2235 Static void
2236 rum_amrr_timeout(void *arg)
2238 struct rum_softc *sc = arg;
2239 usb_device_request_t req;
2242 * Asynchronously read statistic registers (cleared by read).
2244 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2245 req.bRequest = RT2573_READ_MULTI_MAC;
2246 USETW(req.wValue, 0);
2247 USETW(req.wIndex, RT2573_STA_CSR0);
2248 USETW(req.wLength, sizeof sc->sta);
2250 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2251 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2252 rum_amrr_update);
2253 (void)usbd_transfer(sc->amrr_xfer);
2256 Static void
2257 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2258 usbd_status status)
2260 struct rum_softc *sc = (struct rum_softc *)priv;
2261 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2263 if (status != USBD_NORMAL_COMPLETION) {
2264 printf("%s: could not retrieve Tx statistics - cancelling "
2265 "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2266 return;
2269 /* count TX retry-fail as Tx errors */
2270 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2272 sc->amn.amn_retrycnt =
2273 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2274 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2275 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2277 sc->amn.amn_txcnt =
2278 sc->amn.amn_retrycnt +
2279 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2281 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2283 usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc);
2287 rum_activate(device_ptr_t self, enum devact act)
2289 switch (act) {
2290 case DVACT_DEACTIVATE:
2291 /*if_deactivate(&sc->sc_ic.ic_if);*/
2292 return 0;
2293 default:
2294 return EOPNOTSUPP;