69160c9ee7f2e345009a116346255b5104af8d7c
[dragonfly.git] / sys / dev / netif / rum / if_rum.c
1 /*      $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $      */
2 /*      $DragonFly: src/sys/dev/netif/rum/if_rum.c,v 1.16 2007/07/01 21:24:02 hasso Exp $       */
3
4 /*-
5  * Copyright (c) 2005, 2006 Damien Bergamini <damien.bergamini@free.fr>
6  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 /*-
22  * Ralink Technology RT2501USB/RT2601USB chipset driver
23  * http://www.ralinktech.com/
24  */
25
26 #include <sys/param.h>
27 #include <sys/bus.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31 #include <sys/mbuf.h>
32 #include <sys/rman.h>
33 #include <sys/serialize.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36
37 #include <net/bpf.h>
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/ifq_var.h>
44
45 #include <netproto/802_11/ieee80211_var.h>
46 #include <netproto/802_11/ieee80211_radiotap.h>
47 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
48
49 #include <bus/usb/usb.h>
50 #include <bus/usb/usbdi.h>
51 #include <bus/usb/usbdi_util.h>
52 #include <bus/usb/usbdevs.h>
53
54 #include "if_rumreg.h"
55 #include "if_rumvar.h"
56 #include "rum_ucode.h"
57
58 #ifdef USB_DEBUG
59 #define RUM_DEBUG
60 #endif
61
62 #ifdef RUM_DEBUG
63 #define DPRINTF(x)      do { if (rum_debug) kprintf x; } while (0)
64 #define DPRINTFN(n, x)  do { if (rum_debug >= (n)) kprintf x; } while (0)
65 int rum_debug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n, x)
69 #endif
70
71 /* various supported device vendors/products */
72 static const struct usb_devno rum_devs[] = {
73         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_HWU54DM },
74         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573 },
75         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_1 },
76         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_2 },
77         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_WUG2700 },
78         { USB_VENDOR_AMIT,              USB_PRODUCT_AMIT_CGWLUSB2GO },
79         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_WL167G_2 },
80         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_WL167G_3 },
81         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D7050A },
82         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D9050V3 },
83         { USB_VENDOR_LINKSYS4,          USB_PRODUCT_LINKSYS4_WUSB54GC },
84         { USB_VENDOR_LINKSYS4,          USB_PRODUCT_LINKSYS4_WUSB54GR },
85         { USB_VENDOR_CONCEPTRONIC,      USB_PRODUCT_CONCEPTRONIC_C54RU2 },
86         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_CWD854F },
87         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_RT2573 },
88         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_DWLG122C1 },
89         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_WUA1340 },
90         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWB01GS },
91         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWI05GS },
92         { USB_VENDOR_GIGASET,           USB_PRODUCT_GIGASET_RT2573 },
93         { USB_VENDOR_GOODWAY,           USB_PRODUCT_GOODWAY_RT2573 },
94         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
95         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
96         { USB_VENDOR_HUAWEI3COM,        USB_PRODUCT_HUAWEI3COM_RT2573 },
97         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_G54HP },
98         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_SG54HP },
99         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573 },
100         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_1 },
101         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_2 },
102         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_3 },
103         { USB_VENDOR_NOVATECH,          USB_PRODUCT_NOVATECH_RT2573 },
104         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54HP },
105         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54MINI2 },
106         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUSMM },
107         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573 },
108         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573_2 },
109         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573 },
110         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2671 },
111         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573_2 },
112         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL113R2 },
113         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL172 },
114         { USB_VENDOR_SURECOM,           USB_PRODUCT_SURECOM_RT2573 }
115 };
116
117 static int              rum_alloc_tx_list(struct rum_softc *);
118 static void             rum_free_tx_list(struct rum_softc *);
119 static int              rum_alloc_rx_list(struct rum_softc *);
120 static void             rum_free_rx_list(struct rum_softc *);
121 static int              rum_media_change(struct ifnet *);
122 static void             rum_next_scan(void *);
123 static void             rum_task(void *);
124 static int              rum_newstate(struct ieee80211com *,
125                             enum ieee80211_state, int);
126 static void             rum_txeof(usbd_xfer_handle, usbd_private_handle,
127                             usbd_status);
128 static void             rum_rxeof(usbd_xfer_handle, usbd_private_handle,
129                             usbd_status);
130 static uint8_t          rum_rxrate(struct rum_rx_desc *);
131 static uint8_t          rum_plcp_signal(int);
132 static void             rum_setup_tx_desc(struct rum_softc *,
133                             struct rum_tx_desc *, uint32_t, uint16_t, int,
134                             int);
135 static int              rum_tx_data(struct rum_softc *, struct mbuf *,
136                             struct ieee80211_node *);
137 static void             rum_start(struct ifnet *);
138 static void             rum_watchdog(struct ifnet *);
139 static int              rum_ioctl(struct ifnet *, u_long, caddr_t,
140                                   struct ucred *);
141 static void             rum_eeprom_read(struct rum_softc *, uint16_t, void *,
142                             int);
143 static uint32_t         rum_read(struct rum_softc *, uint16_t);
144 static void             rum_read_multi(struct rum_softc *, uint16_t, void *,
145                             int);
146 static void             rum_write(struct rum_softc *, uint16_t, uint32_t);
147 static void             rum_write_multi(struct rum_softc *, uint16_t, void *,
148                             size_t);
149 static void             rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
150 static uint8_t          rum_bbp_read(struct rum_softc *, uint8_t);
151 static void             rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
152 static void             rum_select_antenna(struct rum_softc *);
153 static void             rum_enable_mrr(struct rum_softc *);
154 static void             rum_set_txpreamble(struct rum_softc *);
155 static void             rum_set_basicrates(struct rum_softc *);
156 static void             rum_select_band(struct rum_softc *,
157                             struct ieee80211_channel *);
158 static void             rum_set_chan(struct rum_softc *,
159                             struct ieee80211_channel *);
160 static void             rum_enable_tsf_sync(struct rum_softc *);
161 static void             rum_update_slot(struct rum_softc *);
162 static void             rum_set_bssid(struct rum_softc *, const uint8_t *);
163 static void             rum_set_macaddr(struct rum_softc *, const uint8_t *);
164 static void             rum_update_promisc(struct rum_softc *);
165 static const char       *rum_get_rf(int);
166 static void             rum_read_eeprom(struct rum_softc *);
167 static int              rum_bbp_init(struct rum_softc *);
168 static void             rum_init(void *);
169 static void             rum_stop(struct rum_softc *);
170 static int              rum_load_microcode(struct rum_softc *, const uint8_t *,
171                             size_t);
172 static int              rum_prepare_beacon(struct rum_softc *);
173
174 static void             rum_stats_timeout(void *);
175 static void             rum_stats_update(usbd_xfer_handle, usbd_private_handle,
176                                          usbd_status);
177 static void             rum_stats(struct ieee80211com *,
178                                   struct ieee80211_node *,
179                                   struct ieee80211_ratectl_stats *);
180 static void             rum_ratectl_change(struct ieee80211com *ic, u_int,
181                                            u_int);
182 static int              rum_get_rssi(struct rum_softc *, uint8_t);
183
184 /*
185  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
186  */
187 static const struct ieee80211_rateset rum_rateset_11a =
188         { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
189
190 static const struct ieee80211_rateset rum_rateset_11b =
191         { 4, { 2, 4, 11, 22 } };
192
193 static const struct ieee80211_rateset rum_rateset_11g =
194         { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
195
196 static const struct {
197         uint32_t        reg;
198         uint32_t        val;
199 } rum_def_mac[] = {
200         RT2573_DEF_MAC
201 };
202
203 static const struct {
204         uint8_t reg;
205         uint8_t val;
206 } rum_def_bbp[] = {
207         RT2573_DEF_BBP
208 };
209
210 static const struct rfprog {
211         uint8_t         chan;
212         uint32_t        r1, r2, r3, r4;
213 }  rum_rf5226[] = {
214         RT2573_RF5226
215 }, rum_rf5225[] = {
216         RT2573_RF5225
217 };
218
219 USB_DECLARE_DRIVER(rum);
220 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);
221
222 static int
223 rum_match(device_t self)
224 {
225         struct usb_attach_arg *uaa = device_get_ivars(self);
226
227         if (uaa->iface != NULL)
228                 return UMATCH_NONE;
229
230         return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
231             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
232 }
233
234 static int
235 rum_attach(device_t self)
236 {
237         struct rum_softc *sc = device_get_softc(self);
238         struct usb_attach_arg *uaa = device_get_ivars(self);
239         struct ieee80211com *ic = &sc->sc_ic;
240         struct ifnet *ifp = &ic->ic_if;
241         usb_interface_descriptor_t *id;
242         usb_endpoint_descriptor_t *ed;
243         usbd_status error;
244         char devinfo[1024];
245         int i, ntries;
246         uint32_t tmp;
247
248         sc->sc_udev = uaa->device;
249
250         usbd_devinfo(uaa->device, 0, devinfo);
251         sc->sc_dev = self;
252         device_set_desc_copy(self, devinfo);
253
254         if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
255                 kprintf("%s: could not set configuration no\n",
256                     device_get_nameunit(sc->sc_dev));
257                 return ENXIO;
258         }
259
260         /* get the first interface handle */
261         error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
262             &sc->sc_iface);
263         if (error != 0) {
264                 kprintf("%s: could not get interface handle\n",
265                     device_get_nameunit(sc->sc_dev));
266                 return ENXIO;
267         }
268
269         /*
270          * Find endpoints.
271          */
272         id = usbd_get_interface_descriptor(sc->sc_iface);
273
274         sc->sc_rx_no = sc->sc_tx_no = -1;
275         for (i = 0; i < id->bNumEndpoints; i++) {
276                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
277                 if (ed == NULL) {
278                         kprintf("%s: no endpoint descriptor for iface %d\n",
279                             device_get_nameunit(sc->sc_dev), i);
280                         return ENXIO;
281                 }
282
283                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
284                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
285                         sc->sc_rx_no = ed->bEndpointAddress;
286                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
287                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
288                         sc->sc_tx_no = ed->bEndpointAddress;
289         }
290         if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
291                 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
292                 return ENXIO;
293         }
294
295         usb_init_task(&sc->sc_task, rum_task, sc);
296
297         callout_init(&sc->scan_ch);
298         callout_init(&sc->stats_ch);
299
300         /* retrieve RT2573 rev. no */
301         for (ntries = 0; ntries < 1000; ntries++) {
302                 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
303                         break;
304                 DELAY(1000);
305         }
306         if (ntries == 1000) {
307                 kprintf("%s: timeout waiting for chip to settle\n",
308                     device_get_nameunit(sc->sc_dev));
309                 return ENXIO;
310         }
311
312         /* retrieve MAC address and various other things from EEPROM */
313         rum_read_eeprom(sc);
314
315         kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %6D\n",
316             device_get_nameunit(sc->sc_dev), sc->macbbp_rev, tmp,
317             rum_get_rf(sc->rf_rev), ic->ic_myaddr, ":");
318
319         error = rum_load_microcode(sc, rt2573, sizeof(rt2573));
320         if (error != 0) {
321                 device_printf(self, "can't load microcode\n");
322                 return ENXIO;
323         }
324
325         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
326         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
327         ic->ic_state = IEEE80211_S_INIT;
328
329         /* set device capabilities */
330         ic->ic_caps =
331             IEEE80211_C_IBSS |          /* IBSS mode supported */
332             IEEE80211_C_MONITOR |       /* monitor mode supported */
333             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
334             IEEE80211_C_TXPMGT |        /* tx power management */
335             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
336             IEEE80211_C_SHSLOT |        /* short slot time supported */
337             IEEE80211_C_WPA;            /* WPA 1+2 */
338
339         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
340                 /* set supported .11a rates */
341                 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
342
343                 /* set supported .11a channels */
344                 for (i = 34; i <= 46; i += 4) {
345                         ic->ic_channels[i].ic_freq =
346                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
347                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
348                 }
349                 for (i = 36; i <= 64; i += 4) {
350                         ic->ic_channels[i].ic_freq =
351                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
352                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
353                 }
354                 for (i = 100; i <= 140; i += 4) {
355                         ic->ic_channels[i].ic_freq =
356                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
357                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
358                 }
359                 for (i = 149; i <= 165; i += 4) {
360                         ic->ic_channels[i].ic_freq =
361                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
362                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
363                 }
364         }
365
366         /* set supported .11b and .11g rates */
367         ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
368         ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
369
370         /* set supported .11b and .11g channels (1 through 14) */
371         for (i = 1; i <= 14; i++) {
372                 ic->ic_channels[i].ic_freq =
373                     ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
374                 ic->ic_channels[i].ic_flags =
375                     IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
376                     IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
377         }
378
379         sc->sc_sifs = IEEE80211_DUR_SIFS;       /* Default SIFS */
380
381         if_initname(ifp, device_get_name(self), device_get_unit(self));
382         ifp->if_softc = sc;
383         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
384         ifp->if_init = rum_init;
385         ifp->if_ioctl = rum_ioctl;
386         ifp->if_start = rum_start;
387         ifp->if_watchdog = rum_watchdog;
388         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
389         ifq_set_ready(&ifp->if_snd);
390
391         ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
392         ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
393         ic->ic_ratectl.rc_st_valid_stats =
394                 IEEE80211_RATECTL_STATS_PKT_NORETRY |
395                 IEEE80211_RATECTL_STATS_PKT_OK |
396                 IEEE80211_RATECTL_STATS_PKT_ERR |
397                 IEEE80211_RATECTL_STATS_RETRIES;
398         ic->ic_ratectl.rc_st_stats = rum_stats;
399         ic->ic_ratectl.rc_st_change = rum_ratectl_change;
400
401         ieee80211_ifattach(ic);
402
403         /* Enable software beacon missing handling. */
404         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
405
406         /* override state transition machine */
407         sc->sc_newstate = ic->ic_newstate;
408         ic->ic_newstate = rum_newstate;
409         ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
410
411         bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
412             sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
413             &sc->sc_drvbpf);
414
415         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
416         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
417         sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
418
419         sc->sc_txtap_len = sizeof sc->sc_txtapu;
420         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
421         sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
422
423         if (bootverbose)
424                 ieee80211_announce(ic);
425
426         return 0;
427 }
428
429 static int
430 rum_detach(device_t self)
431 {
432         struct rum_softc *sc = device_get_softc(self);
433         struct ifnet *ifp = &sc->sc_ic.ic_if;
434 #ifdef INVARIANTS
435         int i;
436 #endif
437
438         crit_enter();
439
440         callout_stop(&sc->scan_ch);
441         callout_stop(&sc->stats_ch);
442
443         lwkt_serialize_enter(ifp->if_serializer);
444         rum_stop(sc);
445         lwkt_serialize_exit(ifp->if_serializer);
446
447         usb_rem_task(sc->sc_udev, &sc->sc_task);
448
449         bpfdetach(ifp);
450         ieee80211_ifdetach(&sc->sc_ic); /* free all nodes */
451
452         crit_exit();
453
454         KKASSERT(sc->stats_xfer == NULL);
455         KKASSERT(sc->sc_rx_pipeh == NULL);
456         KKASSERT(sc->sc_tx_pipeh == NULL);
457
458 #ifdef INVARIANTS
459         /*
460          * Make sure TX/RX list is empty
461          */
462         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
463                 struct rum_tx_data *data = &sc->tx_data[i];
464
465                 KKASSERT(data->xfer == NULL);
466                 KKASSERT(data->ni == NULL);
467                 KKASSERT(data->m == NULL);
468         }
469         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
470                 struct rum_rx_data *data = &sc->rx_data[i];
471
472                 KKASSERT(data->xfer == NULL);
473                 KKASSERT(data->m == NULL);
474         }
475 #endif
476         return 0;
477 }
478
479 static int
480 rum_alloc_tx_list(struct rum_softc *sc)
481 {
482         int i;
483
484         sc->tx_queued = 0;
485         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
486                 struct rum_tx_data *data = &sc->tx_data[i];
487
488                 data->sc = sc;
489
490                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
491                 if (data->xfer == NULL) {
492                         kprintf("%s: could not allocate tx xfer\n",
493                             device_get_nameunit(sc->sc_dev));
494                         return ENOMEM;
495                 }
496
497                 data->buf = usbd_alloc_buffer(data->xfer,
498                     RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
499                 if (data->buf == NULL) {
500                         kprintf("%s: could not allocate tx buffer\n",
501                             device_get_nameunit(sc->sc_dev));
502                         return ENOMEM;
503                 }
504
505                 /* clean Tx descriptor */
506                 bzero(data->buf, RT2573_TX_DESC_SIZE);
507         }
508         return 0;
509 }
510
511 static void
512 rum_free_tx_list(struct rum_softc *sc)
513 {
514         int i;
515
516         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
517                 struct rum_tx_data *data = &sc->tx_data[i];
518
519                 if (data->xfer != NULL) {
520                         usbd_free_xfer(data->xfer);
521                         data->xfer = NULL;
522                 }
523                 if (data->ni != NULL) {
524                         ieee80211_free_node(data->ni);
525                         data->ni = NULL;
526                 }
527                 if (data->m != NULL) {
528                         m_freem(data->m);
529                         data->m = NULL;
530                 }
531         }
532         sc->tx_queued = 0;
533 }
534
535 static int
536 rum_alloc_rx_list(struct rum_softc *sc)
537 {
538         int i;
539
540         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
541                 struct rum_rx_data *data = &sc->rx_data[i];
542
543                 data->sc = sc;
544
545                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
546                 if (data->xfer == NULL) {
547                         kprintf("%s: could not allocate rx xfer\n",
548                             device_get_nameunit(sc->sc_dev));
549                         return ENOMEM;
550                 }
551
552                 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
553                         kprintf("%s: could not allocate rx buffer\n",
554                             device_get_nameunit(sc->sc_dev));
555                         return ENOMEM;
556                 }
557
558                 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
559
560                 data->buf = mtod(data->m, uint8_t *);
561                 bzero(data->buf, sizeof(struct rum_rx_desc));
562         }
563         return 0;
564 }
565
566 static void
567 rum_free_rx_list(struct rum_softc *sc)
568 {
569         int i;
570
571         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
572                 struct rum_rx_data *data = &sc->rx_data[i];
573
574                 if (data->xfer != NULL) {
575                         usbd_free_xfer(data->xfer);
576                         data->xfer = NULL;
577                 }
578                 if (data->m != NULL) {
579                         m_freem(data->m);
580                         data->m = NULL;
581                 }
582         }
583 }
584
585 static int
586 rum_media_change(struct ifnet *ifp)
587 {
588         int error;
589
590         error = ieee80211_media_change(ifp);
591         if (error != ENETRESET)
592                 return error;
593
594         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
595                 rum_init(ifp->if_softc);
596
597         return 0;
598 }
599
600 /*
601  * This function is called periodically (every 200ms) during scanning to
602  * switch from one channel to another.
603  */
604 static void
605 rum_next_scan(void *arg)
606 {
607         struct rum_softc *sc = arg;
608         struct ieee80211com *ic = &sc->sc_ic;
609         struct ifnet *ifp = &ic->ic_if;
610
611         if (sc->sc_stopped)
612                 return;
613
614         crit_enter();
615
616         if (ic->ic_state == IEEE80211_S_SCAN) {
617                 lwkt_serialize_enter(ifp->if_serializer);
618                 ieee80211_next_scan(ic);
619                 lwkt_serialize_exit(ifp->if_serializer);
620         }
621
622         crit_exit();
623 }
624
625 static void
626 rum_task(void *xarg)
627 {
628         struct rum_softc *sc = xarg;
629         struct ieee80211com *ic = &sc->sc_ic;
630         struct ifnet *ifp = &ic->ic_if;
631         enum ieee80211_state nstate;
632         struct ieee80211_node *ni;
633         int arg;
634
635         if (sc->sc_stopped)
636                 return;
637
638         crit_enter();
639
640         nstate = sc->sc_state;
641         arg = sc->sc_arg;
642
643         KASSERT(nstate != IEEE80211_S_INIT,
644                 ("->INIT state transition should not be defered\n"));
645         rum_set_chan(sc, ic->ic_curchan);
646
647         switch (nstate) {
648         case IEEE80211_S_RUN:
649                 ni = ic->ic_bss;
650
651                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
652                         rum_update_slot(sc);
653                         rum_enable_mrr(sc);
654                         rum_set_txpreamble(sc);
655                         rum_set_basicrates(sc);
656                         rum_set_bssid(sc, ni->ni_bssid);
657                 }
658
659                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
660                     ic->ic_opmode == IEEE80211_M_IBSS)
661                         rum_prepare_beacon(sc);
662
663                 if (ic->ic_opmode != IEEE80211_M_MONITOR)
664                         rum_enable_tsf_sync(sc);
665
666                 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
667                 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
668                 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
669                 break;
670
671         case IEEE80211_S_SCAN:
672                 callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
673                 break;
674
675         default:
676                 break;
677         }
678
679         lwkt_serialize_enter(ifp->if_serializer);
680         ieee80211_ratectl_newstate(ic, nstate);
681         sc->sc_newstate(ic, nstate, arg);
682         lwkt_serialize_exit(ifp->if_serializer);
683
684         crit_exit();
685 }
686
687 static int
688 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
689 {
690         struct rum_softc *sc = ic->ic_if.if_softc;
691         struct ifnet *ifp = &ic->ic_if;
692
693         crit_enter();
694
695         ASSERT_SERIALIZED(ifp->if_serializer);
696
697         callout_stop(&sc->scan_ch);
698         callout_stop(&sc->stats_ch);
699
700         /* do it in a process context */
701         sc->sc_state = nstate;
702         sc->sc_arg = arg;
703
704         lwkt_serialize_exit(ifp->if_serializer);
705         usb_rem_task(sc->sc_udev, &sc->sc_task);
706
707         if (nstate == IEEE80211_S_INIT) {
708                 lwkt_serialize_enter(ifp->if_serializer);
709                 ieee80211_ratectl_newstate(ic, nstate);
710                 sc->sc_newstate(ic, nstate, arg);
711         } else {
712                 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
713                 lwkt_serialize_enter(ifp->if_serializer);
714         }
715
716         crit_exit();
717         return 0;
718 }
719
720 /* quickly determine if a given rate is CCK or OFDM */
721 #define RUM_RATE_IS_OFDM(rate)  ((rate) >= 12 && (rate) != 22)
722
723 #define RUM_ACK_SIZE    (sizeof(struct ieee80211_frame_ack) + IEEE80211_FCS_LEN)
724
725 static void
726 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
727 {
728         struct rum_tx_data *data = priv;
729         struct rum_softc *sc = data->sc;
730         struct ieee80211com *ic = &sc->sc_ic;
731         struct ifnet *ifp = &ic->ic_if;
732         struct ieee80211_node *ni;
733
734         if (sc->sc_stopped)
735                 return;
736
737         crit_enter();
738
739         if (status != USBD_NORMAL_COMPLETION) {
740                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
741                         crit_exit();
742                         return;
743                 }
744
745                 kprintf("%s: could not transmit buffer: %s\n",
746                     device_get_nameunit(sc->sc_dev), usbd_errstr(status));
747
748                 if (status == USBD_STALLED)
749                         usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
750
751                 ifp->if_oerrors++;
752                 crit_exit();
753                 return;
754         }
755
756         m_freem(data->m);
757         data->m = NULL;
758         ni = data->ni;
759         data->ni = NULL;
760
761         bzero(data->buf, sizeof(struct rum_tx_data));
762         sc->tx_queued--;
763         ifp->if_opackets++;     /* XXX may fail too */
764
765         DPRINTFN(10, ("tx done\n"));
766
767         sc->sc_tx_timer = 0;
768         ifp->if_flags &= ~IFF_OACTIVE;
769
770         lwkt_serialize_enter(ifp->if_serializer);
771         ieee80211_free_node(ni);
772         ifp->if_start(ifp);
773         lwkt_serialize_exit(ifp->if_serializer);
774
775         crit_exit();
776 }
777
778 static void
779 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
780 {
781         struct rum_rx_data *data = priv;
782         struct rum_softc *sc = data->sc;
783         struct ieee80211com *ic = &sc->sc_ic;
784         struct ifnet *ifp = &ic->ic_if;
785         struct rum_rx_desc *desc;
786         struct ieee80211_frame_min *wh;
787         struct ieee80211_node *ni;
788         struct mbuf *mnew, *m;
789         int len, rssi;
790
791         if (sc->sc_stopped)
792                 return;
793
794         crit_enter();
795
796         if (status != USBD_NORMAL_COMPLETION) {
797                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
798                         crit_exit();
799                         return;
800                 }
801
802                 if (status == USBD_STALLED)
803                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
804                 goto skip;
805         }
806
807         usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
808
809         if (len < RT2573_RX_DESC_SIZE + sizeof(struct ieee80211_frame_min)) {
810                 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
811                     len));
812                 ifp->if_ierrors++;
813                 goto skip;
814         }
815
816         desc = (struct rum_rx_desc *)data->buf;
817
818         if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
819                 /*
820                  * This should not happen since we did not request to receive
821                  * those frames when we filled RT2573_TXRX_CSR0.
822                  */
823                 DPRINTFN(5, ("CRC error\n"));
824                 ifp->if_ierrors++;
825                 goto skip;
826         }
827
828         mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
829         if (mnew == NULL) {
830                 kprintf("%s: could not allocate rx mbuf\n",
831                     device_get_nameunit(sc->sc_dev));
832                 ifp->if_ierrors++;
833                 goto skip;
834         }
835
836         m = data->m;
837         data->m = NULL;
838         data->buf = NULL;
839
840         lwkt_serialize_enter(ifp->if_serializer);
841
842         /* finalize mbuf */
843         m->m_pkthdr.rcvif = ifp;
844         m->m_data = (caddr_t)(desc + 1);
845         m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
846
847         rssi = rum_get_rssi(sc, desc->rssi);
848
849         wh = mtod(m, struct ieee80211_frame_min *);
850         ni = ieee80211_find_rxnode(ic, wh);
851
852         /* Error happened during RSSI conversion. */
853         if (rssi < 0)
854                 rssi = ni->ni_rssi;
855
856         if (sc->sc_drvbpf != NULL) {
857                 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
858
859                 tap->wr_flags = 0;
860                 tap->wr_rate = rum_rxrate(desc);
861                 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
862                 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
863                 tap->wr_antenna = sc->rx_ant;
864                 tap->wr_antsignal = rssi;
865
866                 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
867         }
868
869         /* send the frame to the 802.11 layer */
870         ieee80211_input(ic, m, ni, rssi, 0);
871
872         /* node is no longer needed */
873         ieee80211_free_node(ni);
874
875         if ((ifp->if_flags & IFF_OACTIVE) == 0)
876                 ifp->if_start(ifp);
877
878         lwkt_serialize_exit(ifp->if_serializer);
879
880         data->m = mnew;
881         data->buf = mtod(data->m, uint8_t *);
882
883         DPRINTFN(15, ("rx done\n"));
884
885 skip:   /* setup a new transfer */
886         bzero(data->buf, sizeof(struct rum_rx_desc));
887         usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
888             USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
889         usbd_transfer(xfer);
890
891         crit_exit();
892 }
893
894 /*
895  * This function is only used by the Rx radiotap code. It returns the rate at
896  * which a given frame was received.
897  */
898 static uint8_t
899 rum_rxrate(struct rum_rx_desc *desc)
900 {
901         if (le32toh(desc->flags) & RT2573_RX_OFDM) {
902                 /* reverse function of rum_plcp_signal */
903                 switch (desc->rate) {
904                 case 0xb:       return 12;
905                 case 0xf:       return 18;
906                 case 0xa:       return 24;
907                 case 0xe:       return 36;
908                 case 0x9:       return 48;
909                 case 0xd:       return 72;
910                 case 0x8:       return 96;
911                 case 0xc:       return 108;
912                 }
913         } else {
914                 if (desc->rate == 10)
915                         return 2;
916                 if (desc->rate == 20)
917                         return 4;
918                 if (desc->rate == 55)
919                         return 11;
920                 if (desc->rate == 110)
921                         return 22;
922         }
923         return 2;       /* should not get there */
924 }
925
926 static uint8_t
927 rum_plcp_signal(int rate)
928 {
929         switch (rate) {
930         /* CCK rates (returned values are device-dependent) */
931         case 2:         return 0x0;
932         case 4:         return 0x1;
933         case 11:        return 0x2;
934         case 22:        return 0x3;
935
936         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
937         case 12:        return 0xb;
938         case 18:        return 0xf;
939         case 24:        return 0xa;
940         case 36:        return 0xe;
941         case 48:        return 0x9;
942         case 72:        return 0xd;
943         case 96:        return 0x8;
944         case 108:       return 0xc;
945
946         /* unsupported rates (should not get there) */
947         default:        return 0xff;
948         }
949 }
950
951 static void
952 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
953     uint32_t flags, uint16_t xflags, int len, int rate)
954 {
955         struct ieee80211com *ic = &sc->sc_ic;
956         uint16_t plcp_length;
957         int remainder;
958
959         desc->flags = htole32(flags);
960         desc->flags |= htole32(len << 16);
961
962         desc->xflags = htole16(xflags);
963
964         desc->wme = htole16(
965             RT2573_QID(0) |
966             RT2573_AIFSN(2) |
967             RT2573_LOGCWMIN(4) |
968             RT2573_LOGCWMAX(10));
969
970         /* setup PLCP fields */
971         desc->plcp_signal  = rum_plcp_signal(rate);
972         desc->plcp_service = 4;
973
974         len += IEEE80211_CRC_LEN;
975         if (RUM_RATE_IS_OFDM(rate)) {
976                 desc->flags |= htole32(RT2573_TX_OFDM);
977
978                 plcp_length = len & 0xfff;
979                 desc->plcp_length_hi = plcp_length >> 6;
980                 desc->plcp_length_lo = plcp_length & 0x3f;
981         } else {
982                 plcp_length = (16 * len + rate - 1) / rate;
983                 if (rate == 22) {
984                         remainder = (16 * len) % 22;
985                         if (remainder != 0 && remainder < 7)
986                                 desc->plcp_service |= RT2573_PLCP_LENGEXT;
987                 }
988                 desc->plcp_length_hi = plcp_length >> 8;
989                 desc->plcp_length_lo = plcp_length & 0xff;
990
991                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
992                         desc->plcp_signal |= 0x08;
993         }
994         desc->flags |= htole32(RT2573_TX_VALID);
995 }
996
997 #define RUM_TX_TIMEOUT  5000
998
999 static int
1000 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1001 {
1002         struct ieee80211com *ic = &sc->sc_ic;
1003         struct ifnet *ifp = &ic->ic_if;
1004         struct rum_tx_desc *desc;
1005         struct rum_tx_data *data;
1006         struct ieee80211_frame *wh;
1007         uint32_t flags = 0;
1008         uint16_t dur;
1009         usbd_status error;
1010         int xferlen, rate, rateidx;
1011
1012         wh = mtod(m0, struct ieee80211_frame *);
1013
1014         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1015                 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1016                         m_freem(m0);
1017                         return ENOBUFS;
1018                 }
1019
1020                 /* packet header may have moved, reset our local pointer */
1021                 wh = mtod(m0, struct ieee80211_frame *);
1022         }
1023
1024         /* pickup a rate */
1025         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1026             IEEE80211_FC0_TYPE_MGT) {
1027                 /* mgmt frames are sent at the lowest available bit-rate */
1028                 rateidx = 0;
1029         } else {
1030                 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1031         }
1032         rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1033
1034         data = &sc->tx_data[0];
1035         desc = (struct rum_tx_desc *)data->buf;
1036
1037         data->m = m0;
1038         data->ni = ni;
1039
1040         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1041                 flags |= RT2573_TX_ACK;
1042
1043                 dur = ieee80211_txtime(ni, RUM_ACK_SIZE,
1044                         ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1045                         sc->sc_sifs;
1046                 *(uint16_t *)wh->i_dur = htole16(dur);
1047
1048                 /* tell hardware to set timestamp in probe responses */
1049                 if ((wh->i_fc[0] &
1050                     (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1051                     (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1052                         flags |= RT2573_TX_TIMESTAMP;
1053         }
1054
1055         if (sc->sc_drvbpf != NULL) {
1056                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1057
1058                 tap->wt_flags = 0;
1059                 tap->wt_rate = rate;
1060                 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1061                 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1062                 tap->wt_antenna = sc->tx_ant;
1063
1064                 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1065         }
1066
1067         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1068         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1069
1070         /* Align end on a 4-bytes boundary */
1071         xferlen = roundup(RT2573_TX_DESC_SIZE + m0->m_pkthdr.len, 4);
1072
1073         /*
1074          * No space left in the last URB to store the extra 4 bytes, force
1075          * sending of another URB.
1076          */
1077         if ((xferlen % 64) == 0)
1078                 xferlen += 4;
1079
1080         DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1081             m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1082
1083         lwkt_serialize_exit(ifp->if_serializer);
1084
1085         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1086             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1087
1088         error = usbd_transfer(data->xfer);
1089         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1090                 m_freem(m0);
1091                 data->m = NULL;
1092                 data->ni = NULL;
1093         } else {
1094                 sc->tx_queued++;
1095                 error = 0;
1096         }
1097
1098         lwkt_serialize_enter(ifp->if_serializer);
1099         return error;
1100 }
1101
1102 static void
1103 rum_start(struct ifnet *ifp)
1104 {
1105         struct rum_softc *sc = ifp->if_softc;
1106         struct ieee80211com *ic = &sc->sc_ic;
1107
1108         ASSERT_SERIALIZED(ifp->if_serializer);
1109
1110         if (sc->sc_stopped)
1111                 return;
1112
1113         crit_enter();
1114
1115         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
1116                 crit_exit();
1117                 return;
1118         }
1119
1120         for (;;) {
1121                 struct ieee80211_node *ni;
1122                 struct mbuf *m0;
1123
1124                 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1125                         if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1126                                 ifp->if_flags |= IFF_OACTIVE;
1127                                 break;
1128                         }
1129                         IF_DEQUEUE(&ic->ic_mgtq, m0);
1130
1131                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1132                         m0->m_pkthdr.rcvif = NULL;
1133
1134                         BPF_MTAP(ifp, m0);
1135
1136                         if (rum_tx_data(sc, m0, ni) != 0) {
1137                                 ieee80211_free_node(ni);
1138                                 break;
1139                         }
1140                 } else {
1141                         struct ether_header *eh;
1142
1143                         if (ic->ic_state != IEEE80211_S_RUN)
1144                                 break;
1145
1146                         m0 = ifq_poll(&ifp->if_snd);
1147                         if (m0 == NULL)
1148                                 break;
1149                         if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1150                                 ifp->if_flags |= IFF_OACTIVE;
1151                                 break;
1152                         }
1153                         ifq_dequeue(&ifp->if_snd, m0);
1154
1155                         if (m0->m_len < sizeof(struct ether_header)) {
1156                                 m0 = m_pullup(m0, sizeof(struct ether_header));
1157                                 if (m0 == NULL) {
1158                                         ifp->if_oerrors++;
1159                                         continue;
1160                                 }
1161                         }
1162                         eh = mtod(m0, struct ether_header *);
1163
1164                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1165                         if (ni == NULL) {
1166                                 m_freem(m0);
1167                                 continue;
1168                         }
1169
1170                         BPF_MTAP(ifp, m0);
1171
1172                         m0 = ieee80211_encap(ic, m0, ni);
1173                         if (m0 == NULL) {
1174                                 ieee80211_free_node(ni);
1175                                 continue;
1176                         }
1177
1178                         if (ic->ic_rawbpf != NULL)
1179                                 bpf_mtap(ic->ic_rawbpf, m0);
1180
1181                         if (rum_tx_data(sc, m0, ni) != 0) {
1182                                 ieee80211_free_node(ni);
1183                                 ifp->if_oerrors++;
1184                                 break;
1185                         }
1186                 }
1187
1188                 sc->sc_tx_timer = 5;
1189                 ifp->if_timer = 1;
1190         }
1191
1192         crit_exit();
1193 }
1194
1195 static void
1196 rum_watchdog(struct ifnet *ifp)
1197 {
1198         struct rum_softc *sc = ifp->if_softc;
1199
1200         ASSERT_SERIALIZED(ifp->if_serializer);
1201
1202         crit_enter();
1203
1204         ifp->if_timer = 0;
1205
1206         if (sc->sc_tx_timer > 0) {
1207                 if (--sc->sc_tx_timer == 0) {
1208                         kprintf("%s: device timeout\n", device_get_nameunit(sc->sc_dev));
1209                         /*rum_init(sc); XXX needs a process context! */
1210                         ifp->if_oerrors++;
1211
1212                         crit_exit();
1213                         return;
1214                 }
1215                 ifp->if_timer = 1;
1216         }
1217
1218         ieee80211_watchdog(&sc->sc_ic);
1219
1220         crit_exit();
1221 }
1222
1223 static int
1224 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1225 {
1226         struct rum_softc *sc = ifp->if_softc;
1227         struct ieee80211com *ic = &sc->sc_ic;
1228         int error = 0;
1229
1230         ASSERT_SERIALIZED(ifp->if_serializer);
1231
1232         crit_enter();
1233
1234         switch (cmd) {
1235         case SIOCSIFFLAGS:
1236                 if (ifp->if_flags & IFF_UP) {
1237                         if (ifp->if_flags & IFF_RUNNING) {
1238                                 lwkt_serialize_exit(ifp->if_serializer);
1239                                 rum_update_promisc(sc);
1240                                 lwkt_serialize_enter(ifp->if_serializer);
1241                         } else {
1242                                 rum_init(sc);
1243                         }
1244                 } else {
1245                         if (ifp->if_flags & IFF_RUNNING)
1246                                 rum_stop(sc);
1247                 }
1248                 break;
1249         default:
1250                 error = ieee80211_ioctl(ic, cmd, data, cr);
1251                 break;
1252         }
1253
1254         if (error == ENETRESET) {
1255                 struct ieee80211req *ireq = (struct ieee80211req *)data;
1256
1257                 if (cmd == SIOCS80211 &&
1258                     ireq->i_type == IEEE80211_IOC_CHANNEL &&
1259                     ic->ic_opmode == IEEE80211_M_MONITOR) {
1260                         /*
1261                          * This allows for fast channel switching in monitor
1262                          * mode (used by kismet). In IBSS mode, we must
1263                          * explicitly reset the interface to generate a new
1264                          * beacon frame.
1265                          */
1266                         lwkt_serialize_exit(ifp->if_serializer);
1267                         rum_set_chan(sc, ic->ic_ibss_chan);
1268                         lwkt_serialize_enter(ifp->if_serializer);
1269                 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1270                            (IFF_UP | IFF_RUNNING)) {
1271                         rum_init(sc);
1272                 }
1273                 error = 0;
1274         }
1275
1276         crit_exit();
1277         return error;
1278 }
1279
1280 static void
1281 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1282 {
1283         usb_device_request_t req;
1284         usbd_status error;
1285
1286         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1287         req.bRequest = RT2573_READ_EEPROM;
1288         USETW(req.wValue, 0);
1289         USETW(req.wIndex, addr);
1290         USETW(req.wLength, len);
1291
1292         error = usbd_do_request(sc->sc_udev, &req, buf);
1293         if (error != 0) {
1294                 kprintf("%s: could not read EEPROM: %s\n",
1295                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1296         }
1297 }
1298
1299 static uint32_t
1300 rum_read(struct rum_softc *sc, uint16_t reg)
1301 {
1302         uint32_t val;
1303
1304         rum_read_multi(sc, reg, &val, sizeof val);
1305
1306         return le32toh(val);
1307 }
1308
1309 static void
1310 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1311 {
1312         usb_device_request_t req;
1313         usbd_status error;
1314
1315         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1316         req.bRequest = RT2573_READ_MULTI_MAC;
1317         USETW(req.wValue, 0);
1318         USETW(req.wIndex, reg);
1319         USETW(req.wLength, len);
1320
1321         error = usbd_do_request(sc->sc_udev, &req, buf);
1322         if (error != 0) {
1323                 kprintf("%s: could not multi read MAC register: %s\n",
1324                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1325         }
1326 }
1327
1328 static void
1329 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1330 {
1331         uint32_t tmp = htole32(val);
1332
1333         rum_write_multi(sc, reg, &tmp, sizeof tmp);
1334 }
1335
1336 static void
1337 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1338 {
1339         usb_device_request_t req;
1340         usbd_status error;
1341
1342         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1343         req.bRequest = RT2573_WRITE_MULTI_MAC;
1344         USETW(req.wValue, 0);
1345         USETW(req.wIndex, reg);
1346         USETW(req.wLength, len);
1347
1348         error = usbd_do_request(sc->sc_udev, &req, buf);
1349         if (error != 0) {
1350                 kprintf("%s: could not multi write MAC register: %s\n",
1351                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1352         }
1353 }
1354
1355 static void
1356 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1357 {
1358         uint32_t tmp;
1359         int ntries;
1360
1361         for (ntries = 0; ntries < 5; ntries++) {
1362                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1363                         break;
1364         }
1365         if (ntries == 5) {
1366                 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1367                 return;
1368         }
1369
1370         tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1371         rum_write(sc, RT2573_PHY_CSR3, tmp);
1372 }
1373
1374 static uint8_t
1375 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1376 {
1377         uint32_t val;
1378         int ntries;
1379
1380         for (ntries = 0; ntries < 5; ntries++) {
1381                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1382                         break;
1383         }
1384         if (ntries == 5) {
1385                 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1386                 return 0;
1387         }
1388
1389         val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1390         rum_write(sc, RT2573_PHY_CSR3, val);
1391
1392         for (ntries = 0; ntries < 100; ntries++) {
1393                 val = rum_read(sc, RT2573_PHY_CSR3);
1394                 if (!(val & RT2573_BBP_BUSY))
1395                         return val & 0xff;
1396                 DELAY(1);
1397         }
1398
1399         kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1400         return 0;
1401 }
1402
1403 static void
1404 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1405 {
1406         uint32_t tmp;
1407         int ntries;
1408
1409         for (ntries = 0; ntries < 5; ntries++) {
1410                 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1411                         break;
1412         }
1413         if (ntries == 5) {
1414                 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1415                 return;
1416         }
1417
1418         tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1419             (reg & 3);
1420         rum_write(sc, RT2573_PHY_CSR4, tmp);
1421
1422         /* remember last written value in sc */
1423         sc->rf_regs[reg] = val;
1424
1425         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1426 }
1427
1428 static void
1429 rum_select_antenna(struct rum_softc *sc)
1430 {
1431         uint8_t bbp4, bbp77;
1432         uint32_t tmp;
1433
1434         bbp4  = rum_bbp_read(sc, 4);
1435         bbp77 = rum_bbp_read(sc, 77);
1436
1437         /* TBD */
1438
1439         /* make sure Rx is disabled before switching antenna */
1440         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1441         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1442
1443         rum_bbp_write(sc,  4, bbp4);
1444         rum_bbp_write(sc, 77, bbp77);
1445
1446         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1447 }
1448
1449 /*
1450  * Enable multi-rate retries for frames sent at OFDM rates.
1451  * In 802.11b/g mode, allow fallback to CCK rates.
1452  */
1453 static void
1454 rum_enable_mrr(struct rum_softc *sc)
1455 {
1456         struct ieee80211com *ic = &sc->sc_ic;
1457         uint32_t tmp;
1458
1459         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1460
1461         tmp &= ~RT2573_MRR_CCK_FALLBACK;
1462         if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1463                 tmp |= RT2573_MRR_CCK_FALLBACK;
1464         tmp |= RT2573_MRR_ENABLED;
1465
1466         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1467 }
1468
1469 static void
1470 rum_set_txpreamble(struct rum_softc *sc)
1471 {
1472         uint32_t tmp;
1473
1474         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1475
1476         tmp &= ~RT2573_SHORT_PREAMBLE;
1477         if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1478                 tmp |= RT2573_SHORT_PREAMBLE;
1479
1480         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1481 }
1482
1483 static void
1484 rum_set_basicrates(struct rum_softc *sc)
1485 {
1486         struct ieee80211com *ic = &sc->sc_ic;
1487
1488         /* update basic rate set */
1489         if (ic->ic_curmode == IEEE80211_MODE_11B) {
1490                 /* 11b basic rates: 1, 2Mbps */
1491                 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1492         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1493                 /* 11a basic rates: 6, 12, 24Mbps */
1494                 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1495         } else {
1496                 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1497                 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1498         }
1499 }
1500
1501 /*
1502  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1503  * driver.
1504  */
1505 static void
1506 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1507 {
1508         uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1509         uint32_t tmp;
1510
1511         /* update all BBP registers that depend on the band */
1512         bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1513         bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1514         if (IEEE80211_IS_CHAN_5GHZ(c)) {
1515                 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1516                 bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1517         }
1518         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1519             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1520                 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1521         }
1522
1523         sc->bbp17 = bbp17;
1524         rum_bbp_write(sc,  17, bbp17);
1525         rum_bbp_write(sc,  96, bbp96);
1526         rum_bbp_write(sc, 104, bbp104);
1527
1528         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1529             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1530                 rum_bbp_write(sc, 75, 0x80);
1531                 rum_bbp_write(sc, 86, 0x80);
1532                 rum_bbp_write(sc, 88, 0x80);
1533         }
1534
1535         rum_bbp_write(sc, 35, bbp35);
1536         rum_bbp_write(sc, 97, bbp97);
1537         rum_bbp_write(sc, 98, bbp98);
1538
1539         tmp = rum_read(sc, RT2573_PHY_CSR0);
1540         tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1541         if (IEEE80211_IS_CHAN_2GHZ(c))
1542                 tmp |= RT2573_PA_PE_2GHZ;
1543         else
1544                 tmp |= RT2573_PA_PE_5GHZ;
1545         rum_write(sc, RT2573_PHY_CSR0, tmp);
1546 }
1547
1548 static void
1549 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1550 {
1551         struct ieee80211com *ic = &sc->sc_ic;
1552         const struct rfprog *rfprog;
1553         uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1554         int8_t power;
1555         u_int i, chan;
1556
1557         chan = ieee80211_chan2ieee(ic, c);
1558         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1559                 return;
1560
1561         /* select the appropriate RF settings based on what EEPROM says */
1562         rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1563                   sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1564
1565         /* find the settings for this channel (we know it exists) */
1566         for (i = 0; rfprog[i].chan != chan; i++)
1567                 ;       /* EMPTY */
1568
1569         power = sc->txpow[i];
1570         if (power < 0) {
1571                 bbp94 += power;
1572                 power = 0;
1573         } else if (power > 31) {
1574                 bbp94 += power - 31;
1575                 power = 31;
1576         }
1577
1578         /*
1579          * If we are switching from the 2GHz band to the 5GHz band or
1580          * vice-versa, BBP registers need to be reprogrammed.
1581          */
1582         if (c->ic_flags != sc->sc_curchan->ic_flags) {
1583                 rum_select_band(sc, c);
1584                 rum_select_antenna(sc);
1585         }
1586         sc->sc_curchan = c;
1587
1588         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1589         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1590         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1591         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1592
1593         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1594         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1595         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1596         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1597
1598         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1599         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1600         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1601         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1602
1603         DELAY(10);
1604
1605         /* enable smart mode for MIMO-capable RFs */
1606         bbp3 = rum_bbp_read(sc, 3);
1607
1608         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1609                 bbp3 &= ~RT2573_SMART_MODE;
1610         else
1611                 bbp3 |= RT2573_SMART_MODE;
1612
1613         rum_bbp_write(sc, 3, bbp3);
1614
1615         if (bbp94 != RT2573_BBPR94_DEFAULT)
1616                 rum_bbp_write(sc, 94, bbp94);
1617
1618         sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1619                                                 : IEEE80211_DUR_SIFS;
1620 }
1621
1622 /*
1623  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1624  * and HostAP operating modes.
1625  */
1626 static void
1627 rum_enable_tsf_sync(struct rum_softc *sc)
1628 {
1629         struct ieee80211com *ic = &sc->sc_ic;
1630         uint32_t tmp;
1631
1632         if (ic->ic_opmode != IEEE80211_M_STA) {
1633                 /*
1634                  * Change default 16ms TBTT adjustment to 8ms.
1635                  * Must be done before enabling beacon generation.
1636                  */
1637                 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1638         }
1639
1640         tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1641
1642         /* set beacon interval (in 1/16ms unit) */
1643         tmp |= ic->ic_bss->ni_intval * 16;
1644
1645         tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1646         if (ic->ic_opmode == IEEE80211_M_STA)
1647                 tmp |= RT2573_TSF_MODE(1);
1648         else
1649                 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1650
1651         rum_write(sc, RT2573_TXRX_CSR9, tmp);
1652 }
1653
1654 static void
1655 rum_update_slot(struct rum_softc *sc)
1656 {
1657         struct ieee80211com *ic = &sc->sc_ic;
1658         uint8_t slottime;
1659         uint32_t tmp;
1660
1661         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1662
1663         tmp = rum_read(sc, RT2573_MAC_CSR9);
1664         tmp = (tmp & ~0xff) | slottime;
1665         rum_write(sc, RT2573_MAC_CSR9, tmp);
1666
1667         DPRINTF(("setting slot time to %uus\n", slottime));
1668 }
1669
1670 static void
1671 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1672 {
1673         uint32_t tmp;
1674
1675         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1676         rum_write(sc, RT2573_MAC_CSR4, tmp);
1677
1678         tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1679         rum_write(sc, RT2573_MAC_CSR5, tmp);
1680 }
1681
1682 static void
1683 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1684 {
1685         uint32_t tmp;
1686
1687         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1688         rum_write(sc, RT2573_MAC_CSR2, tmp);
1689
1690         tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1691         rum_write(sc, RT2573_MAC_CSR3, tmp);
1692 }
1693
1694 static void
1695 rum_update_promisc(struct rum_softc *sc)
1696 {
1697         struct ifnet *ifp = &sc->sc_ic.ic_if;
1698         uint32_t tmp;
1699
1700         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1701
1702         tmp &= ~RT2573_DROP_NOT_TO_ME;
1703         if (!(ifp->if_flags & IFF_PROMISC))
1704                 tmp |= RT2573_DROP_NOT_TO_ME;
1705
1706         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1707
1708         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1709             "entering" : "leaving"));
1710 }
1711
1712 static const char *
1713 rum_get_rf(int rev)
1714 {
1715         switch (rev) {
1716         case RT2573_RF_2527:    return "RT2527 (MIMO XR)";
1717         case RT2573_RF_2528:    return "RT2528";
1718         case RT2573_RF_5225:    return "RT5225 (MIMO XR)";
1719         case RT2573_RF_5226:    return "RT5226";
1720         default:                return "unknown";
1721         }
1722 }
1723
1724 static void
1725 rum_read_eeprom(struct rum_softc *sc)
1726 {
1727         struct ieee80211com *ic = &sc->sc_ic;
1728         uint16_t val;
1729 #ifdef RUM_DEBUG
1730         int i;
1731 #endif
1732
1733         /* read MAC/BBP type */
1734         rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1735         sc->macbbp_rev = le16toh(val);
1736
1737         /* read MAC address */
1738         rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1739
1740         rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1741         val = le16toh(val);
1742         sc->rf_rev =   (val >> 11) & 0x1f;
1743         sc->hw_radio = (val >> 10) & 0x1;
1744         sc->rx_ant =   (val >> 4)  & 0x3;
1745         sc->tx_ant =   (val >> 2)  & 0x3;
1746         sc->nb_ant =   val & 0x3;
1747
1748         DPRINTF(("RF revision=%d\n", sc->rf_rev));
1749
1750         rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1751         val = le16toh(val);
1752         sc->ext_5ghz_lna = (val >> 6) & 0x1;
1753         sc->ext_2ghz_lna = (val >> 4) & 0x1;
1754
1755         DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1756             sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1757
1758         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1759         val = le16toh(val);
1760         if ((val & 0xff) != 0xff)
1761                 sc->rssi_2ghz_corr = (int8_t)(val & 0xff);      /* signed */
1762
1763         /* Only [-10, 10] is valid */
1764         if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1765                 sc->rssi_2ghz_corr = 0;
1766
1767         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1768         val = le16toh(val);
1769         if ((val & 0xff) != 0xff)
1770                 sc->rssi_5ghz_corr = (int8_t)(val & 0xff);      /* signed */
1771
1772         /* Only [-10, 10] is valid */
1773         if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1774                 sc->rssi_5ghz_corr = 0;
1775
1776         if (sc->ext_2ghz_lna)
1777                 sc->rssi_2ghz_corr -= 14;
1778         if (sc->ext_5ghz_lna)
1779                 sc->rssi_5ghz_corr -= 14;
1780
1781         DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1782             sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1783
1784         rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1785         val = le16toh(val);
1786         if ((val & 0xff) != 0xff)
1787                 sc->rffreq = val & 0xff;
1788
1789         DPRINTF(("RF freq=%d\n", sc->rffreq));
1790
1791         /* read Tx power for all a/b/g channels */
1792         rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1793         /* XXX default Tx power for 802.11a channels */
1794         memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1795 #ifdef RUM_DEBUG
1796         for (i = 0; i < 14; i++)
1797                 DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
1798 #endif
1799
1800         /* read default values for BBP registers */
1801         rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1802 #ifdef RUM_DEBUG
1803         for (i = 0; i < 14; i++) {
1804                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1805                         continue;
1806                 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1807                     sc->bbp_prom[i].val));
1808         }
1809 #endif
1810 }
1811
1812 static int
1813 rum_bbp_init(struct rum_softc *sc)
1814 {
1815 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
1816         int i, ntries;
1817         uint8_t val;
1818
1819         /* wait for BBP to be ready */
1820         for (ntries = 0; ntries < 100; ntries++) {
1821                 val = rum_bbp_read(sc, 0);
1822                 if (val != 0 && val != 0xff)
1823                         break;
1824                 DELAY(1000);
1825         }
1826         if (ntries == 100) {
1827                 kprintf("%s: timeout waiting for BBP\n",
1828                     device_get_nameunit(sc->sc_dev));
1829                 return EIO;
1830         }
1831
1832         /* initialize BBP registers to default values */
1833         for (i = 0; i < N(rum_def_bbp); i++)
1834                 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1835
1836         /* write vendor-specific BBP values (from EEPROM) */
1837         for (i = 0; i < 16; i++) {
1838                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1839                         continue;
1840                 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1841         }
1842
1843         return 0;
1844 #undef N
1845 }
1846
1847 static void
1848 rum_init(void *xsc)
1849 {
1850 #define N(a)    (sizeof(a) / sizeof((a)[0]))
1851         struct rum_softc *sc = xsc;
1852         struct ieee80211com *ic = &sc->sc_ic;
1853         struct ifnet *ifp = &ic->ic_if;
1854         struct rum_rx_data *data;
1855         uint32_t tmp;
1856         usbd_status usb_err;
1857         int i, ntries, error;
1858
1859         ASSERT_SERIALIZED(ifp->if_serializer);
1860
1861         crit_enter();
1862
1863         rum_stop(sc);
1864         sc->sc_stopped = 0;
1865
1866         lwkt_serialize_exit(ifp->if_serializer);
1867
1868         /* initialize MAC registers to default values */
1869         for (i = 0; i < N(rum_def_mac); i++)
1870                 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1871
1872         /* set host ready */
1873         rum_write(sc, RT2573_MAC_CSR1, 3);
1874         rum_write(sc, RT2573_MAC_CSR1, 0);
1875
1876         /* wait for BBP/RF to wakeup */
1877         for (ntries = 0; ntries < 1000; ntries++) {
1878                 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1879                         break;
1880                 rum_write(sc, RT2573_MAC_CSR12, 4);     /* force wakeup */
1881                 DELAY(1000);
1882         }
1883         if (ntries == 1000) {
1884                 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1885                         device_get_nameunit(sc->sc_dev));
1886                 error = ETIMEDOUT;
1887                 goto fail;
1888         }
1889
1890         error = rum_bbp_init(sc);
1891         if (error)
1892                 goto fail;
1893
1894         /* select default channel */
1895         sc->sc_curchan = ic->ic_curchan = ic->ic_ibss_chan;
1896
1897         rum_select_band(sc, sc->sc_curchan);
1898         rum_select_antenna(sc);
1899         rum_set_chan(sc, sc->sc_curchan);
1900
1901         /* clear STA registers */
1902         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1903
1904         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1905         rum_set_macaddr(sc, ic->ic_myaddr);
1906
1907         /* initialize ASIC */
1908         rum_write(sc, RT2573_MAC_CSR1, 4);
1909
1910         /*
1911          * Allocate xfer for AMRR statistics requests.
1912          */
1913         sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
1914         if (sc->stats_xfer == NULL) {
1915                 kprintf("%s: could not allocate AMRR xfer\n",
1916                         device_get_nameunit(sc->sc_dev));
1917                 error = ENOMEM;
1918                 goto fail;
1919         }
1920
1921         /*
1922          * Open Tx and Rx USB bulk pipes.
1923          */
1924         usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1925                                  &sc->sc_tx_pipeh);
1926         if (usb_err != USBD_NORMAL_COMPLETION) {
1927                 kprintf("%s: could not open Tx pipe: %s\n",
1928                         device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1929                 error = EIO;
1930                 goto fail;
1931         }
1932
1933         usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1934                                  &sc->sc_rx_pipeh);
1935         if (usb_err != USBD_NORMAL_COMPLETION) {
1936                 kprintf("%s: could not open Rx pipe: %s\n",
1937                     device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1938                 error = EIO;
1939                 goto fail;
1940         }
1941
1942         /*
1943          * Allocate Tx and Rx xfer queues.
1944          */
1945         error = rum_alloc_tx_list(sc);
1946         if (error) {
1947                 kprintf("%s: could not allocate Tx list\n",
1948                         device_get_nameunit(sc->sc_dev));
1949                 goto fail;
1950         }
1951
1952         error = rum_alloc_rx_list(sc);
1953         if (error) {
1954                 kprintf("%s: could not allocate Rx list\n",
1955                         device_get_nameunit(sc->sc_dev));
1956                 goto fail;
1957         }
1958
1959         /*
1960          * Start up the receive pipe.
1961          */
1962         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1963                 data = &sc->rx_data[i];
1964
1965                 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
1966                     MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
1967                 usbd_transfer(data->xfer);
1968         }
1969
1970         /* update Rx filter */
1971         tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
1972
1973         tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
1974         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1975                 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
1976                        RT2573_DROP_ACKCTS;
1977                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1978                         tmp |= RT2573_DROP_TODS;
1979                 if (!(ifp->if_flags & IFF_PROMISC))
1980                         tmp |= RT2573_DROP_NOT_TO_ME;
1981         }
1982         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1983 fail:
1984         lwkt_serialize_enter(ifp->if_serializer);
1985
1986         if (error) {
1987                 rum_stop(sc);
1988         } else {
1989                 ifp->if_flags &= ~IFF_OACTIVE;
1990                 ifp->if_flags |= IFF_RUNNING;
1991
1992                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1993                         if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1994                                 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1995                 } else {
1996                         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1997                 }
1998         }
1999
2000         crit_exit();
2001 #undef N
2002 }
2003
2004 static void
2005 rum_stop(struct rum_softc *sc)
2006 {
2007         struct ieee80211com *ic = &sc->sc_ic;
2008         struct ifnet *ifp = &ic->ic_if;
2009         uint32_t tmp;
2010
2011         ASSERT_SERIALIZED(ifp->if_serializer);
2012
2013         crit_enter();
2014
2015         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2016         sc->sc_stopped = 1;
2017
2018         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);  /* free all nodes */
2019
2020         sc->sc_tx_timer = 0;
2021         ifp->if_timer = 0;
2022
2023         lwkt_serialize_exit(ifp->if_serializer);
2024
2025         /* disable Rx */
2026         tmp = rum_read(sc, RT2573_TXRX_CSR0);
2027         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2028
2029         /* reset ASIC */
2030         rum_write(sc, RT2573_MAC_CSR1, 3);
2031         rum_write(sc, RT2573_MAC_CSR1, 0);
2032
2033         if (sc->stats_xfer != NULL) {
2034                 usbd_free_xfer(sc->stats_xfer);
2035                 sc->stats_xfer = NULL;
2036         }
2037
2038         if (sc->sc_rx_pipeh != NULL) {
2039                 usbd_abort_pipe(sc->sc_rx_pipeh);
2040                 usbd_close_pipe(sc->sc_rx_pipeh);
2041                 sc->sc_rx_pipeh = NULL;
2042         }
2043
2044         if (sc->sc_tx_pipeh != NULL) {
2045                 usbd_abort_pipe(sc->sc_tx_pipeh);
2046                 usbd_close_pipe(sc->sc_tx_pipeh);
2047                 sc->sc_tx_pipeh = NULL;
2048         }
2049
2050         lwkt_serialize_enter(ifp->if_serializer);
2051
2052         rum_free_rx_list(sc);
2053         rum_free_tx_list(sc);
2054
2055         crit_exit();
2056 }
2057
2058 static int
2059 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2060 {
2061         usb_device_request_t req;
2062         uint16_t reg = RT2573_MCU_CODE_BASE;
2063         usbd_status error;
2064
2065         /* copy firmware image into NIC */
2066         for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2067                 rum_write(sc, reg, UGETDW(ucode));
2068
2069         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2070         req.bRequest = RT2573_MCU_CNTL;
2071         USETW(req.wValue, RT2573_MCU_RUN);
2072         USETW(req.wIndex, 0);
2073         USETW(req.wLength, 0);
2074
2075         error = usbd_do_request(sc->sc_udev, &req, NULL);
2076         if (error != 0) {
2077                 kprintf("%s: could not run firmware: %s\n",
2078                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2079         }
2080         return error;
2081 }
2082
2083 static int
2084 rum_prepare_beacon(struct rum_softc *sc)
2085 {
2086         struct ieee80211com *ic = &sc->sc_ic;
2087         struct ifnet *ifp = &ic->ic_if;
2088         struct ieee80211_beacon_offsets bo;
2089         struct rum_tx_desc desc;
2090         struct mbuf *m0;
2091         int rate;
2092
2093         lwkt_serialize_enter(ifp->if_serializer);
2094         m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2095         lwkt_serialize_exit(ifp->if_serializer);
2096
2097         if (m0 == NULL) {
2098                 if_printf(&ic->ic_if, "could not allocate beacon frame\n");
2099                 return ENOBUFS;
2100         }
2101
2102         /* send beacons at the lowest available rate */
2103         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2104
2105         rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2106             m0->m_pkthdr.len, rate);
2107
2108         /* copy the first 24 bytes of Tx descriptor into NIC memory */
2109         rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2110
2111         /* copy beacon header and payload into NIC memory */
2112         rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2113             m0->m_pkthdr.len);
2114
2115         m_freem(m0);
2116
2117         return 0;
2118 }
2119
2120 static void
2121 rum_stats_timeout(void *arg)
2122 {
2123         struct rum_softc *sc = arg;
2124         usb_device_request_t req;
2125
2126         if (sc->sc_stopped)
2127                 return;
2128
2129         crit_enter();
2130
2131         /*
2132          * Asynchronously read statistic registers (cleared by read).
2133          */
2134         req.bmRequestType = UT_READ_VENDOR_DEVICE;
2135         req.bRequest = RT2573_READ_MULTI_MAC;
2136         USETW(req.wValue, 0);
2137         USETW(req.wIndex, RT2573_STA_CSR0);
2138         USETW(req.wLength, sizeof(sc->sta));
2139
2140         usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2141                                 USBD_DEFAULT_TIMEOUT, &req,
2142                                 sc->sta, sizeof(sc->sta), 0,
2143                                 rum_stats_update);
2144         usbd_transfer(sc->stats_xfer);
2145
2146         crit_exit();
2147 }
2148
2149 static void
2150 rum_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2151                  usbd_status status)
2152 {
2153         struct rum_softc *sc = (struct rum_softc *)priv;
2154         struct ifnet *ifp = &sc->sc_ic.ic_if;
2155         struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2156
2157         if (status != USBD_NORMAL_COMPLETION) {
2158                 kprintf("%s: could not retrieve Tx statistics - cancelling "
2159                     "automatic rate control\n", device_get_nameunit(sc->sc_dev));
2160                 return;
2161         }
2162
2163         crit_enter();
2164
2165         /* count TX retry-fail as Tx errors */
2166         ifp->if_oerrors += RUM_TX_PKT_FAIL(sc);
2167
2168         stats->stats_pkt_noretry += RUM_TX_PKT_NO_RETRY(sc);
2169         stats->stats_pkt_ok += RUM_TX_PKT_NO_RETRY(sc) +
2170                                RUM_TX_PKT_ONE_RETRY(sc) +
2171                                RUM_TX_PKT_MULTI_RETRY(sc);
2172         stats->stats_pkt_err += RUM_TX_PKT_FAIL(sc);
2173
2174         stats->stats_retries += RUM_TX_PKT_ONE_RETRY(sc);
2175 #if 1
2176         /*
2177          * XXX Estimated average:
2178          * Actual number of retries for each packet should belong to
2179          * [2, RUM_TX_SHORT_RETRY_MAX]
2180          */
2181         stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc) *
2182                                 ((2 + RUM_TX_SHORT_RETRY_MAX) / 2);
2183 #else
2184         stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc);
2185 #endif
2186         stats->stats_retries += RUM_TX_PKT_FAIL(sc) * RUM_TX_SHORT_RETRY_MAX;
2187
2188         callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
2189
2190         crit_exit();
2191 }
2192
2193 static void
2194 rum_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2195           struct ieee80211_ratectl_stats *stats)
2196 {
2197         struct ifnet *ifp = &ic->ic_if;
2198         struct rum_softc *sc = ifp->if_softc;
2199
2200         ASSERT_SERIALIZED(ifp->if_serializer);
2201
2202         bcopy(&sc->sc_stats, stats, sizeof(*stats));
2203         bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2204 }
2205
2206 static void
2207 rum_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2208 {
2209         struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2210         struct ieee80211_onoe_param *oparam;
2211
2212         if (st->rc_st_param != NULL) {
2213                 kfree(st->rc_st_param, M_DEVBUF);
2214                 st->rc_st_param = NULL;
2215         }
2216
2217         switch (nrc) {
2218         case IEEE80211_RATECTL_ONOE:
2219                 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2220
2221                 IEEE80211_ONOE_PARAM_SETUP(oparam);
2222                 oparam->onoe_raise = 15;
2223
2224                 st->rc_st_param = oparam;
2225                 break;
2226         case IEEE80211_RATECTL_NONE:
2227                 /* This could only happen during detaching */
2228                 break;
2229         default:
2230                 panic("unknown rate control algo %u\n", nrc);
2231         }
2232 }
2233
2234 static int
2235 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2236 {
2237         int lna, agc, rssi;
2238
2239         lna = (raw >> 5) & 0x3;
2240         agc = raw & 0x1f;
2241
2242         if (lna == 0) {
2243                 /*
2244                  * No RSSI mapping
2245                  *
2246                  * NB: Since RSSI is relative to noise floor, -1 is
2247                  *     adequate for caller to know error happened.
2248                  */
2249                 return -1;
2250         }
2251
2252         rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2253
2254         if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2255                 rssi += sc->rssi_2ghz_corr;
2256
2257                 if (lna == 1)
2258                         rssi -= 64;
2259                 else if (lna == 2)
2260                         rssi -= 74;
2261                 else if (lna == 3)
2262                         rssi -= 90;
2263         } else {
2264                 rssi += sc->rssi_5ghz_corr;
2265
2266                 if (!sc->ext_5ghz_lna && lna != 1)
2267                         rssi += 4;
2268
2269                 if (lna == 1)
2270                         rssi -= 64;
2271                 else if (lna == 2)
2272                         rssi -= 86;
2273                 else if (lna == 3)
2274                         rssi -= 100;
2275         }
2276         return rssi;
2277 }