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 $ */
5 * Copyright (c) 2005, 2006
6 * Damien Bergamini <damien.bergamini@free.fr>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Ralink Technology RT2500USB chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/sysctl.h>
38 #include <net/ethernet.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>
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>
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>
54 #include "if_uralreg.h"
55 #include "if_uralvar.h"
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)
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,
66 #define DPRINTFN(n, x)
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 }
94 MODULE_DEPEND(ural, wlan, 1, 1, 1);
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,
108 Static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
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,
126 Static void ural_set_testmode(struct ural_softc *);
127 Static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
129 Static uint16_t ural_read(struct ural_softc *, uint16_t);
130 Static void ural_read_multi(struct ural_softc *, uint16_t, void *,
132 Static void ural_write(struct ural_softc *, uint16_t, uint16_t);
133 Static void ural_write_multi(struct ural_softc *, uint16_t, void *,
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,
165 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
167 static const struct ieee80211_rateset ural_rateset_11a =
168 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
170 static const struct ieee80211_rateset ural_rateset_11b =
171 { 4, { 2, 4, 11, 22 } };
173 static const struct ieee80211_rateset ural_rateset_11g =
174 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
177 * Default values for MAC registers; values taken from the reference driver.
179 static const struct {
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 }
203 * Default values for BBP registers; values taken from the reference driver.
205 static const struct {
244 * Default values for RF register R2 indexed by channel numbers.
246 static const uint32_t ural_rf2522_r2[] = {
247 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
248 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
251 static const uint32_t ural_rf2523_r2[] = {
252 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
253 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
256 static const uint32_t ural_rf2524_r2[] = {
257 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
258 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
261 static const uint32_t ural_rf2525_r2[] = {
262 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
263 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
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
271 static const uint32_t ural_rf2525e_r2[] = {
272 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
273 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
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
281 static const uint32_t ural_rf2526_r2[] = {
282 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
283 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
287 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
288 * values taken from the reference driver.
290 static const struct {
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 },
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 },
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 },
332 { 149, 0x08808, 0x02429, 0x00281 },
333 { 153, 0x08808, 0x0242b, 0x00281 },
334 { 157, 0x08808, 0x0242d, 0x00281 },
335 { 161, 0x08808, 0x0242f, 0x00281 }
338 USB_DECLARE_DRIVER(ural);
342 USB_MATCH_START(ural, uaa);
344 if (uaa->iface != NULL)
347 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
348 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
353 USB_ATTACH_START(ural, sc, uaa);
355 struct ieee80211com *ic = &sc->sc_ic;
356 usb_interface_descriptor_t *id;
357 usb_endpoint_descriptor_t *ed;
362 sc->sc_udev = uaa->device;
363 sc->sc_tx_retries = 7; /* TODO tunable/sysctl */
365 usbd_devinfo(sc->sc_udev, 0, devinfo);
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;
374 /* get the first interface handle */
375 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
378 kprintf("%s: could not get interface handle\n",
379 USBDEVNAME(sc->sc_dev));
380 USB_ATTACH_ERROR_RETURN;
386 id = usbd_get_interface_descriptor(sc->sc_iface);
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);
392 kprintf("%s: no endpoint descriptor for %d\n",
393 USBDEVNAME(sc->sc_dev), i);
394 USB_ATTACH_ERROR_RETURN;
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;
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;
410 mtx_init(&sc->sc_mtx, USBDEVNAME(sc->sc_dev), MTX_NETWORK_LOCK,
411 MTX_DEF | MTX_RECURSE);
414 usb_init_task(&sc->sc_task, ural_task, sc);
415 callout_init(&sc->scan_ch);
416 callout_init(&sc->stats_ch);
418 /* retrieve RT2570 rev. no */
419 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
421 /* retrieve MAC address and various other things from EEPROM */
422 ural_read_eeprom(sc);
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));
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);
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;
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;
452 /* set device capabilities */
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 */
462 if (sc->rf_rev == RAL_RF_5222) {
463 /* set supported .11a rates */
464 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
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;
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;
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;
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;
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;
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;
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);
507 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
508 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
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);
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);
519 ieee80211_announce(ic);
521 USB_ATTACH_SUCCESS_RETURN;
526 USB_DETACH_START(ural, sc);
527 struct ieee80211com *ic = &sc->sc_ic;
528 struct ifnet *ifp = &ic->ic_if;
530 lwkt_serialize_enter(ifp->if_serializer);
532 callout_stop(&sc->scan_ch);
533 callout_stop(&sc->stats_ch);
535 sc->sc_flags |= URAL_FLAG_SYNCTASK;
538 lwkt_serialize_exit(ifp->if_serializer);
540 usb_rem_task(sc->sc_udev, &sc->sc_task);
542 if (sc->stats_xfer != NULL) {
543 usbd_free_xfer(sc->stats_xfer);
544 sc->stats_xfer = NULL;
547 if (sc->sc_rx_pipeh != NULL) {
548 usbd_abort_pipe(sc->sc_rx_pipeh);
549 usbd_close_pipe(sc->sc_rx_pipeh);
552 if (sc->sc_tx_pipeh != NULL) {
553 usbd_abort_pipe(sc->sc_tx_pipeh);
554 usbd_close_pipe(sc->sc_tx_pipeh);
557 ural_free_rx_list(sc);
558 ural_free_tx_list(sc);
561 ieee80211_ifdetach(ic);
564 mtx_destroy(&sc->sc_mtx);
571 ural_alloc_tx_list(struct ural_softc *sc)
573 struct ural_tx_data *data;
578 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
579 data = &sc->tx_data[i];
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));
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));
603 fail: ural_free_tx_list(sc);
608 ural_free_tx_list(struct ural_softc *sc)
610 struct ural_tx_data *data;
613 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
614 data = &sc->tx_data[i];
616 if (data->xfer != NULL) {
617 usbd_free_xfer(data->xfer);
621 if (data->ni != NULL) {
622 ieee80211_free_node(data->ni);
629 ural_alloc_rx_list(struct ural_softc *sc)
631 struct ural_rx_data *data;
634 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
635 data = &sc->rx_data[i];
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));
647 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
648 kprintf("%s: could not allocate rx buffer\n",
649 USBDEVNAME(sc->sc_dev));
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));
662 data->buf = mtod(data->m, uint8_t *);
667 fail: ural_free_tx_list(sc);
672 ural_free_rx_list(struct ural_softc *sc)
674 struct ural_rx_data *data;
677 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
678 data = &sc->rx_data[i];
680 if (data->xfer != NULL) {
681 usbd_free_xfer(data->xfer);
685 if (data->m != NULL) {
693 ural_media_change(struct ifnet *ifp)
695 struct ural_softc *sc = ifp->if_softc;
698 error = ieee80211_media_change(ifp);
699 if (error != ENETRESET)
702 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
709 * This function is called periodically (every 200ms) during scanning to
710 * switch from one channel to another.
713 ural_next_scan(void *arg)
715 struct ural_softc *sc = arg;
716 struct ieee80211com *ic = &sc->sc_ic;
717 struct ifnet *ifp = &ic->ic_if;
719 lwkt_serialize_enter(ifp->if_serializer);
721 if (ic->ic_state == IEEE80211_S_SCAN)
722 ieee80211_next_scan(ic);
724 lwkt_serialize_exit(ifp->if_serializer);
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;
737 lwkt_serialize_enter(ifp->if_serializer);
739 ieee80211_ratectl_newstate(ic, sc->sc_state);
741 ostate = ic->ic_state;
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);
749 /* force tx led to stop blinking */
750 ural_write(sc, RAL_MAC_CSR20, 0);
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);
759 case IEEE80211_S_AUTH:
760 ural_set_chan(sc, ic->ic_curchan);
763 case IEEE80211_S_ASSOC:
764 ural_set_chan(sc, ic->ic_curchan);
767 case IEEE80211_S_RUN:
768 ural_set_chan(sc, ic->ic_curchan);
772 lwkt_serialize_exit(ifp->if_serializer);
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);
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);
785 kprintf("%s: could not allocate beacon\n",
786 USBDEVNAME(sc->sc_dev));
790 if (ural_tx_bcn(sc, m, ni) != 0) {
791 kprintf("%s: could not send beacon\n",
792 USBDEVNAME(sc->sc_dev));
797 /* make tx led blink on tx (controlled by ASIC) */
798 ural_write(sc, RAL_MAC_CSR20, 1);
800 if (ic->ic_opmode != IEEE80211_M_MONITOR)
801 ural_enable_tsf_sync(sc);
803 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
804 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
806 lwkt_serialize_enter(ifp->if_serializer);
808 callout_reset(&sc->stats_ch, 4 * hz / 5,
809 ural_stats_timeout, sc);
814 sc->sc_newstate(ic, sc->sc_state, -1);
816 lwkt_serialize_exit(ifp->if_serializer);
820 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
822 struct ifnet *ifp = &ic->ic_if;
823 struct ural_softc *sc = ifp->if_softc;
825 ASSERT_SERIALIZED(ifp->if_serializer);
827 callout_stop(&sc->scan_ch);
828 callout_stop(&sc->stats_ch);
830 /* do it in a process context */
831 sc->sc_state = nstate;
833 lwkt_serialize_exit(ifp->if_serializer);
834 usb_rem_task(sc->sc_udev, &sc->sc_task);
836 if (sc->sc_flags & URAL_FLAG_SYNCTASK) {
837 usb_do_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER,
840 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
842 lwkt_serialize_enter(ifp->if_serializer);
847 /* quickly determine if a given rate is CCK or OFDM */
848 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
850 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
851 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
853 #define RAL_SIFS 10 /* us */
855 #define RAL_RXTX_TURNAROUND 5 /* us */
858 * This function is only used by the Rx radiotap code.
861 ural_rxrate(struct ural_rx_desc *desc)
863 if (le32toh(desc->flags) & RAL_RX_OFDM) {
864 /* reverse function of ural_plcp_signal */
865 switch (desc->rate) {
873 case 0xc: return 108;
876 if (desc->rate == 10)
878 if (desc->rate == 20)
880 if (desc->rate == 55)
882 if (desc->rate == 110)
885 return 2; /* should not get there */
889 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
891 struct ural_tx_data *data = priv;
892 struct ural_softc *sc = data->sc;
893 struct ifnet *ifp = &sc->sc_ic.ic_if;
895 if (status != USBD_NORMAL_COMPLETION) {
896 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
899 kprintf("%s: could not transmit buffer: %s\n",
900 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
902 if (status == USBD_STALLED)
903 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
909 lwkt_serialize_enter(ifp->if_serializer);
913 ieee80211_free_node(data->ni);
919 DPRINTFN(10, ("tx done\n"));
922 ifp->if_flags &= ~IFF_OACTIVE;
925 lwkt_serialize_exit(ifp->if_serializer);
929 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
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;
941 if (status != USBD_NORMAL_COMPLETION) {
942 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
945 if (status == USBD_STALLED)
946 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
950 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
952 if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
953 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
959 /* rx descriptor is located at the end */
960 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
962 if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
963 (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
965 * This should not happen since we did not request to receive
966 * those frames when we filled RAL_TXRX_CSR2.
968 DPRINTFN(5, ("PHY or CRC error\n"));
973 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
981 data->buf = mtod(data->m, uint8_t *);
984 m->m_pkthdr.rcvif = ifp;
985 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
987 lwkt_serialize_enter(ifp->if_serializer);
989 if (sc->sc_drvbpf != NULL) {
990 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
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;
999 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1002 /* trim CRC here so WEP can find its own CRC at the end of packet. */
1003 m_adj(m, -IEEE80211_CRC_LEN);
1005 wh = mtod(m, struct ieee80211_frame *);
1006 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1008 /* send the frame to the 802.11 layer */
1009 ieee80211_input(ic, m, ni, desc->rssi, 0);
1011 /* node is no longer needed */
1012 ieee80211_free_node(ni);
1014 DPRINTFN(15, ("rx done\n"));
1016 lwkt_serialize_exit(ifp->if_serializer);
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);
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.
1029 ural_ack_rate(struct ieee80211com *ic, int rate)
1038 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1054 /* default to 1Mbps */
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.
1064 ural_txtime(int len, int rate, uint32_t flags)
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;
1073 /* IEEE Std 802.11b-1999, pp. 28 */
1074 txtime = (16 * len + rate - 1) / rate;
1075 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1084 ural_plcp_signal(int rate)
1087 /* CCK rates (returned values are device-dependent) */
1090 case 11: return 0x2;
1091 case 22: return 0x3;
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;
1103 /* unsupported rates (should not get there) */
1104 default: return 0xff;
1109 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1110 uint32_t flags, int len, int rate)
1112 struct ieee80211com *ic = &sc->sc_ic;
1113 uint16_t plcp_length;
1116 desc->flags = htole32(flags);
1117 desc->flags |= htole32(RAL_TX_NEWSEQ);
1118 desc->flags |= htole32(len << 16);
1120 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1121 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1123 /* setup PLCP fields */
1124 desc->plcp_signal = ural_plcp_signal(rate);
1125 desc->plcp_service = 4;
1127 len += IEEE80211_CRC_LEN;
1128 if (RAL_RATE_IS_OFDM(rate)) {
1129 desc->flags |= htole32(RAL_TX_OFDM);
1131 plcp_length = len & 0xfff;
1132 desc->plcp_length_hi = plcp_length >> 6;
1133 desc->plcp_length_lo = plcp_length & 0x3f;
1135 plcp_length = (16 * len + rate - 1) / rate;
1137 remainder = (16 * len) % 22;
1138 if (remainder != 0 && remainder < 7)
1139 desc->plcp_service |= RAL_PLCP_LENGEXT;
1141 desc->plcp_length_hi = plcp_length >> 8;
1142 desc->plcp_length_lo = plcp_length & 0xff;
1144 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1145 desc->plcp_signal |= 0x08;
1152 #define RAL_TX_TIMEOUT 5000
1155 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1157 struct ural_tx_desc *desc;
1158 usbd_xfer_handle xfer;
1164 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1166 xfer = usbd_alloc_xfer(sc->sc_udev);
1170 /* xfer length needs to be a multiple of two! */
1171 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1173 buf = usbd_alloc_buffer(xfer, xferlen);
1175 usbd_free_xfer(xfer);
1179 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1180 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1182 error = usbd_sync_transfer(xfer);
1184 usbd_free_xfer(xfer);
1188 desc = (struct ural_tx_desc *)buf;
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);
1194 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1195 m0->m_pkthdr.len, rate, xferlen));
1197 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1198 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1200 error = usbd_sync_transfer(xfer);
1201 usbd_free_xfer(xfer);
1207 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1209 struct ieee80211com *ic = &sc->sc_ic;
1210 struct ural_tx_desc *desc;
1211 struct ural_tx_data *data;
1212 struct ieee80211_frame *wh;
1218 data = &sc->tx_data[0];
1219 desc = (struct ural_tx_desc *)data->buf;
1221 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1226 wh = mtod(m0, struct ieee80211_frame *);
1228 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1229 flags |= RAL_TX_ACK;
1231 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
1232 *(uint16_t *)wh->i_dur = htole16(dur);
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;
1242 if (sc->sc_drvbpf != NULL) {
1243 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
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;
1251 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
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);
1257 /* align end on a 2-bytes boundary */
1258 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1261 * No space left in the last URB to store the extra 2 bytes, force
1262 * sending of another URB.
1264 if ((xferlen % 64) == 0)
1267 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1268 m0->m_pkthdr.len, rate, xferlen));
1270 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1271 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1274 error = usbd_transfer(data->xfer);
1275 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1284 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
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;
1294 int xferlen, rate, rate_idx;
1296 wh = mtod(m0, struct ieee80211_frame *);
1298 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1299 rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1301 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1302 k = ieee80211_crypto_encap(ic, ni, m0);
1308 /* packet header may have moved, reset our local pointer */
1309 wh = mtod(m0, struct ieee80211_frame *);
1312 data = &sc->tx_data[0];
1313 desc = (struct ural_tx_desc *)data->buf;
1318 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1319 flags |= RAL_TX_ACK;
1320 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
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);
1327 if (sc->sc_drvbpf != NULL) {
1328 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
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;
1336 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
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);
1342 /* align end on a 2-bytes boundary */
1343 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1346 * No space left in the last URB to store the extra 2 bytes, force
1347 * sending of another URB.
1349 if ((xferlen % 64) == 0)
1352 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1353 m0->m_pkthdr.len, rate, xferlen));
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,
1359 error = usbd_transfer(data->xfer);
1360 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1369 ural_start(struct ifnet *ifp)
1371 struct ural_softc *sc = ifp->if_softc;
1372 struct ieee80211com *ic = &sc->sc_ic;
1374 struct ether_header *eh;
1375 struct ieee80211_node *ni;
1377 ASSERT_SERIALIZED(ifp->if_serializer);
1379 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING)
1383 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1384 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1385 ifp->if_flags |= IFF_OACTIVE;
1388 IF_DEQUEUE(&ic->ic_mgtq, m0);
1390 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1391 m0->m_pkthdr.rcvif = NULL;
1393 if (ic->ic_rawbpf != NULL)
1394 bpf_mtap(ic->ic_rawbpf, m0);
1396 if (ural_tx_mgt(sc, m0, ni) != 0)
1400 if (ic->ic_state != IEEE80211_S_RUN)
1402 m0 = ifq_poll(&ifp->if_snd);
1405 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1406 ifp->if_flags |= IFF_OACTIVE;
1410 ifq_dequeue(&ifp->if_snd, m0);
1412 if (m0->m_len < sizeof (struct ether_header) &&
1413 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1416 eh = mtod(m0, struct ether_header *);
1417 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1424 m0 = ieee80211_encap(ic, m0, ni);
1426 ieee80211_free_node(ni);
1430 if (ic->ic_rawbpf != NULL)
1431 bpf_mtap(ic->ic_rawbpf, m0);
1433 if (ural_tx_data(sc, m0, ni) != 0) {
1434 ieee80211_free_node(ni);
1440 sc->sc_tx_timer = 5;
1446 ural_watchdog(struct ifnet *ifp)
1448 struct ural_softc *sc = ifp->if_softc;
1449 struct ieee80211com *ic = &sc->sc_ic;
1451 ASSERT_SERIALIZED(ifp->if_serializer);
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! */
1465 ieee80211_watchdog(ic);
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.
1474 ural_reset(struct ifnet *ifp)
1476 struct ural_softc *sc = ifp->if_softc;
1477 struct ieee80211com *ic = &sc->sc_ic;
1479 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1482 ural_set_chan(sc, ic->ic_curchan);
1488 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1490 struct ural_softc *sc = ifp->if_softc;
1491 struct ieee80211com *ic = &sc->sc_ic;
1494 ASSERT_SERIALIZED(ifp->if_serializer);
1498 if (ifp->if_flags & IFF_UP) {
1499 if (ifp->if_flags & IFF_RUNNING)
1500 ural_update_promisc(sc);
1504 if (ifp->if_flags & IFF_RUNNING)
1510 error = ieee80211_ioctl(ic, cmd, data, cr);
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)
1524 ural_set_testmode(struct ural_softc *sc)
1526 usb_device_request_t req;
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);
1535 error = usbd_do_request(sc->sc_udev, &req, NULL);
1537 kprintf("%s: could not set test mode: %s\n",
1538 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1543 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1545 usb_device_request_t req;
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);
1554 error = usbd_do_request(sc->sc_udev, &req, buf);
1556 kprintf("%s: could not read EEPROM: %s\n",
1557 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1562 ural_read(struct ural_softc *sc, uint16_t reg)
1564 usb_device_request_t req;
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));
1574 error = usbd_do_request(sc->sc_udev, &req, &val);
1576 kprintf("%s: could not read MAC register: %s\n",
1577 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1581 return le16toh(val);
1585 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1587 usb_device_request_t req;
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);
1596 error = usbd_do_request(sc->sc_udev, &req, buf);
1598 kprintf("%s: could not read MAC register: %s\n",
1599 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1604 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1606 usb_device_request_t req;
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);
1615 error = usbd_do_request(sc->sc_udev, &req, NULL);
1617 kprintf("%s: could not write MAC register: %s\n",
1618 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1623 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1625 usb_device_request_t req;
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);
1634 error = usbd_do_request(sc->sc_udev, &req, buf);
1636 kprintf("%s: could not write MAC register: %s\n",
1637 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1642 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1647 for (ntries = 0; ntries < 5; ntries++) {
1648 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1652 kprintf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1656 tmp = reg << 8 | val;
1657 ural_write(sc, RAL_PHY_CSR7, tmp);
1661 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1666 val = RAL_BBP_WRITE | reg << 8;
1667 ural_write(sc, RAL_PHY_CSR7, val);
1669 for (ntries = 0; ntries < 5; ntries++) {
1670 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1674 kprintf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1678 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1682 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1687 for (ntries = 0; ntries < 5; ntries++) {
1688 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1692 kprintf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
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);
1700 /* remember last written value in sc */
1701 sc->rf_regs[reg] = val;
1703 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1707 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1709 struct ieee80211com *ic = &sc->sc_ic;
1710 struct ifnet *ifp = &ic->ic_if;
1714 ASSERT_SERIALIZED(ifp->if_serializer);
1716 chan = ieee80211_chan2ieee(ic, c);
1717 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1720 lwkt_serialize_exit(ifp->if_serializer);
1722 if (IEEE80211_IS_CHAN_2GHZ(c))
1723 power = min(sc->txpow[chan - 1], 31);
1727 /* adjust txpower using ifconfig settings */
1728 power -= (100 - ic->ic_txpowlimit) / 8;
1730 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1732 switch (sc->rf_rev) {
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);
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);
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);
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);
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);
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);
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);
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);
1784 for (i = 0; ural_rf5222[i].chan != chan; i++)
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);
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);
1799 tmp &= ~RAL_JAPAN_FILTER;
1801 tmp |= RAL_JAPAN_FILTER;
1803 ural_bbp_write(sc, 70, tmp);
1805 /* clear CRC errors */
1806 ural_read(sc, RAL_STA_CSR0);
1809 ural_disable_rf_tune(sc);
1812 lwkt_serialize_enter(ifp->if_serializer);
1816 * Disable RF auto-tuning.
1819 ural_disable_rf_tune(struct ural_softc *sc)
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);
1828 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1829 ural_rf_write(sc, RAL_RF3, tmp);
1831 DPRINTFN(2, ("disabling RF autotune\n"));
1835 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1839 ural_enable_tsf_sync(struct ural_softc *sc)
1841 struct ieee80211com *ic = &sc->sc_ic;
1842 uint16_t logcwmin, preload, tmp;
1844 /* first, disable TSF synchronization */
1845 ural_write(sc, RAL_TXRX_CSR19, 0);
1847 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1848 ural_write(sc, RAL_TXRX_CSR18, tmp);
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);
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);
1860 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1861 ural_write(sc, RAL_TXRX_CSR19, tmp);
1863 DPRINTF(("enabling TSF synchronization\n"));
1867 ural_update_slot(struct ifnet *ifp)
1869 struct ural_softc *sc = ifp->if_softc;
1870 struct ieee80211com *ic = &sc->sc_ic;
1871 uint16_t slottime, sifs, eifs;
1873 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1876 * These settings may sound a bit inconsistent but this is what the
1877 * reference driver does.
1879 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1880 sifs = 16 - RAL_RXTX_TURNAROUND;
1883 sifs = 10 - RAL_RXTX_TURNAROUND;
1887 ural_write(sc, RAL_MAC_CSR10, slottime);
1888 ural_write(sc, RAL_MAC_CSR11, sifs);
1889 ural_write(sc, RAL_MAC_CSR12, eifs);
1893 ural_set_txpreamble(struct ural_softc *sc)
1897 tmp = ural_read(sc, RAL_TXRX_CSR10);
1899 tmp &= ~RAL_SHORT_PREAMBLE;
1900 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1901 tmp |= RAL_SHORT_PREAMBLE;
1903 ural_write(sc, RAL_TXRX_CSR10, tmp);
1907 ural_set_basicrates(struct ural_softc *sc)
1909 struct ieee80211com *ic = &sc->sc_ic;
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);
1919 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1920 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1925 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1929 tmp = bssid[0] | bssid[1] << 8;
1930 ural_write(sc, RAL_MAC_CSR5, tmp);
1932 tmp = bssid[2] | bssid[3] << 8;
1933 ural_write(sc, RAL_MAC_CSR6, tmp);
1935 tmp = bssid[4] | bssid[5] << 8;
1936 ural_write(sc, RAL_MAC_CSR7, tmp);
1938 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
1942 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1946 tmp = addr[0] | addr[1] << 8;
1947 ural_write(sc, RAL_MAC_CSR2, tmp);
1949 tmp = addr[2] | addr[3] << 8;
1950 ural_write(sc, RAL_MAC_CSR3, tmp);
1952 tmp = addr[4] | addr[5] << 8;
1953 ural_write(sc, RAL_MAC_CSR4, tmp);
1955 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
1959 ural_update_promisc(struct ural_softc *sc)
1961 struct ifnet *ifp = &sc->sc_ic.ic_if;
1964 tmp = ural_read(sc, RAL_TXRX_CSR2);
1966 tmp &= ~RAL_DROP_NOT_TO_ME;
1967 if (!(ifp->if_flags & IFF_PROMISC))
1968 tmp |= RAL_DROP_NOT_TO_ME;
1970 ural_write(sc, RAL_TXRX_CSR2, tmp);
1972 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1973 "entering" : "leaving"));
1977 ural_get_rf(int 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";
1992 ural_read_eeprom(struct ural_softc *sc)
1994 struct ieee80211com *ic = &sc->sc_ic;
1997 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
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;
2006 /* read MAC address */
2007 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2009 /* read default values for BBP registers */
2010 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2012 /* read Tx power for all b/g channels */
2013 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2017 ural_bbp_init(struct ural_softc *sc)
2019 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2022 /* wait for BBP to be ready */
2023 for (ntries = 0; ntries < 100; ntries++) {
2024 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2028 if (ntries == 100) {
2029 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
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);
2038 /* initialize BBP registers to values stored in EEPROM */
2039 for (i = 0; i < 16; i++) {
2040 if (sc->bbp_prom[i].reg == 0xff)
2042 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2051 ural_set_txantenna(struct ural_softc *sc, int antenna)
2056 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2059 else if (antenna == 2)
2062 tx |= RAL_BBP_DIVERSITY;
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;
2069 ural_bbp_write(sc, RAL_BBP_TX, tx);
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));
2075 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2076 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2080 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2084 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2087 else if (antenna == 2)
2090 rx |= RAL_BBP_DIVERSITY;
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;
2096 ural_bbp_write(sc, RAL_BBP_RX, rx);
2100 ural_init(void *priv)
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;
2112 ASSERT_SERIALIZED(ifp->if_serializer);
2114 ural_set_testmode(sc);
2115 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
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);
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))
2131 if (ntries == 100) {
2132 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
2133 USBDEVNAME(sc->sc_dev));
2138 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2140 /* set basic rate set (will be updated later) */
2141 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2143 if (ural_bbp_init(sc) != 0)
2146 /* set default BSS channel */
2147 ural_set_chan(sc, ic->ic_curchan);
2149 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2150 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2152 ural_set_txantenna(sc, sc->tx_ant);
2153 ural_set_rxantenna(sc, sc->rx_ant);
2155 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2156 ural_set_macaddr(sc, ic->ic_myaddr);
2159 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
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);
2168 * Allocate xfer for AMRR statistics requests.
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));
2178 * Open Tx and Rx USB bulk pipes.
2180 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2183 kprintf("%s: could not open Tx pipe: %s\n",
2184 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2188 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2191 kprintf("%s: could not open Rx pipe: %s\n",
2192 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2197 * Allocate Tx and Rx xfer queues.
2199 error = ural_alloc_tx_list(sc);
2201 kprintf("%s: could not allocate Tx list\n",
2202 USBDEVNAME(sc->sc_dev));
2206 error = ural_alloc_rx_list(sc);
2208 kprintf("%s: could not allocate Rx list\n",
2209 USBDEVNAME(sc->sc_dev));
2214 * Start up the receive pipe.
2216 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2217 data = &sc->rx_data[i];
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);
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;
2233 ural_write(sc, RAL_TXRX_CSR2, tmp);
2235 ifp->if_flags &= ~IFF_OACTIVE;
2236 ifp->if_flags |= IFF_RUNNING;
2238 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2239 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
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);
2245 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2249 fail: ural_stop(sc);
2254 ural_stop(struct ural_softc *sc)
2256 struct ieee80211com *ic = &sc->sc_ic;
2257 struct ifnet *ifp = &ic->ic_if;
2259 ASSERT_SERIALIZED(ifp->if_serializer);
2261 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2263 sc->sc_tx_timer = 0;
2265 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2268 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
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);
2274 if (sc->stats_xfer != NULL) {
2275 usbd_free_xfer(sc->stats_xfer);
2276 sc->stats_xfer = NULL;
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;
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;
2291 ural_free_rx_list(sc);
2292 ural_free_tx_list(sc);
2296 ural_stats_timeout(void *arg)
2298 struct ural_softc *sc = (struct ural_softc *)arg;
2299 struct ifnet *ifp = &sc->sc_ic.ic_if;
2300 usb_device_request_t req;
2302 lwkt_serialize_enter(ifp->if_serializer);
2305 * Asynchronously read statistic registers (cleared by read).
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));
2313 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2314 USBD_DEFAULT_TIMEOUT, &req,
2315 sc->sta, sizeof(sc->sta), 0,
2317 usbd_transfer(sc->stats_xfer);
2319 lwkt_serialize_exit(ifp->if_serializer);
2323 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
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;
2330 if (status != USBD_NORMAL_COMPLETION) {
2331 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2332 "cancelling automatic rate control\n");
2336 lwkt_serialize_enter(ifp->if_serializer);
2338 /* count TX retry-fail as Tx errors */
2339 ifp->if_oerrors += sc->sta[RAL_TX_PKT_FAIL];
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];
2345 stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2347 stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2349 stats->stats_short_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2352 * XXX Estimated average:
2353 * Actual number of retries for each packet should belong to
2354 * [2, sc->sc_tx_retries]
2356 stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2357 ((2 + sc->sc_tx_retries) / 2);
2359 stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2361 stats->stats_short_retries +=
2362 sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2364 callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2366 lwkt_serialize_exit(ifp->if_serializer);
2370 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2371 struct ieee80211_ratectl_stats *stats)
2373 struct ifnet *ifp = &ic->ic_if;
2374 struct ural_softc *sc = ifp->if_softc;
2376 ASSERT_SERIALIZED(ifp->if_serializer);
2378 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2379 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2383 ural_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2385 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2386 struct ieee80211_onoe_param *oparam;
2388 if (st->rc_st_param != NULL) {
2389 kfree(st->rc_st_param, M_DEVBUF);
2390 st->rc_st_param = NULL;
2394 case IEEE80211_RATECTL_ONOE:
2395 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2397 IEEE80211_ONOE_PARAM_SETUP(oparam);
2398 oparam->onoe_raise = 20;
2400 st->rc_st_param = oparam;
2402 case IEEE80211_RATECTL_NONE:
2403 /* This could only happen during detaching */
2406 panic("unknown rate control algo %u\n", nrc);
2410 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);