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.19 2007/07/27 18:07:21 hasso 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 <dev/netif/ural/if_uralreg.h>
55 #include <dev/netif/ural/if_uralvar.h>
58 #define DPRINTF(x) do { if (uraldebug > 0) kprintf x; } while (0)
59 #define DPRINTFN(n, x) do { if (uraldebug >= (n)) kprintf 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 #define URAL_RSSI(rssi) \
70 ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
71 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
73 /* various supported device vendors/products */
74 static const struct usb_devno ural_devs[] = {
75 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G },
76 { USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570 },
77 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
78 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU },
79 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122 },
80 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG },
81 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254 },
82 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54G },
83 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54GP },
84 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_HU200TS },
85 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 },
86 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI },
87 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB },
88 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI },
89 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570 },
90 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2 },
91 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3 },
92 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_NV902W },
93 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570 },
94 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2 },
95 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_3 },
96 { USB_VENDOR_SPHAIRON, USB_PRODUCT_SPHAIRON_UB801R },
97 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2570 },
98 { USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570 },
99 { USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570 }
102 MODULE_DEPEND(ural, wlan, 1, 1, 1);
104 static int ural_alloc_tx_list(struct ural_softc *);
105 static void ural_free_tx_list(struct ural_softc *);
106 static int ural_alloc_rx_list(struct ural_softc *);
107 static void ural_free_rx_list(struct ural_softc *);
108 static int ural_media_change(struct ifnet *);
109 static void ural_next_scan(void *);
110 static void ural_task(void *);
111 static int ural_newstate(struct ieee80211com *,
112 enum ieee80211_state, int);
113 static int ural_rxrate(struct ural_rx_desc *);
114 static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
116 static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
118 static uint8_t ural_plcp_signal(int);
119 static void ural_setup_tx_desc(struct ural_softc *,
120 struct ural_tx_desc *, uint32_t, int, int);
121 static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
122 struct ieee80211_node *);
123 static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
124 struct ieee80211_node *);
125 static int ural_tx_data(struct ural_softc *, struct mbuf *,
126 struct ieee80211_node *);
127 static void ural_start(struct ifnet *);
128 static void ural_watchdog(struct ifnet *);
129 static int ural_reset(struct ifnet *);
130 static int ural_ioctl(struct ifnet *, u_long, caddr_t,
132 static void ural_set_testmode(struct ural_softc *);
133 static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
135 static uint16_t ural_read(struct ural_softc *, uint16_t);
136 static void ural_read_multi(struct ural_softc *, uint16_t, void *,
138 static void ural_write(struct ural_softc *, uint16_t, uint16_t);
139 static void ural_write_multi(struct ural_softc *, uint16_t, void *,
141 static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
142 static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
143 static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
144 static void ural_set_chan(struct ural_softc *,
145 struct ieee80211_channel *);
146 static void ural_disable_rf_tune(struct ural_softc *);
147 static void ural_enable_tsf_sync(struct ural_softc *);
148 static void ural_update_slot(struct ifnet *);
149 static void ural_set_txpreamble(struct ural_softc *);
150 static void ural_set_basicrates(struct ural_softc *);
151 static void ural_set_bssid(struct ural_softc *, uint8_t *);
152 static void ural_set_macaddr(struct ural_softc *, uint8_t *);
153 static void ural_update_promisc(struct ural_softc *);
154 static const char *ural_get_rf(int);
155 static void ural_read_eeprom(struct ural_softc *);
156 static int ural_bbp_init(struct ural_softc *);
157 static void ural_set_txantenna(struct ural_softc *, int);
158 static void ural_set_rxantenna(struct ural_softc *, int);
159 static void ural_init(void *);
160 static void ural_stop(struct ural_softc *);
161 static void ural_stats(struct ieee80211com *,
162 struct ieee80211_node *,
163 struct ieee80211_ratectl_stats *);
164 static void ural_stats_update(usbd_xfer_handle,
165 usbd_private_handle, usbd_status);
166 static void ural_stats_timeout(void *);
167 static void ural_ratectl_change(struct ieee80211com *ic, u_int,
171 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
173 static const struct ieee80211_rateset ural_rateset_11a =
174 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
176 static const struct ieee80211_rateset ural_rateset_11b =
177 { 4, { 2, 4, 11, 22 } };
179 static const struct ieee80211_rateset ural_rateset_11g =
180 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
183 * Default values for MAC registers; values taken from the reference driver.
185 static const struct {
189 { RAL_TXRX_CSR5, 0x8c8d },
190 { RAL_TXRX_CSR6, 0x8b8a },
191 { RAL_TXRX_CSR7, 0x8687 },
192 { RAL_TXRX_CSR8, 0x0085 },
193 { RAL_MAC_CSR13, 0x1111 },
194 { RAL_MAC_CSR14, 0x1e11 },
195 { RAL_TXRX_CSR21, 0xe78f },
196 { RAL_MAC_CSR9, 0xff1d },
197 { RAL_MAC_CSR11, 0x0002 },
198 { RAL_MAC_CSR22, 0x0053 },
199 { RAL_MAC_CSR15, 0x0000 },
200 { RAL_MAC_CSR8, 0x0780 },
201 { RAL_TXRX_CSR19, 0x0000 },
202 { RAL_TXRX_CSR18, 0x005a },
203 { RAL_PHY_CSR2, 0x0000 },
204 { RAL_TXRX_CSR0, 0x1ec0 },
205 { RAL_PHY_CSR4, 0x000f }
209 * Default values for BBP registers; values taken from the reference driver.
211 static const struct {
250 * Default values for RF register R2 indexed by channel numbers.
252 static const uint32_t ural_rf2522_r2[] = {
253 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
254 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
257 static const uint32_t ural_rf2523_r2[] = {
258 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
259 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
262 static const uint32_t ural_rf2524_r2[] = {
263 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
264 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
267 static const uint32_t ural_rf2525_r2[] = {
268 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
269 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
272 static const uint32_t ural_rf2525_hi_r2[] = {
273 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
274 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
277 static const uint32_t ural_rf2525e_r2[] = {
278 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
279 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
282 static const uint32_t ural_rf2526_hi_r2[] = {
283 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
284 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
287 static const uint32_t ural_rf2526_r2[] = {
288 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
289 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
293 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
294 * values taken from the reference driver.
296 static const struct {
302 { 1, 0x08808, 0x0044d, 0x00282 },
303 { 2, 0x08808, 0x0044e, 0x00282 },
304 { 3, 0x08808, 0x0044f, 0x00282 },
305 { 4, 0x08808, 0x00460, 0x00282 },
306 { 5, 0x08808, 0x00461, 0x00282 },
307 { 6, 0x08808, 0x00462, 0x00282 },
308 { 7, 0x08808, 0x00463, 0x00282 },
309 { 8, 0x08808, 0x00464, 0x00282 },
310 { 9, 0x08808, 0x00465, 0x00282 },
311 { 10, 0x08808, 0x00466, 0x00282 },
312 { 11, 0x08808, 0x00467, 0x00282 },
313 { 12, 0x08808, 0x00468, 0x00282 },
314 { 13, 0x08808, 0x00469, 0x00282 },
315 { 14, 0x08808, 0x0046b, 0x00286 },
317 { 36, 0x08804, 0x06225, 0x00287 },
318 { 40, 0x08804, 0x06226, 0x00287 },
319 { 44, 0x08804, 0x06227, 0x00287 },
320 { 48, 0x08804, 0x06228, 0x00287 },
321 { 52, 0x08804, 0x06229, 0x00287 },
322 { 56, 0x08804, 0x0622a, 0x00287 },
323 { 60, 0x08804, 0x0622b, 0x00287 },
324 { 64, 0x08804, 0x0622c, 0x00287 },
326 { 100, 0x08804, 0x02200, 0x00283 },
327 { 104, 0x08804, 0x02201, 0x00283 },
328 { 108, 0x08804, 0x02202, 0x00283 },
329 { 112, 0x08804, 0x02203, 0x00283 },
330 { 116, 0x08804, 0x02204, 0x00283 },
331 { 120, 0x08804, 0x02205, 0x00283 },
332 { 124, 0x08804, 0x02206, 0x00283 },
333 { 128, 0x08804, 0x02207, 0x00283 },
334 { 132, 0x08804, 0x02208, 0x00283 },
335 { 136, 0x08804, 0x02209, 0x00283 },
336 { 140, 0x08804, 0x0220a, 0x00283 },
338 { 149, 0x08808, 0x02429, 0x00281 },
339 { 153, 0x08808, 0x0242b, 0x00281 },
340 { 157, 0x08808, 0x0242d, 0x00281 },
341 { 161, 0x08808, 0x0242f, 0x00281 }
344 static device_probe_t ural_match;
345 static device_attach_t ural_attach;
346 static device_detach_t ural_detach;
348 static devclass_t ural_devclass;
350 static kobj_method_t ural_methods[] = {
351 DEVMETHOD(device_probe, ural_match),
352 DEVMETHOD(device_attach, ural_attach),
353 DEVMETHOD(device_detach, ural_detach),
357 static driver_t ural_driver = {
360 sizeof(struct ural_softc)
363 MODULE_DEPEND(ural, usb, 1, 1, 1);
366 ural_match(device_t self)
368 struct usb_attach_arg *uaa = device_get_ivars(self);
370 if (uaa->iface != NULL)
373 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
374 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
378 ural_attach(device_t self)
380 struct ural_softc *sc = device_get_softc(self);
381 struct usb_attach_arg *uaa = device_get_ivars(self);
383 struct ieee80211com *ic = &sc->sc_ic;
384 usb_interface_descriptor_t *id;
385 usb_endpoint_descriptor_t *ed;
390 sc->sc_udev = uaa->device;
391 sc->sc_tx_retries = 7; /* TODO tunable/sysctl */
393 usbd_devinfo(sc->sc_udev, 0, devinfo);
395 device_set_desc_copy(self, devinfo);
397 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
398 kprintf("%s: could not set configuration no\n",
399 device_get_nameunit(sc->sc_dev));
403 /* get the first interface handle */
404 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
407 kprintf("%s: could not get interface handle\n",
408 device_get_nameunit(sc->sc_dev));
415 id = usbd_get_interface_descriptor(sc->sc_iface);
417 sc->sc_rx_no = sc->sc_tx_no = -1;
418 for (i = 0; i < id->bNumEndpoints; i++) {
419 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
421 kprintf("%s: no endpoint descriptor for %d\n",
422 device_get_nameunit(sc->sc_dev), i);
426 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
427 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
428 sc->sc_rx_no = ed->bEndpointAddress;
429 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
430 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
431 sc->sc_tx_no = ed->bEndpointAddress;
433 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
434 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
438 usb_init_task(&sc->sc_task, ural_task, sc);
439 callout_init(&sc->scan_ch);
440 callout_init(&sc->stats_ch);
442 /* retrieve RT2570 rev. no */
443 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
445 /* retrieve MAC address and various other things from EEPROM */
446 ural_read_eeprom(sc);
448 kprintf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
449 device_get_nameunit(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
453 if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
454 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
455 ifp->if_init = ural_init;
456 ifp->if_ioctl = ural_ioctl;
457 ifp->if_start = ural_start;
458 ifp->if_watchdog = ural_watchdog;
459 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
460 ifq_set_ready(&ifp->if_snd);
462 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
463 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
464 ic->ic_ratectl.rc_st_valid_stats =
465 IEEE80211_RATECTL_STATS_PKT_NORETRY |
466 IEEE80211_RATECTL_STATS_PKT_OK |
467 IEEE80211_RATECTL_STATS_PKT_ERR |
468 IEEE80211_RATECTL_STATS_RETRIES;
469 ic->ic_ratectl.rc_st_stats = ural_stats;
470 ic->ic_ratectl.rc_st_change = ural_ratectl_change;
472 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
473 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
474 ic->ic_state = IEEE80211_S_INIT;
476 /* set device capabilities */
478 IEEE80211_C_IBSS | /* IBSS mode supported */
479 IEEE80211_C_MONITOR | /* monitor mode supported */
480 IEEE80211_C_HOSTAP | /* HostAp mode supported */
481 IEEE80211_C_TXPMGT | /* tx power management */
482 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
483 IEEE80211_C_SHSLOT | /* short slot time supported */
484 IEEE80211_C_WPA; /* 802.11i */
486 if (sc->rf_rev == RAL_RF_5222) {
487 /* set supported .11a rates */
488 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
490 /* set supported .11a channels */
491 for (i = 36; i <= 64; i += 4) {
492 ic->ic_channels[i].ic_freq =
493 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
494 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
496 for (i = 100; i <= 140; i += 4) {
497 ic->ic_channels[i].ic_freq =
498 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
499 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
501 for (i = 149; i <= 161; i += 4) {
502 ic->ic_channels[i].ic_freq =
503 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
504 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
508 /* set supported .11b and .11g rates */
509 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
510 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
512 /* set supported .11b and .11g channels (1 through 14) */
513 for (i = 1; i <= 14; i++) {
514 ic->ic_channels[i].ic_freq =
515 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
516 ic->ic_channels[i].ic_flags =
517 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
518 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
521 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
523 ieee80211_ifattach(ic);
524 ic->ic_reset = ural_reset;
525 /* enable s/w bmiss handling in sta mode */
526 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
528 /* override state transition machine */
529 sc->sc_newstate = ic->ic_newstate;
530 ic->ic_newstate = ural_newstate;
531 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
533 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
534 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
536 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
537 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
538 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
540 sc->sc_txtap_len = sizeof sc->sc_txtapu;
541 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
542 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
545 ieee80211_announce(ic);
551 ural_detach(device_t self)
553 struct ural_softc *sc = device_get_softc(self);
554 struct ieee80211com *ic = &sc->sc_ic;
555 struct ifnet *ifp = &ic->ic_if;
562 callout_stop(&sc->scan_ch);
563 callout_stop(&sc->stats_ch);
565 lwkt_serialize_enter(ifp->if_serializer);
567 lwkt_serialize_exit(ifp->if_serializer);
569 usb_rem_task(sc->sc_udev, &sc->sc_task);
572 ieee80211_ifdetach(ic);
576 KKASSERT(sc->stats_xfer == NULL);
577 KKASSERT(sc->sc_rx_pipeh == NULL);
578 KKASSERT(sc->sc_tx_pipeh == NULL);
582 * Make sure TX/RX list is empty
584 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
585 struct ural_tx_data *data = &sc->tx_data[i];
587 KKASSERT(data->xfer == NULL);
588 KKASSERT(data->ni == NULL);
589 KKASSERT(data->m == NULL);
591 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
592 struct ural_rx_data *data = &sc->rx_data[i];
594 KKASSERT(data->xfer == NULL);
595 KKASSERT(data->m == NULL);
603 ural_alloc_tx_list(struct ural_softc *sc)
609 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
610 struct ural_tx_data *data = &sc->tx_data[i];
614 data->xfer = usbd_alloc_xfer(sc->sc_udev);
615 if (data->xfer == NULL) {
616 kprintf("%s: could not allocate tx xfer\n",
617 device_get_nameunit(sc->sc_dev));
621 data->buf = usbd_alloc_buffer(data->xfer,
622 RAL_TX_DESC_SIZE + MCLBYTES);
623 if (data->buf == NULL) {
624 kprintf("%s: could not allocate tx buffer\n",
625 device_get_nameunit(sc->sc_dev));
633 ural_free_tx_list(struct ural_softc *sc)
637 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
638 struct ural_tx_data *data = &sc->tx_data[i];
640 if (data->xfer != NULL) {
641 usbd_free_xfer(data->xfer);
645 if (data->ni != NULL) {
646 ieee80211_free_node(data->ni);
649 if (data->m != NULL) {
658 ural_alloc_rx_list(struct ural_softc *sc)
662 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
663 struct ural_rx_data *data = &sc->rx_data[i];
667 data->xfer = usbd_alloc_xfer(sc->sc_udev);
668 if (data->xfer == NULL) {
669 kprintf("%s: could not allocate rx xfer\n",
670 device_get_nameunit(sc->sc_dev));
674 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
675 kprintf("%s: could not allocate rx buffer\n",
676 device_get_nameunit(sc->sc_dev));
680 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
681 if (data->m == NULL) {
682 kprintf("%s: could not allocate rx mbuf\n",
683 device_get_nameunit(sc->sc_dev));
687 data->buf = mtod(data->m, uint8_t *);
693 ural_free_rx_list(struct ural_softc *sc)
697 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
698 struct ural_rx_data *data = &sc->rx_data[i];
700 if (data->xfer != NULL) {
701 usbd_free_xfer(data->xfer);
705 if (data->m != NULL) {
713 ural_media_change(struct ifnet *ifp)
715 struct ural_softc *sc = ifp->if_softc;
718 error = ieee80211_media_change(ifp);
719 if (error != ENETRESET)
722 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
729 * This function is called periodically (every 200ms) during scanning to
730 * switch from one channel to another.
733 ural_next_scan(void *arg)
735 struct ural_softc *sc = arg;
736 struct ieee80211com *ic = &sc->sc_ic;
737 struct ifnet *ifp = &ic->ic_if;
744 if (ic->ic_state == IEEE80211_S_SCAN) {
745 lwkt_serialize_enter(ifp->if_serializer);
746 ieee80211_next_scan(ic);
747 lwkt_serialize_exit(ifp->if_serializer);
754 ural_task(void *xarg)
756 struct ural_softc *sc = xarg;
757 struct ieee80211com *ic = &sc->sc_ic;
758 struct ifnet *ifp = &ic->ic_if;
759 enum ieee80211_state nstate;
760 struct ieee80211_node *ni;
769 nstate = sc->sc_state;
772 KASSERT(nstate != IEEE80211_S_INIT,
773 ("->INIT state transition should not be defered\n"));
774 ural_set_chan(sc, ic->ic_curchan);
776 switch (sc->sc_state) {
777 case IEEE80211_S_RUN:
780 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
781 ural_update_slot(&ic->ic_if);
782 ural_set_txpreamble(sc);
783 ural_set_basicrates(sc);
784 ural_set_bssid(sc, ni->ni_bssid);
787 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
788 ic->ic_opmode == IEEE80211_M_IBSS) {
789 lwkt_serialize_enter(ifp->if_serializer);
790 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
791 lwkt_serialize_exit(ifp->if_serializer);
794 kprintf("%s: could not allocate beacon\n",
795 device_get_nameunit(sc->sc_dev));
800 if (ural_tx_bcn(sc, m, ni) != 0) {
801 kprintf("%s: could not send beacon\n",
802 device_get_nameunit(sc->sc_dev));
808 /* make tx led blink on tx (controlled by ASIC) */
809 ural_write(sc, RAL_MAC_CSR20, 1);
811 if (ic->ic_opmode != IEEE80211_M_MONITOR)
812 ural_enable_tsf_sync(sc);
814 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
815 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
817 callout_reset(&sc->stats_ch, 4 * hz / 5,
818 ural_stats_timeout, sc);
821 case IEEE80211_S_SCAN:
822 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
829 lwkt_serialize_enter(ifp->if_serializer);
830 ieee80211_ratectl_newstate(ic, sc->sc_state);
831 sc->sc_newstate(ic, sc->sc_state, arg);
832 lwkt_serialize_exit(ifp->if_serializer);
838 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
840 struct ifnet *ifp = &ic->ic_if;
841 struct ural_softc *sc = ifp->if_softc;
843 ASSERT_SERIALIZED(ifp->if_serializer);
847 callout_stop(&sc->scan_ch);
848 callout_stop(&sc->stats_ch);
850 /* do it in a process context */
851 sc->sc_state = nstate;
854 lwkt_serialize_exit(ifp->if_serializer);
855 usb_rem_task(sc->sc_udev, &sc->sc_task);
857 if (nstate == IEEE80211_S_INIT) {
858 lwkt_serialize_enter(ifp->if_serializer);
859 ieee80211_ratectl_newstate(ic, nstate);
860 sc->sc_newstate(ic, nstate, arg);
862 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
863 lwkt_serialize_enter(ifp->if_serializer);
870 /* quickly determine if a given rate is CCK or OFDM */
871 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
873 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_FCS_LEN)
875 #define RAL_RXTX_TURNAROUND 5 /* us */
878 * This function is only used by the Rx radiotap code.
881 ural_rxrate(struct ural_rx_desc *desc)
883 if (le32toh(desc->flags) & RAL_RX_OFDM) {
884 /* reverse function of ural_plcp_signal */
885 switch (desc->rate) {
893 case 0xc: return 108;
896 if (desc->rate == 10)
898 if (desc->rate == 20)
900 if (desc->rate == 55)
902 if (desc->rate == 110)
905 return 2; /* should not get there */
909 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
911 struct ural_tx_data *data = priv;
912 struct ural_softc *sc = data->sc;
913 struct ieee80211_node *ni;
914 struct ifnet *ifp = &sc->sc_ic.ic_if;
921 if (status != USBD_NORMAL_COMPLETION) {
922 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
927 kprintf("%s: could not transmit buffer: %s\n",
928 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
930 if (status == USBD_STALLED)
931 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
946 DPRINTFN(10, ("tx done\n"));
949 ifp->if_flags &= ~IFF_OACTIVE;
951 lwkt_serialize_enter(ifp->if_serializer);
952 ieee80211_free_node(ni);
954 lwkt_serialize_exit(ifp->if_serializer);
960 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
962 struct ural_rx_data *data = priv;
963 struct ural_softc *sc = data->sc;
964 struct ieee80211com *ic = &sc->sc_ic;
965 struct ifnet *ifp = &ic->ic_if;
966 struct ural_rx_desc *desc;
967 struct ieee80211_frame *wh;
968 struct ieee80211_node *ni;
969 struct mbuf *mnew, *m;
977 if (status != USBD_NORMAL_COMPLETION) {
978 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
983 if (status == USBD_STALLED)
984 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
988 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
990 if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
991 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
997 /* rx descriptor is located at the end */
998 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
1000 if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
1001 (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
1003 * This should not happen since we did not request to receive
1004 * those frames when we filled RAL_TXRX_CSR2.
1006 DPRINTFN(5, ("PHY or CRC error\n"));
1011 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1021 lwkt_serialize_enter(ifp->if_serializer);
1024 m->m_pkthdr.rcvif = ifp;
1025 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
1027 if (sc->sc_drvbpf != NULL) {
1028 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
1030 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; /* h/w leaves FCS */
1031 tap->wr_rate = ural_rxrate(desc);
1032 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1033 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1034 tap->wr_antenna = sc->rx_ant;
1035 tap->wr_antsignal = URAL_RSSI(desc->rssi);
1037 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1040 /* trim CRC here so WEP can find its own CRC at the end of packet. */
1041 m_adj(m, -IEEE80211_CRC_LEN);
1043 wh = mtod(m, struct ieee80211_frame *);
1044 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1046 /* send the frame to the 802.11 layer */
1047 ieee80211_input(ic, m, ni, URAL_RSSI(desc->rssi), 0);
1049 /* node is no longer needed */
1050 ieee80211_free_node(ni);
1052 lwkt_serialize_exit(ifp->if_serializer);
1055 data->buf = mtod(data->m, uint8_t *);
1057 DPRINTFN(15, ("rx done\n"));
1059 skip: /* setup a new transfer */
1060 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
1061 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
1062 usbd_transfer(xfer);
1068 ural_plcp_signal(int rate)
1071 /* CCK rates (returned values are device-dependent) */
1074 case 11: return 0x2;
1075 case 22: return 0x3;
1077 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1078 case 12: return 0xb;
1079 case 18: return 0xf;
1080 case 24: return 0xa;
1081 case 36: return 0xe;
1082 case 48: return 0x9;
1083 case 72: return 0xd;
1084 case 96: return 0x8;
1085 case 108: return 0xc;
1087 /* unsupported rates (should not get there) */
1088 default: return 0xff;
1093 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1094 uint32_t flags, int len, int rate)
1096 struct ieee80211com *ic = &sc->sc_ic;
1097 uint16_t plcp_length;
1100 desc->flags = htole32(flags);
1101 desc->flags |= htole32(RAL_TX_NEWSEQ);
1102 desc->flags |= htole32(len << 16);
1104 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1105 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1107 /* setup PLCP fields */
1108 desc->plcp_signal = ural_plcp_signal(rate);
1109 desc->plcp_service = 4;
1111 len += IEEE80211_CRC_LEN;
1112 if (RAL_RATE_IS_OFDM(rate)) {
1113 desc->flags |= htole32(RAL_TX_OFDM);
1115 plcp_length = len & 0xfff;
1116 desc->plcp_length_hi = plcp_length >> 6;
1117 desc->plcp_length_lo = plcp_length & 0x3f;
1119 plcp_length = (16 * len + rate - 1) / rate;
1121 remainder = (16 * len) % 22;
1122 if (remainder != 0 && remainder < 7)
1123 desc->plcp_service |= RAL_PLCP_LENGEXT;
1125 desc->plcp_length_hi = plcp_length >> 8;
1126 desc->plcp_length_lo = plcp_length & 0xff;
1128 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1129 desc->plcp_signal |= 0x08;
1136 #define RAL_TX_TIMEOUT 5000
1139 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1141 struct ural_tx_desc *desc;
1142 usbd_xfer_handle xfer;
1148 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1150 xfer = usbd_alloc_xfer(sc->sc_udev);
1154 /* xfer length needs to be a multiple of two! */
1155 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1157 buf = usbd_alloc_buffer(xfer, xferlen);
1159 usbd_free_xfer(xfer);
1163 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1164 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1166 error = usbd_sync_transfer(xfer);
1168 usbd_free_xfer(xfer);
1172 desc = (struct ural_tx_desc *)buf;
1174 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1175 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1176 m0->m_pkthdr.len, rate);
1178 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1179 m0->m_pkthdr.len, rate, xferlen));
1181 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1182 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1184 error = usbd_sync_transfer(xfer);
1185 usbd_free_xfer(xfer);
1191 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1193 struct ieee80211com *ic = &sc->sc_ic;
1194 struct ifnet *ifp = &ic->ic_if;
1195 struct ural_tx_desc *desc;
1196 struct ural_tx_data *data;
1197 struct ieee80211_frame *wh;
1203 data = &sc->tx_data[0];
1204 desc = (struct ural_tx_desc *)data->buf;
1206 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1211 wh = mtod(m0, struct ieee80211_frame *);
1213 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1214 flags |= RAL_TX_ACK;
1216 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1218 *(uint16_t *)wh->i_dur = htole16(dur);
1220 /* tell hardware to add timestamp for probe responses */
1221 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1222 IEEE80211_FC0_TYPE_MGT &&
1223 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1224 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1225 flags |= RAL_TX_TIMESTAMP;
1228 if (sc->sc_drvbpf != NULL) {
1229 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1232 tap->wt_rate = rate;
1233 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1234 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1235 tap->wt_antenna = sc->tx_ant;
1237 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1240 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1241 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1243 /* align end on a 2-bytes boundary */
1244 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1247 * No space left in the last URB to store the extra 2 bytes, force
1248 * sending of another URB.
1250 if ((xferlen % 64) == 0)
1253 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1254 m0->m_pkthdr.len, rate, xferlen));
1256 lwkt_serialize_exit(ifp->if_serializer);
1258 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1259 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1262 error = usbd_transfer(data->xfer);
1263 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1272 lwkt_serialize_enter(ifp->if_serializer);
1277 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1279 struct ieee80211com *ic = &sc->sc_ic;
1280 struct ifnet *ifp = &ic->ic_if;
1281 struct ural_tx_desc *desc;
1282 struct ural_tx_data *data;
1283 struct ieee80211_frame *wh;
1284 struct ieee80211_key *k;
1288 int xferlen, rate, rate_idx;
1290 wh = mtod(m0, struct ieee80211_frame *);
1292 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1293 rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1295 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1296 k = ieee80211_crypto_encap(ic, ni, m0);
1302 /* packet header may have moved, reset our local pointer */
1303 wh = mtod(m0, struct ieee80211_frame *);
1306 data = &sc->tx_data[0];
1307 desc = (struct ural_tx_desc *)data->buf;
1312 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1313 flags |= RAL_TX_ACK;
1314 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
1316 dur = ieee80211_txtime(ni, RAL_ACK_SIZE,
1317 ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1319 *(uint16_t *)wh->i_dur = htole16(dur);
1322 if (sc->sc_drvbpf != NULL) {
1323 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1326 tap->wt_rate = rate;
1327 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1328 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1329 tap->wt_antenna = sc->tx_ant;
1331 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1334 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1335 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1337 /* align end on a 2-bytes boundary */
1338 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1341 * No space left in the last URB to store the extra 2 bytes, force
1342 * sending of another URB.
1344 if ((xferlen % 64) == 0)
1347 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1348 m0->m_pkthdr.len, rate, xferlen));
1350 lwkt_serialize_exit(ifp->if_serializer);
1352 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1353 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1356 error = usbd_transfer(data->xfer);
1357 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1366 lwkt_serialize_enter(ifp->if_serializer);
1371 ural_start(struct ifnet *ifp)
1373 struct ural_softc *sc = ifp->if_softc;
1374 struct ieee80211com *ic = &sc->sc_ic;
1376 ASSERT_SERIALIZED(ifp->if_serializer);
1383 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
1389 struct ieee80211_node *ni;
1392 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1393 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1394 ifp->if_flags |= IFF_OACTIVE;
1397 IF_DEQUEUE(&ic->ic_mgtq, m0);
1399 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1400 m0->m_pkthdr.rcvif = NULL;
1402 if (ic->ic_rawbpf != NULL)
1403 bpf_mtap(ic->ic_rawbpf, m0);
1405 if (ural_tx_mgt(sc, m0, ni) != 0) {
1406 ieee80211_free_node(ni);
1410 struct ether_header *eh;
1412 if (ic->ic_state != IEEE80211_S_RUN)
1414 m0 = ifq_poll(&ifp->if_snd);
1417 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1418 ifp->if_flags |= IFF_OACTIVE;
1422 ifq_dequeue(&ifp->if_snd, m0);
1424 if (m0->m_len < sizeof (struct ether_header)) {
1425 m0 = m_pullup(m0, sizeof (struct ether_header));
1432 eh = mtod(m0, struct ether_header *);
1433 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1440 m0 = ieee80211_encap(ic, m0, ni);
1442 ieee80211_free_node(ni);
1446 if (ic->ic_rawbpf != NULL)
1447 bpf_mtap(ic->ic_rawbpf, m0);
1449 if (ural_tx_data(sc, m0, ni) != 0) {
1450 ieee80211_free_node(ni);
1456 sc->sc_tx_timer = 5;
1464 ural_watchdog(struct ifnet *ifp)
1466 struct ural_softc *sc = ifp->if_softc;
1467 struct ieee80211com *ic = &sc->sc_ic;
1469 ASSERT_SERIALIZED(ifp->if_serializer);
1475 if (sc->sc_tx_timer > 0) {
1476 if (--sc->sc_tx_timer == 0) {
1477 device_printf(sc->sc_dev, "device timeout\n");
1478 /*ural_init(sc); XXX needs a process context! */
1486 ieee80211_watchdog(ic);
1492 * This function allows for fast channel switching in monitor mode (used by
1493 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1494 * generate a new beacon frame.
1497 ural_reset(struct ifnet *ifp)
1499 struct ural_softc *sc = ifp->if_softc;
1500 struct ieee80211com *ic = &sc->sc_ic;
1502 ASSERT_SERIALIZED(ifp->if_serializer);
1504 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1509 lwkt_serialize_exit(ifp->if_serializer);
1510 ural_set_chan(sc, ic->ic_curchan);
1511 lwkt_serialize_enter(ifp->if_serializer);
1519 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1521 struct ural_softc *sc = ifp->if_softc;
1522 struct ieee80211com *ic = &sc->sc_ic;
1525 ASSERT_SERIALIZED(ifp->if_serializer);
1531 if (ifp->if_flags & IFF_UP) {
1532 if (ifp->if_flags & IFF_RUNNING) {
1533 lwkt_serialize_exit(ifp->if_serializer);
1534 ural_update_promisc(sc);
1535 lwkt_serialize_enter(ifp->if_serializer);
1540 if (ifp->if_flags & IFF_RUNNING)
1546 error = ieee80211_ioctl(ic, cmd, data, cr);
1549 if (error == ENETRESET) {
1550 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1551 (IFF_UP | IFF_RUNNING) &&
1552 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1562 ural_set_testmode(struct ural_softc *sc)
1564 usb_device_request_t req;
1567 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1568 req.bRequest = RAL_VENDOR_REQUEST;
1569 USETW(req.wValue, 4);
1570 USETW(req.wIndex, 1);
1571 USETW(req.wLength, 0);
1573 error = usbd_do_request(sc->sc_udev, &req, NULL);
1575 kprintf("%s: could not set test mode: %s\n",
1576 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1581 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1583 usb_device_request_t req;
1586 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1587 req.bRequest = RAL_READ_EEPROM;
1588 USETW(req.wValue, 0);
1589 USETW(req.wIndex, addr);
1590 USETW(req.wLength, len);
1592 error = usbd_do_request(sc->sc_udev, &req, buf);
1594 kprintf("%s: could not read EEPROM: %s\n",
1595 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1600 ural_read(struct ural_softc *sc, uint16_t reg)
1602 usb_device_request_t req;
1606 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1607 req.bRequest = RAL_READ_MAC;
1608 USETW(req.wValue, 0);
1609 USETW(req.wIndex, reg);
1610 USETW(req.wLength, sizeof (uint16_t));
1612 error = usbd_do_request(sc->sc_udev, &req, &val);
1614 kprintf("%s: could not read MAC register: %s\n",
1615 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1619 return le16toh(val);
1623 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1625 usb_device_request_t req;
1628 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1629 req.bRequest = RAL_READ_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 read MAC register: %s\n",
1637 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1642 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1644 usb_device_request_t req;
1647 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1648 req.bRequest = RAL_WRITE_MAC;
1649 USETW(req.wValue, val);
1650 USETW(req.wIndex, reg);
1651 USETW(req.wLength, 0);
1653 error = usbd_do_request(sc->sc_udev, &req, NULL);
1655 kprintf("%s: could not write MAC register: %s\n",
1656 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1661 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1663 usb_device_request_t req;
1666 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1667 req.bRequest = RAL_WRITE_MULTI_MAC;
1668 USETW(req.wValue, 0);
1669 USETW(req.wIndex, reg);
1670 USETW(req.wLength, len);
1672 error = usbd_do_request(sc->sc_udev, &req, buf);
1674 kprintf("%s: could not write MAC register: %s\n",
1675 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1680 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1685 for (ntries = 0; ntries < 5; ntries++) {
1686 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1690 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1694 tmp = reg << 8 | val;
1695 ural_write(sc, RAL_PHY_CSR7, tmp);
1699 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1704 val = RAL_BBP_WRITE | reg << 8;
1705 ural_write(sc, RAL_PHY_CSR7, val);
1707 for (ntries = 0; ntries < 5; ntries++) {
1708 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1712 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1716 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1720 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1725 for (ntries = 0; ntries < 5; ntries++) {
1726 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1730 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1734 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1735 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1736 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1738 /* remember last written value in sc */
1739 sc->rf_regs[reg] = val;
1741 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1745 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1747 struct ieee80211com *ic = &sc->sc_ic;
1751 chan = ieee80211_chan2ieee(ic, c);
1752 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1755 if (IEEE80211_IS_CHAN_2GHZ(c))
1756 power = min(sc->txpow[chan - 1], 31);
1760 /* adjust txpower using ifconfig settings */
1761 power -= (100 - ic->ic_txpowlimit) / 8;
1763 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1765 switch (sc->rf_rev) {
1767 ural_rf_write(sc, RAL_RF1, 0x00814);
1768 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1769 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1773 ural_rf_write(sc, RAL_RF1, 0x08804);
1774 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1775 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1776 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1780 ural_rf_write(sc, RAL_RF1, 0x0c808);
1781 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1782 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1783 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1787 ural_rf_write(sc, RAL_RF1, 0x08808);
1788 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1789 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1790 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1792 ural_rf_write(sc, RAL_RF1, 0x08808);
1793 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1794 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1795 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1799 ural_rf_write(sc, RAL_RF1, 0x08808);
1800 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1801 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1802 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1806 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1807 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1808 ural_rf_write(sc, RAL_RF1, 0x08804);
1810 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1811 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1812 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1817 for (i = 0; ural_rf5222[i].chan != chan; i++)
1820 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1821 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1822 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1823 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1827 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1828 ic->ic_state != IEEE80211_S_SCAN) {
1829 /* set Japan filter bit for channel 14 */
1830 tmp = ural_bbp_read(sc, 70);
1832 tmp &= ~RAL_JAPAN_FILTER;
1834 tmp |= RAL_JAPAN_FILTER;
1836 ural_bbp_write(sc, 70, tmp);
1838 /* clear CRC errors */
1839 ural_read(sc, RAL_STA_CSR0);
1842 ural_disable_rf_tune(sc);
1845 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1846 : IEEE80211_DUR_SIFS;
1850 * Disable RF auto-tuning.
1853 ural_disable_rf_tune(struct ural_softc *sc)
1857 if (sc->rf_rev != RAL_RF_2523) {
1858 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1859 ural_rf_write(sc, RAL_RF1, tmp);
1862 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1863 ural_rf_write(sc, RAL_RF3, tmp);
1865 DPRINTFN(2, ("disabling RF autotune\n"));
1869 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1873 ural_enable_tsf_sync(struct ural_softc *sc)
1875 struct ieee80211com *ic = &sc->sc_ic;
1876 uint16_t logcwmin, preload, tmp;
1878 /* first, disable TSF synchronization */
1879 ural_write(sc, RAL_TXRX_CSR19, 0);
1881 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1882 ural_write(sc, RAL_TXRX_CSR18, tmp);
1884 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1885 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1886 tmp = logcwmin << 12 | preload;
1887 ural_write(sc, RAL_TXRX_CSR20, tmp);
1889 /* finally, enable TSF synchronization */
1890 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1891 if (ic->ic_opmode == IEEE80211_M_STA)
1892 tmp |= RAL_ENABLE_TSF_SYNC(1);
1894 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1895 ural_write(sc, RAL_TXRX_CSR19, tmp);
1897 DPRINTF(("enabling TSF synchronization\n"));
1901 ural_update_slot(struct ifnet *ifp)
1903 struct ural_softc *sc = ifp->if_softc;
1904 struct ieee80211com *ic = &sc->sc_ic;
1905 uint16_t slottime, sifs, eifs;
1907 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1910 * These settings may sound a bit inconsistent but this is what the
1911 * reference driver does.
1913 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1914 sifs = 16 - RAL_RXTX_TURNAROUND;
1917 sifs = 10 - RAL_RXTX_TURNAROUND;
1921 ural_write(sc, RAL_MAC_CSR10, slottime);
1922 ural_write(sc, RAL_MAC_CSR11, sifs);
1923 ural_write(sc, RAL_MAC_CSR12, eifs);
1927 ural_set_txpreamble(struct ural_softc *sc)
1931 tmp = ural_read(sc, RAL_TXRX_CSR10);
1933 tmp &= ~RAL_SHORT_PREAMBLE;
1934 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1935 tmp |= RAL_SHORT_PREAMBLE;
1937 ural_write(sc, RAL_TXRX_CSR10, tmp);
1941 ural_set_basicrates(struct ural_softc *sc)
1943 struct ieee80211com *ic = &sc->sc_ic;
1945 /* update basic rate set */
1946 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1947 /* 11b basic rates: 1, 2Mbps */
1948 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1949 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1950 /* 11a basic rates: 6, 12, 24Mbps */
1951 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1953 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1954 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1959 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1963 tmp = bssid[0] | bssid[1] << 8;
1964 ural_write(sc, RAL_MAC_CSR5, tmp);
1966 tmp = bssid[2] | bssid[3] << 8;
1967 ural_write(sc, RAL_MAC_CSR6, tmp);
1969 tmp = bssid[4] | bssid[5] << 8;
1970 ural_write(sc, RAL_MAC_CSR7, tmp);
1972 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
1976 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1980 tmp = addr[0] | addr[1] << 8;
1981 ural_write(sc, RAL_MAC_CSR2, tmp);
1983 tmp = addr[2] | addr[3] << 8;
1984 ural_write(sc, RAL_MAC_CSR3, tmp);
1986 tmp = addr[4] | addr[5] << 8;
1987 ural_write(sc, RAL_MAC_CSR4, tmp);
1989 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
1993 ural_update_promisc(struct ural_softc *sc)
1995 struct ifnet *ifp = &sc->sc_ic.ic_if;
1998 tmp = ural_read(sc, RAL_TXRX_CSR2);
2000 tmp &= ~RAL_DROP_NOT_TO_ME;
2001 if (!(ifp->if_flags & IFF_PROMISC))
2002 tmp |= RAL_DROP_NOT_TO_ME;
2004 ural_write(sc, RAL_TXRX_CSR2, tmp);
2006 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2007 "entering" : "leaving"));
2011 ural_get_rf(int rev)
2014 case RAL_RF_2522: return "RT2522";
2015 case RAL_RF_2523: return "RT2523";
2016 case RAL_RF_2524: return "RT2524";
2017 case RAL_RF_2525: return "RT2525";
2018 case RAL_RF_2525E: return "RT2525e";
2019 case RAL_RF_2526: return "RT2526";
2020 case RAL_RF_5222: return "RT5222";
2021 default: return "unknown";
2026 ural_read_eeprom(struct ural_softc *sc)
2028 struct ieee80211com *ic = &sc->sc_ic;
2031 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
2033 sc->rf_rev = (val >> 11) & 0x7;
2034 sc->hw_radio = (val >> 10) & 0x1;
2035 sc->led_mode = (val >> 6) & 0x7;
2036 sc->rx_ant = (val >> 4) & 0x3;
2037 sc->tx_ant = (val >> 2) & 0x3;
2038 sc->nb_ant = val & 0x3;
2040 /* read MAC address */
2041 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2043 /* read default values for BBP registers */
2044 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2046 /* read Tx power for all b/g channels */
2047 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2051 ural_bbp_init(struct ural_softc *sc)
2053 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2056 /* wait for BBP to be ready */
2057 for (ntries = 0; ntries < 100; ntries++) {
2058 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2062 if (ntries == 100) {
2063 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2067 /* initialize BBP registers to default values */
2068 for (i = 0; i < N(ural_def_bbp); i++)
2069 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2072 /* initialize BBP registers to values stored in EEPROM */
2073 for (i = 0; i < 16; i++) {
2074 if (sc->bbp_prom[i].reg == 0xff)
2076 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2085 ural_set_txantenna(struct ural_softc *sc, int antenna)
2090 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2093 else if (antenna == 2)
2096 tx |= RAL_BBP_DIVERSITY;
2098 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2099 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2100 sc->rf_rev == RAL_RF_5222)
2101 tx |= RAL_BBP_FLIPIQ;
2103 ural_bbp_write(sc, RAL_BBP_TX, tx);
2105 /* update values in PHY_CSR5 and PHY_CSR6 */
2106 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2107 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2109 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2110 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2114 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2118 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2121 else if (antenna == 2)
2124 rx |= RAL_BBP_DIVERSITY;
2126 /* need to force no I/Q flip for RF 2525e and 2526 */
2127 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2128 rx &= ~RAL_BBP_FLIPIQ;
2130 ural_bbp_write(sc, RAL_BBP_RX, rx);
2134 ural_init(void *priv)
2136 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2137 struct ural_softc *sc = priv;
2138 struct ieee80211com *ic = &sc->sc_ic;
2139 struct ifnet *ifp = &ic->ic_if;
2140 struct ural_rx_data *data;
2142 usbd_status usb_err;
2143 int i, ntries, error;
2145 ASSERT_SERIALIZED(ifp->if_serializer);
2149 lwkt_serialize_exit(ifp->if_serializer);
2150 ural_set_testmode(sc);
2151 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
2152 lwkt_serialize_enter(ifp->if_serializer);
2157 lwkt_serialize_exit(ifp->if_serializer);
2159 /* initialize MAC registers to default values */
2160 for (i = 0; i < N(ural_def_mac); i++)
2161 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2163 /* wait for BBP and RF to wake up (this can take a long time!) */
2164 for (ntries = 0; ntries < 100; ntries++) {
2165 tmp = ural_read(sc, RAL_MAC_CSR17);
2166 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2167 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2171 if (ntries == 100) {
2172 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
2173 device_get_nameunit(sc->sc_dev));
2179 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2181 /* set basic rate set (will be updated later) */
2182 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2184 error = ural_bbp_init(sc);
2188 /* set default BSS channel */
2189 ural_set_chan(sc, ic->ic_curchan);
2191 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2192 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2194 ural_set_txantenna(sc, sc->tx_ant);
2195 ural_set_rxantenna(sc, sc->rx_ant);
2197 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2198 ural_set_macaddr(sc, ic->ic_myaddr);
2201 * Allocate xfer for AMRR statistics requests.
2203 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
2204 if (sc->stats_xfer == NULL) {
2205 kprintf("%s: could not allocate AMRR xfer\n",
2206 device_get_nameunit(sc->sc_dev));
2212 * Open Tx and Rx USB bulk pipes.
2214 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2217 kprintf("%s: could not open Tx pipe: %s\n",
2218 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2223 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2226 kprintf("%s: could not open Rx pipe: %s\n",
2227 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2233 * Allocate Tx and Rx xfer queues.
2235 error = ural_alloc_tx_list(sc);
2237 kprintf("%s: could not allocate Tx list\n",
2238 device_get_nameunit(sc->sc_dev));
2242 error = ural_alloc_rx_list(sc);
2244 kprintf("%s: could not allocate Rx list\n",
2245 device_get_nameunit(sc->sc_dev));
2250 * Start up the receive pipe.
2252 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2253 data = &sc->rx_data[i];
2255 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2256 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2257 usbd_transfer(data->xfer);
2261 tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2262 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2263 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2264 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2265 tmp |= RAL_DROP_TODS;
2266 if (!(ifp->if_flags & IFF_PROMISC))
2267 tmp |= RAL_DROP_NOT_TO_ME;
2269 ural_write(sc, RAL_TXRX_CSR2, tmp);
2271 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2272 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2274 lwkt_serialize_enter(ifp->if_serializer);
2278 ifp->if_flags &= ~IFF_OACTIVE;
2279 ifp->if_flags |= IFF_RUNNING;
2281 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2282 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2283 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2285 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2294 ural_stop(struct ural_softc *sc)
2296 struct ieee80211com *ic = &sc->sc_ic;
2297 struct ifnet *ifp = &ic->ic_if;
2299 ASSERT_SERIALIZED(ifp->if_serializer);
2303 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2306 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2308 sc->sc_tx_timer = 0;
2311 lwkt_serialize_exit(ifp->if_serializer);
2314 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2316 /* reset ASIC and BBP (but won't reset MAC registers!) */
2317 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2318 ural_write(sc, RAL_MAC_CSR1, 0);
2320 if (sc->stats_xfer != NULL) {
2321 usbd_free_xfer(sc->stats_xfer);
2322 sc->stats_xfer = NULL;
2325 if (sc->sc_rx_pipeh != NULL) {
2326 usbd_abort_pipe(sc->sc_rx_pipeh);
2327 usbd_close_pipe(sc->sc_rx_pipeh);
2328 sc->sc_rx_pipeh = NULL;
2331 if (sc->sc_tx_pipeh != NULL) {
2332 usbd_abort_pipe(sc->sc_tx_pipeh);
2333 usbd_close_pipe(sc->sc_tx_pipeh);
2334 sc->sc_tx_pipeh = NULL;
2337 lwkt_serialize_enter(ifp->if_serializer);
2339 ural_free_rx_list(sc);
2340 ural_free_tx_list(sc);
2346 ural_stats_timeout(void *arg)
2348 struct ural_softc *sc = (struct ural_softc *)arg;
2349 usb_device_request_t req;
2357 * Asynchronously read statistic registers (cleared by read).
2359 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2360 req.bRequest = RAL_READ_MULTI_MAC;
2361 USETW(req.wValue, 0);
2362 USETW(req.wIndex, RAL_STA_CSR0);
2363 USETW(req.wLength, sizeof(sc->sta));
2365 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2366 USBD_DEFAULT_TIMEOUT, &req,
2367 sc->sta, sizeof(sc->sta), 0,
2369 usbd_transfer(sc->stats_xfer);
2375 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2378 struct ural_softc *sc = (struct ural_softc *)priv;
2379 struct ifnet *ifp = &sc->sc_ic.ic_if;
2380 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2382 if (status != USBD_NORMAL_COMPLETION) {
2383 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2384 "cancelling automatic rate control\n");
2390 /* count TX retry-fail as Tx errors */
2391 ifp->if_oerrors += sc->sta[RAL_TX_PKT_FAIL];
2393 stats->stats_pkt_ok += sc->sta[RAL_TX_PKT_NO_RETRY] +
2394 sc->sta[RAL_TX_PKT_ONE_RETRY] +
2395 sc->sta[RAL_TX_PKT_MULTI_RETRY];
2397 stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2399 stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2401 stats->stats_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2404 * XXX Estimated average:
2405 * Actual number of retries for each packet should belong to
2406 * [2, sc->sc_tx_retries]
2408 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2409 ((2 + sc->sc_tx_retries) / 2);
2411 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2413 stats->stats_retries += sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2415 callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2421 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2422 struct ieee80211_ratectl_stats *stats)
2424 struct ifnet *ifp = &ic->ic_if;
2425 struct ural_softc *sc = ifp->if_softc;
2427 ASSERT_SERIALIZED(ifp->if_serializer);
2429 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2430 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2434 ural_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2436 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2437 struct ieee80211_onoe_param *oparam;
2439 if (st->rc_st_param != NULL) {
2440 kfree(st->rc_st_param, M_DEVBUF);
2441 st->rc_st_param = NULL;
2445 case IEEE80211_RATECTL_ONOE:
2446 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2448 IEEE80211_ONOE_PARAM_SETUP(oparam);
2449 oparam->onoe_raise = 20;
2451 st->rc_st_param = oparam;
2453 case IEEE80211_RATECTL_NONE:
2454 /* This could only happen during detaching */
2457 panic("unknown rate control algo %u\n", nrc);
2461 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);