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