1 /* $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.10.2.8 2006/07/08 07:48:43 maxim Exp $ */
4 * Copyright (c) 2005, 2006
5 * Damien Bergamini <damien.bergamini@free.fr>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Ralink Technology RT2500USB chipset driver
22 * http://www.ralinktech.com/
25 #include <sys/param.h>
27 #include <sys/endian.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.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>
52 #include <dev/netif/ural/if_uralreg.h>
53 #include <dev/netif/ural/if_uralvar.h>
56 #define DPRINTF(x) do { if (uraldebug > 0) kprintf x; } while (0)
57 #define DPRINTFN(n, x) do { if (uraldebug >= (n)) kprintf x; } while (0)
59 SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
60 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &uraldebug, 0,
64 #define DPRINTFN(n, x)
67 #define URAL_RSSI(rssi) \
68 ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
69 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
71 /* various supported device vendors/products */
72 static const struct usb_devno ural_devs[] = {
73 { USB_DEVICE(0x0411, 0x005e) }, /* Melco WLI-U2-KG54-YB */
74 { USB_DEVICE(0x0411, 0x0066) }, /* Melco WLI-U2-KG54 */
75 { USB_DEVICE(0x0411, 0x0067) }, /* Melco WLI-U2-KG54-AI */
76 { USB_DEVICE(0x0411, 0x008b) }, /* Melco Nintendo Wi-Fi */
77 { USB_DEVICE(0x050d, 0x7050) }, /* Belkin Components F5D7050 */
78 { USB_DEVICE(0x06f8, 0xe000) }, /* Guillemot HWGUSB254 */
79 { USB_DEVICE(0x0769, 0x11f3) }, /* Surecom RT2570 */
80 { USB_DEVICE(0x0b05, 0x1706) }, /* Ralink (XXX) RT2500USB */
81 { USB_DEVICE(0x0b05, 0x1707) }, /* Asus WL167G */
82 { USB_DEVICE(0x0db0, 0x6861) }, /* MSI RT2570 */
83 { USB_DEVICE(0x0db0, 0x6865) }, /* MSI RT2570 */
84 { USB_DEVICE(0x0db0, 0x6869) }, /* MSI RT2570 */
85 { USB_DEVICE(0x0eb0, 0x9020) }, /* Nova Technology NV-902W */
86 { USB_DEVICE(0x0f88, 0x3012) }, /* VTech RT2570 */
87 { USB_DEVICE(0x1044, 0x8007) }, /* GIGABYTE GN-WBKG */
88 { USB_DEVICE(0x114b, 0x0110) }, /* Sphairon UB801R */
89 { USB_DEVICE(0x148f, 0x1706) }, /* Ralink RT2570 */
90 { USB_DEVICE(0x148f, 0x2570) }, /* Ralink RT2570 */
91 { USB_DEVICE(0x148f, 0x9020) }, /* Ralink RT2570 */
92 { USB_DEVICE(0x14b2, 0x3c02) }, /* Conceptronic C54RU */
93 { USB_DEVICE(0x1737, 0x000d) }, /* Linksys WUSB54G */
94 { USB_DEVICE(0x1737, 0x0011) }, /* Linksys WUSB54GP */
95 { USB_DEVICE(0x1737, 0x001a) }, /* Linksys HU200TS */
96 { USB_DEVICE(0x2001, 0x3c00) }, /* D-Link DWL-G122 */
97 { USB_DEVICE(0x5a57, 0x0260) }, /* Zinwell RT2570 */
100 static int ural_alloc_tx_list(struct ural_softc *);
101 static void ural_free_tx_list(struct ural_softc *);
102 static int ural_alloc_rx_list(struct ural_softc *);
103 static void ural_free_rx_list(struct ural_softc *);
104 static int ural_media_change(struct ifnet *);
105 static void ural_next_scan(void *);
106 static void ural_task(void *);
107 static int ural_newstate(struct ieee80211com *,
108 enum ieee80211_state, int);
109 static int ural_rxrate(struct ural_rx_desc *);
110 static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
112 static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
114 static uint8_t ural_plcp_signal(int);
115 static void ural_setup_tx_desc(struct ural_softc *,
116 struct ural_tx_desc *, uint32_t, int, int);
117 static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
118 struct ieee80211_node *);
119 static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
120 struct ieee80211_node *);
121 static int ural_tx_data(struct ural_softc *, struct mbuf *,
122 struct ieee80211_node *);
123 static void ural_start(struct ifnet *, struct ifaltq_subque *);
124 static void ural_watchdog(struct ifnet *);
125 static int ural_reset(struct ifnet *);
126 static int ural_ioctl(struct ifnet *, u_long, caddr_t,
128 static void ural_set_testmode(struct ural_softc *);
129 static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
131 static uint16_t ural_read(struct ural_softc *, uint16_t);
132 static void ural_read_multi(struct ural_softc *, uint16_t, void *,
134 static void ural_write(struct ural_softc *, uint16_t, uint16_t);
135 static void ural_write_multi(struct ural_softc *, uint16_t, void *,
137 static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
138 static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
139 static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
140 static void ural_set_chan(struct ural_softc *,
141 struct ieee80211_channel *);
142 static void ural_disable_rf_tune(struct ural_softc *);
143 static void ural_enable_tsf_sync(struct ural_softc *);
144 static void ural_update_slot(struct ifnet *);
145 static void ural_set_txpreamble(struct ural_softc *);
146 static void ural_set_basicrates(struct ural_softc *);
147 static void ural_set_bssid(struct ural_softc *, uint8_t *);
148 static void ural_set_macaddr(struct ural_softc *, uint8_t *);
149 static void ural_update_promisc(struct ural_softc *);
150 static const char *ural_get_rf(int);
151 static void ural_read_eeprom(struct ural_softc *);
152 static int ural_bbp_init(struct ural_softc *);
153 static void ural_set_txantenna(struct ural_softc *, int);
154 static void ural_set_rxantenna(struct ural_softc *, int);
155 static void ural_init(void *);
156 static void ural_stop(struct ural_softc *);
157 static void ural_stats(struct ieee80211com *,
158 struct ieee80211_node *,
159 struct ieee80211_ratectl_stats *);
160 static void ural_stats_update(usbd_xfer_handle,
161 usbd_private_handle, usbd_status);
162 static void ural_stats_timeout(void *);
163 static void *ural_ratectl_attach(struct ieee80211com *ic, u_int);
166 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
168 static const struct ieee80211_rateset ural_rateset_11a =
169 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
171 static const struct ieee80211_rateset ural_rateset_11b =
172 { 4, { 2, 4, 11, 22 } };
174 static const struct ieee80211_rateset ural_rateset_11g =
175 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
178 * Default values for MAC registers; values taken from the reference driver.
180 static const struct {
184 { RAL_TXRX_CSR5, 0x8c8d },
185 { RAL_TXRX_CSR6, 0x8b8a },
186 { RAL_TXRX_CSR7, 0x8687 },
187 { RAL_TXRX_CSR8, 0x0085 },
188 { RAL_MAC_CSR13, 0x1111 },
189 { RAL_MAC_CSR14, 0x1e11 },
190 { RAL_TXRX_CSR21, 0xe78f },
191 { RAL_MAC_CSR9, 0xff1d },
192 { RAL_MAC_CSR11, 0x0002 },
193 { RAL_MAC_CSR22, 0x0053 },
194 { RAL_MAC_CSR15, 0x0000 },
195 { RAL_MAC_CSR8, 0x0780 },
196 { RAL_TXRX_CSR19, 0x0000 },
197 { RAL_TXRX_CSR18, 0x005a },
198 { RAL_PHY_CSR2, 0x0000 },
199 { RAL_TXRX_CSR0, 0x1ec0 },
200 { RAL_PHY_CSR4, 0x000f }
204 * Default values for BBP registers; values taken from the reference driver.
206 static const struct {
245 * Default values for RF register R2 indexed by channel numbers.
247 static const uint32_t ural_rf2522_r2[] = {
248 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
249 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
252 static const uint32_t ural_rf2523_r2[] = {
253 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
254 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
257 static const uint32_t ural_rf2524_r2[] = {
258 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
259 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
262 static const uint32_t ural_rf2525_r2[] = {
263 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
264 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
267 static const uint32_t ural_rf2525_hi_r2[] = {
268 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
269 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
272 static const uint32_t ural_rf2525e_r2[] = {
273 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
274 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
277 static const uint32_t ural_rf2526_hi_r2[] = {
278 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
279 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
282 static const uint32_t ural_rf2526_r2[] = {
283 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
284 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
288 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
289 * values taken from the reference driver.
291 static const struct {
297 { 1, 0x08808, 0x0044d, 0x00282 },
298 { 2, 0x08808, 0x0044e, 0x00282 },
299 { 3, 0x08808, 0x0044f, 0x00282 },
300 { 4, 0x08808, 0x00460, 0x00282 },
301 { 5, 0x08808, 0x00461, 0x00282 },
302 { 6, 0x08808, 0x00462, 0x00282 },
303 { 7, 0x08808, 0x00463, 0x00282 },
304 { 8, 0x08808, 0x00464, 0x00282 },
305 { 9, 0x08808, 0x00465, 0x00282 },
306 { 10, 0x08808, 0x00466, 0x00282 },
307 { 11, 0x08808, 0x00467, 0x00282 },
308 { 12, 0x08808, 0x00468, 0x00282 },
309 { 13, 0x08808, 0x00469, 0x00282 },
310 { 14, 0x08808, 0x0046b, 0x00286 },
312 { 36, 0x08804, 0x06225, 0x00287 },
313 { 40, 0x08804, 0x06226, 0x00287 },
314 { 44, 0x08804, 0x06227, 0x00287 },
315 { 48, 0x08804, 0x06228, 0x00287 },
316 { 52, 0x08804, 0x06229, 0x00287 },
317 { 56, 0x08804, 0x0622a, 0x00287 },
318 { 60, 0x08804, 0x0622b, 0x00287 },
319 { 64, 0x08804, 0x0622c, 0x00287 },
321 { 100, 0x08804, 0x02200, 0x00283 },
322 { 104, 0x08804, 0x02201, 0x00283 },
323 { 108, 0x08804, 0x02202, 0x00283 },
324 { 112, 0x08804, 0x02203, 0x00283 },
325 { 116, 0x08804, 0x02204, 0x00283 },
326 { 120, 0x08804, 0x02205, 0x00283 },
327 { 124, 0x08804, 0x02206, 0x00283 },
328 { 128, 0x08804, 0x02207, 0x00283 },
329 { 132, 0x08804, 0x02208, 0x00283 },
330 { 136, 0x08804, 0x02209, 0x00283 },
331 { 140, 0x08804, 0x0220a, 0x00283 },
333 { 149, 0x08808, 0x02429, 0x00281 },
334 { 153, 0x08808, 0x0242b, 0x00281 },
335 { 157, 0x08808, 0x0242d, 0x00281 },
336 { 161, 0x08808, 0x0242f, 0x00281 }
339 static device_probe_t ural_match;
340 static device_attach_t ural_attach;
341 static device_detach_t ural_detach;
343 static devclass_t ural_devclass;
345 static kobj_method_t ural_methods[] = {
346 DEVMETHOD(device_probe, ural_match),
347 DEVMETHOD(device_attach, ural_attach),
348 DEVMETHOD(device_detach, ural_detach),
352 static driver_t ural_driver = {
355 sizeof(struct ural_softc)
358 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, NULL);
360 MODULE_DEPEND(ural, usb, 1, 1, 1);
361 MODULE_DEPEND(ural, wlan, 1, 1, 1);
362 MODULE_DEPEND(ural, wlan_ratectl_onoe, 1, 1, 1);
365 ural_match(device_t self)
367 struct usb_attach_arg *uaa = device_get_ivars(self);
369 if (uaa->iface != NULL)
372 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
373 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
377 ural_attach(device_t self)
379 struct ural_softc *sc = device_get_softc(self);
380 struct usb_attach_arg *uaa = device_get_ivars(self);
382 struct ieee80211com *ic = &sc->sc_ic;
383 usb_interface_descriptor_t *id;
384 usb_endpoint_descriptor_t *ed;
388 sc->sc_udev = uaa->device;
389 sc->sc_tx_retries = 7; /* TODO tunable/sysctl */
393 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
394 kprintf("%s: could not set configuration no\n",
395 device_get_nameunit(sc->sc_dev));
399 /* get the first interface handle */
400 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
403 kprintf("%s: could not get interface handle\n",
404 device_get_nameunit(sc->sc_dev));
411 id = usbd_get_interface_descriptor(sc->sc_iface);
413 sc->sc_rx_no = sc->sc_tx_no = -1;
414 for (i = 0; i < id->bNumEndpoints; i++) {
415 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
417 kprintf("%s: no endpoint descriptor for %d\n",
418 device_get_nameunit(sc->sc_dev), i);
422 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
423 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
424 sc->sc_rx_no = ed->bEndpointAddress;
425 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
426 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
427 sc->sc_tx_no = ed->bEndpointAddress;
429 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
430 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
434 usb_init_task(&sc->sc_task, ural_task, sc);
435 callout_init(&sc->scan_ch);
436 callout_init(&sc->stats_ch);
438 /* retrieve RT2570 rev. no */
439 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
441 /* retrieve MAC address and various other things from EEPROM */
442 ural_read_eeprom(sc);
444 kprintf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
445 device_get_nameunit(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
449 if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
450 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
451 ifp->if_init = ural_init;
452 ifp->if_ioctl = ural_ioctl;
453 ifp->if_start = ural_start;
454 ifp->if_watchdog = ural_watchdog;
455 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
457 ifq_set_ready(&ifp->if_snd);
460 IEEE80211_ONOE_PARAM_SETUP(&sc->sc_onoe_param);
461 sc->sc_onoe_param.onoe_raise = 20;
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_stats = ural_stats;
465 ic->ic_ratectl.rc_st_attach = ural_ratectl_attach;
467 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
468 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
469 ic->ic_state = IEEE80211_S_INIT;
471 /* set device capabilities */
473 IEEE80211_C_IBSS | /* IBSS mode supported */
474 IEEE80211_C_MONITOR | /* monitor mode supported */
475 IEEE80211_C_HOSTAP | /* HostAp mode supported */
476 IEEE80211_C_TXPMGT | /* tx power management */
477 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
478 IEEE80211_C_SHSLOT | /* short slot time supported */
479 IEEE80211_C_WPA; /* 802.11i */
481 if (sc->rf_rev == RAL_RF_5222) {
482 /* set supported .11a rates */
483 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
485 /* set supported .11a channels */
486 for (i = 36; i <= 64; i += 4) {
487 ic->ic_channels[i].ic_freq =
488 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
489 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
491 for (i = 100; i <= 140; 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 = 149; i <= 161; 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;
503 /* set supported .11b and .11g rates */
504 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
505 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
507 /* set supported .11b and .11g channels (1 through 14) */
508 for (i = 1; i <= 14; i++) {
509 ic->ic_channels[i].ic_freq =
510 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
511 ic->ic_channels[i].ic_flags =
512 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
513 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
516 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
518 ieee80211_ifattach(ic);
519 ic->ic_reset = ural_reset;
520 /* enable s/w bmiss handling in sta mode */
521 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
523 /* override state transition machine */
524 sc->sc_newstate = ic->ic_newstate;
525 ic->ic_newstate = ural_newstate;
526 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
528 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
529 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
531 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
532 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
533 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
535 sc->sc_txtap_len = sizeof sc->sc_txtapu;
536 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
537 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
540 ieee80211_announce(ic);
546 ural_detach(device_t self)
548 struct ural_softc *sc = device_get_softc(self);
549 struct ieee80211com *ic = &sc->sc_ic;
550 struct ifnet *ifp = &ic->ic_if;
557 callout_stop(&sc->scan_ch);
558 callout_stop(&sc->stats_ch);
560 lwkt_serialize_enter(ifp->if_serializer);
562 lwkt_serialize_exit(ifp->if_serializer);
564 usb_rem_task(sc->sc_udev, &sc->sc_task);
567 ieee80211_ifdetach(ic);
571 KKASSERT(sc->stats_xfer == NULL);
572 KKASSERT(sc->sc_rx_pipeh == NULL);
573 KKASSERT(sc->sc_tx_pipeh == NULL);
577 * Make sure TX/RX list is empty
579 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
580 struct ural_tx_data *data = &sc->tx_data[i];
582 KKASSERT(data->xfer == NULL);
583 KKASSERT(data->ni == NULL);
584 KKASSERT(data->m == NULL);
586 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
587 struct ural_rx_data *data = &sc->rx_data[i];
589 KKASSERT(data->xfer == NULL);
590 KKASSERT(data->m == NULL);
598 ural_alloc_tx_list(struct ural_softc *sc)
604 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
605 struct ural_tx_data *data = &sc->tx_data[i];
609 data->xfer = usbd_alloc_xfer(sc->sc_udev);
610 if (data->xfer == NULL) {
611 kprintf("%s: could not allocate tx xfer\n",
612 device_get_nameunit(sc->sc_dev));
616 data->buf = usbd_alloc_buffer(data->xfer,
617 RAL_TX_DESC_SIZE + MCLBYTES);
618 if (data->buf == NULL) {
619 kprintf("%s: could not allocate tx buffer\n",
620 device_get_nameunit(sc->sc_dev));
628 ural_free_tx_list(struct ural_softc *sc)
632 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
633 struct ural_tx_data *data = &sc->tx_data[i];
635 if (data->xfer != NULL) {
636 usbd_free_xfer(data->xfer);
640 if (data->ni != NULL) {
641 ieee80211_free_node(data->ni);
644 if (data->m != NULL) {
653 ural_alloc_rx_list(struct ural_softc *sc)
657 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
658 struct ural_rx_data *data = &sc->rx_data[i];
662 data->xfer = usbd_alloc_xfer(sc->sc_udev);
663 if (data->xfer == NULL) {
664 kprintf("%s: could not allocate rx xfer\n",
665 device_get_nameunit(sc->sc_dev));
669 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
670 kprintf("%s: could not allocate rx buffer\n",
671 device_get_nameunit(sc->sc_dev));
675 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
676 if (data->m == NULL) {
677 kprintf("%s: could not allocate rx mbuf\n",
678 device_get_nameunit(sc->sc_dev));
682 data->buf = mtod(data->m, uint8_t *);
688 ural_free_rx_list(struct ural_softc *sc)
692 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
693 struct ural_rx_data *data = &sc->rx_data[i];
695 if (data->xfer != NULL) {
696 usbd_free_xfer(data->xfer);
700 if (data->m != NULL) {
708 ural_media_change(struct ifnet *ifp)
710 struct ural_softc *sc = ifp->if_softc;
713 error = ieee80211_media_change(ifp);
714 if (error != ENETRESET)
717 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
724 * This function is called periodically (every 200ms) during scanning to
725 * switch from one channel to another.
728 ural_next_scan(void *arg)
730 struct ural_softc *sc = arg;
731 struct ieee80211com *ic = &sc->sc_ic;
732 struct ifnet *ifp = &ic->ic_if;
739 if (ic->ic_state == IEEE80211_S_SCAN) {
740 lwkt_serialize_enter(ifp->if_serializer);
741 ieee80211_next_scan(ic);
742 lwkt_serialize_exit(ifp->if_serializer);
749 ural_task(void *xarg)
751 struct ural_softc *sc = xarg;
752 struct ieee80211com *ic = &sc->sc_ic;
753 struct ifnet *ifp = &ic->ic_if;
754 enum ieee80211_state nstate;
755 struct ieee80211_node *ni;
764 nstate = sc->sc_state;
767 KASSERT(nstate != IEEE80211_S_INIT,
768 ("->INIT state transition should not be defered\n"));
769 ural_set_chan(sc, ic->ic_curchan);
771 switch (sc->sc_state) {
772 case IEEE80211_S_RUN:
775 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
776 ural_update_slot(&ic->ic_if);
777 ural_set_txpreamble(sc);
778 ural_set_basicrates(sc);
779 ural_set_bssid(sc, ni->ni_bssid);
782 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
783 ic->ic_opmode == IEEE80211_M_IBSS) {
784 lwkt_serialize_enter(ifp->if_serializer);
785 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
786 lwkt_serialize_exit(ifp->if_serializer);
789 kprintf("%s: could not allocate beacon\n",
790 device_get_nameunit(sc->sc_dev));
795 if (ural_tx_bcn(sc, m, ni) != 0) {
796 kprintf("%s: could not send beacon\n",
797 device_get_nameunit(sc->sc_dev));
803 /* make tx led blink on tx (controlled by ASIC) */
804 ural_write(sc, RAL_MAC_CSR20, 1);
806 if (ic->ic_opmode != IEEE80211_M_MONITOR)
807 ural_enable_tsf_sync(sc);
809 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
810 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
812 callout_reset(&sc->stats_ch, 4 * hz / 5,
813 ural_stats_timeout, sc);
816 case IEEE80211_S_SCAN:
817 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
824 lwkt_serialize_enter(ifp->if_serializer);
825 ieee80211_ratectl_newstate(ic, sc->sc_state);
826 sc->sc_newstate(ic, sc->sc_state, arg);
827 lwkt_serialize_exit(ifp->if_serializer);
833 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
835 struct ifnet *ifp = &ic->ic_if;
836 struct ural_softc *sc = ifp->if_softc;
838 ASSERT_SERIALIZED(ifp->if_serializer);
842 callout_stop(&sc->scan_ch);
843 callout_stop(&sc->stats_ch);
845 /* do it in a process context */
846 sc->sc_state = nstate;
849 lwkt_serialize_exit(ifp->if_serializer);
850 usb_rem_task(sc->sc_udev, &sc->sc_task);
852 if (nstate == IEEE80211_S_INIT) {
853 lwkt_serialize_enter(ifp->if_serializer);
854 ieee80211_ratectl_newstate(ic, nstate);
855 sc->sc_newstate(ic, nstate, arg);
857 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
858 lwkt_serialize_enter(ifp->if_serializer);
865 /* quickly determine if a given rate is CCK or OFDM */
866 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
868 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
870 #define RAL_RXTX_TURNAROUND 5 /* us */
873 * This function is only used by the Rx radiotap code.
876 ural_rxrate(struct ural_rx_desc *desc)
878 if (le32toh(desc->flags) & RAL_RX_OFDM) {
879 /* reverse function of ural_plcp_signal */
880 switch (desc->rate) {
888 case 0xc: return 108;
891 if (desc->rate == 10)
893 if (desc->rate == 20)
895 if (desc->rate == 55)
897 if (desc->rate == 110)
900 return 2; /* should not get there */
904 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
906 struct ural_tx_data *data = priv;
907 struct ural_softc *sc = data->sc;
908 struct ieee80211_node *ni;
909 struct ifnet *ifp = &sc->sc_ic.ic_if;
916 if (status != USBD_NORMAL_COMPLETION) {
917 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
922 kprintf("%s: could not transmit buffer: %s\n",
923 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
925 if (status == USBD_STALLED)
926 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
928 IFNET_STAT_INC(ifp, oerrors, 1);
939 IFNET_STAT_INC(ifp, opackets, 1);
941 DPRINTFN(10, ("tx done\n"));
944 ifq_clr_oactive(&ifp->if_snd);
946 lwkt_serialize_enter(ifp->if_serializer);
947 ieee80211_free_node(ni);
949 lwkt_serialize_exit(ifp->if_serializer);
955 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
957 struct ural_rx_data *data = priv;
958 struct ural_softc *sc = data->sc;
959 struct ieee80211com *ic = &sc->sc_ic;
960 struct ifnet *ifp = &ic->ic_if;
961 struct ural_rx_desc *desc;
962 struct ieee80211_frame *wh;
963 struct ieee80211_node *ni;
964 struct mbuf *mnew, *m;
972 if (status != USBD_NORMAL_COMPLETION) {
973 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
978 if (status == USBD_STALLED)
979 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
983 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
985 if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
986 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
988 IFNET_STAT_INC(ifp, ierrors, 1);
992 /* rx descriptor is located at the end */
993 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
995 if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
996 (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
998 * This should not happen since we did not request to receive
999 * those frames when we filled RAL_TXRX_CSR2.
1001 DPRINTFN(5, ("PHY or CRC error\n"));
1002 IFNET_STAT_INC(ifp, ierrors, 1);
1006 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1008 IFNET_STAT_INC(ifp, ierrors, 1);
1016 lwkt_serialize_enter(ifp->if_serializer);
1019 m->m_pkthdr.rcvif = ifp;
1020 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
1022 if (sc->sc_drvbpf != NULL) {
1023 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
1025 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; /* h/w leaves FCS */
1026 tap->wr_rate = ural_rxrate(desc);
1027 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1028 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1029 tap->wr_antenna = sc->rx_ant;
1030 tap->wr_antsignal = URAL_RSSI(desc->rssi);
1032 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1035 /* trim CRC here so WEP can find its own CRC at the end of packet. */
1036 m_adj(m, -IEEE80211_CRC_LEN);
1038 wh = mtod(m, struct ieee80211_frame *);
1039 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1041 /* send the frame to the 802.11 layer */
1042 ieee80211_input(ic, m, ni, URAL_RSSI(desc->rssi), 0);
1044 /* node is no longer needed */
1045 ieee80211_free_node(ni);
1047 lwkt_serialize_exit(ifp->if_serializer);
1050 data->buf = mtod(data->m, uint8_t *);
1052 DPRINTFN(15, ("rx done\n"));
1054 skip: /* setup a new transfer */
1055 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
1056 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
1057 usbd_transfer(xfer);
1063 ural_plcp_signal(int rate)
1066 /* CCK rates (returned values are device-dependent) */
1069 case 11: return 0x2;
1070 case 22: return 0x3;
1072 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1073 case 12: return 0xb;
1074 case 18: return 0xf;
1075 case 24: return 0xa;
1076 case 36: return 0xe;
1077 case 48: return 0x9;
1078 case 72: return 0xd;
1079 case 96: return 0x8;
1080 case 108: return 0xc;
1082 /* unsupported rates (should not get there) */
1083 default: return 0xff;
1088 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1089 uint32_t flags, int len, int rate)
1091 struct ieee80211com *ic = &sc->sc_ic;
1092 uint16_t plcp_length;
1095 desc->flags = htole32(flags);
1096 desc->flags |= htole32(RAL_TX_NEWSEQ);
1097 desc->flags |= htole32(len << 16);
1099 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1100 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1102 /* setup PLCP fields */
1103 desc->plcp_signal = ural_plcp_signal(rate);
1104 desc->plcp_service = 4;
1106 len += IEEE80211_CRC_LEN;
1107 if (RAL_RATE_IS_OFDM(rate)) {
1108 desc->flags |= htole32(RAL_TX_OFDM);
1110 plcp_length = len & 0xfff;
1111 desc->plcp_length_hi = plcp_length >> 6;
1112 desc->plcp_length_lo = plcp_length & 0x3f;
1114 plcp_length = (16 * len + rate - 1) / rate;
1116 remainder = (16 * len) % 22;
1117 if (remainder != 0 && remainder < 7)
1118 desc->plcp_service |= RAL_PLCP_LENGEXT;
1120 desc->plcp_length_hi = plcp_length >> 8;
1121 desc->plcp_length_lo = plcp_length & 0xff;
1123 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1124 desc->plcp_signal |= 0x08;
1131 #define RAL_TX_TIMEOUT 5000
1134 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1136 struct ural_tx_desc *desc;
1137 usbd_xfer_handle xfer;
1143 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1145 xfer = usbd_alloc_xfer(sc->sc_udev);
1149 /* xfer length needs to be a multiple of two! */
1150 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1152 buf = usbd_alloc_buffer(xfer, xferlen);
1154 usbd_free_xfer(xfer);
1158 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1159 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1161 error = usbd_sync_transfer(xfer);
1163 usbd_free_xfer(xfer);
1167 desc = (struct ural_tx_desc *)buf;
1169 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1170 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1171 m0->m_pkthdr.len, rate);
1173 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1174 m0->m_pkthdr.len, rate, xferlen));
1176 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1177 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1179 error = usbd_sync_transfer(xfer);
1180 usbd_free_xfer(xfer);
1186 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1188 struct ieee80211com *ic = &sc->sc_ic;
1189 struct ifnet *ifp = &ic->ic_if;
1190 struct ural_tx_desc *desc;
1191 struct ural_tx_data *data;
1192 struct ieee80211_frame *wh;
1198 data = &sc->tx_data[0];
1199 desc = (struct ural_tx_desc *)data->buf;
1201 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1206 wh = mtod(m0, struct ieee80211_frame *);
1208 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1209 flags |= RAL_TX_ACK;
1211 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1213 *(uint16_t *)wh->i_dur = htole16(dur);
1215 /* tell hardware to add timestamp for probe responses */
1216 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1217 IEEE80211_FC0_TYPE_MGT &&
1218 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1219 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1220 flags |= RAL_TX_TIMESTAMP;
1223 if (sc->sc_drvbpf != NULL) {
1224 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1227 tap->wt_rate = rate;
1228 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1229 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1230 tap->wt_antenna = sc->tx_ant;
1232 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1235 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1236 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1238 /* align end on a 2-bytes boundary */
1239 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1242 * No space left in the last URB to store the extra 2 bytes, force
1243 * sending of another URB.
1245 if ((xferlen % 64) == 0)
1248 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1249 m0->m_pkthdr.len, rate, xferlen));
1251 lwkt_serialize_exit(ifp->if_serializer);
1253 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1254 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1257 error = usbd_transfer(data->xfer);
1258 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1267 lwkt_serialize_enter(ifp->if_serializer);
1272 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1274 struct ieee80211com *ic = &sc->sc_ic;
1275 struct ifnet *ifp = &ic->ic_if;
1276 struct ural_tx_desc *desc;
1277 struct ural_tx_data *data;
1278 struct ieee80211_frame *wh;
1279 struct ieee80211_key *k;
1283 int xferlen, rate, rate_idx;
1285 wh = mtod(m0, struct ieee80211_frame *);
1287 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1288 rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1290 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1291 k = ieee80211_crypto_encap(ic, ni, m0);
1297 /* packet header may have moved, reset our local pointer */
1298 wh = mtod(m0, struct ieee80211_frame *);
1301 data = &sc->tx_data[0];
1302 desc = (struct ural_tx_desc *)data->buf;
1307 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1308 flags |= RAL_TX_ACK;
1309 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
1311 dur = ieee80211_txtime(ni, RAL_ACK_SIZE,
1312 ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1314 *(uint16_t *)wh->i_dur = htole16(dur);
1317 if (sc->sc_drvbpf != NULL) {
1318 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1321 tap->wt_rate = rate;
1322 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1323 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1324 tap->wt_antenna = sc->tx_ant;
1326 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1329 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1330 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1332 /* align end on a 2-bytes boundary */
1333 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1336 * No space left in the last URB to store the extra 2 bytes, force
1337 * sending of another URB.
1339 if ((xferlen % 64) == 0)
1342 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1343 m0->m_pkthdr.len, rate, xferlen));
1345 lwkt_serialize_exit(ifp->if_serializer);
1347 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1348 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1351 error = usbd_transfer(data->xfer);
1352 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1361 lwkt_serialize_enter(ifp->if_serializer);
1366 ural_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1368 struct ural_softc *sc = ifp->if_softc;
1369 struct ieee80211com *ic = &sc->sc_ic;
1371 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1372 ASSERT_SERIALIZED(ifp->if_serializer);
1374 if (sc->sc_stopped) {
1375 ifq_purge(&ifp->if_snd);
1381 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1382 ifq_is_oactive(&ifp->if_snd)) {
1388 struct ieee80211_node *ni;
1391 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1392 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1393 ifq_set_oactive(&ifp->if_snd);
1396 IF_DEQUEUE(&ic->ic_mgtq, m0);
1398 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1399 m0->m_pkthdr.rcvif = NULL;
1401 if (ic->ic_rawbpf != NULL)
1402 bpf_mtap(ic->ic_rawbpf, m0);
1404 if (ural_tx_mgt(sc, m0, ni) != 0) {
1405 ieee80211_free_node(ni);
1409 struct ether_header *eh;
1411 if (ic->ic_state != IEEE80211_S_RUN) {
1412 ifq_purge(&ifp->if_snd);
1416 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1417 ifq_set_oactive(&ifp->if_snd);
1421 m0 = ifq_dequeue(&ifp->if_snd);
1425 if (m0->m_len < sizeof (struct ether_header)) {
1426 m0 = m_pullup(m0, sizeof (struct ether_header));
1428 IFNET_STAT_INC(ifp, oerrors, 1);
1433 eh = mtod(m0, struct ether_header *);
1434 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1441 m0 = ieee80211_encap(ic, m0, ni);
1443 ieee80211_free_node(ni);
1447 if (ic->ic_rawbpf != NULL)
1448 bpf_mtap(ic->ic_rawbpf, m0);
1450 if (ural_tx_data(sc, m0, ni) != 0) {
1451 ieee80211_free_node(ni);
1452 IFNET_STAT_INC(ifp, oerrors, 1);
1457 sc->sc_tx_timer = 5;
1465 ural_watchdog(struct ifnet *ifp)
1467 struct ural_softc *sc = ifp->if_softc;
1468 struct ieee80211com *ic = &sc->sc_ic;
1470 ASSERT_SERIALIZED(ifp->if_serializer);
1476 if (sc->sc_tx_timer > 0) {
1477 if (--sc->sc_tx_timer == 0) {
1478 device_printf(sc->sc_dev, "device timeout\n");
1479 /*ural_init(sc); XXX needs a process context! */
1480 IFNET_STAT_INC(ifp, oerrors, 1);
1487 ieee80211_watchdog(ic);
1493 * This function allows for fast channel switching in monitor mode (used by
1494 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1495 * generate a new beacon frame.
1498 ural_reset(struct ifnet *ifp)
1500 struct ural_softc *sc = ifp->if_softc;
1501 struct ieee80211com *ic = &sc->sc_ic;
1503 ASSERT_SERIALIZED(ifp->if_serializer);
1505 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1510 lwkt_serialize_exit(ifp->if_serializer);
1511 ural_set_chan(sc, ic->ic_curchan);
1512 lwkt_serialize_enter(ifp->if_serializer);
1520 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1522 struct ural_softc *sc = ifp->if_softc;
1523 struct ieee80211com *ic = &sc->sc_ic;
1526 ASSERT_SERIALIZED(ifp->if_serializer);
1532 if (ifp->if_flags & IFF_UP) {
1533 if (ifp->if_flags & IFF_RUNNING) {
1534 lwkt_serialize_exit(ifp->if_serializer);
1535 ural_update_promisc(sc);
1536 lwkt_serialize_enter(ifp->if_serializer);
1541 if (ifp->if_flags & IFF_RUNNING)
1547 error = ieee80211_ioctl(ic, cmd, data, cr);
1550 if (error == ENETRESET) {
1551 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1552 (IFF_UP | IFF_RUNNING) &&
1553 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1563 ural_set_testmode(struct ural_softc *sc)
1565 usb_device_request_t req;
1568 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1569 req.bRequest = RAL_VENDOR_REQUEST;
1570 USETW(req.wValue, 4);
1571 USETW(req.wIndex, 1);
1572 USETW(req.wLength, 0);
1574 error = usbd_do_request(sc->sc_udev, &req, NULL);
1576 kprintf("%s: could not set test mode: %s\n",
1577 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1582 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1584 usb_device_request_t req;
1587 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1588 req.bRequest = RAL_READ_EEPROM;
1589 USETW(req.wValue, 0);
1590 USETW(req.wIndex, addr);
1591 USETW(req.wLength, len);
1593 error = usbd_do_request(sc->sc_udev, &req, buf);
1595 kprintf("%s: could not read EEPROM: %s\n",
1596 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1601 ural_read(struct ural_softc *sc, uint16_t reg)
1603 usb_device_request_t req;
1607 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1608 req.bRequest = RAL_READ_MAC;
1609 USETW(req.wValue, 0);
1610 USETW(req.wIndex, reg);
1611 USETW(req.wLength, sizeof (uint16_t));
1613 error = usbd_do_request(sc->sc_udev, &req, &val);
1615 kprintf("%s: could not read MAC register: %s\n",
1616 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1620 return le16toh(val);
1624 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1626 usb_device_request_t req;
1629 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1630 req.bRequest = RAL_READ_MULTI_MAC;
1631 USETW(req.wValue, 0);
1632 USETW(req.wIndex, reg);
1633 USETW(req.wLength, len);
1635 error = usbd_do_request(sc->sc_udev, &req, buf);
1637 kprintf("%s: could not read MAC register: %s\n",
1638 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1643 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1645 usb_device_request_t req;
1648 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1649 req.bRequest = RAL_WRITE_MAC;
1650 USETW(req.wValue, val);
1651 USETW(req.wIndex, reg);
1652 USETW(req.wLength, 0);
1654 error = usbd_do_request(sc->sc_udev, &req, NULL);
1656 kprintf("%s: could not write MAC register: %s\n",
1657 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1662 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1664 usb_device_request_t req;
1667 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1668 req.bRequest = RAL_WRITE_MULTI_MAC;
1669 USETW(req.wValue, 0);
1670 USETW(req.wIndex, reg);
1671 USETW(req.wLength, len);
1673 error = usbd_do_request(sc->sc_udev, &req, buf);
1675 kprintf("%s: could not write MAC register: %s\n",
1676 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1681 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1686 for (ntries = 0; ntries < 5; ntries++) {
1687 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1691 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1695 tmp = reg << 8 | val;
1696 ural_write(sc, RAL_PHY_CSR7, tmp);
1700 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1705 val = RAL_BBP_WRITE | reg << 8;
1706 ural_write(sc, RAL_PHY_CSR7, val);
1708 for (ntries = 0; ntries < 5; ntries++) {
1709 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1713 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1717 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1721 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1726 for (ntries = 0; ntries < 5; ntries++) {
1727 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1731 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1735 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1736 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1737 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1739 /* remember last written value in sc */
1740 sc->rf_regs[reg] = val;
1742 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1746 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1748 struct ieee80211com *ic = &sc->sc_ic;
1752 chan = ieee80211_chan2ieee(ic, c);
1753 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1756 if (IEEE80211_IS_CHAN_2GHZ(c))
1757 power = min(sc->txpow[chan - 1], 31);
1761 /* adjust txpower using ifconfig settings */
1762 power -= (100 - ic->ic_txpowlimit) / 8;
1764 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1766 switch (sc->rf_rev) {
1768 ural_rf_write(sc, RAL_RF1, 0x00814);
1769 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1770 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1774 ural_rf_write(sc, RAL_RF1, 0x08804);
1775 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1776 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1777 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1781 ural_rf_write(sc, RAL_RF1, 0x0c808);
1782 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1783 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1784 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1788 ural_rf_write(sc, RAL_RF1, 0x08808);
1789 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1790 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1791 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1793 ural_rf_write(sc, RAL_RF1, 0x08808);
1794 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1795 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1796 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1800 ural_rf_write(sc, RAL_RF1, 0x08808);
1801 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1802 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1803 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1807 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1808 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1809 ural_rf_write(sc, RAL_RF1, 0x08804);
1811 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1812 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1813 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1818 for (i = 0; ural_rf5222[i].chan != chan; i++)
1821 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1822 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1823 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1824 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1828 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1829 ic->ic_state != IEEE80211_S_SCAN) {
1830 /* set Japan filter bit for channel 14 */
1831 tmp = ural_bbp_read(sc, 70);
1833 tmp &= ~RAL_JAPAN_FILTER;
1835 tmp |= RAL_JAPAN_FILTER;
1837 ural_bbp_write(sc, 70, tmp);
1839 /* clear CRC errors */
1840 ural_read(sc, RAL_STA_CSR0);
1843 ural_disable_rf_tune(sc);
1846 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1847 : IEEE80211_DUR_SIFS;
1851 * Disable RF auto-tuning.
1854 ural_disable_rf_tune(struct ural_softc *sc)
1858 if (sc->rf_rev != RAL_RF_2523) {
1859 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1860 ural_rf_write(sc, RAL_RF1, tmp);
1863 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1864 ural_rf_write(sc, RAL_RF3, tmp);
1866 DPRINTFN(2, ("disabling RF autotune\n"));
1870 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1874 ural_enable_tsf_sync(struct ural_softc *sc)
1876 struct ieee80211com *ic = &sc->sc_ic;
1877 uint16_t logcwmin, preload, tmp;
1879 /* first, disable TSF synchronization */
1880 ural_write(sc, RAL_TXRX_CSR19, 0);
1882 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1883 ural_write(sc, RAL_TXRX_CSR18, tmp);
1885 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1886 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1887 tmp = logcwmin << 12 | preload;
1888 ural_write(sc, RAL_TXRX_CSR20, tmp);
1890 /* finally, enable TSF synchronization */
1891 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1892 if (ic->ic_opmode == IEEE80211_M_STA)
1893 tmp |= RAL_ENABLE_TSF_SYNC(1);
1895 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1896 ural_write(sc, RAL_TXRX_CSR19, tmp);
1898 DPRINTF(("enabling TSF synchronization\n"));
1902 ural_update_slot(struct ifnet *ifp)
1904 struct ural_softc *sc = ifp->if_softc;
1905 struct ieee80211com *ic = &sc->sc_ic;
1906 uint16_t slottime, sifs, eifs;
1908 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1911 * These settings may sound a bit inconsistent but this is what the
1912 * reference driver does.
1914 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1915 sifs = 16 - RAL_RXTX_TURNAROUND;
1918 sifs = 10 - RAL_RXTX_TURNAROUND;
1922 ural_write(sc, RAL_MAC_CSR10, slottime);
1923 ural_write(sc, RAL_MAC_CSR11, sifs);
1924 ural_write(sc, RAL_MAC_CSR12, eifs);
1928 ural_set_txpreamble(struct ural_softc *sc)
1932 tmp = ural_read(sc, RAL_TXRX_CSR10);
1934 tmp &= ~RAL_SHORT_PREAMBLE;
1935 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1936 tmp |= RAL_SHORT_PREAMBLE;
1938 ural_write(sc, RAL_TXRX_CSR10, tmp);
1942 ural_set_basicrates(struct ural_softc *sc)
1944 struct ieee80211com *ic = &sc->sc_ic;
1946 /* update basic rate set */
1947 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1948 /* 11b basic rates: 1, 2Mbps */
1949 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1950 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1951 /* 11a basic rates: 6, 12, 24Mbps */
1952 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1954 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1955 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1960 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1963 struct ether_addr eaddr;
1964 char ethstr[ETHER_ADDRSTRLEN + 1];
1966 tmp = bssid[0] | bssid[1] << 8;
1967 ural_write(sc, RAL_MAC_CSR5, tmp);
1969 tmp = bssid[2] | bssid[3] << 8;
1970 ural_write(sc, RAL_MAC_CSR6, tmp);
1972 tmp = bssid[4] | bssid[5] << 8;
1973 ural_write(sc, RAL_MAC_CSR7, tmp);
1975 bcopy(bssid, &eaddr.octet, sizeof(eaddr.octet));
1976 DPRINTF(("setting BSSID to %s\n", kether_ntoa(&eaddr, ethstr)));
1980 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1983 struct ether_addr eaddr;
1984 char ethstr[ETHER_ADDRSTRLEN + 1];
1986 tmp = addr[0] | addr[1] << 8;
1987 ural_write(sc, RAL_MAC_CSR2, tmp);
1989 tmp = addr[2] | addr[3] << 8;
1990 ural_write(sc, RAL_MAC_CSR3, tmp);
1992 tmp = addr[4] | addr[5] << 8;
1993 ural_write(sc, RAL_MAC_CSR4, tmp);
1995 bcopy(bssid, &eaddr.octet, sizeof(eaddr.octet));
1996 DPRINTF(("setting MAC address to %s\n", kether_ntoa(&eaddr, ethstr)));
2000 ural_update_promisc(struct ural_softc *sc)
2002 struct ifnet *ifp = &sc->sc_ic.ic_if;
2005 tmp = ural_read(sc, RAL_TXRX_CSR2);
2007 tmp &= ~RAL_DROP_NOT_TO_ME;
2008 if (!(ifp->if_flags & IFF_PROMISC))
2009 tmp |= RAL_DROP_NOT_TO_ME;
2011 ural_write(sc, RAL_TXRX_CSR2, tmp);
2013 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2014 "entering" : "leaving"));
2018 ural_get_rf(int rev)
2021 case RAL_RF_2522: return "RT2522";
2022 case RAL_RF_2523: return "RT2523";
2023 case RAL_RF_2524: return "RT2524";
2024 case RAL_RF_2525: return "RT2525";
2025 case RAL_RF_2525E: return "RT2525e";
2026 case RAL_RF_2526: return "RT2526";
2027 case RAL_RF_5222: return "RT5222";
2028 default: return "unknown";
2033 ural_read_eeprom(struct ural_softc *sc)
2035 struct ieee80211com *ic = &sc->sc_ic;
2038 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
2040 sc->rf_rev = (val >> 11) & 0x7;
2041 sc->hw_radio = (val >> 10) & 0x1;
2042 sc->led_mode = (val >> 6) & 0x7;
2043 sc->rx_ant = (val >> 4) & 0x3;
2044 sc->tx_ant = (val >> 2) & 0x3;
2045 sc->nb_ant = val & 0x3;
2047 /* read MAC address */
2048 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2050 /* read default values for BBP registers */
2051 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2053 /* read Tx power for all b/g channels */
2054 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2058 ural_bbp_init(struct ural_softc *sc)
2060 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2063 /* wait for BBP to be ready */
2064 for (ntries = 0; ntries < 100; ntries++) {
2065 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2069 if (ntries == 100) {
2070 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2074 /* initialize BBP registers to default values */
2075 for (i = 0; i < N(ural_def_bbp); i++)
2076 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2079 /* initialize BBP registers to values stored in EEPROM */
2080 for (i = 0; i < 16; i++) {
2081 if (sc->bbp_prom[i].reg == 0xff)
2083 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2092 ural_set_txantenna(struct ural_softc *sc, int antenna)
2097 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2100 else if (antenna == 2)
2103 tx |= RAL_BBP_DIVERSITY;
2105 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2106 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2107 sc->rf_rev == RAL_RF_5222)
2108 tx |= RAL_BBP_FLIPIQ;
2110 ural_bbp_write(sc, RAL_BBP_TX, tx);
2112 /* update values in PHY_CSR5 and PHY_CSR6 */
2113 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2114 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2116 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2117 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2121 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2125 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2128 else if (antenna == 2)
2131 rx |= RAL_BBP_DIVERSITY;
2133 /* need to force no I/Q flip for RF 2525e and 2526 */
2134 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2135 rx &= ~RAL_BBP_FLIPIQ;
2137 ural_bbp_write(sc, RAL_BBP_RX, rx);
2141 ural_init(void *priv)
2143 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2144 struct ural_softc *sc = priv;
2145 struct ieee80211com *ic = &sc->sc_ic;
2146 struct ifnet *ifp = &ic->ic_if;
2147 struct ural_rx_data *data;
2149 usbd_status usb_err;
2150 int i, ntries, error;
2152 ASSERT_SERIALIZED(ifp->if_serializer);
2156 lwkt_serialize_exit(ifp->if_serializer);
2157 ural_set_testmode(sc);
2158 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
2159 lwkt_serialize_enter(ifp->if_serializer);
2164 lwkt_serialize_exit(ifp->if_serializer);
2166 /* initialize MAC registers to default values */
2167 for (i = 0; i < N(ural_def_mac); i++)
2168 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2170 /* wait for BBP and RF to wake up (this can take a long time!) */
2171 for (ntries = 0; ntries < 100; ntries++) {
2172 tmp = ural_read(sc, RAL_MAC_CSR17);
2173 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2174 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2178 if (ntries == 100) {
2179 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
2180 device_get_nameunit(sc->sc_dev));
2186 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2188 /* set basic rate set (will be updated later) */
2189 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2191 error = ural_bbp_init(sc);
2195 /* set default BSS channel */
2196 ural_set_chan(sc, ic->ic_curchan);
2198 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2199 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2201 ural_set_txantenna(sc, sc->tx_ant);
2202 ural_set_rxantenna(sc, sc->rx_ant);
2204 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2205 ural_set_macaddr(sc, ic->ic_myaddr);
2208 * Allocate xfer for AMRR statistics requests.
2210 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
2211 if (sc->stats_xfer == NULL) {
2212 kprintf("%s: could not allocate AMRR xfer\n",
2213 device_get_nameunit(sc->sc_dev));
2219 * Open Tx and Rx USB bulk pipes.
2221 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2224 kprintf("%s: could not open Tx pipe: %s\n",
2225 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2230 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2233 kprintf("%s: could not open Rx pipe: %s\n",
2234 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2240 * Allocate Tx and Rx xfer queues.
2242 error = ural_alloc_tx_list(sc);
2244 kprintf("%s: could not allocate Tx list\n",
2245 device_get_nameunit(sc->sc_dev));
2249 error = ural_alloc_rx_list(sc);
2251 kprintf("%s: could not allocate Rx list\n",
2252 device_get_nameunit(sc->sc_dev));
2257 * Start up the receive pipe.
2259 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2260 data = &sc->rx_data[i];
2262 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2263 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2264 usbd_transfer(data->xfer);
2268 tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2269 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2270 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2271 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2272 tmp |= RAL_DROP_TODS;
2273 if (!(ifp->if_flags & IFF_PROMISC))
2274 tmp |= RAL_DROP_NOT_TO_ME;
2276 ural_write(sc, RAL_TXRX_CSR2, tmp);
2278 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2279 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2281 lwkt_serialize_enter(ifp->if_serializer);
2285 ifq_clr_oactive(&ifp->if_snd);
2286 ifp->if_flags |= IFF_RUNNING;
2288 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2289 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2290 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2292 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2301 ural_stop(struct ural_softc *sc)
2303 struct ieee80211com *ic = &sc->sc_ic;
2304 struct ifnet *ifp = &ic->ic_if;
2306 ASSERT_SERIALIZED(ifp->if_serializer);
2310 ifp->if_flags &= ~IFF_RUNNING;
2311 ifq_clr_oactive(&ifp->if_snd);
2314 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2316 sc->sc_tx_timer = 0;
2319 lwkt_serialize_exit(ifp->if_serializer);
2322 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2324 /* reset ASIC and BBP (but won't reset MAC registers!) */
2325 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2326 ural_write(sc, RAL_MAC_CSR1, 0);
2328 if (sc->stats_xfer != NULL) {
2329 usbd_free_xfer(sc->stats_xfer);
2330 sc->stats_xfer = NULL;
2333 if (sc->sc_rx_pipeh != NULL) {
2334 usbd_abort_pipe(sc->sc_rx_pipeh);
2335 usbd_close_pipe(sc->sc_rx_pipeh);
2336 sc->sc_rx_pipeh = NULL;
2339 if (sc->sc_tx_pipeh != NULL) {
2340 usbd_abort_pipe(sc->sc_tx_pipeh);
2341 usbd_close_pipe(sc->sc_tx_pipeh);
2342 sc->sc_tx_pipeh = NULL;
2345 lwkt_serialize_enter(ifp->if_serializer);
2347 ural_free_rx_list(sc);
2348 ural_free_tx_list(sc);
2354 ural_stats_timeout(void *arg)
2356 struct ural_softc *sc = (struct ural_softc *)arg;
2357 usb_device_request_t req;
2365 * Asynchronously read statistic registers (cleared by read).
2367 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2368 req.bRequest = RAL_READ_MULTI_MAC;
2369 USETW(req.wValue, 0);
2370 USETW(req.wIndex, RAL_STA_CSR0);
2371 USETW(req.wLength, sizeof(sc->sta));
2373 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2374 USBD_DEFAULT_TIMEOUT, &req,
2375 sc->sta, sizeof(sc->sta), 0,
2377 usbd_transfer(sc->stats_xfer);
2383 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2386 struct ural_softc *sc = (struct ural_softc *)priv;
2387 struct ifnet *ifp = &sc->sc_ic.ic_if;
2388 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2390 if (status != USBD_NORMAL_COMPLETION) {
2391 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2392 "cancelling automatic rate control\n");
2398 /* count TX retry-fail as Tx errors */
2399 IFNET_STAT_INC(ifp, oerrors, sc->sta[RAL_TX_PKT_FAIL]);
2401 stats->stats_pkt_ok += sc->sta[RAL_TX_PKT_NO_RETRY] +
2402 sc->sta[RAL_TX_PKT_ONE_RETRY] +
2403 sc->sta[RAL_TX_PKT_MULTI_RETRY];
2405 stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2407 stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2409 stats->stats_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2412 * XXX Estimated average:
2413 * Actual number of retries for each packet should belong to
2414 * [2, sc->sc_tx_retries]
2416 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2417 ((2 + sc->sc_tx_retries) / 2);
2419 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2421 stats->stats_retries += sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2423 callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2429 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2430 struct ieee80211_ratectl_stats *stats)
2432 struct ifnet *ifp = &ic->ic_if;
2433 struct ural_softc *sc = ifp->if_softc;
2435 ASSERT_SERIALIZED(ifp->if_serializer);
2437 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2438 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2442 ural_ratectl_attach(struct ieee80211com *ic, u_int rc)
2444 struct ural_softc *sc = ic->ic_if.if_softc;
2447 case IEEE80211_RATECTL_ONOE:
2448 return &sc->sc_onoe_param;
2449 case IEEE80211_RATECTL_NONE:
2450 /* This could only happen during detaching */
2453 panic("unknown rate control algo %u", rc);