Add support for RT2500USB devices.
[dragonfly.git] / sys / dev / netif / ural / if_ural.c
1 /*      $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.10.2.8 2006/07/08 07:48:43 maxim Exp $  */
2 /*      $DragonFly: src/sys/dev/netif/ural/if_ural.c,v 1.1 2006/12/10 02:53:34 sephe Exp $      */
3
4 /*-
5  * Copyright (c) 2005, 2006
6  *      Damien Bergamini <damien.bergamini@free.fr>
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 RT2500USB 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/socket.h>
34 #include <sys/sockio.h>
35
36 #include <net/bpf.h>
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/ifq_var.h>
43
44 #include <netproto/802_11/ieee80211_var.h>
45 #include <netproto/802_11/ieee80211_radiotap.h>
46 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
47
48 #include <bus/usb/usb.h>
49 #include <bus/usb/usbdi.h>
50 #include <bus/usb/usbdi_util.h>
51 #include <bus/usb/usbdevs.h>
52
53 #include "if_uralreg.h"
54 #include "if_uralvar.h"
55
56 #ifdef USB_DEBUG
57 #define DPRINTF(x)      do { if (uraldebug > 0) logprintf x; } while (0)
58 #define DPRINTFN(n, x)  do { if (uraldebug >= (n)) logprintf x; } while (0)
59 int uraldebug = 0;
60 SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
61 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &uraldebug, 0,
62     "ural debug level");
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n, x)
66 #endif
67
68 /* various supported device vendors/products */
69 static const struct usb_devno ural_devs[] = {
70         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_WL167G },
71         { USB_VENDOR_ASUS,              USB_PRODUCT_RALINK_RT2570 },
72         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D7050 },
73         { USB_VENDOR_CONCEPTRONIC,      USB_PRODUCT_CONCEPTRONIC_C54U },
74         { USB_VENDOR_DLINK,             USB_PRODUCT_DLINK_DWLG122 },
75         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWBKG },
76         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254 },
77         { USB_VENDOR_LINKSYS4,          USB_PRODUCT_LINKSYS4_WUSB54G },
78         { USB_VENDOR_LINKSYS4,          USB_PRODUCT_LINKSYS4_WUSB54GP },
79         { USB_VENDOR_LINKSYS4,          USB_PRODUCT_LINKSYS4_HU200TS },
80         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_KG54 },
81         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_KG54AI },
82         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_KG54YB },
83         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_NINWIFI },
84         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2570 },
85         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2570_2 },
86         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2570_3 },
87         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2570 },
88         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2570_2 },
89         { USB_VENDOR_VTECH,             USB_PRODUCT_VTECH_RT2570 },
90         { USB_VENDOR_ZINWELL,           USB_PRODUCT_ZINWELL_RT2570 }
91 };
92
93 MODULE_DEPEND(ural, wlan, 1, 1, 1);
94
95 Static int              ural_alloc_tx_list(struct ural_softc *);
96 Static void             ural_free_tx_list(struct ural_softc *);
97 Static int              ural_alloc_rx_list(struct ural_softc *);
98 Static void             ural_free_rx_list(struct ural_softc *);
99 Static int              ural_media_change(struct ifnet *);
100 Static void             ural_next_scan(void *);
101 Static void             ural_task(void *);
102 Static int              ural_newstate(struct ieee80211com *,
103                             enum ieee80211_state, int);
104 Static int              ural_rxrate(struct ural_rx_desc *);
105 Static void             ural_txeof(usbd_xfer_handle, usbd_private_handle,
106                             usbd_status);
107 Static void             ural_rxeof(usbd_xfer_handle, usbd_private_handle,
108                             usbd_status);
109 Static int              ural_ack_rate(struct ieee80211com *, int);
110 Static uint16_t         ural_txtime(int, int, uint32_t);
111 Static uint8_t          ural_plcp_signal(int);
112 Static void             ural_setup_tx_desc(struct ural_softc *,
113                             struct ural_tx_desc *, uint32_t, int, int);
114 Static int              ural_tx_bcn(struct ural_softc *, struct mbuf *,
115                             struct ieee80211_node *);
116 Static int              ural_tx_mgt(struct ural_softc *, struct mbuf *,
117                             struct ieee80211_node *);
118 Static int              ural_tx_data(struct ural_softc *, struct mbuf *,
119                             struct ieee80211_node *);
120 Static void             ural_start(struct ifnet *);
121 Static void             ural_watchdog(struct ifnet *);
122 Static int              ural_reset(struct ifnet *);
123 Static int              ural_ioctl(struct ifnet *, u_long, caddr_t,
124                             struct ucred *);
125 Static void             ural_set_testmode(struct ural_softc *);
126 Static void             ural_eeprom_read(struct ural_softc *, uint16_t, void *,
127                             int);
128 Static uint16_t         ural_read(struct ural_softc *, uint16_t);
129 Static void             ural_read_multi(struct ural_softc *, uint16_t, void *,
130                             int);
131 Static void             ural_write(struct ural_softc *, uint16_t, uint16_t);
132 Static void             ural_write_multi(struct ural_softc *, uint16_t, void *,
133                             int);
134 Static void             ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
135 Static uint8_t          ural_bbp_read(struct ural_softc *, uint8_t);
136 Static void             ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
137 Static void             ural_set_chan(struct ural_softc *,
138                             struct ieee80211_channel *);
139 Static void             ural_disable_rf_tune(struct ural_softc *);
140 Static void             ural_enable_tsf_sync(struct ural_softc *);
141 Static void             ural_update_slot(struct ifnet *);
142 Static void             ural_set_txpreamble(struct ural_softc *);
143 Static void             ural_set_basicrates(struct ural_softc *);
144 Static void             ural_set_bssid(struct ural_softc *, uint8_t *);
145 Static void             ural_set_macaddr(struct ural_softc *, uint8_t *);
146 Static void             ural_update_promisc(struct ural_softc *);
147 Static const char       *ural_get_rf(int);
148 Static void             ural_read_eeprom(struct ural_softc *);
149 Static int              ural_bbp_init(struct ural_softc *);
150 Static void             ural_set_txantenna(struct ural_softc *, int);
151 Static void             ural_set_rxantenna(struct ural_softc *, int);
152 Static void             ural_init(void *);
153 Static void             ural_stop(struct ural_softc *);
154 Static void             ural_stats(struct ieee80211com *,
155                                    struct ieee80211_node *,
156                                    struct ieee80211_ratectl_stats *);
157 Static void             ural_stats_update(usbd_xfer_handle,
158                                           usbd_private_handle, usbd_status);
159 Static void             ural_stats_timeout(void *);
160 Static void             ural_ratectl_change(struct ieee80211com *ic, u_int,
161                                             u_int);
162
163 /*
164  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
165  */
166 static const struct ieee80211_rateset ural_rateset_11a =
167         { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
168
169 static const struct ieee80211_rateset ural_rateset_11b =
170         { 4, { 2, 4, 11, 22 } };
171
172 static const struct ieee80211_rateset ural_rateset_11g =
173         { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
174
175 /*
176  * Default values for MAC registers; values taken from the reference driver.
177  */
178 static const struct {
179         uint16_t        reg;
180         uint16_t        val;
181 } ural_def_mac[] = {
182         { RAL_TXRX_CSR5,  0x8c8d },
183         { RAL_TXRX_CSR6,  0x8b8a },
184         { RAL_TXRX_CSR7,  0x8687 },
185         { RAL_TXRX_CSR8,  0x0085 },
186         { RAL_MAC_CSR13,  0x1111 },
187         { RAL_MAC_CSR14,  0x1e11 },
188         { RAL_TXRX_CSR21, 0xe78f },
189         { RAL_MAC_CSR9,   0xff1d },
190         { RAL_MAC_CSR11,  0x0002 },
191         { RAL_MAC_CSR22,  0x0053 },
192         { RAL_MAC_CSR15,  0x0000 },
193         { RAL_MAC_CSR8,   0x0780 },
194         { RAL_TXRX_CSR19, 0x0000 },
195         { RAL_TXRX_CSR18, 0x005a },
196         { RAL_PHY_CSR2,   0x0000 },
197         { RAL_TXRX_CSR0,  0x1ec0 },
198         { RAL_PHY_CSR4,   0x000f }
199 };
200
201 /*
202  * Default values for BBP registers; values taken from the reference driver.
203  */
204 static const struct {
205         uint8_t reg;
206         uint8_t val;
207 } ural_def_bbp[] = {
208         {  3, 0x02 },
209         {  4, 0x19 },
210         { 14, 0x1c },
211         { 15, 0x30 },
212         { 16, 0xac },
213         { 17, 0x48 },
214         { 18, 0x18 },
215         { 19, 0xff },
216         { 20, 0x1e },
217         { 21, 0x08 },
218         { 22, 0x08 },
219         { 23, 0x08 },
220         { 24, 0x80 },
221         { 25, 0x50 },
222         { 26, 0x08 },
223         { 27, 0x23 },
224         { 30, 0x10 },
225         { 31, 0x2b },
226         { 32, 0xb9 },
227         { 34, 0x12 },
228         { 35, 0x50 },
229         { 39, 0xc4 },
230         { 40, 0x02 },
231         { 41, 0x60 },
232         { 53, 0x10 },
233         { 54, 0x18 },
234         { 56, 0x08 },
235         { 57, 0x10 },
236         { 58, 0x08 },
237         { 61, 0x60 },
238         { 62, 0x10 },
239         { 75, 0xff }
240 };
241
242 /*
243  * Default values for RF register R2 indexed by channel numbers.
244  */
245 static const uint32_t ural_rf2522_r2[] = {
246         0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
247         0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
248 };
249
250 static const uint32_t ural_rf2523_r2[] = {
251         0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
252         0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
253 };
254
255 static const uint32_t ural_rf2524_r2[] = {
256         0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
257         0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
258 };
259
260 static const uint32_t ural_rf2525_r2[] = {
261         0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
262         0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
263 };
264
265 static const uint32_t ural_rf2525_hi_r2[] = {
266         0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
267         0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
268 };
269
270 static const uint32_t ural_rf2525e_r2[] = {
271         0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
272         0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
273 };
274
275 static const uint32_t ural_rf2526_hi_r2[] = {
276         0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
277         0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
278 };
279
280 static const uint32_t ural_rf2526_r2[] = {
281         0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
282         0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
283 };
284
285 /*
286  * For dual-band RF, RF registers R1 and R4 also depend on channel number;
287  * values taken from the reference driver.
288  */
289 static const struct {
290         uint8_t         chan;
291         uint32_t        r1;
292         uint32_t        r2;
293         uint32_t        r4;
294 } ural_rf5222[] = {
295         {   1, 0x08808, 0x0044d, 0x00282 },
296         {   2, 0x08808, 0x0044e, 0x00282 },
297         {   3, 0x08808, 0x0044f, 0x00282 },
298         {   4, 0x08808, 0x00460, 0x00282 },
299         {   5, 0x08808, 0x00461, 0x00282 },
300         {   6, 0x08808, 0x00462, 0x00282 },
301         {   7, 0x08808, 0x00463, 0x00282 },
302         {   8, 0x08808, 0x00464, 0x00282 },
303         {   9, 0x08808, 0x00465, 0x00282 },
304         {  10, 0x08808, 0x00466, 0x00282 },
305         {  11, 0x08808, 0x00467, 0x00282 },
306         {  12, 0x08808, 0x00468, 0x00282 },
307         {  13, 0x08808, 0x00469, 0x00282 },
308         {  14, 0x08808, 0x0046b, 0x00286 },
309
310         {  36, 0x08804, 0x06225, 0x00287 },
311         {  40, 0x08804, 0x06226, 0x00287 },
312         {  44, 0x08804, 0x06227, 0x00287 },
313         {  48, 0x08804, 0x06228, 0x00287 },
314         {  52, 0x08804, 0x06229, 0x00287 },
315         {  56, 0x08804, 0x0622a, 0x00287 },
316         {  60, 0x08804, 0x0622b, 0x00287 },
317         {  64, 0x08804, 0x0622c, 0x00287 },
318
319         { 100, 0x08804, 0x02200, 0x00283 },
320         { 104, 0x08804, 0x02201, 0x00283 },
321         { 108, 0x08804, 0x02202, 0x00283 },
322         { 112, 0x08804, 0x02203, 0x00283 },
323         { 116, 0x08804, 0x02204, 0x00283 },
324         { 120, 0x08804, 0x02205, 0x00283 },
325         { 124, 0x08804, 0x02206, 0x00283 },
326         { 128, 0x08804, 0x02207, 0x00283 },
327         { 132, 0x08804, 0x02208, 0x00283 },
328         { 136, 0x08804, 0x02209, 0x00283 },
329         { 140, 0x08804, 0x0220a, 0x00283 },
330
331         { 149, 0x08808, 0x02429, 0x00281 },
332         { 153, 0x08808, 0x0242b, 0x00281 },
333         { 157, 0x08808, 0x0242d, 0x00281 },
334         { 161, 0x08808, 0x0242f, 0x00281 }
335 };
336
337 USB_DECLARE_DRIVER(ural);
338
339 USB_MATCH(ural)
340 {
341         USB_MATCH_START(ural, uaa);
342
343         if (uaa->iface != NULL)
344                 return UMATCH_NONE;
345
346         return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
347             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
348 }
349
350 USB_ATTACH(ural)
351 {
352         USB_ATTACH_START(ural, sc, uaa);
353         struct ifnet *ifp;
354         struct ieee80211com *ic = &sc->sc_ic;
355         usb_interface_descriptor_t *id;
356         usb_endpoint_descriptor_t *ed;
357         usbd_status error;
358         char devinfo[1024];
359         int i;
360
361         sc->sc_udev = uaa->device;
362         sc->sc_tx_retries = 7;  /* TODO tunable/sysctl */
363
364         usbd_devinfo(sc->sc_udev, 0, devinfo);
365         USB_ATTACH_SETUP;
366
367         if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
368                 printf("%s: could not set configuration no\n",
369                     USBDEVNAME(sc->sc_dev));
370                 USB_ATTACH_ERROR_RETURN;
371         }
372
373         /* get the first interface handle */
374         error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
375             &sc->sc_iface);
376         if (error != 0) {
377                 printf("%s: could not get interface handle\n",
378                     USBDEVNAME(sc->sc_dev));
379                 USB_ATTACH_ERROR_RETURN;
380         }
381
382         /*
383          * Find endpoints.
384          */
385         id = usbd_get_interface_descriptor(sc->sc_iface);
386
387         sc->sc_rx_no = sc->sc_tx_no = -1;
388         for (i = 0; i < id->bNumEndpoints; i++) {
389                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
390                 if (ed == NULL) {
391                         printf("%s: no endpoint descriptor for %d\n",
392                             USBDEVNAME(sc->sc_dev), i);
393                         USB_ATTACH_ERROR_RETURN;
394                 }
395
396                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
397                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
398                         sc->sc_rx_no = ed->bEndpointAddress;
399                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
400                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
401                         sc->sc_tx_no = ed->bEndpointAddress;
402         }
403         if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
404                 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
405                 USB_ATTACH_ERROR_RETURN;
406         }
407
408 #if 0
409         mtx_init(&sc->sc_mtx, USBDEVNAME(sc->sc_dev), MTX_NETWORK_LOCK,
410             MTX_DEF | MTX_RECURSE);
411 #endif
412
413         usb_init_task(&sc->sc_task, ural_task, sc);
414         callout_init(&sc->scan_ch);
415         callout_init(&sc->stats_ch);
416
417         /* retrieve RT2570 rev. no */
418         sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
419
420         /* retrieve MAC address and various other things from EEPROM */
421         ural_read_eeprom(sc);
422
423         printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
424             USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
425
426         ifp = &ic->ic_if;
427         ifp->if_softc = sc;
428         if_initname(ifp, "ural", USBDEVUNIT(sc->sc_dev));
429         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
430         ifp->if_init = ural_init;
431         ifp->if_ioctl = ural_ioctl;
432         ifp->if_start = ural_start;
433         ifp->if_watchdog = ural_watchdog;
434         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
435         ifq_set_ready(&ifp->if_snd);
436
437         ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
438         ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
439         ic->ic_ratectl.rc_st_valid_stats =
440                 IEEE80211_RATECTL_STATS_PKT_NORETRY |
441                 IEEE80211_RATECTL_STATS_PKT_OK |
442                 IEEE80211_RATECTL_STATS_PKT_ERR |
443                 IEEE80211_RATECTL_STATS_RETRIES;
444         ic->ic_ratectl.rc_st_stats = ural_stats;
445         ic->ic_ratectl.rc_st_change = ural_ratectl_change;
446
447         ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
448         ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
449         ic->ic_state = IEEE80211_S_INIT;
450
451         /* set device capabilities */
452         ic->ic_caps =
453             IEEE80211_C_IBSS |          /* IBSS mode supported */
454             IEEE80211_C_MONITOR |       /* monitor mode supported */
455             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
456             IEEE80211_C_TXPMGT |        /* tx power management */
457             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
458             IEEE80211_C_SHSLOT |        /* short slot time supported */
459             IEEE80211_C_WPA;            /* 802.11i */
460
461         if (sc->rf_rev == RAL_RF_5222) {
462                 /* set supported .11a rates */
463                 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
464
465                 /* set supported .11a channels */
466                 for (i = 36; i <= 64; i += 4) {
467                         ic->ic_channels[i].ic_freq =
468                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
469                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
470                 }
471                 for (i = 100; i <= 140; i += 4) {
472                         ic->ic_channels[i].ic_freq =
473                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
474                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
475                 }
476                 for (i = 149; i <= 161; i += 4) {
477                         ic->ic_channels[i].ic_freq =
478                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
479                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
480                 }
481         }
482
483         /* set supported .11b and .11g rates */
484         ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
485         ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
486
487         /* set supported .11b and .11g channels (1 through 14) */
488         for (i = 1; i <= 14; i++) {
489                 ic->ic_channels[i].ic_freq =
490                     ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
491                 ic->ic_channels[i].ic_flags =
492                     IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
493                     IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
494         }
495
496         ieee80211_ifattach(ic);
497         ic->ic_reset = ural_reset;
498         /* enable s/w bmiss handling in sta mode */
499         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
500
501         /* override state transition machine */
502         sc->sc_newstate = ic->ic_newstate;
503         ic->ic_newstate = ural_newstate;
504         ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
505
506         bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
507             sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
508
509         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
510         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
511         sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
512
513         sc->sc_txtap_len = sizeof sc->sc_txtapu;
514         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
515         sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
516
517         if (bootverbose)
518                 ieee80211_announce(ic);
519
520         USB_ATTACH_SUCCESS_RETURN;
521 }
522
523 USB_DETACH(ural)
524 {
525         USB_DETACH_START(ural, sc);
526         struct ieee80211com *ic = &sc->sc_ic;
527         struct ifnet *ifp = &ic->ic_if;
528
529         lwkt_serialize_enter(ifp->if_serializer);
530
531         callout_stop(&sc->scan_ch);
532         callout_stop(&sc->stats_ch);
533
534         sc->sc_flags |= URAL_FLAG_SYNCTASK;
535         ural_stop(sc);
536
537         lwkt_serialize_exit(ifp->if_serializer);
538
539         usb_rem_task(sc->sc_udev, &sc->sc_task);
540
541         if (sc->stats_xfer != NULL) {
542                 usbd_free_xfer(sc->stats_xfer);
543                 sc->stats_xfer = NULL;
544         }
545
546         if (sc->sc_rx_pipeh != NULL) {
547                 usbd_abort_pipe(sc->sc_rx_pipeh);
548                 usbd_close_pipe(sc->sc_rx_pipeh);
549         }
550
551         if (sc->sc_tx_pipeh != NULL) {
552                 usbd_abort_pipe(sc->sc_tx_pipeh);
553                 usbd_close_pipe(sc->sc_tx_pipeh);
554         }
555
556         ural_free_rx_list(sc);
557         ural_free_tx_list(sc);
558
559         bpfdetach(ifp);
560         ieee80211_ifdetach(ic);
561
562 #if 0
563         mtx_destroy(&sc->sc_mtx);
564 #endif
565
566         return 0;
567 }
568
569 Static int
570 ural_alloc_tx_list(struct ural_softc *sc)
571 {
572         struct ural_tx_data *data;
573         int i, error;
574
575         sc->tx_queued = 0;
576
577         for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
578                 data = &sc->tx_data[i];
579
580                 data->sc = sc;
581
582                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
583                 if (data->xfer == NULL) {
584                         printf("%s: could not allocate tx xfer\n",
585                             USBDEVNAME(sc->sc_dev));
586                         error = ENOMEM;
587                         goto fail;
588                 }
589
590                 data->buf = usbd_alloc_buffer(data->xfer,
591                     RAL_TX_DESC_SIZE + MCLBYTES);
592                 if (data->buf == NULL) {
593                         printf("%s: could not allocate tx buffer\n",
594                             USBDEVNAME(sc->sc_dev));
595                         error = ENOMEM;
596                         goto fail;
597                 }
598         }
599
600         return 0;
601
602 fail:   ural_free_tx_list(sc);
603         return error;
604 }
605
606 Static void
607 ural_free_tx_list(struct ural_softc *sc)
608 {
609         struct ural_tx_data *data;
610         int i;
611
612         for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
613                 data = &sc->tx_data[i];
614
615                 if (data->xfer != NULL) {
616                         usbd_free_xfer(data->xfer);
617                         data->xfer = NULL;
618                 }
619
620                 if (data->ni != NULL) {
621                         ieee80211_free_node(data->ni);
622                         data->ni = NULL;
623                 }
624         }
625 }
626
627 Static int
628 ural_alloc_rx_list(struct ural_softc *sc)
629 {
630         struct ural_rx_data *data;
631         int i, error;
632
633         for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
634                 data = &sc->rx_data[i];
635
636                 data->sc = sc;
637
638                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
639                 if (data->xfer == NULL) {
640                         printf("%s: could not allocate rx xfer\n",
641                             USBDEVNAME(sc->sc_dev));
642                         error = ENOMEM;
643                         goto fail;
644                 }
645
646                 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
647                         printf("%s: could not allocate rx buffer\n",
648                             USBDEVNAME(sc->sc_dev));
649                         error = ENOMEM;
650                         goto fail;
651                 }
652
653                 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
654                 if (data->m == NULL) {
655                         printf("%s: could not allocate rx mbuf\n",
656                             USBDEVNAME(sc->sc_dev));
657                         error = ENOMEM;
658                         goto fail;
659                 }
660
661                 data->buf = mtod(data->m, uint8_t *);
662         }
663
664         return 0;
665
666 fail:   ural_free_tx_list(sc);
667         return error;
668 }
669
670 Static void
671 ural_free_rx_list(struct ural_softc *sc)
672 {
673         struct ural_rx_data *data;
674         int i;
675
676         for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
677                 data = &sc->rx_data[i];
678
679                 if (data->xfer != NULL) {
680                         usbd_free_xfer(data->xfer);
681                         data->xfer = NULL;
682                 }
683
684                 if (data->m != NULL) {
685                         m_freem(data->m);
686                         data->m = NULL;
687                 }
688         }
689 }
690
691 Static int
692 ural_media_change(struct ifnet *ifp)
693 {
694         struct ural_softc *sc = ifp->if_softc;
695         int error;
696
697         error = ieee80211_media_change(ifp);
698         if (error != ENETRESET)
699                 return error;
700
701         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
702                 ural_init(sc);
703
704         return 0;
705 }
706
707 /*
708  * This function is called periodically (every 200ms) during scanning to
709  * switch from one channel to another.
710  */
711 Static void
712 ural_next_scan(void *arg)
713 {
714         struct ural_softc *sc = arg;
715         struct ieee80211com *ic = &sc->sc_ic;
716         struct ifnet *ifp = &ic->ic_if;
717
718         lwkt_serialize_enter(ifp->if_serializer);
719
720         if (ic->ic_state == IEEE80211_S_SCAN)
721                 ieee80211_next_scan(ic);
722
723         lwkt_serialize_exit(ifp->if_serializer);
724 }
725
726 Static void
727 ural_task(void *arg)
728 {
729         struct ural_softc *sc = arg;
730         struct ieee80211com *ic = &sc->sc_ic;
731         struct ifnet *ifp = &ic->ic_if;
732         enum ieee80211_state ostate;
733         struct ieee80211_node *ni;
734         struct mbuf *m;
735
736         lwkt_serialize_enter(ifp->if_serializer);
737
738         ieee80211_ratectl_newstate(ic, sc->sc_state);
739
740         ostate = ic->ic_state;
741
742         switch (sc->sc_state) {
743         case IEEE80211_S_INIT:
744                 if (ostate == IEEE80211_S_RUN) {
745                         /* abort TSF synchronization */
746                         ural_write(sc, RAL_TXRX_CSR19, 0);
747
748                         /* force tx led to stop blinking */
749                         ural_write(sc, RAL_MAC_CSR20, 0);
750                 }
751                 break;
752
753         case IEEE80211_S_SCAN:
754                 ural_set_chan(sc, ic->ic_curchan);
755                 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
756                 break;
757
758         case IEEE80211_S_AUTH:
759                 ural_set_chan(sc, ic->ic_curchan);
760                 break;
761
762         case IEEE80211_S_ASSOC:
763                 ural_set_chan(sc, ic->ic_curchan);
764                 break;
765
766         case IEEE80211_S_RUN:
767                 ural_set_chan(sc, ic->ic_curchan);
768
769                 ni = ic->ic_bss;
770
771                 lwkt_serialize_exit(ifp->if_serializer);
772
773                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
774                         ural_update_slot(&ic->ic_if);
775                         ural_set_txpreamble(sc);
776                         ural_set_basicrates(sc);
777                         ural_set_bssid(sc, ni->ni_bssid);
778                 }
779
780                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
781                     ic->ic_opmode == IEEE80211_M_IBSS) {
782                         m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
783                         if (m == NULL) {
784                                 printf("%s: could not allocate beacon\n",
785                                     USBDEVNAME(sc->sc_dev));
786                                 return;
787                         }
788
789                         if (ural_tx_bcn(sc, m, ni) != 0) {
790                                 printf("%s: could not send beacon\n",
791                                     USBDEVNAME(sc->sc_dev));
792                                 return;
793                         }
794                 }
795
796                 /* make tx led blink on tx (controlled by ASIC) */
797                 ural_write(sc, RAL_MAC_CSR20, 1);
798
799                 if (ic->ic_opmode != IEEE80211_M_MONITOR)
800                         ural_enable_tsf_sync(sc);
801
802                 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
803                 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
804
805                 lwkt_serialize_enter(ifp->if_serializer);
806
807                 callout_reset(&sc->stats_ch, 4 * hz / 5,
808                               ural_stats_timeout, sc);
809
810                 break;
811         }
812
813         sc->sc_newstate(ic, sc->sc_state, -1);
814
815         lwkt_serialize_exit(ifp->if_serializer);
816 }
817
818 Static int
819 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
820 {
821         struct ifnet *ifp = &ic->ic_if;
822         struct ural_softc *sc = ifp->if_softc;
823
824         ASSERT_SERIALIZED(ifp->if_serializer);
825
826         callout_stop(&sc->scan_ch);
827         callout_stop(&sc->stats_ch);
828
829         /* do it in a process context */
830         sc->sc_state = nstate;
831
832         lwkt_serialize_exit(ifp->if_serializer);
833         usb_rem_task(sc->sc_udev, &sc->sc_task);
834
835         if (sc->sc_flags & URAL_FLAG_SYNCTASK) {
836                 usb_do_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER,
837                             USBD_NO_TIMEOUT);
838         } else {
839                 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
840         }
841         lwkt_serialize_enter(ifp->if_serializer);
842
843         return 0;
844 }
845
846 /* quickly determine if a given rate is CCK or OFDM */
847 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
848
849 #define RAL_ACK_SIZE    14      /* 10 + 4(FCS) */
850 #define RAL_CTS_SIZE    14      /* 10 + 4(FCS) */
851
852 #define RAL_SIFS                10      /* us */
853
854 #define RAL_RXTX_TURNAROUND     5       /* us */
855
856 /*
857  * This function is only used by the Rx radiotap code.
858  */
859 Static int
860 ural_rxrate(struct ural_rx_desc *desc)
861 {
862         if (le32toh(desc->flags) & RAL_RX_OFDM) {
863                 /* reverse function of ural_plcp_signal */
864                 switch (desc->rate) {
865                 case 0xb:       return 12;
866                 case 0xf:       return 18;
867                 case 0xa:       return 24;
868                 case 0xe:       return 36;
869                 case 0x9:       return 48;
870                 case 0xd:       return 72;
871                 case 0x8:       return 96;
872                 case 0xc:       return 108;
873                 }
874         } else {
875                 if (desc->rate == 10)
876                         return 2;
877                 if (desc->rate == 20)
878                         return 4;
879                 if (desc->rate == 55)
880                         return 11;
881                 if (desc->rate == 110)
882                         return 22;
883         }
884         return 2;       /* should not get there */
885 }
886
887 Static void
888 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
889 {
890         struct ural_tx_data *data = priv;
891         struct ural_softc *sc = data->sc;
892         struct ifnet *ifp = &sc->sc_ic.ic_if;
893
894         if (status != USBD_NORMAL_COMPLETION) {
895                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
896                         return;
897
898                 printf("%s: could not transmit buffer: %s\n",
899                     USBDEVNAME(sc->sc_dev), usbd_errstr(status));
900
901                 if (status == USBD_STALLED)
902                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
903
904                 ifp->if_oerrors++;
905                 return;
906         }
907
908         lwkt_serialize_enter(ifp->if_serializer);
909
910         m_freem(data->m);
911         data->m = NULL;
912         ieee80211_free_node(data->ni);
913         data->ni = NULL;
914
915         sc->tx_queued--;
916         ifp->if_opackets++;
917
918         DPRINTFN(10, ("tx done\n"));
919
920         sc->sc_tx_timer = 0;
921         ifp->if_flags &= ~IFF_OACTIVE;
922         ural_start(ifp);
923
924         lwkt_serialize_exit(ifp->if_serializer);
925 }
926
927 Static void
928 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
929 {
930         struct ural_rx_data *data = priv;
931         struct ural_softc *sc = data->sc;
932         struct ieee80211com *ic = &sc->sc_ic;
933         struct ifnet *ifp = &ic->ic_if;
934         struct ural_rx_desc *desc;
935         struct ieee80211_frame *wh;
936         struct ieee80211_node *ni;
937         struct mbuf *mnew, *m;
938         int len;
939
940         if (status != USBD_NORMAL_COMPLETION) {
941                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
942                         return;
943
944                 if (status == USBD_STALLED)
945                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
946                 goto skip;
947         }
948
949         usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
950
951         if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
952                 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
953                     len));
954                 ifp->if_ierrors++;
955                 goto skip;
956         }
957
958         /* rx descriptor is located at the end */
959         desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
960
961         if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
962             (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
963                 /*
964                  * This should not happen since we did not request to receive
965                  * those frames when we filled RAL_TXRX_CSR2.
966                  */
967                 DPRINTFN(5, ("PHY or CRC error\n"));
968                 ifp->if_ierrors++;
969                 goto skip;
970         }
971
972         mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
973         if (mnew == NULL) {
974                 ifp->if_ierrors++;
975                 goto skip;
976         }
977
978         m = data->m;
979         data->m = mnew;
980         data->buf = mtod(data->m, uint8_t *);
981
982         /* finalize mbuf */
983         m->m_pkthdr.rcvif = ifp;
984         m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
985
986         lwkt_serialize_enter(ifp->if_serializer);
987
988         if (sc->sc_drvbpf != NULL) {
989                 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
990
991                 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; /* h/w leaves FCS */
992                 tap->wr_rate = ural_rxrate(desc);
993                 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
994                 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
995                 tap->wr_antenna = sc->rx_ant;
996                 tap->wr_antsignal = desc->rssi;
997
998                 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
999         }
1000
1001         /* trim CRC here so WEP can find its own CRC at the end of packet. */
1002         m_adj(m, -IEEE80211_CRC_LEN);
1003
1004         wh = mtod(m, struct ieee80211_frame *);
1005         ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1006
1007         /* send the frame to the 802.11 layer */
1008         ieee80211_input(ic, m, ni, desc->rssi, 0);
1009
1010         /* node is no longer needed */
1011         ieee80211_free_node(ni);
1012
1013         DPRINTFN(15, ("rx done\n"));
1014
1015         lwkt_serialize_exit(ifp->if_serializer);
1016
1017 skip:   /* setup a new transfer */
1018         usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
1019             USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
1020         usbd_transfer(xfer);
1021 }
1022
1023 /*
1024  * Return the expected ack rate for a frame transmitted at rate `rate'.
1025  * XXX: this should depend on the destination node basic rate set.
1026  */
1027 Static int
1028 ural_ack_rate(struct ieee80211com *ic, int rate)
1029 {
1030         switch (rate) {
1031         /* CCK rates */
1032         case 2:
1033                 return 2;
1034         case 4:
1035         case 11:
1036         case 22:
1037                 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1038
1039         /* OFDM rates */
1040         case 12:
1041         case 18:
1042                 return 12;
1043         case 24:
1044         case 36:
1045                 return 24;
1046         case 48:
1047         case 72:
1048         case 96:
1049         case 108:
1050                 return 48;
1051         }
1052
1053         /* default to 1Mbps */
1054         return 2;
1055 }
1056
1057 /*
1058  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1059  * The function automatically determines the operating mode depending on the
1060  * given rate. `flags' indicates whether short preamble is in use or not.
1061  */
1062 Static uint16_t
1063 ural_txtime(int len, int rate, uint32_t flags)
1064 {
1065         uint16_t txtime;
1066
1067         if (RAL_RATE_IS_OFDM(rate)) {
1068                 /* IEEE Std 802.11a-1999, pp. 37 */
1069                 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1070                 txtime = 16 + 4 + 4 * txtime + 6;
1071         } else {
1072                 /* IEEE Std 802.11b-1999, pp. 28 */
1073                 txtime = (16 * len + rate - 1) / rate;
1074                 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1075                         txtime +=  72 + 24;
1076                 else
1077                         txtime += 144 + 48;
1078         }
1079         return txtime;
1080 }
1081
1082 Static uint8_t
1083 ural_plcp_signal(int rate)
1084 {
1085         switch (rate) {
1086         /* CCK rates (returned values are device-dependent) */
1087         case 2:         return 0x0;
1088         case 4:         return 0x1;
1089         case 11:        return 0x2;
1090         case 22:        return 0x3;
1091
1092         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1093         case 12:        return 0xb;
1094         case 18:        return 0xf;
1095         case 24:        return 0xa;
1096         case 36:        return 0xe;
1097         case 48:        return 0x9;
1098         case 72:        return 0xd;
1099         case 96:        return 0x8;
1100         case 108:       return 0xc;
1101
1102         /* unsupported rates (should not get there) */
1103         default:        return 0xff;
1104         }
1105 }
1106
1107 Static void
1108 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1109     uint32_t flags, int len, int rate)
1110 {
1111         struct ieee80211com *ic = &sc->sc_ic;
1112         uint16_t plcp_length;
1113         int remainder;
1114
1115         desc->flags = htole32(flags);
1116         desc->flags |= htole32(RAL_TX_NEWSEQ);
1117         desc->flags |= htole32(len << 16);
1118
1119         desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1120         desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1121
1122         /* setup PLCP fields */
1123         desc->plcp_signal  = ural_plcp_signal(rate);
1124         desc->plcp_service = 4;
1125
1126         len += IEEE80211_CRC_LEN;
1127         if (RAL_RATE_IS_OFDM(rate)) {
1128                 desc->flags |= htole32(RAL_TX_OFDM);
1129
1130                 plcp_length = len & 0xfff;
1131                 desc->plcp_length_hi = plcp_length >> 6;
1132                 desc->plcp_length_lo = plcp_length & 0x3f;
1133         } else {
1134                 plcp_length = (16 * len + rate - 1) / rate;
1135                 if (rate == 22) {
1136                         remainder = (16 * len) % 22;
1137                         if (remainder != 0 && remainder < 7)
1138                                 desc->plcp_service |= RAL_PLCP_LENGEXT;
1139                 }
1140                 desc->plcp_length_hi = plcp_length >> 8;
1141                 desc->plcp_length_lo = plcp_length & 0xff;
1142
1143                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1144                         desc->plcp_signal |= 0x08;
1145         }
1146
1147         desc->iv = 0;
1148         desc->eiv = 0;
1149 }
1150
1151 #define RAL_TX_TIMEOUT  5000
1152
1153 Static int
1154 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1155 {
1156         struct ural_tx_desc *desc;
1157         usbd_xfer_handle xfer;
1158         uint8_t cmd = 0;
1159         usbd_status error;
1160         uint8_t *buf;
1161         int xferlen, rate;
1162
1163         rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1164
1165         xfer = usbd_alloc_xfer(sc->sc_udev);
1166         if (xfer == NULL)
1167                 return ENOMEM;
1168
1169         /* xfer length needs to be a multiple of two! */
1170         xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1171
1172         buf = usbd_alloc_buffer(xfer, xferlen);
1173         if (buf == NULL) {
1174                 usbd_free_xfer(xfer);
1175                 return ENOMEM;
1176         }
1177
1178         usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1179             USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1180
1181         error = usbd_sync_transfer(xfer);
1182         if (error != 0) {
1183                 usbd_free_xfer(xfer);
1184                 return error;
1185         }
1186
1187         desc = (struct ural_tx_desc *)buf;
1188
1189         m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1190         ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1191             m0->m_pkthdr.len, rate);
1192
1193         DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1194             m0->m_pkthdr.len, rate, xferlen));
1195
1196         usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1197             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1198
1199         error = usbd_sync_transfer(xfer);
1200         usbd_free_xfer(xfer);
1201
1202         return error;
1203 }
1204
1205 Static int
1206 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1207 {
1208         struct ieee80211com *ic = &sc->sc_ic;
1209         struct ural_tx_desc *desc;
1210         struct ural_tx_data *data;
1211         struct ieee80211_frame *wh;
1212         uint32_t flags = 0;
1213         uint16_t dur;
1214         usbd_status error;
1215         int xferlen, rate;
1216
1217         data = &sc->tx_data[0];
1218         desc = (struct ural_tx_desc *)data->buf;
1219
1220         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1221
1222         data->m = m0;
1223         data->ni = ni;
1224
1225         wh = mtod(m0, struct ieee80211_frame *);
1226
1227         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1228                 flags |= RAL_TX_ACK;
1229
1230                 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
1231                 *(uint16_t *)wh->i_dur = htole16(dur);
1232
1233                 /* tell hardware to add timestamp for probe responses */
1234                 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1235                     IEEE80211_FC0_TYPE_MGT &&
1236                     (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1237                     IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1238                         flags |= RAL_TX_TIMESTAMP;
1239         }
1240
1241         if (sc->sc_drvbpf != NULL) {
1242                 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1243
1244                 tap->wt_flags = 0;
1245                 tap->wt_rate = rate;
1246                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1247                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1248                 tap->wt_antenna = sc->tx_ant;
1249
1250                 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1251         }
1252
1253         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1254         ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1255
1256         /* align end on a 2-bytes boundary */
1257         xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1258
1259         /*
1260          * No space left in the last URB to store the extra 2 bytes, force
1261          * sending of another URB.
1262          */
1263         if ((xferlen % 64) == 0)
1264                 xferlen += 2;
1265
1266         DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1267             m0->m_pkthdr.len, rate, xferlen));
1268
1269         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1270             xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1271             ural_txeof);
1272
1273         error = usbd_transfer(data->xfer);
1274         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1275                 return error;
1276
1277         sc->tx_queued++;
1278
1279         return 0;
1280 }
1281
1282 Static int
1283 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1284 {
1285         struct ieee80211com *ic = &sc->sc_ic;
1286         struct ural_tx_desc *desc;
1287         struct ural_tx_data *data;
1288         struct ieee80211_frame *wh;
1289         struct ieee80211_key *k;
1290         uint32_t flags = 0;
1291         uint16_t dur;
1292         usbd_status error;
1293         int xferlen, rate, rate_idx;
1294
1295         wh = mtod(m0, struct ieee80211_frame *);
1296
1297         ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1298         rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1299
1300         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1301                 k = ieee80211_crypto_encap(ic, ni, m0);
1302                 if (k == NULL) {
1303                         m_freem(m0);
1304                         return ENOBUFS;
1305                 }
1306
1307                 /* packet header may have moved, reset our local pointer */
1308                 wh = mtod(m0, struct ieee80211_frame *);
1309         }
1310
1311         data = &sc->tx_data[0];
1312         desc = (struct ural_tx_desc *)data->buf;
1313
1314         data->m = m0;
1315         data->ni = ni;
1316
1317         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1318                 flags |= RAL_TX_ACK;
1319                 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
1320
1321                 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(ic, rate),
1322                     ic->ic_flags) + RAL_SIFS;
1323                 *(uint16_t *)wh->i_dur = htole16(dur);
1324         }
1325
1326         if (sc->sc_drvbpf != NULL) {
1327                 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1328
1329                 tap->wt_flags = 0;
1330                 tap->wt_rate = rate;
1331                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1332                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1333                 tap->wt_antenna = sc->tx_ant;
1334
1335                 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1336         }
1337
1338         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1339         ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1340
1341         /* align end on a 2-bytes boundary */
1342         xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1343
1344         /*
1345          * No space left in the last URB to store the extra 2 bytes, force
1346          * sending of another URB.
1347          */
1348         if ((xferlen % 64) == 0)
1349                 xferlen += 2;
1350
1351         DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1352             m0->m_pkthdr.len, rate, xferlen));
1353
1354         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1355             xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1356             ural_txeof);
1357
1358         error = usbd_transfer(data->xfer);
1359         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1360                 return error;
1361
1362         sc->tx_queued++;
1363
1364         return 0;
1365 }
1366
1367 Static void
1368 ural_start(struct ifnet *ifp)
1369 {
1370         struct ural_softc *sc = ifp->if_softc;
1371         struct ieee80211com *ic = &sc->sc_ic;
1372         struct mbuf *m0;
1373         struct ether_header *eh;
1374         struct ieee80211_node *ni;
1375
1376         ASSERT_SERIALIZED(ifp->if_serializer);
1377
1378         if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING)
1379                 return;
1380
1381         for (;;) {
1382                 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1383                         if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1384                                 ifp->if_flags |= IFF_OACTIVE;
1385                                 break;
1386                         }
1387                         IF_DEQUEUE(&ic->ic_mgtq, m0);
1388
1389                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1390                         m0->m_pkthdr.rcvif = NULL;
1391
1392                         if (ic->ic_rawbpf != NULL)
1393                                 bpf_mtap(ic->ic_rawbpf, m0);
1394
1395                         if (ural_tx_mgt(sc, m0, ni) != 0)
1396                                 break;
1397
1398                 } else {
1399                         if (ic->ic_state != IEEE80211_S_RUN)
1400                                 break;
1401                         m0 = ifq_poll(&ifp->if_snd);
1402                         if (m0 == NULL)
1403                                 break;
1404                         if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1405                                 ifp->if_flags |= IFF_OACTIVE;
1406                                 break;
1407                         }
1408
1409                         ifq_dequeue(&ifp->if_snd, m0);
1410
1411                         if (m0->m_len < sizeof (struct ether_header) &&
1412                             !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1413                                 continue;
1414
1415                         eh = mtod(m0, struct ether_header *);
1416                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1417                         if (ni == NULL) {
1418                                 m_freem(m0);
1419                                 continue;
1420                         }
1421                         BPF_MTAP(ifp, m0);
1422
1423                         m0 = ieee80211_encap(ic, m0, ni);
1424                         if (m0 == NULL) {
1425                                 ieee80211_free_node(ni);
1426                                 continue;
1427                         }
1428
1429                         if (ic->ic_rawbpf != NULL)
1430                                 bpf_mtap(ic->ic_rawbpf, m0);
1431
1432                         if (ural_tx_data(sc, m0, ni) != 0) {
1433                                 ieee80211_free_node(ni);
1434                                 ifp->if_oerrors++;
1435                                 break;
1436                         }
1437                 }
1438
1439                 sc->sc_tx_timer = 5;
1440                 ifp->if_timer = 1;
1441         }
1442 }
1443
1444 Static void
1445 ural_watchdog(struct ifnet *ifp)
1446 {
1447         struct ural_softc *sc = ifp->if_softc;
1448         struct ieee80211com *ic = &sc->sc_ic;
1449
1450         ASSERT_SERIALIZED(ifp->if_serializer);
1451
1452         ifp->if_timer = 0;
1453
1454         if (sc->sc_tx_timer > 0) {
1455                 if (--sc->sc_tx_timer == 0) {
1456                         device_printf(sc->sc_dev, "device timeout\n");
1457                         /*ural_init(sc); XXX needs a process context! */
1458                         ifp->if_oerrors++;
1459                         return;
1460                 }
1461                 ifp->if_timer = 1;
1462         }
1463
1464         ieee80211_watchdog(ic);
1465 }
1466
1467 /*
1468  * This function allows for fast channel switching in monitor mode (used by
1469  * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1470  * generate a new beacon frame.
1471  */
1472 Static int
1473 ural_reset(struct ifnet *ifp)
1474 {
1475         struct ural_softc *sc = ifp->if_softc;
1476         struct ieee80211com *ic = &sc->sc_ic;
1477
1478         if (ic->ic_opmode != IEEE80211_M_MONITOR)
1479                 return ENETRESET;
1480
1481         ural_set_chan(sc, ic->ic_curchan);
1482
1483         return 0;
1484 }
1485
1486 Static int
1487 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1488 {
1489         struct ural_softc *sc = ifp->if_softc;
1490         struct ieee80211com *ic = &sc->sc_ic;
1491         int error = 0;
1492
1493         ASSERT_SERIALIZED(ifp->if_serializer);
1494
1495         switch (cmd) {
1496         case SIOCSIFFLAGS:
1497                 if (ifp->if_flags & IFF_UP) {
1498                         if (ifp->if_flags & IFF_RUNNING)
1499                                 ural_update_promisc(sc);
1500                         else
1501                                 ural_init(sc);
1502                 } else {
1503                         if (ifp->if_flags & IFF_RUNNING)
1504                                 ural_stop(sc);
1505                 }
1506                 break;
1507
1508         default:
1509                 error = ieee80211_ioctl(ic, cmd, data, cr);
1510         }
1511
1512         if (error == ENETRESET) {
1513                 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1514                     (IFF_UP | IFF_RUNNING) &&
1515                     ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1516                         ural_init(sc);
1517                 error = 0;
1518         }
1519         return error;
1520 }
1521
1522 Static void
1523 ural_set_testmode(struct ural_softc *sc)
1524 {
1525         usb_device_request_t req;
1526         usbd_status error;
1527
1528         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1529         req.bRequest = RAL_VENDOR_REQUEST;
1530         USETW(req.wValue, 4);
1531         USETW(req.wIndex, 1);
1532         USETW(req.wLength, 0);
1533
1534         error = usbd_do_request(sc->sc_udev, &req, NULL);
1535         if (error != 0) {
1536                 printf("%s: could not set test mode: %s\n",
1537                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1538         }
1539 }
1540
1541 Static void
1542 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1543 {
1544         usb_device_request_t req;
1545         usbd_status error;
1546
1547         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1548         req.bRequest = RAL_READ_EEPROM;
1549         USETW(req.wValue, 0);
1550         USETW(req.wIndex, addr);
1551         USETW(req.wLength, len);
1552
1553         error = usbd_do_request(sc->sc_udev, &req, buf);
1554         if (error != 0) {
1555                 printf("%s: could not read EEPROM: %s\n",
1556                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1557         }
1558 }
1559
1560 Static uint16_t
1561 ural_read(struct ural_softc *sc, uint16_t reg)
1562 {
1563         usb_device_request_t req;
1564         usbd_status error;
1565         uint16_t val;
1566
1567         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1568         req.bRequest = RAL_READ_MAC;
1569         USETW(req.wValue, 0);
1570         USETW(req.wIndex, reg);
1571         USETW(req.wLength, sizeof (uint16_t));
1572
1573         error = usbd_do_request(sc->sc_udev, &req, &val);
1574         if (error != 0) {
1575                 printf("%s: could not read MAC register: %s\n",
1576                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1577                 return 0;
1578         }
1579
1580         return le16toh(val);
1581 }
1582
1583 Static void
1584 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1585 {
1586         usb_device_request_t req;
1587         usbd_status error;
1588
1589         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1590         req.bRequest = RAL_READ_MULTI_MAC;
1591         USETW(req.wValue, 0);
1592         USETW(req.wIndex, reg);
1593         USETW(req.wLength, len);
1594
1595         error = usbd_do_request(sc->sc_udev, &req, buf);
1596         if (error != 0) {
1597                 printf("%s: could not read MAC register: %s\n",
1598                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1599         }
1600 }
1601
1602 Static void
1603 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1604 {
1605         usb_device_request_t req;
1606         usbd_status error;
1607
1608         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1609         req.bRequest = RAL_WRITE_MAC;
1610         USETW(req.wValue, val);
1611         USETW(req.wIndex, reg);
1612         USETW(req.wLength, 0);
1613
1614         error = usbd_do_request(sc->sc_udev, &req, NULL);
1615         if (error != 0) {
1616                 printf("%s: could not write MAC register: %s\n",
1617                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1618         }
1619 }
1620
1621 Static void
1622 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1623 {
1624         usb_device_request_t req;
1625         usbd_status error;
1626
1627         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1628         req.bRequest = RAL_WRITE_MULTI_MAC;
1629         USETW(req.wValue, 0);
1630         USETW(req.wIndex, reg);
1631         USETW(req.wLength, len);
1632
1633         error = usbd_do_request(sc->sc_udev, &req, buf);
1634         if (error != 0) {
1635                 printf("%s: could not write MAC register: %s\n",
1636                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1637         }
1638 }
1639
1640 Static void
1641 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1642 {
1643         uint16_t tmp;
1644         int ntries;
1645
1646         for (ntries = 0; ntries < 5; ntries++) {
1647                 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1648                         break;
1649         }
1650         if (ntries == 5) {
1651                 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1652                 return;
1653         }
1654
1655         tmp = reg << 8 | val;
1656         ural_write(sc, RAL_PHY_CSR7, tmp);
1657 }
1658
1659 Static uint8_t
1660 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1661 {
1662         uint16_t val;
1663         int ntries;
1664
1665         val = RAL_BBP_WRITE | reg << 8;
1666         ural_write(sc, RAL_PHY_CSR7, val);
1667
1668         for (ntries = 0; ntries < 5; ntries++) {
1669                 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1670                         break;
1671         }
1672         if (ntries == 5) {
1673                 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1674                 return 0;
1675         }
1676
1677         return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1678 }
1679
1680 Static void
1681 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1682 {
1683         uint32_t tmp;
1684         int ntries;
1685
1686         for (ntries = 0; ntries < 5; ntries++) {
1687                 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1688                         break;
1689         }
1690         if (ntries == 5) {
1691                 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1692                 return;
1693         }
1694
1695         tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1696         ural_write(sc, RAL_PHY_CSR9,  tmp & 0xffff);
1697         ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1698
1699         /* remember last written value in sc */
1700         sc->rf_regs[reg] = val;
1701
1702         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1703 }
1704
1705 Static void
1706 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1707 {
1708         struct ieee80211com *ic = &sc->sc_ic;
1709         struct ifnet *ifp = &ic->ic_if;
1710         uint8_t power, tmp;
1711         u_int i, chan;
1712
1713         ASSERT_SERIALIZED(ifp->if_serializer);
1714
1715         chan = ieee80211_chan2ieee(ic, c);
1716         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1717                 return;
1718
1719         lwkt_serialize_exit(ifp->if_serializer);
1720
1721         if (IEEE80211_IS_CHAN_2GHZ(c))
1722                 power = min(sc->txpow[chan - 1], 31);
1723         else
1724                 power = 31;
1725
1726         /* adjust txpower using ifconfig settings */
1727         power -= (100 - ic->ic_txpowlimit) / 8;
1728
1729         DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1730
1731         switch (sc->rf_rev) {
1732         case RAL_RF_2522:
1733                 ural_rf_write(sc, RAL_RF1, 0x00814);
1734                 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1735                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1736                 break;
1737
1738         case RAL_RF_2523:
1739                 ural_rf_write(sc, RAL_RF1, 0x08804);
1740                 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1741                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1742                 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1743                 break;
1744
1745         case RAL_RF_2524:
1746                 ural_rf_write(sc, RAL_RF1, 0x0c808);
1747                 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1748                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1749                 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1750                 break;
1751
1752         case RAL_RF_2525:
1753                 ural_rf_write(sc, RAL_RF1, 0x08808);
1754                 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1755                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1756                 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1757
1758                 ural_rf_write(sc, RAL_RF1, 0x08808);
1759                 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1760                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1761                 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1762                 break;
1763
1764         case RAL_RF_2525E:
1765                 ural_rf_write(sc, RAL_RF1, 0x08808);
1766                 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1767                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1768                 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1769                 break;
1770
1771         case RAL_RF_2526:
1772                 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1773                 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1774                 ural_rf_write(sc, RAL_RF1, 0x08804);
1775
1776                 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1777                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1778                 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1779                 break;
1780
1781         /* dual-band RF */
1782         case RAL_RF_5222:
1783                 for (i = 0; ural_rf5222[i].chan != chan; i++)
1784                         ; /* EMPTY */
1785
1786                 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1787                 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1788                 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1789                 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1790                 break;
1791         }
1792
1793         if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1794             ic->ic_state != IEEE80211_S_SCAN) {
1795                 /* set Japan filter bit for channel 14 */
1796                 tmp = ural_bbp_read(sc, 70);
1797
1798                 tmp &= ~RAL_JAPAN_FILTER;
1799                 if (chan == 14)
1800                         tmp |= RAL_JAPAN_FILTER;
1801
1802                 ural_bbp_write(sc, 70, tmp);
1803
1804                 /* clear CRC errors */
1805                 ural_read(sc, RAL_STA_CSR0);
1806
1807                 DELAY(10000);
1808                 ural_disable_rf_tune(sc);
1809         }
1810
1811         lwkt_serialize_enter(ifp->if_serializer);
1812 }
1813
1814 /*
1815  * Disable RF auto-tuning.
1816  */
1817 Static void
1818 ural_disable_rf_tune(struct ural_softc *sc)
1819 {
1820         uint32_t tmp;
1821
1822         if (sc->rf_rev != RAL_RF_2523) {
1823                 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1824                 ural_rf_write(sc, RAL_RF1, tmp);
1825         }
1826
1827         tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1828         ural_rf_write(sc, RAL_RF3, tmp);
1829
1830         DPRINTFN(2, ("disabling RF autotune\n"));
1831 }
1832
1833 /*
1834  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1835  * synchronization.
1836  */
1837 Static void
1838 ural_enable_tsf_sync(struct ural_softc *sc)
1839 {
1840         struct ieee80211com *ic = &sc->sc_ic;
1841         uint16_t logcwmin, preload, tmp;
1842
1843         /* first, disable TSF synchronization */
1844         ural_write(sc, RAL_TXRX_CSR19, 0);
1845
1846         tmp = (16 * ic->ic_bss->ni_intval) << 4;
1847         ural_write(sc, RAL_TXRX_CSR18, tmp);
1848
1849         logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1850         preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1851         tmp = logcwmin << 12 | preload;
1852         ural_write(sc, RAL_TXRX_CSR20, tmp);
1853
1854         /* finally, enable TSF synchronization */
1855         tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1856         if (ic->ic_opmode == IEEE80211_M_STA)
1857                 tmp |= RAL_ENABLE_TSF_SYNC(1);
1858         else
1859                 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1860         ural_write(sc, RAL_TXRX_CSR19, tmp);
1861
1862         DPRINTF(("enabling TSF synchronization\n"));
1863 }
1864
1865 Static void
1866 ural_update_slot(struct ifnet *ifp)
1867 {
1868         struct ural_softc *sc = ifp->if_softc;
1869         struct ieee80211com *ic = &sc->sc_ic;
1870         uint16_t slottime, sifs, eifs;
1871
1872         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1873
1874         /*
1875          * These settings may sound a bit inconsistent but this is what the
1876          * reference driver does.
1877          */
1878         if (ic->ic_curmode == IEEE80211_MODE_11B) {
1879                 sifs = 16 - RAL_RXTX_TURNAROUND;
1880                 eifs = 364;
1881         } else {
1882                 sifs = 10 - RAL_RXTX_TURNAROUND;
1883                 eifs = 64;
1884         }
1885
1886         ural_write(sc, RAL_MAC_CSR10, slottime);
1887         ural_write(sc, RAL_MAC_CSR11, sifs);
1888         ural_write(sc, RAL_MAC_CSR12, eifs);
1889 }
1890
1891 Static void
1892 ural_set_txpreamble(struct ural_softc *sc)
1893 {
1894         uint16_t tmp;
1895
1896         tmp = ural_read(sc, RAL_TXRX_CSR10);
1897
1898         tmp &= ~RAL_SHORT_PREAMBLE;
1899         if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1900                 tmp |= RAL_SHORT_PREAMBLE;
1901
1902         ural_write(sc, RAL_TXRX_CSR10, tmp);
1903 }
1904
1905 Static void
1906 ural_set_basicrates(struct ural_softc *sc)
1907 {
1908         struct ieee80211com *ic = &sc->sc_ic;
1909
1910         /* update basic rate set */
1911         if (ic->ic_curmode == IEEE80211_MODE_11B) {
1912                 /* 11b basic rates: 1, 2Mbps */
1913                 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1914         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1915                 /* 11a basic rates: 6, 12, 24Mbps */
1916                 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1917         } else {
1918                 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1919                 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1920         }
1921 }
1922
1923 Static void
1924 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1925 {
1926         uint16_t tmp;
1927
1928         tmp = bssid[0] | bssid[1] << 8;
1929         ural_write(sc, RAL_MAC_CSR5, tmp);
1930
1931         tmp = bssid[2] | bssid[3] << 8;
1932         ural_write(sc, RAL_MAC_CSR6, tmp);
1933
1934         tmp = bssid[4] | bssid[5] << 8;
1935         ural_write(sc, RAL_MAC_CSR7, tmp);
1936
1937         DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
1938 }
1939
1940 Static void
1941 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1942 {
1943         uint16_t tmp;
1944
1945         tmp = addr[0] | addr[1] << 8;
1946         ural_write(sc, RAL_MAC_CSR2, tmp);
1947
1948         tmp = addr[2] | addr[3] << 8;
1949         ural_write(sc, RAL_MAC_CSR3, tmp);
1950
1951         tmp = addr[4] | addr[5] << 8;
1952         ural_write(sc, RAL_MAC_CSR4, tmp);
1953
1954         DPRINTF(("setting MAC address to %6D\n", addr, ":"));
1955 }
1956
1957 Static void
1958 ural_update_promisc(struct ural_softc *sc)
1959 {
1960         struct ifnet *ifp = &sc->sc_ic.ic_if;
1961         uint32_t tmp;
1962
1963         tmp = ural_read(sc, RAL_TXRX_CSR2);
1964
1965         tmp &= ~RAL_DROP_NOT_TO_ME;
1966         if (!(ifp->if_flags & IFF_PROMISC))
1967                 tmp |= RAL_DROP_NOT_TO_ME;
1968
1969         ural_write(sc, RAL_TXRX_CSR2, tmp);
1970
1971         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1972             "entering" : "leaving"));
1973 }
1974
1975 Static const char *
1976 ural_get_rf(int rev)
1977 {
1978         switch (rev) {
1979         case RAL_RF_2522:       return "RT2522";
1980         case RAL_RF_2523:       return "RT2523";
1981         case RAL_RF_2524:       return "RT2524";
1982         case RAL_RF_2525:       return "RT2525";
1983         case RAL_RF_2525E:      return "RT2525e";
1984         case RAL_RF_2526:       return "RT2526";
1985         case RAL_RF_5222:       return "RT5222";
1986         default:                return "unknown";
1987         }
1988 }
1989
1990 Static void
1991 ural_read_eeprom(struct ural_softc *sc)
1992 {
1993         struct ieee80211com *ic = &sc->sc_ic;
1994         uint16_t val;
1995
1996         ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1997         val = le16toh(val);
1998         sc->rf_rev =   (val >> 11) & 0x7;
1999         sc->hw_radio = (val >> 10) & 0x1;
2000         sc->led_mode = (val >> 6)  & 0x7;
2001         sc->rx_ant =   (val >> 4)  & 0x3;
2002         sc->tx_ant =   (val >> 2)  & 0x3;
2003         sc->nb_ant =   val & 0x3;
2004
2005         /* read MAC address */
2006         ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2007
2008         /* read default values for BBP registers */
2009         ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2010
2011         /* read Tx power for all b/g channels */
2012         ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2013 }
2014
2015 Static int
2016 ural_bbp_init(struct ural_softc *sc)
2017 {
2018 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2019         int i, ntries;
2020
2021         /* wait for BBP to be ready */
2022         for (ntries = 0; ntries < 100; ntries++) {
2023                 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2024                         break;
2025                 DELAY(1000);
2026         }
2027         if (ntries == 100) {
2028                 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2029                 return EIO;
2030         }
2031
2032         /* initialize BBP registers to default values */
2033         for (i = 0; i < N(ural_def_bbp); i++)
2034                 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2035
2036 #if 0
2037         /* initialize BBP registers to values stored in EEPROM */
2038         for (i = 0; i < 16; i++) {
2039                 if (sc->bbp_prom[i].reg == 0xff)
2040                         continue;
2041                 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2042         }
2043 #endif
2044
2045         return 0;
2046 #undef N
2047 }
2048
2049 Static void
2050 ural_set_txantenna(struct ural_softc *sc, int antenna)
2051 {
2052         uint16_t tmp;
2053         uint8_t tx;
2054
2055         tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2056         if (antenna == 1)
2057                 tx |= RAL_BBP_ANTA;
2058         else if (antenna == 2)
2059                 tx |= RAL_BBP_ANTB;
2060         else
2061                 tx |= RAL_BBP_DIVERSITY;
2062
2063         /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2064         if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2065             sc->rf_rev == RAL_RF_5222)
2066                 tx |= RAL_BBP_FLIPIQ;
2067
2068         ural_bbp_write(sc, RAL_BBP_TX, tx);
2069
2070         /* update values in PHY_CSR5 and PHY_CSR6 */
2071         tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2072         ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2073
2074         tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2075         ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2076 }
2077
2078 Static void
2079 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2080 {
2081         uint8_t rx;
2082
2083         rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2084         if (antenna == 1)
2085                 rx |= RAL_BBP_ANTA;
2086         else if (antenna == 2)
2087                 rx |= RAL_BBP_ANTB;
2088         else
2089                 rx |= RAL_BBP_DIVERSITY;
2090
2091         /* need to force no I/Q flip for RF 2525e and 2526 */
2092         if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2093                 rx &= ~RAL_BBP_FLIPIQ;
2094
2095         ural_bbp_write(sc, RAL_BBP_RX, rx);
2096 }
2097
2098 Static void
2099 ural_init(void *priv)
2100 {
2101 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2102         struct ural_softc *sc = priv;
2103         struct ieee80211com *ic = &sc->sc_ic;
2104         struct ifnet *ifp = &ic->ic_if;
2105         struct ieee80211_key *wk;
2106         struct ural_rx_data *data;
2107         uint16_t tmp;
2108         usbd_status error;
2109         int i, ntries;
2110
2111         ASSERT_SERIALIZED(ifp->if_serializer);
2112
2113         ural_set_testmode(sc);
2114         ural_write(sc, 0x308, 0x00f0);  /* XXX magic */
2115
2116         ural_stop(sc);
2117
2118         /* initialize MAC registers to default values */
2119         for (i = 0; i < N(ural_def_mac); i++)
2120                 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2121
2122         /* wait for BBP and RF to wake up (this can take a long time!) */
2123         for (ntries = 0; ntries < 100; ntries++) {
2124                 tmp = ural_read(sc, RAL_MAC_CSR17);
2125                 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2126                     (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2127                         break;
2128                 DELAY(1000);
2129         }
2130         if (ntries == 100) {
2131                 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2132                     USBDEVNAME(sc->sc_dev));
2133                 goto fail;
2134         }
2135
2136         /* we're ready! */
2137         ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2138
2139         /* set basic rate set (will be updated later) */
2140         ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2141
2142         if (ural_bbp_init(sc) != 0)
2143                 goto fail;
2144
2145         /* set default BSS channel */
2146         ural_set_chan(sc, ic->ic_curchan);
2147
2148         /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2149         ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2150
2151         ural_set_txantenna(sc, sc->tx_ant);
2152         ural_set_rxantenna(sc, sc->rx_ant);
2153
2154         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2155         ural_set_macaddr(sc, ic->ic_myaddr);
2156
2157         /*
2158          * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
2159          */
2160         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2161                 wk = &ic->ic_crypto.cs_nw_keys[i];
2162                 ural_write_multi(sc, wk->wk_keyix * IEEE80211_KEYBUF_SIZE +
2163                     RAL_SEC_CSR0, wk->wk_key, IEEE80211_KEYBUF_SIZE);
2164         }
2165
2166         /*
2167          * Allocate xfer for AMRR statistics requests.
2168          */
2169         sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
2170         if (sc->stats_xfer == NULL) {
2171                 printf("%s: could not allocate AMRR xfer\n",
2172                     USBDEVNAME(sc->sc_dev));
2173                 goto fail;
2174         }
2175
2176         /*
2177          * Open Tx and Rx USB bulk pipes.
2178          */
2179         error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2180             &sc->sc_tx_pipeh);
2181         if (error != 0) {
2182                 printf("%s: could not open Tx pipe: %s\n",
2183                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2184                 goto fail;
2185         }
2186
2187         error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2188             &sc->sc_rx_pipeh);
2189         if (error != 0) {
2190                 printf("%s: could not open Rx pipe: %s\n",
2191                     USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2192                 goto fail;
2193         }
2194
2195         /*
2196          * Allocate Tx and Rx xfer queues.
2197          */
2198         error = ural_alloc_tx_list(sc);
2199         if (error != 0) {
2200                 printf("%s: could not allocate Tx list\n",
2201                     USBDEVNAME(sc->sc_dev));
2202                 goto fail;
2203         }
2204
2205         error = ural_alloc_rx_list(sc);
2206         if (error != 0) {
2207                 printf("%s: could not allocate Rx list\n",
2208                     USBDEVNAME(sc->sc_dev));
2209                 goto fail;
2210         }
2211
2212         /*
2213          * Start up the receive pipe.
2214          */
2215         for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2216                 data = &sc->rx_data[i];
2217
2218                 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2219                     MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2220                 usbd_transfer(data->xfer);
2221         }
2222
2223         /* kick Rx */
2224         tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2225         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2226                 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2227                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2228                         tmp |= RAL_DROP_TODS;
2229                 if (!(ifp->if_flags & IFF_PROMISC))
2230                         tmp |= RAL_DROP_NOT_TO_ME;
2231         }
2232         ural_write(sc, RAL_TXRX_CSR2, tmp);
2233
2234         ifp->if_flags &= ~IFF_OACTIVE;
2235         ifp->if_flags |= IFF_RUNNING;
2236
2237         /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2238         ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2239
2240         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2241                 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2242                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2243         } else
2244                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2245
2246         return;
2247
2248 fail:   ural_stop(sc);
2249 #undef N
2250 }
2251
2252 Static void
2253 ural_stop(struct ural_softc *sc)
2254 {
2255         struct ieee80211com *ic = &sc->sc_ic;
2256         struct ifnet *ifp = &ic->ic_if;
2257
2258         ASSERT_SERIALIZED(ifp->if_serializer);
2259
2260         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2261
2262         sc->sc_tx_timer = 0;
2263         ifp->if_timer = 0;
2264         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2265
2266         /* disable Rx */
2267         ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2268
2269         /* reset ASIC and BBP (but won't reset MAC registers!) */
2270         ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2271         ural_write(sc, RAL_MAC_CSR1, 0);
2272
2273         if (sc->stats_xfer != NULL) {
2274                 usbd_free_xfer(sc->stats_xfer);
2275                 sc->stats_xfer = NULL;
2276         }
2277
2278         if (sc->sc_rx_pipeh != NULL) {
2279                 usbd_abort_pipe(sc->sc_rx_pipeh);
2280                 usbd_close_pipe(sc->sc_rx_pipeh);
2281                 sc->sc_rx_pipeh = NULL;
2282         }
2283
2284         if (sc->sc_tx_pipeh != NULL) {
2285                 usbd_abort_pipe(sc->sc_tx_pipeh);
2286                 usbd_close_pipe(sc->sc_tx_pipeh);
2287                 sc->sc_tx_pipeh = NULL;
2288         }
2289
2290         ural_free_rx_list(sc);
2291         ural_free_tx_list(sc);
2292 }
2293
2294 Static void
2295 ural_stats_timeout(void *arg)
2296 {
2297         struct ural_softc *sc = (struct ural_softc *)arg;
2298         struct ifnet *ifp = &sc->sc_ic.ic_if;
2299         usb_device_request_t req;
2300
2301         lwkt_serialize_enter(ifp->if_serializer);
2302
2303         /*
2304          * Asynchronously read statistic registers (cleared by read).
2305          */
2306         req.bmRequestType = UT_READ_VENDOR_DEVICE;
2307         req.bRequest = RAL_READ_MULTI_MAC;
2308         USETW(req.wValue, 0);
2309         USETW(req.wIndex, RAL_STA_CSR0);
2310         USETW(req.wLength, sizeof(sc->sta));
2311
2312         usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2313                                 USBD_DEFAULT_TIMEOUT, &req,
2314                                 sc->sta, sizeof(sc->sta), 0,
2315                                 ural_stats_update);
2316         usbd_transfer(sc->stats_xfer);
2317
2318         lwkt_serialize_exit(ifp->if_serializer);
2319 }
2320
2321 Static void
2322 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2323                   usbd_status status)
2324 {
2325         struct ural_softc *sc = (struct ural_softc *)priv;
2326         struct ifnet *ifp = &sc->sc_ic.ic_if;
2327         struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2328
2329         if (status != USBD_NORMAL_COMPLETION) {
2330                 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2331                     "cancelling automatic rate control\n");
2332                 return;
2333         }
2334
2335         lwkt_serialize_enter(ifp->if_serializer);
2336
2337         /* count TX retry-fail as Tx errors */
2338         ifp->if_oerrors += sc->sta[RAL_TX_PKT_FAIL];
2339
2340         stats->stats_pkt_ok += sc->sta[RAL_TX_PKT_NO_RETRY] +
2341                                sc->sta[RAL_TX_PKT_ONE_RETRY] +
2342                                sc->sta[RAL_TX_PKT_MULTI_RETRY];
2343
2344         stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2345
2346         stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2347
2348         stats->stats_short_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2349 #if 1
2350         /*
2351          * XXX Estimated average:
2352          * Actual number of retries for each packet should belong to
2353          * [2, sc->sc_tx_retries]
2354          */
2355         stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2356                                       ((2 + sc->sc_tx_retries) / 2);
2357 #else
2358         stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2359 #endif
2360         stats->stats_short_retries +=
2361                 sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2362
2363         callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2364
2365         lwkt_serialize_exit(ifp->if_serializer);
2366 }
2367
2368 Static void
2369 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2370            struct ieee80211_ratectl_stats *stats)
2371 {
2372         struct ifnet *ifp = &ic->ic_if;
2373         struct ural_softc *sc = ifp->if_softc;
2374
2375         ASSERT_SERIALIZED(ifp->if_serializer);
2376
2377         bcopy(&sc->sc_stats, stats, sizeof(*stats));
2378         bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2379 }
2380
2381 Static void
2382 ural_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2383 {
2384         struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2385         struct ieee80211_onoe_param *oparam;
2386
2387         if (st->rc_st_param != NULL) {
2388                 kfree(st->rc_st_param, M_DEVBUF);
2389                 st->rc_st_param = NULL;
2390         }
2391
2392         switch (nrc) {
2393         case IEEE80211_RATECTL_ONOE:
2394                 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2395
2396                 IEEE80211_ONOE_PARAM_SETUP(oparam);
2397                 oparam->onoe_raise = 20;
2398
2399                 st->rc_st_param = oparam;
2400                 break;
2401         case IEEE80211_RATECTL_NONE:
2402                 /* This could only happen during detaching */
2403                 break;
2404         default:
2405                 panic("unknown rate control algo %u\n", nrc);
2406         }
2407 }
2408
2409 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);