1 /* $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.10.2.8 2006/07/08 07:48:43 maxim Exp $ */
2 /* $DragonFly: src/sys/dev/netif/ural/if_ural.c,v 1.1 2006/12/10 02:53:34 sephe Exp $ */
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>
37 #include <net/ethernet.h>
39 #include <net/if_arp.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/ifq_var.h>
44 #include <netproto/802_11/ieee80211_var.h>
45 #include <netproto/802_11/ieee80211_radiotap.h>
46 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
48 #include <bus/usb/usb.h>
49 #include <bus/usb/usbdi.h>
50 #include <bus/usb/usbdi_util.h>
51 #include <bus/usb/usbdevs.h>
53 #include "if_uralreg.h"
54 #include "if_uralvar.h"
57 #define DPRINTF(x) do { if (uraldebug > 0) logprintf x; } while (0)
58 #define DPRINTFN(n, x) do { if (uraldebug >= (n)) logprintf x; } while (0)
60 SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
61 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &uraldebug, 0,
65 #define DPRINTFN(n, x)
68 /* various supported device vendors/products */
69 static const struct usb_devno ural_devs[] = {
70 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G },
71 { USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570 },
72 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
73 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54U },
74 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122 },
75 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG },
76 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254 },
77 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54G },
78 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54GP },
79 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_HU200TS },
80 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 },
81 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI },
82 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB },
83 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI },
84 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570 },
85 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2 },
86 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3 },
87 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570 },
88 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2 },
89 { USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570 },
90 { USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570 }
93 MODULE_DEPEND(ural, wlan, 1, 1, 1);
95 Static int ural_alloc_tx_list(struct ural_softc *);
96 Static void ural_free_tx_list(struct ural_softc *);
97 Static int ural_alloc_rx_list(struct ural_softc *);
98 Static void ural_free_rx_list(struct ural_softc *);
99 Static int ural_media_change(struct ifnet *);
100 Static void ural_next_scan(void *);
101 Static void ural_task(void *);
102 Static int ural_newstate(struct ieee80211com *,
103 enum ieee80211_state, int);
104 Static int ural_rxrate(struct ural_rx_desc *);
105 Static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
107 Static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
109 Static int ural_ack_rate(struct ieee80211com *, int);
110 Static uint16_t ural_txtime(int, int, uint32_t);
111 Static uint8_t ural_plcp_signal(int);
112 Static void ural_setup_tx_desc(struct ural_softc *,
113 struct ural_tx_desc *, uint32_t, int, int);
114 Static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
115 struct ieee80211_node *);
116 Static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
117 struct ieee80211_node *);
118 Static int ural_tx_data(struct ural_softc *, struct mbuf *,
119 struct ieee80211_node *);
120 Static void ural_start(struct ifnet *);
121 Static void ural_watchdog(struct ifnet *);
122 Static int ural_reset(struct ifnet *);
123 Static int ural_ioctl(struct ifnet *, u_long, caddr_t,
125 Static void ural_set_testmode(struct ural_softc *);
126 Static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
128 Static uint16_t ural_read(struct ural_softc *, uint16_t);
129 Static void ural_read_multi(struct ural_softc *, uint16_t, void *,
131 Static void ural_write(struct ural_softc *, uint16_t, uint16_t);
132 Static void ural_write_multi(struct ural_softc *, uint16_t, void *,
134 Static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
135 Static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
136 Static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
137 Static void ural_set_chan(struct ural_softc *,
138 struct ieee80211_channel *);
139 Static void ural_disable_rf_tune(struct ural_softc *);
140 Static void ural_enable_tsf_sync(struct ural_softc *);
141 Static void ural_update_slot(struct ifnet *);
142 Static void ural_set_txpreamble(struct ural_softc *);
143 Static void ural_set_basicrates(struct ural_softc *);
144 Static void ural_set_bssid(struct ural_softc *, uint8_t *);
145 Static void ural_set_macaddr(struct ural_softc *, uint8_t *);
146 Static void ural_update_promisc(struct ural_softc *);
147 Static const char *ural_get_rf(int);
148 Static void ural_read_eeprom(struct ural_softc *);
149 Static int ural_bbp_init(struct ural_softc *);
150 Static void ural_set_txantenna(struct ural_softc *, int);
151 Static void ural_set_rxantenna(struct ural_softc *, int);
152 Static void ural_init(void *);
153 Static void ural_stop(struct ural_softc *);
154 Static void ural_stats(struct ieee80211com *,
155 struct ieee80211_node *,
156 struct ieee80211_ratectl_stats *);
157 Static void ural_stats_update(usbd_xfer_handle,
158 usbd_private_handle, usbd_status);
159 Static void ural_stats_timeout(void *);
160 Static void ural_ratectl_change(struct ieee80211com *ic, u_int,
164 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
166 static const struct ieee80211_rateset ural_rateset_11a =
167 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
169 static const struct ieee80211_rateset ural_rateset_11b =
170 { 4, { 2, 4, 11, 22 } };
172 static const struct ieee80211_rateset ural_rateset_11g =
173 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
176 * Default values for MAC registers; values taken from the reference driver.
178 static const struct {
182 { RAL_TXRX_CSR5, 0x8c8d },
183 { RAL_TXRX_CSR6, 0x8b8a },
184 { RAL_TXRX_CSR7, 0x8687 },
185 { RAL_TXRX_CSR8, 0x0085 },
186 { RAL_MAC_CSR13, 0x1111 },
187 { RAL_MAC_CSR14, 0x1e11 },
188 { RAL_TXRX_CSR21, 0xe78f },
189 { RAL_MAC_CSR9, 0xff1d },
190 { RAL_MAC_CSR11, 0x0002 },
191 { RAL_MAC_CSR22, 0x0053 },
192 { RAL_MAC_CSR15, 0x0000 },
193 { RAL_MAC_CSR8, 0x0780 },
194 { RAL_TXRX_CSR19, 0x0000 },
195 { RAL_TXRX_CSR18, 0x005a },
196 { RAL_PHY_CSR2, 0x0000 },
197 { RAL_TXRX_CSR0, 0x1ec0 },
198 { RAL_PHY_CSR4, 0x000f }
202 * Default values for BBP registers; values taken from the reference driver.
204 static const struct {
243 * Default values for RF register R2 indexed by channel numbers.
245 static const uint32_t ural_rf2522_r2[] = {
246 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
247 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
250 static const uint32_t ural_rf2523_r2[] = {
251 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
252 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
255 static const uint32_t ural_rf2524_r2[] = {
256 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
257 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
260 static const uint32_t ural_rf2525_r2[] = {
261 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
262 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
265 static const uint32_t ural_rf2525_hi_r2[] = {
266 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
267 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
270 static const uint32_t ural_rf2525e_r2[] = {
271 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
272 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
275 static const uint32_t ural_rf2526_hi_r2[] = {
276 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
277 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
280 static const uint32_t ural_rf2526_r2[] = {
281 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
282 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
286 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
287 * values taken from the reference driver.
289 static const struct {
295 { 1, 0x08808, 0x0044d, 0x00282 },
296 { 2, 0x08808, 0x0044e, 0x00282 },
297 { 3, 0x08808, 0x0044f, 0x00282 },
298 { 4, 0x08808, 0x00460, 0x00282 },
299 { 5, 0x08808, 0x00461, 0x00282 },
300 { 6, 0x08808, 0x00462, 0x00282 },
301 { 7, 0x08808, 0x00463, 0x00282 },
302 { 8, 0x08808, 0x00464, 0x00282 },
303 { 9, 0x08808, 0x00465, 0x00282 },
304 { 10, 0x08808, 0x00466, 0x00282 },
305 { 11, 0x08808, 0x00467, 0x00282 },
306 { 12, 0x08808, 0x00468, 0x00282 },
307 { 13, 0x08808, 0x00469, 0x00282 },
308 { 14, 0x08808, 0x0046b, 0x00286 },
310 { 36, 0x08804, 0x06225, 0x00287 },
311 { 40, 0x08804, 0x06226, 0x00287 },
312 { 44, 0x08804, 0x06227, 0x00287 },
313 { 48, 0x08804, 0x06228, 0x00287 },
314 { 52, 0x08804, 0x06229, 0x00287 },
315 { 56, 0x08804, 0x0622a, 0x00287 },
316 { 60, 0x08804, 0x0622b, 0x00287 },
317 { 64, 0x08804, 0x0622c, 0x00287 },
319 { 100, 0x08804, 0x02200, 0x00283 },
320 { 104, 0x08804, 0x02201, 0x00283 },
321 { 108, 0x08804, 0x02202, 0x00283 },
322 { 112, 0x08804, 0x02203, 0x00283 },
323 { 116, 0x08804, 0x02204, 0x00283 },
324 { 120, 0x08804, 0x02205, 0x00283 },
325 { 124, 0x08804, 0x02206, 0x00283 },
326 { 128, 0x08804, 0x02207, 0x00283 },
327 { 132, 0x08804, 0x02208, 0x00283 },
328 { 136, 0x08804, 0x02209, 0x00283 },
329 { 140, 0x08804, 0x0220a, 0x00283 },
331 { 149, 0x08808, 0x02429, 0x00281 },
332 { 153, 0x08808, 0x0242b, 0x00281 },
333 { 157, 0x08808, 0x0242d, 0x00281 },
334 { 161, 0x08808, 0x0242f, 0x00281 }
337 USB_DECLARE_DRIVER(ural);
341 USB_MATCH_START(ural, uaa);
343 if (uaa->iface != NULL)
346 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
347 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
352 USB_ATTACH_START(ural, sc, uaa);
354 struct ieee80211com *ic = &sc->sc_ic;
355 usb_interface_descriptor_t *id;
356 usb_endpoint_descriptor_t *ed;
361 sc->sc_udev = uaa->device;
362 sc->sc_tx_retries = 7; /* TODO tunable/sysctl */
364 usbd_devinfo(sc->sc_udev, 0, devinfo);
367 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
368 printf("%s: could not set configuration no\n",
369 USBDEVNAME(sc->sc_dev));
370 USB_ATTACH_ERROR_RETURN;
373 /* get the first interface handle */
374 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
377 printf("%s: could not get interface handle\n",
378 USBDEVNAME(sc->sc_dev));
379 USB_ATTACH_ERROR_RETURN;
385 id = usbd_get_interface_descriptor(sc->sc_iface);
387 sc->sc_rx_no = sc->sc_tx_no = -1;
388 for (i = 0; i < id->bNumEndpoints; i++) {
389 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
391 printf("%s: no endpoint descriptor for %d\n",
392 USBDEVNAME(sc->sc_dev), i);
393 USB_ATTACH_ERROR_RETURN;
396 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
397 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
398 sc->sc_rx_no = ed->bEndpointAddress;
399 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
400 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
401 sc->sc_tx_no = ed->bEndpointAddress;
403 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
404 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
405 USB_ATTACH_ERROR_RETURN;
409 mtx_init(&sc->sc_mtx, USBDEVNAME(sc->sc_dev), MTX_NETWORK_LOCK,
410 MTX_DEF | MTX_RECURSE);
413 usb_init_task(&sc->sc_task, ural_task, sc);
414 callout_init(&sc->scan_ch);
415 callout_init(&sc->stats_ch);
417 /* retrieve RT2570 rev. no */
418 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
420 /* retrieve MAC address and various other things from EEPROM */
421 ural_read_eeprom(sc);
423 printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
424 USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
428 if_initname(ifp, "ural", USBDEVUNIT(sc->sc_dev));
429 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
430 ifp->if_init = ural_init;
431 ifp->if_ioctl = ural_ioctl;
432 ifp->if_start = ural_start;
433 ifp->if_watchdog = ural_watchdog;
434 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
435 ifq_set_ready(&ifp->if_snd);
437 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
438 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
439 ic->ic_ratectl.rc_st_valid_stats =
440 IEEE80211_RATECTL_STATS_PKT_NORETRY |
441 IEEE80211_RATECTL_STATS_PKT_OK |
442 IEEE80211_RATECTL_STATS_PKT_ERR |
443 IEEE80211_RATECTL_STATS_RETRIES;
444 ic->ic_ratectl.rc_st_stats = ural_stats;
445 ic->ic_ratectl.rc_st_change = ural_ratectl_change;
447 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
448 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
449 ic->ic_state = IEEE80211_S_INIT;
451 /* set device capabilities */
453 IEEE80211_C_IBSS | /* IBSS mode supported */
454 IEEE80211_C_MONITOR | /* monitor mode supported */
455 IEEE80211_C_HOSTAP | /* HostAp mode supported */
456 IEEE80211_C_TXPMGT | /* tx power management */
457 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
458 IEEE80211_C_SHSLOT | /* short slot time supported */
459 IEEE80211_C_WPA; /* 802.11i */
461 if (sc->rf_rev == RAL_RF_5222) {
462 /* set supported .11a rates */
463 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
465 /* set supported .11a channels */
466 for (i = 36; i <= 64; i += 4) {
467 ic->ic_channels[i].ic_freq =
468 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
469 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
471 for (i = 100; i <= 140; i += 4) {
472 ic->ic_channels[i].ic_freq =
473 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
474 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
476 for (i = 149; i <= 161; i += 4) {
477 ic->ic_channels[i].ic_freq =
478 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
479 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
483 /* set supported .11b and .11g rates */
484 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
485 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
487 /* set supported .11b and .11g channels (1 through 14) */
488 for (i = 1; i <= 14; i++) {
489 ic->ic_channels[i].ic_freq =
490 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
491 ic->ic_channels[i].ic_flags =
492 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
493 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
496 ieee80211_ifattach(ic);
497 ic->ic_reset = ural_reset;
498 /* enable s/w bmiss handling in sta mode */
499 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
501 /* override state transition machine */
502 sc->sc_newstate = ic->ic_newstate;
503 ic->ic_newstate = ural_newstate;
504 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
506 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
507 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
509 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
510 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
511 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
513 sc->sc_txtap_len = sizeof sc->sc_txtapu;
514 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
515 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
518 ieee80211_announce(ic);
520 USB_ATTACH_SUCCESS_RETURN;
525 USB_DETACH_START(ural, sc);
526 struct ieee80211com *ic = &sc->sc_ic;
527 struct ifnet *ifp = &ic->ic_if;
529 lwkt_serialize_enter(ifp->if_serializer);
531 callout_stop(&sc->scan_ch);
532 callout_stop(&sc->stats_ch);
534 sc->sc_flags |= URAL_FLAG_SYNCTASK;
537 lwkt_serialize_exit(ifp->if_serializer);
539 usb_rem_task(sc->sc_udev, &sc->sc_task);
541 if (sc->stats_xfer != NULL) {
542 usbd_free_xfer(sc->stats_xfer);
543 sc->stats_xfer = NULL;
546 if (sc->sc_rx_pipeh != NULL) {
547 usbd_abort_pipe(sc->sc_rx_pipeh);
548 usbd_close_pipe(sc->sc_rx_pipeh);
551 if (sc->sc_tx_pipeh != NULL) {
552 usbd_abort_pipe(sc->sc_tx_pipeh);
553 usbd_close_pipe(sc->sc_tx_pipeh);
556 ural_free_rx_list(sc);
557 ural_free_tx_list(sc);
560 ieee80211_ifdetach(ic);
563 mtx_destroy(&sc->sc_mtx);
570 ural_alloc_tx_list(struct ural_softc *sc)
572 struct ural_tx_data *data;
577 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
578 data = &sc->tx_data[i];
582 data->xfer = usbd_alloc_xfer(sc->sc_udev);
583 if (data->xfer == NULL) {
584 printf("%s: could not allocate tx xfer\n",
585 USBDEVNAME(sc->sc_dev));
590 data->buf = usbd_alloc_buffer(data->xfer,
591 RAL_TX_DESC_SIZE + MCLBYTES);
592 if (data->buf == NULL) {
593 printf("%s: could not allocate tx buffer\n",
594 USBDEVNAME(sc->sc_dev));
602 fail: ural_free_tx_list(sc);
607 ural_free_tx_list(struct ural_softc *sc)
609 struct ural_tx_data *data;
612 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
613 data = &sc->tx_data[i];
615 if (data->xfer != NULL) {
616 usbd_free_xfer(data->xfer);
620 if (data->ni != NULL) {
621 ieee80211_free_node(data->ni);
628 ural_alloc_rx_list(struct ural_softc *sc)
630 struct ural_rx_data *data;
633 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
634 data = &sc->rx_data[i];
638 data->xfer = usbd_alloc_xfer(sc->sc_udev);
639 if (data->xfer == NULL) {
640 printf("%s: could not allocate rx xfer\n",
641 USBDEVNAME(sc->sc_dev));
646 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
647 printf("%s: could not allocate rx buffer\n",
648 USBDEVNAME(sc->sc_dev));
653 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
654 if (data->m == NULL) {
655 printf("%s: could not allocate rx mbuf\n",
656 USBDEVNAME(sc->sc_dev));
661 data->buf = mtod(data->m, uint8_t *);
666 fail: ural_free_tx_list(sc);
671 ural_free_rx_list(struct ural_softc *sc)
673 struct ural_rx_data *data;
676 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
677 data = &sc->rx_data[i];
679 if (data->xfer != NULL) {
680 usbd_free_xfer(data->xfer);
684 if (data->m != NULL) {
692 ural_media_change(struct ifnet *ifp)
694 struct ural_softc *sc = ifp->if_softc;
697 error = ieee80211_media_change(ifp);
698 if (error != ENETRESET)
701 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
708 * This function is called periodically (every 200ms) during scanning to
709 * switch from one channel to another.
712 ural_next_scan(void *arg)
714 struct ural_softc *sc = arg;
715 struct ieee80211com *ic = &sc->sc_ic;
716 struct ifnet *ifp = &ic->ic_if;
718 lwkt_serialize_enter(ifp->if_serializer);
720 if (ic->ic_state == IEEE80211_S_SCAN)
721 ieee80211_next_scan(ic);
723 lwkt_serialize_exit(ifp->if_serializer);
729 struct ural_softc *sc = arg;
730 struct ieee80211com *ic = &sc->sc_ic;
731 struct ifnet *ifp = &ic->ic_if;
732 enum ieee80211_state ostate;
733 struct ieee80211_node *ni;
736 lwkt_serialize_enter(ifp->if_serializer);
738 ieee80211_ratectl_newstate(ic, sc->sc_state);
740 ostate = ic->ic_state;
742 switch (sc->sc_state) {
743 case IEEE80211_S_INIT:
744 if (ostate == IEEE80211_S_RUN) {
745 /* abort TSF synchronization */
746 ural_write(sc, RAL_TXRX_CSR19, 0);
748 /* force tx led to stop blinking */
749 ural_write(sc, RAL_MAC_CSR20, 0);
753 case IEEE80211_S_SCAN:
754 ural_set_chan(sc, ic->ic_curchan);
755 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
758 case IEEE80211_S_AUTH:
759 ural_set_chan(sc, ic->ic_curchan);
762 case IEEE80211_S_ASSOC:
763 ural_set_chan(sc, ic->ic_curchan);
766 case IEEE80211_S_RUN:
767 ural_set_chan(sc, ic->ic_curchan);
771 lwkt_serialize_exit(ifp->if_serializer);
773 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
774 ural_update_slot(&ic->ic_if);
775 ural_set_txpreamble(sc);
776 ural_set_basicrates(sc);
777 ural_set_bssid(sc, ni->ni_bssid);
780 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
781 ic->ic_opmode == IEEE80211_M_IBSS) {
782 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
784 printf("%s: could not allocate beacon\n",
785 USBDEVNAME(sc->sc_dev));
789 if (ural_tx_bcn(sc, m, ni) != 0) {
790 printf("%s: could not send beacon\n",
791 USBDEVNAME(sc->sc_dev));
796 /* make tx led blink on tx (controlled by ASIC) */
797 ural_write(sc, RAL_MAC_CSR20, 1);
799 if (ic->ic_opmode != IEEE80211_M_MONITOR)
800 ural_enable_tsf_sync(sc);
802 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
803 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
805 lwkt_serialize_enter(ifp->if_serializer);
807 callout_reset(&sc->stats_ch, 4 * hz / 5,
808 ural_stats_timeout, sc);
813 sc->sc_newstate(ic, sc->sc_state, -1);
815 lwkt_serialize_exit(ifp->if_serializer);
819 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
821 struct ifnet *ifp = &ic->ic_if;
822 struct ural_softc *sc = ifp->if_softc;
824 ASSERT_SERIALIZED(ifp->if_serializer);
826 callout_stop(&sc->scan_ch);
827 callout_stop(&sc->stats_ch);
829 /* do it in a process context */
830 sc->sc_state = nstate;
832 lwkt_serialize_exit(ifp->if_serializer);
833 usb_rem_task(sc->sc_udev, &sc->sc_task);
835 if (sc->sc_flags & URAL_FLAG_SYNCTASK) {
836 usb_do_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER,
839 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
841 lwkt_serialize_enter(ifp->if_serializer);
846 /* quickly determine if a given rate is CCK or OFDM */
847 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
849 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
850 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
852 #define RAL_SIFS 10 /* us */
854 #define RAL_RXTX_TURNAROUND 5 /* us */
857 * This function is only used by the Rx radiotap code.
860 ural_rxrate(struct ural_rx_desc *desc)
862 if (le32toh(desc->flags) & RAL_RX_OFDM) {
863 /* reverse function of ural_plcp_signal */
864 switch (desc->rate) {
872 case 0xc: return 108;
875 if (desc->rate == 10)
877 if (desc->rate == 20)
879 if (desc->rate == 55)
881 if (desc->rate == 110)
884 return 2; /* should not get there */
888 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
890 struct ural_tx_data *data = priv;
891 struct ural_softc *sc = data->sc;
892 struct ifnet *ifp = &sc->sc_ic.ic_if;
894 if (status != USBD_NORMAL_COMPLETION) {
895 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
898 printf("%s: could not transmit buffer: %s\n",
899 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
901 if (status == USBD_STALLED)
902 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
908 lwkt_serialize_enter(ifp->if_serializer);
912 ieee80211_free_node(data->ni);
918 DPRINTFN(10, ("tx done\n"));
921 ifp->if_flags &= ~IFF_OACTIVE;
924 lwkt_serialize_exit(ifp->if_serializer);
928 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
930 struct ural_rx_data *data = priv;
931 struct ural_softc *sc = data->sc;
932 struct ieee80211com *ic = &sc->sc_ic;
933 struct ifnet *ifp = &ic->ic_if;
934 struct ural_rx_desc *desc;
935 struct ieee80211_frame *wh;
936 struct ieee80211_node *ni;
937 struct mbuf *mnew, *m;
940 if (status != USBD_NORMAL_COMPLETION) {
941 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
944 if (status == USBD_STALLED)
945 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
949 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
951 if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
952 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
958 /* rx descriptor is located at the end */
959 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
961 if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
962 (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
964 * This should not happen since we did not request to receive
965 * those frames when we filled RAL_TXRX_CSR2.
967 DPRINTFN(5, ("PHY or CRC error\n"));
972 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
980 data->buf = mtod(data->m, uint8_t *);
983 m->m_pkthdr.rcvif = ifp;
984 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
986 lwkt_serialize_enter(ifp->if_serializer);
988 if (sc->sc_drvbpf != NULL) {
989 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
991 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; /* h/w leaves FCS */
992 tap->wr_rate = ural_rxrate(desc);
993 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
994 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
995 tap->wr_antenna = sc->rx_ant;
996 tap->wr_antsignal = desc->rssi;
998 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1001 /* trim CRC here so WEP can find its own CRC at the end of packet. */
1002 m_adj(m, -IEEE80211_CRC_LEN);
1004 wh = mtod(m, struct ieee80211_frame *);
1005 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1007 /* send the frame to the 802.11 layer */
1008 ieee80211_input(ic, m, ni, desc->rssi, 0);
1010 /* node is no longer needed */
1011 ieee80211_free_node(ni);
1013 DPRINTFN(15, ("rx done\n"));
1015 lwkt_serialize_exit(ifp->if_serializer);
1017 skip: /* setup a new transfer */
1018 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
1019 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
1020 usbd_transfer(xfer);
1024 * Return the expected ack rate for a frame transmitted at rate `rate'.
1025 * XXX: this should depend on the destination node basic rate set.
1028 ural_ack_rate(struct ieee80211com *ic, int rate)
1037 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1053 /* default to 1Mbps */
1058 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1059 * The function automatically determines the operating mode depending on the
1060 * given rate. `flags' indicates whether short preamble is in use or not.
1063 ural_txtime(int len, int rate, uint32_t flags)
1067 if (RAL_RATE_IS_OFDM(rate)) {
1068 /* IEEE Std 802.11a-1999, pp. 37 */
1069 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1070 txtime = 16 + 4 + 4 * txtime + 6;
1072 /* IEEE Std 802.11b-1999, pp. 28 */
1073 txtime = (16 * len + rate - 1) / rate;
1074 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1083 ural_plcp_signal(int rate)
1086 /* CCK rates (returned values are device-dependent) */
1089 case 11: return 0x2;
1090 case 22: return 0x3;
1092 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1093 case 12: return 0xb;
1094 case 18: return 0xf;
1095 case 24: return 0xa;
1096 case 36: return 0xe;
1097 case 48: return 0x9;
1098 case 72: return 0xd;
1099 case 96: return 0x8;
1100 case 108: return 0xc;
1102 /* unsupported rates (should not get there) */
1103 default: return 0xff;
1108 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1109 uint32_t flags, int len, int rate)
1111 struct ieee80211com *ic = &sc->sc_ic;
1112 uint16_t plcp_length;
1115 desc->flags = htole32(flags);
1116 desc->flags |= htole32(RAL_TX_NEWSEQ);
1117 desc->flags |= htole32(len << 16);
1119 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1120 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1122 /* setup PLCP fields */
1123 desc->plcp_signal = ural_plcp_signal(rate);
1124 desc->plcp_service = 4;
1126 len += IEEE80211_CRC_LEN;
1127 if (RAL_RATE_IS_OFDM(rate)) {
1128 desc->flags |= htole32(RAL_TX_OFDM);
1130 plcp_length = len & 0xfff;
1131 desc->plcp_length_hi = plcp_length >> 6;
1132 desc->plcp_length_lo = plcp_length & 0x3f;
1134 plcp_length = (16 * len + rate - 1) / rate;
1136 remainder = (16 * len) % 22;
1137 if (remainder != 0 && remainder < 7)
1138 desc->plcp_service |= RAL_PLCP_LENGEXT;
1140 desc->plcp_length_hi = plcp_length >> 8;
1141 desc->plcp_length_lo = plcp_length & 0xff;
1143 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1144 desc->plcp_signal |= 0x08;
1151 #define RAL_TX_TIMEOUT 5000
1154 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1156 struct ural_tx_desc *desc;
1157 usbd_xfer_handle xfer;
1163 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1165 xfer = usbd_alloc_xfer(sc->sc_udev);
1169 /* xfer length needs to be a multiple of two! */
1170 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1172 buf = usbd_alloc_buffer(xfer, xferlen);
1174 usbd_free_xfer(xfer);
1178 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1179 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1181 error = usbd_sync_transfer(xfer);
1183 usbd_free_xfer(xfer);
1187 desc = (struct ural_tx_desc *)buf;
1189 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1190 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1191 m0->m_pkthdr.len, rate);
1193 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1194 m0->m_pkthdr.len, rate, xferlen));
1196 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1197 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1199 error = usbd_sync_transfer(xfer);
1200 usbd_free_xfer(xfer);
1206 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1208 struct ieee80211com *ic = &sc->sc_ic;
1209 struct ural_tx_desc *desc;
1210 struct ural_tx_data *data;
1211 struct ieee80211_frame *wh;
1217 data = &sc->tx_data[0];
1218 desc = (struct ural_tx_desc *)data->buf;
1220 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1225 wh = mtod(m0, struct ieee80211_frame *);
1227 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1228 flags |= RAL_TX_ACK;
1230 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
1231 *(uint16_t *)wh->i_dur = htole16(dur);
1233 /* tell hardware to add timestamp for probe responses */
1234 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1235 IEEE80211_FC0_TYPE_MGT &&
1236 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1237 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1238 flags |= RAL_TX_TIMESTAMP;
1241 if (sc->sc_drvbpf != NULL) {
1242 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1245 tap->wt_rate = rate;
1246 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1247 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1248 tap->wt_antenna = sc->tx_ant;
1250 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1253 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1254 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1256 /* align end on a 2-bytes boundary */
1257 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1260 * No space left in the last URB to store the extra 2 bytes, force
1261 * sending of another URB.
1263 if ((xferlen % 64) == 0)
1266 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1267 m0->m_pkthdr.len, rate, xferlen));
1269 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1270 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1273 error = usbd_transfer(data->xfer);
1274 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1283 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1285 struct ieee80211com *ic = &sc->sc_ic;
1286 struct ural_tx_desc *desc;
1287 struct ural_tx_data *data;
1288 struct ieee80211_frame *wh;
1289 struct ieee80211_key *k;
1293 int xferlen, rate, rate_idx;
1295 wh = mtod(m0, struct ieee80211_frame *);
1297 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1298 rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1300 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1301 k = ieee80211_crypto_encap(ic, ni, m0);
1307 /* packet header may have moved, reset our local pointer */
1308 wh = mtod(m0, struct ieee80211_frame *);
1311 data = &sc->tx_data[0];
1312 desc = (struct ural_tx_desc *)data->buf;
1317 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1318 flags |= RAL_TX_ACK;
1319 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
1321 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(ic, rate),
1322 ic->ic_flags) + RAL_SIFS;
1323 *(uint16_t *)wh->i_dur = htole16(dur);
1326 if (sc->sc_drvbpf != NULL) {
1327 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1330 tap->wt_rate = rate;
1331 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1332 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1333 tap->wt_antenna = sc->tx_ant;
1335 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1338 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1339 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1341 /* align end on a 2-bytes boundary */
1342 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1345 * No space left in the last URB to store the extra 2 bytes, force
1346 * sending of another URB.
1348 if ((xferlen % 64) == 0)
1351 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1352 m0->m_pkthdr.len, rate, xferlen));
1354 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1355 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1358 error = usbd_transfer(data->xfer);
1359 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1368 ural_start(struct ifnet *ifp)
1370 struct ural_softc *sc = ifp->if_softc;
1371 struct ieee80211com *ic = &sc->sc_ic;
1373 struct ether_header *eh;
1374 struct ieee80211_node *ni;
1376 ASSERT_SERIALIZED(ifp->if_serializer);
1378 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING)
1382 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1383 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1384 ifp->if_flags |= IFF_OACTIVE;
1387 IF_DEQUEUE(&ic->ic_mgtq, m0);
1389 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1390 m0->m_pkthdr.rcvif = NULL;
1392 if (ic->ic_rawbpf != NULL)
1393 bpf_mtap(ic->ic_rawbpf, m0);
1395 if (ural_tx_mgt(sc, m0, ni) != 0)
1399 if (ic->ic_state != IEEE80211_S_RUN)
1401 m0 = ifq_poll(&ifp->if_snd);
1404 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1405 ifp->if_flags |= IFF_OACTIVE;
1409 ifq_dequeue(&ifp->if_snd, m0);
1411 if (m0->m_len < sizeof (struct ether_header) &&
1412 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1415 eh = mtod(m0, struct ether_header *);
1416 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1423 m0 = ieee80211_encap(ic, m0, ni);
1425 ieee80211_free_node(ni);
1429 if (ic->ic_rawbpf != NULL)
1430 bpf_mtap(ic->ic_rawbpf, m0);
1432 if (ural_tx_data(sc, m0, ni) != 0) {
1433 ieee80211_free_node(ni);
1439 sc->sc_tx_timer = 5;
1445 ural_watchdog(struct ifnet *ifp)
1447 struct ural_softc *sc = ifp->if_softc;
1448 struct ieee80211com *ic = &sc->sc_ic;
1450 ASSERT_SERIALIZED(ifp->if_serializer);
1454 if (sc->sc_tx_timer > 0) {
1455 if (--sc->sc_tx_timer == 0) {
1456 device_printf(sc->sc_dev, "device timeout\n");
1457 /*ural_init(sc); XXX needs a process context! */
1464 ieee80211_watchdog(ic);
1468 * This function allows for fast channel switching in monitor mode (used by
1469 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1470 * generate a new beacon frame.
1473 ural_reset(struct ifnet *ifp)
1475 struct ural_softc *sc = ifp->if_softc;
1476 struct ieee80211com *ic = &sc->sc_ic;
1478 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1481 ural_set_chan(sc, ic->ic_curchan);
1487 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1489 struct ural_softc *sc = ifp->if_softc;
1490 struct ieee80211com *ic = &sc->sc_ic;
1493 ASSERT_SERIALIZED(ifp->if_serializer);
1497 if (ifp->if_flags & IFF_UP) {
1498 if (ifp->if_flags & IFF_RUNNING)
1499 ural_update_promisc(sc);
1503 if (ifp->if_flags & IFF_RUNNING)
1509 error = ieee80211_ioctl(ic, cmd, data, cr);
1512 if (error == ENETRESET) {
1513 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1514 (IFF_UP | IFF_RUNNING) &&
1515 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1523 ural_set_testmode(struct ural_softc *sc)
1525 usb_device_request_t req;
1528 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1529 req.bRequest = RAL_VENDOR_REQUEST;
1530 USETW(req.wValue, 4);
1531 USETW(req.wIndex, 1);
1532 USETW(req.wLength, 0);
1534 error = usbd_do_request(sc->sc_udev, &req, NULL);
1536 printf("%s: could not set test mode: %s\n",
1537 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1542 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1544 usb_device_request_t req;
1547 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1548 req.bRequest = RAL_READ_EEPROM;
1549 USETW(req.wValue, 0);
1550 USETW(req.wIndex, addr);
1551 USETW(req.wLength, len);
1553 error = usbd_do_request(sc->sc_udev, &req, buf);
1555 printf("%s: could not read EEPROM: %s\n",
1556 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1561 ural_read(struct ural_softc *sc, uint16_t reg)
1563 usb_device_request_t req;
1567 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1568 req.bRequest = RAL_READ_MAC;
1569 USETW(req.wValue, 0);
1570 USETW(req.wIndex, reg);
1571 USETW(req.wLength, sizeof (uint16_t));
1573 error = usbd_do_request(sc->sc_udev, &req, &val);
1575 printf("%s: could not read MAC register: %s\n",
1576 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1580 return le16toh(val);
1584 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1586 usb_device_request_t req;
1589 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1590 req.bRequest = RAL_READ_MULTI_MAC;
1591 USETW(req.wValue, 0);
1592 USETW(req.wIndex, reg);
1593 USETW(req.wLength, len);
1595 error = usbd_do_request(sc->sc_udev, &req, buf);
1597 printf("%s: could not read MAC register: %s\n",
1598 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1603 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1605 usb_device_request_t req;
1608 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1609 req.bRequest = RAL_WRITE_MAC;
1610 USETW(req.wValue, val);
1611 USETW(req.wIndex, reg);
1612 USETW(req.wLength, 0);
1614 error = usbd_do_request(sc->sc_udev, &req, NULL);
1616 printf("%s: could not write MAC register: %s\n",
1617 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1622 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1624 usb_device_request_t req;
1627 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1628 req.bRequest = RAL_WRITE_MULTI_MAC;
1629 USETW(req.wValue, 0);
1630 USETW(req.wIndex, reg);
1631 USETW(req.wLength, len);
1633 error = usbd_do_request(sc->sc_udev, &req, buf);
1635 printf("%s: could not write MAC register: %s\n",
1636 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1641 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1646 for (ntries = 0; ntries < 5; ntries++) {
1647 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1651 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1655 tmp = reg << 8 | val;
1656 ural_write(sc, RAL_PHY_CSR7, tmp);
1660 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1665 val = RAL_BBP_WRITE | reg << 8;
1666 ural_write(sc, RAL_PHY_CSR7, val);
1668 for (ntries = 0; ntries < 5; ntries++) {
1669 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1673 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1677 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1681 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1686 for (ntries = 0; ntries < 5; ntries++) {
1687 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1691 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1695 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1696 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1697 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1699 /* remember last written value in sc */
1700 sc->rf_regs[reg] = val;
1702 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1706 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1708 struct ieee80211com *ic = &sc->sc_ic;
1709 struct ifnet *ifp = &ic->ic_if;
1713 ASSERT_SERIALIZED(ifp->if_serializer);
1715 chan = ieee80211_chan2ieee(ic, c);
1716 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1719 lwkt_serialize_exit(ifp->if_serializer);
1721 if (IEEE80211_IS_CHAN_2GHZ(c))
1722 power = min(sc->txpow[chan - 1], 31);
1726 /* adjust txpower using ifconfig settings */
1727 power -= (100 - ic->ic_txpowlimit) / 8;
1729 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1731 switch (sc->rf_rev) {
1733 ural_rf_write(sc, RAL_RF1, 0x00814);
1734 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1735 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1739 ural_rf_write(sc, RAL_RF1, 0x08804);
1740 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1741 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1742 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1746 ural_rf_write(sc, RAL_RF1, 0x0c808);
1747 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1748 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1749 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1753 ural_rf_write(sc, RAL_RF1, 0x08808);
1754 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1755 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1756 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1758 ural_rf_write(sc, RAL_RF1, 0x08808);
1759 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1760 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1761 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1765 ural_rf_write(sc, RAL_RF1, 0x08808);
1766 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1767 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1768 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1772 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1773 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1774 ural_rf_write(sc, RAL_RF1, 0x08804);
1776 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1777 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1778 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1783 for (i = 0; ural_rf5222[i].chan != chan; i++)
1786 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1787 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1788 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1789 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1793 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1794 ic->ic_state != IEEE80211_S_SCAN) {
1795 /* set Japan filter bit for channel 14 */
1796 tmp = ural_bbp_read(sc, 70);
1798 tmp &= ~RAL_JAPAN_FILTER;
1800 tmp |= RAL_JAPAN_FILTER;
1802 ural_bbp_write(sc, 70, tmp);
1804 /* clear CRC errors */
1805 ural_read(sc, RAL_STA_CSR0);
1808 ural_disable_rf_tune(sc);
1811 lwkt_serialize_enter(ifp->if_serializer);
1815 * Disable RF auto-tuning.
1818 ural_disable_rf_tune(struct ural_softc *sc)
1822 if (sc->rf_rev != RAL_RF_2523) {
1823 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1824 ural_rf_write(sc, RAL_RF1, tmp);
1827 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1828 ural_rf_write(sc, RAL_RF3, tmp);
1830 DPRINTFN(2, ("disabling RF autotune\n"));
1834 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1838 ural_enable_tsf_sync(struct ural_softc *sc)
1840 struct ieee80211com *ic = &sc->sc_ic;
1841 uint16_t logcwmin, preload, tmp;
1843 /* first, disable TSF synchronization */
1844 ural_write(sc, RAL_TXRX_CSR19, 0);
1846 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1847 ural_write(sc, RAL_TXRX_CSR18, tmp);
1849 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1850 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1851 tmp = logcwmin << 12 | preload;
1852 ural_write(sc, RAL_TXRX_CSR20, tmp);
1854 /* finally, enable TSF synchronization */
1855 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1856 if (ic->ic_opmode == IEEE80211_M_STA)
1857 tmp |= RAL_ENABLE_TSF_SYNC(1);
1859 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1860 ural_write(sc, RAL_TXRX_CSR19, tmp);
1862 DPRINTF(("enabling TSF synchronization\n"));
1866 ural_update_slot(struct ifnet *ifp)
1868 struct ural_softc *sc = ifp->if_softc;
1869 struct ieee80211com *ic = &sc->sc_ic;
1870 uint16_t slottime, sifs, eifs;
1872 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1875 * These settings may sound a bit inconsistent but this is what the
1876 * reference driver does.
1878 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1879 sifs = 16 - RAL_RXTX_TURNAROUND;
1882 sifs = 10 - RAL_RXTX_TURNAROUND;
1886 ural_write(sc, RAL_MAC_CSR10, slottime);
1887 ural_write(sc, RAL_MAC_CSR11, sifs);
1888 ural_write(sc, RAL_MAC_CSR12, eifs);
1892 ural_set_txpreamble(struct ural_softc *sc)
1896 tmp = ural_read(sc, RAL_TXRX_CSR10);
1898 tmp &= ~RAL_SHORT_PREAMBLE;
1899 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1900 tmp |= RAL_SHORT_PREAMBLE;
1902 ural_write(sc, RAL_TXRX_CSR10, tmp);
1906 ural_set_basicrates(struct ural_softc *sc)
1908 struct ieee80211com *ic = &sc->sc_ic;
1910 /* update basic rate set */
1911 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1912 /* 11b basic rates: 1, 2Mbps */
1913 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1914 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1915 /* 11a basic rates: 6, 12, 24Mbps */
1916 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1918 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1919 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1924 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1928 tmp = bssid[0] | bssid[1] << 8;
1929 ural_write(sc, RAL_MAC_CSR5, tmp);
1931 tmp = bssid[2] | bssid[3] << 8;
1932 ural_write(sc, RAL_MAC_CSR6, tmp);
1934 tmp = bssid[4] | bssid[5] << 8;
1935 ural_write(sc, RAL_MAC_CSR7, tmp);
1937 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
1941 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1945 tmp = addr[0] | addr[1] << 8;
1946 ural_write(sc, RAL_MAC_CSR2, tmp);
1948 tmp = addr[2] | addr[3] << 8;
1949 ural_write(sc, RAL_MAC_CSR3, tmp);
1951 tmp = addr[4] | addr[5] << 8;
1952 ural_write(sc, RAL_MAC_CSR4, tmp);
1954 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
1958 ural_update_promisc(struct ural_softc *sc)
1960 struct ifnet *ifp = &sc->sc_ic.ic_if;
1963 tmp = ural_read(sc, RAL_TXRX_CSR2);
1965 tmp &= ~RAL_DROP_NOT_TO_ME;
1966 if (!(ifp->if_flags & IFF_PROMISC))
1967 tmp |= RAL_DROP_NOT_TO_ME;
1969 ural_write(sc, RAL_TXRX_CSR2, tmp);
1971 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1972 "entering" : "leaving"));
1976 ural_get_rf(int rev)
1979 case RAL_RF_2522: return "RT2522";
1980 case RAL_RF_2523: return "RT2523";
1981 case RAL_RF_2524: return "RT2524";
1982 case RAL_RF_2525: return "RT2525";
1983 case RAL_RF_2525E: return "RT2525e";
1984 case RAL_RF_2526: return "RT2526";
1985 case RAL_RF_5222: return "RT5222";
1986 default: return "unknown";
1991 ural_read_eeprom(struct ural_softc *sc)
1993 struct ieee80211com *ic = &sc->sc_ic;
1996 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1998 sc->rf_rev = (val >> 11) & 0x7;
1999 sc->hw_radio = (val >> 10) & 0x1;
2000 sc->led_mode = (val >> 6) & 0x7;
2001 sc->rx_ant = (val >> 4) & 0x3;
2002 sc->tx_ant = (val >> 2) & 0x3;
2003 sc->nb_ant = val & 0x3;
2005 /* read MAC address */
2006 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2008 /* read default values for BBP registers */
2009 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2011 /* read Tx power for all b/g channels */
2012 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2016 ural_bbp_init(struct ural_softc *sc)
2018 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2021 /* wait for BBP to be ready */
2022 for (ntries = 0; ntries < 100; ntries++) {
2023 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2027 if (ntries == 100) {
2028 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2032 /* initialize BBP registers to default values */
2033 for (i = 0; i < N(ural_def_bbp); i++)
2034 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2037 /* initialize BBP registers to values stored in EEPROM */
2038 for (i = 0; i < 16; i++) {
2039 if (sc->bbp_prom[i].reg == 0xff)
2041 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2050 ural_set_txantenna(struct ural_softc *sc, int antenna)
2055 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2058 else if (antenna == 2)
2061 tx |= RAL_BBP_DIVERSITY;
2063 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2064 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2065 sc->rf_rev == RAL_RF_5222)
2066 tx |= RAL_BBP_FLIPIQ;
2068 ural_bbp_write(sc, RAL_BBP_TX, tx);
2070 /* update values in PHY_CSR5 and PHY_CSR6 */
2071 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2072 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2074 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2075 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2079 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2083 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2086 else if (antenna == 2)
2089 rx |= RAL_BBP_DIVERSITY;
2091 /* need to force no I/Q flip for RF 2525e and 2526 */
2092 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2093 rx &= ~RAL_BBP_FLIPIQ;
2095 ural_bbp_write(sc, RAL_BBP_RX, rx);
2099 ural_init(void *priv)
2101 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2102 struct ural_softc *sc = priv;
2103 struct ieee80211com *ic = &sc->sc_ic;
2104 struct ifnet *ifp = &ic->ic_if;
2105 struct ieee80211_key *wk;
2106 struct ural_rx_data *data;
2111 ASSERT_SERIALIZED(ifp->if_serializer);
2113 ural_set_testmode(sc);
2114 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
2118 /* initialize MAC registers to default values */
2119 for (i = 0; i < N(ural_def_mac); i++)
2120 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2122 /* wait for BBP and RF to wake up (this can take a long time!) */
2123 for (ntries = 0; ntries < 100; ntries++) {
2124 tmp = ural_read(sc, RAL_MAC_CSR17);
2125 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2126 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2130 if (ntries == 100) {
2131 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2132 USBDEVNAME(sc->sc_dev));
2137 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2139 /* set basic rate set (will be updated later) */
2140 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2142 if (ural_bbp_init(sc) != 0)
2145 /* set default BSS channel */
2146 ural_set_chan(sc, ic->ic_curchan);
2148 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2149 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2151 ural_set_txantenna(sc, sc->tx_ant);
2152 ural_set_rxantenna(sc, sc->rx_ant);
2154 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2155 ural_set_macaddr(sc, ic->ic_myaddr);
2158 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
2160 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2161 wk = &ic->ic_crypto.cs_nw_keys[i];
2162 ural_write_multi(sc, wk->wk_keyix * IEEE80211_KEYBUF_SIZE +
2163 RAL_SEC_CSR0, wk->wk_key, IEEE80211_KEYBUF_SIZE);
2167 * Allocate xfer for AMRR statistics requests.
2169 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
2170 if (sc->stats_xfer == NULL) {
2171 printf("%s: could not allocate AMRR xfer\n",
2172 USBDEVNAME(sc->sc_dev));
2177 * Open Tx and Rx USB bulk pipes.
2179 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2182 printf("%s: could not open Tx pipe: %s\n",
2183 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2187 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2190 printf("%s: could not open Rx pipe: %s\n",
2191 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2196 * Allocate Tx and Rx xfer queues.
2198 error = ural_alloc_tx_list(sc);
2200 printf("%s: could not allocate Tx list\n",
2201 USBDEVNAME(sc->sc_dev));
2205 error = ural_alloc_rx_list(sc);
2207 printf("%s: could not allocate Rx list\n",
2208 USBDEVNAME(sc->sc_dev));
2213 * Start up the receive pipe.
2215 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2216 data = &sc->rx_data[i];
2218 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2219 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2220 usbd_transfer(data->xfer);
2224 tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2225 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2226 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2227 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2228 tmp |= RAL_DROP_TODS;
2229 if (!(ifp->if_flags & IFF_PROMISC))
2230 tmp |= RAL_DROP_NOT_TO_ME;
2232 ural_write(sc, RAL_TXRX_CSR2, tmp);
2234 ifp->if_flags &= ~IFF_OACTIVE;
2235 ifp->if_flags |= IFF_RUNNING;
2237 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2238 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2240 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2241 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2242 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2244 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2248 fail: ural_stop(sc);
2253 ural_stop(struct ural_softc *sc)
2255 struct ieee80211com *ic = &sc->sc_ic;
2256 struct ifnet *ifp = &ic->ic_if;
2258 ASSERT_SERIALIZED(ifp->if_serializer);
2260 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2262 sc->sc_tx_timer = 0;
2264 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2267 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2269 /* reset ASIC and BBP (but won't reset MAC registers!) */
2270 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2271 ural_write(sc, RAL_MAC_CSR1, 0);
2273 if (sc->stats_xfer != NULL) {
2274 usbd_free_xfer(sc->stats_xfer);
2275 sc->stats_xfer = NULL;
2278 if (sc->sc_rx_pipeh != NULL) {
2279 usbd_abort_pipe(sc->sc_rx_pipeh);
2280 usbd_close_pipe(sc->sc_rx_pipeh);
2281 sc->sc_rx_pipeh = NULL;
2284 if (sc->sc_tx_pipeh != NULL) {
2285 usbd_abort_pipe(sc->sc_tx_pipeh);
2286 usbd_close_pipe(sc->sc_tx_pipeh);
2287 sc->sc_tx_pipeh = NULL;
2290 ural_free_rx_list(sc);
2291 ural_free_tx_list(sc);
2295 ural_stats_timeout(void *arg)
2297 struct ural_softc *sc = (struct ural_softc *)arg;
2298 struct ifnet *ifp = &sc->sc_ic.ic_if;
2299 usb_device_request_t req;
2301 lwkt_serialize_enter(ifp->if_serializer);
2304 * Asynchronously read statistic registers (cleared by read).
2306 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2307 req.bRequest = RAL_READ_MULTI_MAC;
2308 USETW(req.wValue, 0);
2309 USETW(req.wIndex, RAL_STA_CSR0);
2310 USETW(req.wLength, sizeof(sc->sta));
2312 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2313 USBD_DEFAULT_TIMEOUT, &req,
2314 sc->sta, sizeof(sc->sta), 0,
2316 usbd_transfer(sc->stats_xfer);
2318 lwkt_serialize_exit(ifp->if_serializer);
2322 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2325 struct ural_softc *sc = (struct ural_softc *)priv;
2326 struct ifnet *ifp = &sc->sc_ic.ic_if;
2327 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2329 if (status != USBD_NORMAL_COMPLETION) {
2330 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2331 "cancelling automatic rate control\n");
2335 lwkt_serialize_enter(ifp->if_serializer);
2337 /* count TX retry-fail as Tx errors */
2338 ifp->if_oerrors += sc->sta[RAL_TX_PKT_FAIL];
2340 stats->stats_pkt_ok += sc->sta[RAL_TX_PKT_NO_RETRY] +
2341 sc->sta[RAL_TX_PKT_ONE_RETRY] +
2342 sc->sta[RAL_TX_PKT_MULTI_RETRY];
2344 stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2346 stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2348 stats->stats_short_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2351 * XXX Estimated average:
2352 * Actual number of retries for each packet should belong to
2353 * [2, sc->sc_tx_retries]
2355 stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2356 ((2 + sc->sc_tx_retries) / 2);
2358 stats->stats_short_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2360 stats->stats_short_retries +=
2361 sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2363 callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2365 lwkt_serialize_exit(ifp->if_serializer);
2369 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2370 struct ieee80211_ratectl_stats *stats)
2372 struct ifnet *ifp = &ic->ic_if;
2373 struct ural_softc *sc = ifp->if_softc;
2375 ASSERT_SERIALIZED(ifp->if_serializer);
2377 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2378 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2382 ural_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2384 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2385 struct ieee80211_onoe_param *oparam;
2387 if (st->rc_st_param != NULL) {
2388 kfree(st->rc_st_param, M_DEVBUF);
2389 st->rc_st_param = NULL;
2393 case IEEE80211_RATECTL_ONOE:
2394 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2396 IEEE80211_ONOE_PARAM_SETUP(oparam);
2397 oparam->onoe_raise = 20;
2399 st->rc_st_param = oparam;
2401 case IEEE80211_RATECTL_NONE:
2402 /* This could only happen during detaching */
2405 panic("unknown rate control algo %u\n", nrc);
2409 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);