4 * Copyright (c) 2005, 2006
5 * Damien Bergamini <damien.bergamini@free.fr>
7 * Copyright (c) 2006, 2008
8 * Hans Petter Selasky <hselasky@FreeBSD.org>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
27 * Ralink Technology RT2500USB chipset driver
28 * http://www.ralinktech.com/
31 #include <sys/param.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
35 #include <sys/mutex.h>
37 #include <sys/kernel.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <sys/endian.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/if_ether.h>
64 #include <netinet/ip.h>
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_regdomain.h>
69 #include <net80211/ieee80211_radiotap.h>
70 #include <net80211/ieee80211_ratectl.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
76 #define USB_DEBUG_VAR ural_debug
77 #include <dev/usb/usb_debug.h>
79 #include <dev/usb/wlan/if_uralreg.h>
80 #include <dev/usb/wlan/if_uralvar.h>
83 static int ural_debug = 0;
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
86 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0,
90 #define URAL_RSSI(rssi) \
91 ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
92 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
94 /* various supported device vendors/products */
95 static const STRUCT_USB_HOST_ID ural_devs[] = {
96 #define URAL_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
97 URAL_DEV(ASUS, WL167G),
98 URAL_DEV(ASUS, RT2570),
99 URAL_DEV(BELKIN, F5D7050),
100 URAL_DEV(BELKIN, F5D7051),
101 URAL_DEV(CISCOLINKSYS, HU200TS),
102 URAL_DEV(CISCOLINKSYS, WUSB54G),
103 URAL_DEV(CISCOLINKSYS, WUSB54GP),
104 URAL_DEV(CONCEPTRONIC2, C54RU),
105 URAL_DEV(DLINK, DWLG122),
106 URAL_DEV(GIGABYTE, GN54G),
107 URAL_DEV(GIGABYTE, GNWBKG),
108 URAL_DEV(GUILLEMOT, HWGUSB254),
109 URAL_DEV(MELCO, KG54),
110 URAL_DEV(MELCO, KG54AI),
111 URAL_DEV(MELCO, KG54YB),
112 URAL_DEV(MELCO, NINWIFI),
113 URAL_DEV(MSI, RT2570),
114 URAL_DEV(MSI, RT2570_2),
115 URAL_DEV(MSI, RT2570_3),
116 URAL_DEV(NOVATECH, NV902),
117 URAL_DEV(RALINK, RT2570),
118 URAL_DEV(RALINK, RT2570_2),
119 URAL_DEV(RALINK, RT2570_3),
120 URAL_DEV(SIEMENS2, WL54G),
121 URAL_DEV(SMC, 2862WG),
122 URAL_DEV(SPHAIRON, UB801R),
123 URAL_DEV(SURECOM, RT2570),
124 URAL_DEV(VTECH, RT2570),
125 URAL_DEV(ZINWELL, RT2570),
129 static usb_callback_t ural_bulk_read_callback;
130 static usb_callback_t ural_bulk_write_callback;
132 static usb_error_t ural_do_request(struct ural_softc *sc,
133 struct usb_device_request *req, void *data);
134 static struct ieee80211vap *ural_vap_create(struct ieee80211com *,
135 const char [IFNAMSIZ], int, enum ieee80211_opmode,
136 int, const uint8_t [IEEE80211_ADDR_LEN],
137 const uint8_t [IEEE80211_ADDR_LEN]);
138 static void ural_vap_delete(struct ieee80211vap *);
139 static void ural_tx_free(struct ural_tx_data *, int);
140 static void ural_setup_tx_list(struct ural_softc *);
141 static void ural_unsetup_tx_list(struct ural_softc *);
142 static int ural_newstate(struct ieee80211vap *,
143 enum ieee80211_state, int);
144 static void ural_setup_tx_desc(struct ural_softc *,
145 struct ural_tx_desc *, uint32_t, int, int);
146 static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
147 struct ieee80211_node *);
148 static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
149 struct ieee80211_node *);
150 static int ural_tx_data(struct ural_softc *, struct mbuf *,
151 struct ieee80211_node *);
152 static void ural_start(struct ifnet *);
153 static int ural_ioctl(struct ifnet *, u_long, caddr_t);
154 static void ural_set_testmode(struct ural_softc *);
155 static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
157 static uint16_t ural_read(struct ural_softc *, uint16_t);
158 static void ural_read_multi(struct ural_softc *, uint16_t, void *,
160 static void ural_write(struct ural_softc *, uint16_t, uint16_t);
161 static void ural_write_multi(struct ural_softc *, uint16_t, void *,
163 static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
164 static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
165 static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
166 static void ural_scan_start(struct ieee80211com *);
167 static void ural_scan_end(struct ieee80211com *);
168 static void ural_set_channel(struct ieee80211com *);
169 static void ural_set_chan(struct ural_softc *,
170 struct ieee80211_channel *);
171 static void ural_disable_rf_tune(struct ural_softc *);
172 static void ural_enable_tsf_sync(struct ural_softc *);
173 static void ural_enable_tsf(struct ural_softc *);
174 static void ural_update_slot(struct ifnet *);
175 static void ural_set_txpreamble(struct ural_softc *);
176 static void ural_set_basicrates(struct ural_softc *,
177 const struct ieee80211_channel *);
178 static void ural_set_bssid(struct ural_softc *, const uint8_t *);
179 static void ural_set_macaddr(struct ural_softc *, uint8_t *);
180 static void ural_update_promisc(struct ifnet *);
181 static void ural_setpromisc(struct ural_softc *);
182 static const char *ural_get_rf(int);
183 static void ural_read_eeprom(struct ural_softc *);
184 static int ural_bbp_init(struct ural_softc *);
185 static void ural_set_txantenna(struct ural_softc *, int);
186 static void ural_set_rxantenna(struct ural_softc *, int);
187 static void ural_init_locked(struct ural_softc *);
188 static void ural_init(void *);
189 static void ural_stop(struct ural_softc *);
190 static int ural_raw_xmit(struct ieee80211_node *, struct mbuf *,
191 const struct ieee80211_bpf_params *);
192 static void ural_ratectl_start(struct ural_softc *,
193 struct ieee80211_node *);
194 static void ural_ratectl_timeout(void *);
195 static void ural_ratectl_task(void *, int);
196 static int ural_pause(struct ural_softc *sc, int timeout);
199 * Default values for MAC registers; values taken from the reference driver.
201 static const struct {
205 { RAL_TXRX_CSR5, 0x8c8d },
206 { RAL_TXRX_CSR6, 0x8b8a },
207 { RAL_TXRX_CSR7, 0x8687 },
208 { RAL_TXRX_CSR8, 0x0085 },
209 { RAL_MAC_CSR13, 0x1111 },
210 { RAL_MAC_CSR14, 0x1e11 },
211 { RAL_TXRX_CSR21, 0xe78f },
212 { RAL_MAC_CSR9, 0xff1d },
213 { RAL_MAC_CSR11, 0x0002 },
214 { RAL_MAC_CSR22, 0x0053 },
215 { RAL_MAC_CSR15, 0x0000 },
216 { RAL_MAC_CSR8, RAL_FRAME_SIZE },
217 { RAL_TXRX_CSR19, 0x0000 },
218 { RAL_TXRX_CSR18, 0x005a },
219 { RAL_PHY_CSR2, 0x0000 },
220 { RAL_TXRX_CSR0, 0x1ec0 },
221 { RAL_PHY_CSR4, 0x000f }
225 * Default values for BBP registers; values taken from the reference driver.
227 static const struct {
266 * Default values for RF register R2 indexed by channel numbers.
268 static const uint32_t ural_rf2522_r2[] = {
269 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
270 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
273 static const uint32_t ural_rf2523_r2[] = {
274 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
275 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
278 static const uint32_t ural_rf2524_r2[] = {
279 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
280 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
283 static const uint32_t ural_rf2525_r2[] = {
284 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
285 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
288 static const uint32_t ural_rf2525_hi_r2[] = {
289 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
290 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
293 static const uint32_t ural_rf2525e_r2[] = {
294 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
295 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
298 static const uint32_t ural_rf2526_hi_r2[] = {
299 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
300 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
303 static const uint32_t ural_rf2526_r2[] = {
304 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
305 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
309 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
310 * values taken from the reference driver.
312 static const struct {
318 { 1, 0x08808, 0x0044d, 0x00282 },
319 { 2, 0x08808, 0x0044e, 0x00282 },
320 { 3, 0x08808, 0x0044f, 0x00282 },
321 { 4, 0x08808, 0x00460, 0x00282 },
322 { 5, 0x08808, 0x00461, 0x00282 },
323 { 6, 0x08808, 0x00462, 0x00282 },
324 { 7, 0x08808, 0x00463, 0x00282 },
325 { 8, 0x08808, 0x00464, 0x00282 },
326 { 9, 0x08808, 0x00465, 0x00282 },
327 { 10, 0x08808, 0x00466, 0x00282 },
328 { 11, 0x08808, 0x00467, 0x00282 },
329 { 12, 0x08808, 0x00468, 0x00282 },
330 { 13, 0x08808, 0x00469, 0x00282 },
331 { 14, 0x08808, 0x0046b, 0x00286 },
333 { 36, 0x08804, 0x06225, 0x00287 },
334 { 40, 0x08804, 0x06226, 0x00287 },
335 { 44, 0x08804, 0x06227, 0x00287 },
336 { 48, 0x08804, 0x06228, 0x00287 },
337 { 52, 0x08804, 0x06229, 0x00287 },
338 { 56, 0x08804, 0x0622a, 0x00287 },
339 { 60, 0x08804, 0x0622b, 0x00287 },
340 { 64, 0x08804, 0x0622c, 0x00287 },
342 { 100, 0x08804, 0x02200, 0x00283 },
343 { 104, 0x08804, 0x02201, 0x00283 },
344 { 108, 0x08804, 0x02202, 0x00283 },
345 { 112, 0x08804, 0x02203, 0x00283 },
346 { 116, 0x08804, 0x02204, 0x00283 },
347 { 120, 0x08804, 0x02205, 0x00283 },
348 { 124, 0x08804, 0x02206, 0x00283 },
349 { 128, 0x08804, 0x02207, 0x00283 },
350 { 132, 0x08804, 0x02208, 0x00283 },
351 { 136, 0x08804, 0x02209, 0x00283 },
352 { 140, 0x08804, 0x0220a, 0x00283 },
354 { 149, 0x08808, 0x02429, 0x00281 },
355 { 153, 0x08808, 0x0242b, 0x00281 },
356 { 157, 0x08808, 0x0242d, 0x00281 },
357 { 161, 0x08808, 0x0242f, 0x00281 }
360 static const struct usb_config ural_config[URAL_N_TRANSFER] = {
363 .endpoint = UE_ADDR_ANY,
364 .direction = UE_DIR_OUT,
365 .bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4),
366 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
367 .callback = ural_bulk_write_callback,
368 .timeout = 5000, /* ms */
372 .endpoint = UE_ADDR_ANY,
373 .direction = UE_DIR_IN,
374 .bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE),
375 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
376 .callback = ural_bulk_read_callback,
380 static device_probe_t ural_match;
381 static device_attach_t ural_attach;
382 static device_detach_t ural_detach;
384 static device_method_t ural_methods[] = {
385 /* Device interface */
386 DEVMETHOD(device_probe, ural_match),
387 DEVMETHOD(device_attach, ural_attach),
388 DEVMETHOD(device_detach, ural_detach),
392 static driver_t ural_driver = {
394 .methods = ural_methods,
395 .size = sizeof(struct ural_softc),
398 static devclass_t ural_devclass;
400 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, NULL, 0);
401 MODULE_DEPEND(ural, usb, 1, 1, 1);
402 MODULE_DEPEND(ural, wlan, 1, 1, 1);
403 MODULE_VERSION(ural, 1);
406 ural_match(device_t self)
408 struct usb_attach_arg *uaa = device_get_ivars(self);
410 if (uaa->usb_mode != USB_MODE_HOST)
412 if (uaa->info.bConfigIndex != 0)
414 if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX)
417 return (usbd_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa));
421 ural_attach(device_t self)
423 struct usb_attach_arg *uaa = device_get_ivars(self);
424 struct ural_softc *sc = device_get_softc(self);
426 struct ieee80211com *ic;
427 uint8_t iface_index, bands;
430 device_set_usb_desc(self);
431 sc->sc_udev = uaa->device;
434 mtx_init(&sc->sc_mtx, device_get_nameunit(self),
435 MTX_NETWORK_LOCK, MTX_DEF);
437 iface_index = RAL_IFACE_INDEX;
438 error = usbd_transfer_setup(uaa->device,
439 &iface_index, sc->sc_xfer, ural_config,
440 URAL_N_TRANSFER, sc, &sc->sc_mtx);
442 device_printf(self, "could not allocate USB transfers, "
443 "err=%s\n", usbd_errstr(error));
448 /* retrieve RT2570 rev. no */
449 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
451 /* retrieve MAC address and various other things from EEPROM */
452 ural_read_eeprom(sc);
455 device_printf(self, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
456 sc->asic_rev, ural_get_rf(sc->rf_rev));
458 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
460 device_printf(sc->sc_dev, "can not if_alloc()\n");
466 if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
467 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
468 ifp->if_init = ural_init;
469 ifp->if_ioctl = ural_ioctl;
470 ifp->if_start = ural_start;
471 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
472 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
473 IFQ_SET_READY(&ifp->if_snd);
476 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
478 /* set device capabilities */
480 IEEE80211_C_STA /* station mode supported */
481 | IEEE80211_C_IBSS /* IBSS mode supported */
482 | IEEE80211_C_MONITOR /* monitor mode supported */
483 | IEEE80211_C_HOSTAP /* HostAp mode supported */
484 | IEEE80211_C_TXPMGT /* tx power management */
485 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
486 | IEEE80211_C_SHSLOT /* short slot time supported */
487 | IEEE80211_C_BGSCAN /* bg scanning supported */
488 | IEEE80211_C_WPA /* 802.11i */
492 setbit(&bands, IEEE80211_MODE_11B);
493 setbit(&bands, IEEE80211_MODE_11G);
494 if (sc->rf_rev == RAL_RF_5222)
495 setbit(&bands, IEEE80211_MODE_11A);
496 ieee80211_init_channels(ic, NULL, &bands);
498 ieee80211_ifattach(ic, sc->sc_bssid);
499 ic->ic_update_promisc = ural_update_promisc;
500 ic->ic_raw_xmit = ural_raw_xmit;
501 ic->ic_scan_start = ural_scan_start;
502 ic->ic_scan_end = ural_scan_end;
503 ic->ic_set_channel = ural_set_channel;
505 ic->ic_vap_create = ural_vap_create;
506 ic->ic_vap_delete = ural_vap_delete;
508 ieee80211_radiotap_attach(ic,
509 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
510 RAL_TX_RADIOTAP_PRESENT,
511 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
512 RAL_RX_RADIOTAP_PRESENT);
515 ieee80211_announce(ic);
521 return (ENXIO); /* failure */
525 ural_detach(device_t self)
527 struct ural_softc *sc = device_get_softc(self);
528 struct ifnet *ifp = sc->sc_ifp;
529 struct ieee80211com *ic;
531 /* prevent further ioctls */
536 /* stop all USB transfers */
537 usbd_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER);
539 /* free TX list, if any */
541 ural_unsetup_tx_list(sc);
546 ieee80211_ifdetach(ic);
549 mtx_destroy(&sc->sc_mtx);
555 ural_do_request(struct ural_softc *sc,
556 struct usb_device_request *req, void *data)
562 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
563 req, data, 0, NULL, 250 /* ms */);
567 DPRINTFN(1, "Control request failed, %s (retrying)\n",
569 if (ural_pause(sc, hz / 100))
575 static struct ieee80211vap *
576 ural_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
577 enum ieee80211_opmode opmode, int flags,
578 const uint8_t bssid[IEEE80211_ADDR_LEN],
579 const uint8_t mac[IEEE80211_ADDR_LEN])
581 struct ural_softc *sc = ic->ic_ifp->if_softc;
582 struct ural_vap *uvp;
583 struct ieee80211vap *vap;
585 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
587 uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap),
588 M_80211_VAP, M_NOWAIT | M_ZERO);
592 /* enable s/w bmiss handling for sta mode */
594 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
595 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
597 free(uvp, M_80211_VAP);
601 /* override state transition machine */
602 uvp->newstate = vap->iv_newstate;
603 vap->iv_newstate = ural_newstate;
605 usb_callout_init_mtx(&uvp->ratectl_ch, &sc->sc_mtx, 0);
606 TASK_INIT(&uvp->ratectl_task, 0, ural_ratectl_task, uvp);
607 ieee80211_ratectl_init(vap);
608 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
611 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
612 ic->ic_opmode = opmode;
617 ural_vap_delete(struct ieee80211vap *vap)
619 struct ural_vap *uvp = URAL_VAP(vap);
620 struct ieee80211com *ic = vap->iv_ic;
622 usb_callout_drain(&uvp->ratectl_ch);
623 ieee80211_draintask(ic, &uvp->ratectl_task);
624 ieee80211_ratectl_deinit(vap);
625 ieee80211_vap_detach(vap);
626 free(uvp, M_80211_VAP);
630 ural_tx_free(struct ural_tx_data *data, int txerr)
632 struct ural_softc *sc = data->sc;
634 if (data->m != NULL) {
635 if (data->m->m_flags & M_TXCB)
636 ieee80211_process_callback(data->ni, data->m,
637 txerr ? ETIMEDOUT : 0);
641 ieee80211_free_node(data->ni);
644 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
649 ural_setup_tx_list(struct ural_softc *sc)
651 struct ural_tx_data *data;
655 STAILQ_INIT(&sc->tx_q);
656 STAILQ_INIT(&sc->tx_free);
658 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
659 data = &sc->tx_data[i];
662 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
668 ural_unsetup_tx_list(struct ural_softc *sc)
670 struct ural_tx_data *data;
673 /* make sure any subsequent use of the queues will fail */
675 STAILQ_INIT(&sc->tx_q);
676 STAILQ_INIT(&sc->tx_free);
678 /* free up all node references and mbufs */
679 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
680 data = &sc->tx_data[i];
682 if (data->m != NULL) {
686 if (data->ni != NULL) {
687 ieee80211_free_node(data->ni);
694 ural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
696 struct ural_vap *uvp = URAL_VAP(vap);
697 struct ieee80211com *ic = vap->iv_ic;
698 struct ural_softc *sc = ic->ic_ifp->if_softc;
699 const struct ieee80211_txparam *tp;
700 struct ieee80211_node *ni;
703 DPRINTF("%s -> %s\n",
704 ieee80211_state_name[vap->iv_state],
705 ieee80211_state_name[nstate]);
707 IEEE80211_UNLOCK(ic);
709 usb_callout_stop(&uvp->ratectl_ch);
712 case IEEE80211_S_INIT:
713 if (vap->iv_state == IEEE80211_S_RUN) {
714 /* abort TSF synchronization */
715 ural_write(sc, RAL_TXRX_CSR19, 0);
717 /* force tx led to stop blinking */
718 ural_write(sc, RAL_MAC_CSR20, 0);
722 case IEEE80211_S_RUN:
723 ni = ieee80211_ref_node(vap->iv_bss);
725 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
726 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
729 ieee80211_free_node(ni);
732 ural_update_slot(ic->ic_ifp);
733 ural_set_txpreamble(sc);
734 ural_set_basicrates(sc, ic->ic_bsschan);
735 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
736 ural_set_bssid(sc, sc->sc_bssid);
739 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
740 vap->iv_opmode == IEEE80211_M_IBSS) {
741 m = ieee80211_beacon_alloc(ni, &uvp->bo);
743 device_printf(sc->sc_dev,
744 "could not allocate beacon\n");
747 ieee80211_free_node(ni);
750 ieee80211_ref_node(ni);
751 if (ural_tx_bcn(sc, m, ni) != 0) {
752 device_printf(sc->sc_dev,
753 "could not send beacon\n");
756 ieee80211_free_node(ni);
761 /* make tx led blink on tx (controlled by ASIC) */
762 ural_write(sc, RAL_MAC_CSR20, 1);
764 if (vap->iv_opmode != IEEE80211_M_MONITOR)
765 ural_enable_tsf_sync(sc);
769 /* enable automatic rate adaptation */
770 /* XXX should use ic_bsschan but not valid until after newstate call below */
771 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
772 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
773 ural_ratectl_start(sc, ni);
774 ieee80211_free_node(ni);
782 return (uvp->newstate(vap, nstate, arg));
787 ural_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
789 struct ural_softc *sc = usbd_xfer_softc(xfer);
790 struct ifnet *ifp = sc->sc_ifp;
791 struct ieee80211vap *vap;
792 struct ural_tx_data *data;
794 struct usb_page_cache *pc;
797 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
799 switch (USB_GET_STATE(xfer)) {
800 case USB_ST_TRANSFERRED:
801 DPRINTFN(11, "transfer complete, %d bytes\n", len);
804 data = usbd_xfer_get_priv(xfer);
805 ural_tx_free(data, 0);
806 usbd_xfer_set_priv(xfer, NULL);
809 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
814 data = STAILQ_FIRST(&sc->tx_q);
816 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
819 if (m->m_pkthdr.len > (int)(RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) {
820 DPRINTFN(0, "data overflow, %u bytes\n",
822 m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE);
824 pc = usbd_xfer_get_frame(xfer, 0);
825 usbd_copy_in(pc, 0, &data->desc, RAL_TX_DESC_SIZE);
826 usbd_m_copy_in(pc, RAL_TX_DESC_SIZE, m, 0,
829 vap = data->ni->ni_vap;
830 if (ieee80211_radiotap_active_vap(vap)) {
831 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
834 tap->wt_rate = data->rate;
835 tap->wt_antenna = sc->tx_ant;
837 ieee80211_radiotap_tx(vap, m);
840 /* xfer length needs to be a multiple of two! */
841 len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1;
845 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
846 m->m_pkthdr.len, len);
848 usbd_xfer_set_frame_len(xfer, 0, len);
849 usbd_xfer_set_priv(xfer, data);
851 usbd_transfer_submit(xfer);
859 DPRINTFN(11, "transfer error, %s\n",
863 data = usbd_xfer_get_priv(xfer);
865 ural_tx_free(data, error);
866 usbd_xfer_set_priv(xfer, NULL);
869 if (error == USB_ERR_STALLED) {
870 /* try to clear stall first */
871 usbd_xfer_set_stall(xfer);
874 if (error == USB_ERR_TIMEOUT)
875 device_printf(sc->sc_dev, "device timeout\n");
881 ural_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
883 struct ural_softc *sc = usbd_xfer_softc(xfer);
884 struct ifnet *ifp = sc->sc_ifp;
885 struct ieee80211com *ic = ifp->if_l2com;
886 struct ieee80211_node *ni;
887 struct mbuf *m = NULL;
888 struct usb_page_cache *pc;
890 int8_t rssi = 0, nf = 0;
893 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
895 switch (USB_GET_STATE(xfer)) {
896 case USB_ST_TRANSFERRED:
898 DPRINTFN(15, "rx done, actlen=%d\n", len);
900 if (len < (int)(RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
901 DPRINTF("%s: xfer too short %d\n",
902 device_get_nameunit(sc->sc_dev), len);
907 len -= RAL_RX_DESC_SIZE;
908 /* rx descriptor is located at the end */
909 pc = usbd_xfer_get_frame(xfer, 0);
910 usbd_copy_out(pc, len, &sc->sc_rx_desc, RAL_RX_DESC_SIZE);
912 rssi = URAL_RSSI(sc->sc_rx_desc.rssi);
913 nf = RAL_NOISE_FLOOR;
914 flags = le32toh(sc->sc_rx_desc.flags);
915 if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
917 * This should not happen since we did not
918 * request to receive those frames when we
919 * filled RAL_TXRX_CSR2:
921 DPRINTFN(5, "PHY or CRC error\n");
926 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
928 DPRINTF("could not allocate mbuf\n");
932 usbd_copy_out(pc, 0, mtod(m, uint8_t *), len);
935 m->m_pkthdr.rcvif = ifp;
936 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
938 if (ieee80211_radiotap_active(ic)) {
939 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
943 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
944 (flags & RAL_RX_OFDM) ?
945 IEEE80211_T_OFDM : IEEE80211_T_CCK);
946 tap->wr_antenna = sc->rx_ant;
947 tap->wr_antsignal = nf + rssi;
948 tap->wr_antnoise = nf;
950 /* Strip trailing 802.11 MAC FCS. */
951 m_adj(m, -IEEE80211_CRC_LEN);
956 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
957 usbd_transfer_submit(xfer);
960 * At the end of a USB callback it is always safe to unlock
961 * the private mutex of a device! That is why we do the
962 * "ieee80211_input" here, and not some lines up!
966 ni = ieee80211_find_rxnode(ic,
967 mtod(m, struct ieee80211_frame_min *));
969 (void) ieee80211_input(ni, m, rssi, nf);
970 ieee80211_free_node(ni);
972 (void) ieee80211_input_all(ic, m, rssi, nf);
974 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
975 !IFQ_IS_EMPTY(&ifp->if_snd))
981 if (error != USB_ERR_CANCELLED) {
982 /* try to clear stall first */
983 usbd_xfer_set_stall(xfer);
991 ural_plcp_signal(int rate)
994 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1000 case 72: return 0xd;
1001 case 96: return 0x8;
1002 case 108: return 0xc;
1004 /* CCK rates (NB: not IEEE std, device-specific) */
1007 case 11: return 0x2;
1008 case 22: return 0x3;
1010 return 0xff; /* XXX unsupported/unknown rate */
1014 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1015 uint32_t flags, int len, int rate)
1017 struct ifnet *ifp = sc->sc_ifp;
1018 struct ieee80211com *ic = ifp->if_l2com;
1019 uint16_t plcp_length;
1022 desc->flags = htole32(flags);
1023 desc->flags |= htole32(RAL_TX_NEWSEQ);
1024 desc->flags |= htole32(len << 16);
1026 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1027 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1029 /* setup PLCP fields */
1030 desc->plcp_signal = ural_plcp_signal(rate);
1031 desc->plcp_service = 4;
1033 len += IEEE80211_CRC_LEN;
1034 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1035 desc->flags |= htole32(RAL_TX_OFDM);
1037 plcp_length = len & 0xfff;
1038 desc->plcp_length_hi = plcp_length >> 6;
1039 desc->plcp_length_lo = plcp_length & 0x3f;
1042 rate = 2; /* avoid division by zero */
1043 plcp_length = (16 * len + rate - 1) / rate;
1045 remainder = (16 * len) % 22;
1046 if (remainder != 0 && remainder < 7)
1047 desc->plcp_service |= RAL_PLCP_LENGEXT;
1049 desc->plcp_length_hi = plcp_length >> 8;
1050 desc->plcp_length_lo = plcp_length & 0xff;
1052 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1053 desc->plcp_signal |= 0x08;
1060 #define RAL_TX_TIMEOUT 5000
1063 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1065 struct ieee80211vap *vap = ni->ni_vap;
1066 struct ieee80211com *ic = ni->ni_ic;
1067 struct ifnet *ifp = sc->sc_ifp;
1068 const struct ieee80211_txparam *tp;
1069 struct ural_tx_data *data;
1071 if (sc->tx_nfree == 0) {
1072 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1074 ieee80211_free_node(ni);
1077 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1079 ieee80211_free_node(ni);
1082 data = STAILQ_FIRST(&sc->tx_free);
1083 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1085 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1089 data->rate = tp->mgmtrate;
1091 ural_setup_tx_desc(sc, &data->desc,
1092 RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len,
1095 DPRINTFN(10, "sending beacon frame len=%u rate=%u\n",
1096 m0->m_pkthdr.len, tp->mgmtrate);
1098 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1099 usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1105 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1107 struct ieee80211vap *vap = ni->ni_vap;
1108 struct ieee80211com *ic = ni->ni_ic;
1109 const struct ieee80211_txparam *tp;
1110 struct ural_tx_data *data;
1111 struct ieee80211_frame *wh;
1112 struct ieee80211_key *k;
1116 RAL_LOCK_ASSERT(sc, MA_OWNED);
1118 data = STAILQ_FIRST(&sc->tx_free);
1119 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1122 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1124 wh = mtod(m0, struct ieee80211_frame *);
1125 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1126 k = ieee80211_crypto_encap(ni, m0);
1131 wh = mtod(m0, struct ieee80211_frame *);
1136 data->rate = tp->mgmtrate;
1139 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1140 flags |= RAL_TX_ACK;
1142 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1143 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1144 USETW(wh->i_dur, dur);
1146 /* tell hardware to add timestamp for probe responses */
1147 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1148 IEEE80211_FC0_TYPE_MGT &&
1149 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1150 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1151 flags |= RAL_TX_TIMESTAMP;
1154 ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate);
1156 DPRINTFN(10, "sending mgt frame len=%u rate=%u\n",
1157 m0->m_pkthdr.len, tp->mgmtrate);
1159 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1160 usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1166 ural_sendprot(struct ural_softc *sc,
1167 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1169 struct ieee80211com *ic = ni->ni_ic;
1170 const struct ieee80211_frame *wh;
1171 struct ural_tx_data *data;
1173 int protrate, ackrate, pktlen, flags, isshort;
1176 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1177 ("protection %d", prot));
1179 wh = mtod(m, const struct ieee80211_frame *);
1180 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1182 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1183 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1185 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1186 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1187 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1188 flags = RAL_TX_RETRY(7);
1189 if (prot == IEEE80211_PROT_RTSCTS) {
1190 /* NB: CTS is the same size as an ACK */
1191 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1192 flags |= RAL_TX_ACK;
1193 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1195 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1197 if (mprot == NULL) {
1198 /* XXX stat + msg */
1201 data = STAILQ_FIRST(&sc->tx_free);
1202 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1206 data->ni = ieee80211_ref_node(ni);
1207 data->rate = protrate;
1208 ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate);
1210 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1211 usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1217 ural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1218 const struct ieee80211_bpf_params *params)
1220 struct ieee80211com *ic = ni->ni_ic;
1221 struct ural_tx_data *data;
1226 RAL_LOCK_ASSERT(sc, MA_OWNED);
1227 KASSERT(params != NULL, ("no raw xmit params"));
1229 rate = params->ibp_rate0;
1230 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1235 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1236 flags |= RAL_TX_ACK;
1237 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1238 error = ural_sendprot(sc, m0, ni,
1239 params->ibp_flags & IEEE80211_BPF_RTS ?
1240 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1242 if (error || sc->tx_nfree == 0) {
1246 flags |= RAL_TX_IFS_SIFS;
1249 data = STAILQ_FIRST(&sc->tx_free);
1250 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1257 /* XXX need to setup descriptor ourself */
1258 ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1260 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1261 m0->m_pkthdr.len, rate);
1263 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1264 usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1270 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1272 struct ieee80211vap *vap = ni->ni_vap;
1273 struct ieee80211com *ic = ni->ni_ic;
1274 struct ural_tx_data *data;
1275 struct ieee80211_frame *wh;
1276 const struct ieee80211_txparam *tp;
1277 struct ieee80211_key *k;
1282 RAL_LOCK_ASSERT(sc, MA_OWNED);
1284 wh = mtod(m0, struct ieee80211_frame *);
1286 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1287 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1288 rate = tp->mcastrate;
1289 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1290 rate = tp->ucastrate;
1292 rate = ni->ni_txrate;
1294 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1295 k = ieee80211_crypto_encap(ni, m0);
1300 /* packet header may have moved, reset our local pointer */
1301 wh = mtod(m0, struct ieee80211_frame *);
1304 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1305 int prot = IEEE80211_PROT_NONE;
1306 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1307 prot = IEEE80211_PROT_RTSCTS;
1308 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1309 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1310 prot = ic->ic_protmode;
1311 if (prot != IEEE80211_PROT_NONE) {
1312 error = ural_sendprot(sc, m0, ni, prot, rate);
1313 if (error || sc->tx_nfree == 0) {
1317 flags |= RAL_TX_IFS_SIFS;
1321 data = STAILQ_FIRST(&sc->tx_free);
1322 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1329 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1330 flags |= RAL_TX_ACK;
1331 flags |= RAL_TX_RETRY(7);
1333 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1334 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1335 USETW(wh->i_dur, dur);
1338 ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate);
1340 DPRINTFN(10, "sending data frame len=%u rate=%u\n",
1341 m0->m_pkthdr.len, rate);
1343 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1344 usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]);
1350 ural_start(struct ifnet *ifp)
1352 struct ural_softc *sc = ifp->if_softc;
1353 struct ieee80211_node *ni;
1357 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1362 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1365 if (sc->tx_nfree < RAL_TX_MINFREE) {
1366 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1367 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1370 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1371 if (ural_tx_data(sc, m, ni) != 0) {
1372 ieee80211_free_node(ni);
1381 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1383 struct ural_softc *sc = ifp->if_softc;
1384 struct ieee80211com *ic = ifp->if_l2com;
1385 struct ifreq *ifr = (struct ifreq *) data;
1390 error = sc->sc_detached ? ENXIO : 0;
1398 if (ifp->if_flags & IFF_UP) {
1399 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1400 ural_init_locked(sc);
1403 ural_setpromisc(sc);
1405 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1410 ieee80211_start_all(ic);
1414 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1417 error = ether_ioctl(ifp, cmd, data);
1424 ural_set_testmode(struct ural_softc *sc)
1426 struct usb_device_request req;
1429 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1430 req.bRequest = RAL_VENDOR_REQUEST;
1431 USETW(req.wValue, 4);
1432 USETW(req.wIndex, 1);
1433 USETW(req.wLength, 0);
1435 error = ural_do_request(sc, &req, NULL);
1437 device_printf(sc->sc_dev, "could not set test mode: %s\n",
1438 usbd_errstr(error));
1443 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1445 struct usb_device_request req;
1448 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1449 req.bRequest = RAL_READ_EEPROM;
1450 USETW(req.wValue, 0);
1451 USETW(req.wIndex, addr);
1452 USETW(req.wLength, len);
1454 error = ural_do_request(sc, &req, buf);
1456 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1457 usbd_errstr(error));
1462 ural_read(struct ural_softc *sc, uint16_t reg)
1464 struct usb_device_request req;
1468 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1469 req.bRequest = RAL_READ_MAC;
1470 USETW(req.wValue, 0);
1471 USETW(req.wIndex, reg);
1472 USETW(req.wLength, sizeof (uint16_t));
1474 error = ural_do_request(sc, &req, &val);
1476 device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1477 usbd_errstr(error));
1481 return le16toh(val);
1485 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1487 struct usb_device_request req;
1490 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1491 req.bRequest = RAL_READ_MULTI_MAC;
1492 USETW(req.wValue, 0);
1493 USETW(req.wIndex, reg);
1494 USETW(req.wLength, len);
1496 error = ural_do_request(sc, &req, buf);
1498 device_printf(sc->sc_dev, "could not read MAC register: %s\n",
1499 usbd_errstr(error));
1504 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1506 struct usb_device_request req;
1509 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1510 req.bRequest = RAL_WRITE_MAC;
1511 USETW(req.wValue, val);
1512 USETW(req.wIndex, reg);
1513 USETW(req.wLength, 0);
1515 error = ural_do_request(sc, &req, NULL);
1517 device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1518 usbd_errstr(error));
1523 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1525 struct usb_device_request req;
1528 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1529 req.bRequest = RAL_WRITE_MULTI_MAC;
1530 USETW(req.wValue, 0);
1531 USETW(req.wIndex, reg);
1532 USETW(req.wLength, len);
1534 error = ural_do_request(sc, &req, buf);
1536 device_printf(sc->sc_dev, "could not write MAC register: %s\n",
1537 usbd_errstr(error));
1542 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1547 for (ntries = 0; ntries < 100; ntries++) {
1548 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1550 if (ural_pause(sc, hz / 100))
1553 if (ntries == 100) {
1554 device_printf(sc->sc_dev, "could not write to BBP\n");
1558 tmp = reg << 8 | val;
1559 ural_write(sc, RAL_PHY_CSR7, tmp);
1563 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1568 val = RAL_BBP_WRITE | reg << 8;
1569 ural_write(sc, RAL_PHY_CSR7, val);
1571 for (ntries = 0; ntries < 100; ntries++) {
1572 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1574 if (ural_pause(sc, hz / 100))
1577 if (ntries == 100) {
1578 device_printf(sc->sc_dev, "could not read BBP\n");
1582 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1586 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1591 for (ntries = 0; ntries < 100; ntries++) {
1592 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1594 if (ural_pause(sc, hz / 100))
1597 if (ntries == 100) {
1598 device_printf(sc->sc_dev, "could not write to RF\n");
1602 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1603 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1604 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1606 /* remember last written value in sc */
1607 sc->rf_regs[reg] = val;
1609 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
1613 ural_scan_start(struct ieee80211com *ic)
1615 struct ifnet *ifp = ic->ic_ifp;
1616 struct ural_softc *sc = ifp->if_softc;
1619 ural_write(sc, RAL_TXRX_CSR19, 0);
1620 ural_set_bssid(sc, ifp->if_broadcastaddr);
1625 ural_scan_end(struct ieee80211com *ic)
1627 struct ural_softc *sc = ic->ic_ifp->if_softc;
1630 ural_enable_tsf_sync(sc);
1631 ural_set_bssid(sc, sc->sc_bssid);
1637 ural_set_channel(struct ieee80211com *ic)
1639 struct ural_softc *sc = ic->ic_ifp->if_softc;
1642 ural_set_chan(sc, ic->ic_curchan);
1647 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1649 struct ifnet *ifp = sc->sc_ifp;
1650 struct ieee80211com *ic = ifp->if_l2com;
1654 chan = ieee80211_chan2ieee(ic, c);
1655 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1658 if (IEEE80211_IS_CHAN_2GHZ(c))
1659 power = min(sc->txpow[chan - 1], 31);
1663 /* adjust txpower using ifconfig settings */
1664 power -= (100 - ic->ic_txpowlimit) / 8;
1666 DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power);
1668 switch (sc->rf_rev) {
1670 ural_rf_write(sc, RAL_RF1, 0x00814);
1671 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1672 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1676 ural_rf_write(sc, RAL_RF1, 0x08804);
1677 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1678 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1679 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1683 ural_rf_write(sc, RAL_RF1, 0x0c808);
1684 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1685 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1686 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1690 ural_rf_write(sc, RAL_RF1, 0x08808);
1691 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1692 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1693 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1695 ural_rf_write(sc, RAL_RF1, 0x08808);
1696 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1697 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1698 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1702 ural_rf_write(sc, RAL_RF1, 0x08808);
1703 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1704 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1705 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1709 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1710 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1711 ural_rf_write(sc, RAL_RF1, 0x08804);
1713 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1714 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1715 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1720 for (i = 0; ural_rf5222[i].chan != chan; i++);
1722 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1723 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1724 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1725 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1729 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1730 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1731 /* set Japan filter bit for channel 14 */
1732 tmp = ural_bbp_read(sc, 70);
1734 tmp &= ~RAL_JAPAN_FILTER;
1736 tmp |= RAL_JAPAN_FILTER;
1738 ural_bbp_write(sc, 70, tmp);
1740 /* clear CRC errors */
1741 ural_read(sc, RAL_STA_CSR0);
1743 ural_pause(sc, hz / 100);
1744 ural_disable_rf_tune(sc);
1747 /* XXX doesn't belong here */
1748 /* update basic rate set */
1749 ural_set_basicrates(sc, c);
1751 /* give the hardware some time to do the switchover */
1752 ural_pause(sc, hz / 100);
1756 * Disable RF auto-tuning.
1759 ural_disable_rf_tune(struct ural_softc *sc)
1763 if (sc->rf_rev != RAL_RF_2523) {
1764 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1765 ural_rf_write(sc, RAL_RF1, tmp);
1768 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1769 ural_rf_write(sc, RAL_RF3, tmp);
1771 DPRINTFN(2, "disabling RF autotune\n");
1775 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1779 ural_enable_tsf_sync(struct ural_softc *sc)
1781 struct ifnet *ifp = sc->sc_ifp;
1782 struct ieee80211com *ic = ifp->if_l2com;
1783 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1784 uint16_t logcwmin, preload, tmp;
1786 /* first, disable TSF synchronization */
1787 ural_write(sc, RAL_TXRX_CSR19, 0);
1789 tmp = (16 * vap->iv_bss->ni_intval) << 4;
1790 ural_write(sc, RAL_TXRX_CSR18, tmp);
1792 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1793 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1794 tmp = logcwmin << 12 | preload;
1795 ural_write(sc, RAL_TXRX_CSR20, tmp);
1797 /* finally, enable TSF synchronization */
1798 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1799 if (ic->ic_opmode == IEEE80211_M_STA)
1800 tmp |= RAL_ENABLE_TSF_SYNC(1);
1802 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1803 ural_write(sc, RAL_TXRX_CSR19, tmp);
1805 DPRINTF("enabling TSF synchronization\n");
1809 ural_enable_tsf(struct ural_softc *sc)
1811 /* first, disable TSF synchronization */
1812 ural_write(sc, RAL_TXRX_CSR19, 0);
1813 ural_write(sc, RAL_TXRX_CSR19, RAL_ENABLE_TSF | RAL_ENABLE_TSF_SYNC(2));
1816 #define RAL_RXTX_TURNAROUND 5 /* us */
1818 ural_update_slot(struct ifnet *ifp)
1820 struct ural_softc *sc = ifp->if_softc;
1821 struct ieee80211com *ic = ifp->if_l2com;
1822 uint16_t slottime, sifs, eifs;
1824 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1827 * These settings may sound a bit inconsistent but this is what the
1828 * reference driver does.
1830 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1831 sifs = 16 - RAL_RXTX_TURNAROUND;
1834 sifs = 10 - RAL_RXTX_TURNAROUND;
1838 ural_write(sc, RAL_MAC_CSR10, slottime);
1839 ural_write(sc, RAL_MAC_CSR11, sifs);
1840 ural_write(sc, RAL_MAC_CSR12, eifs);
1844 ural_set_txpreamble(struct ural_softc *sc)
1846 struct ifnet *ifp = sc->sc_ifp;
1847 struct ieee80211com *ic = ifp->if_l2com;
1850 tmp = ural_read(sc, RAL_TXRX_CSR10);
1852 tmp &= ~RAL_SHORT_PREAMBLE;
1853 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1854 tmp |= RAL_SHORT_PREAMBLE;
1856 ural_write(sc, RAL_TXRX_CSR10, tmp);
1860 ural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c)
1862 /* XXX wrong, take from rate set */
1863 /* update basic rate set */
1864 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1865 /* 11a basic rates: 6, 12, 24Mbps */
1866 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1867 } else if (IEEE80211_IS_CHAN_ANYG(c)) {
1868 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1869 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1871 /* 11b basic rates: 1, 2Mbps */
1872 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1877 ural_set_bssid(struct ural_softc *sc, const uint8_t *bssid)
1881 tmp = bssid[0] | bssid[1] << 8;
1882 ural_write(sc, RAL_MAC_CSR5, tmp);
1884 tmp = bssid[2] | bssid[3] << 8;
1885 ural_write(sc, RAL_MAC_CSR6, tmp);
1887 tmp = bssid[4] | bssid[5] << 8;
1888 ural_write(sc, RAL_MAC_CSR7, tmp);
1890 DPRINTF("setting BSSID to %6D\n", bssid, ":");
1894 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1898 tmp = addr[0] | addr[1] << 8;
1899 ural_write(sc, RAL_MAC_CSR2, tmp);
1901 tmp = addr[2] | addr[3] << 8;
1902 ural_write(sc, RAL_MAC_CSR3, tmp);
1904 tmp = addr[4] | addr[5] << 8;
1905 ural_write(sc, RAL_MAC_CSR4, tmp);
1907 DPRINTF("setting MAC address to %6D\n", addr, ":");
1911 ural_setpromisc(struct ural_softc *sc)
1913 struct ifnet *ifp = sc->sc_ifp;
1916 tmp = ural_read(sc, RAL_TXRX_CSR2);
1918 tmp &= ~RAL_DROP_NOT_TO_ME;
1919 if (!(ifp->if_flags & IFF_PROMISC))
1920 tmp |= RAL_DROP_NOT_TO_ME;
1922 ural_write(sc, RAL_TXRX_CSR2, tmp);
1924 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1925 "entering" : "leaving");
1929 ural_update_promisc(struct ifnet *ifp)
1931 struct ural_softc *sc = ifp->if_softc;
1933 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1937 ural_setpromisc(sc);
1942 ural_get_rf(int rev)
1945 case RAL_RF_2522: return "RT2522";
1946 case RAL_RF_2523: return "RT2523";
1947 case RAL_RF_2524: return "RT2524";
1948 case RAL_RF_2525: return "RT2525";
1949 case RAL_RF_2525E: return "RT2525e";
1950 case RAL_RF_2526: return "RT2526";
1951 case RAL_RF_5222: return "RT5222";
1952 default: return "unknown";
1957 ural_read_eeprom(struct ural_softc *sc)
1961 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1963 sc->rf_rev = (val >> 11) & 0x7;
1964 sc->hw_radio = (val >> 10) & 0x1;
1965 sc->led_mode = (val >> 6) & 0x7;
1966 sc->rx_ant = (val >> 4) & 0x3;
1967 sc->tx_ant = (val >> 2) & 0x3;
1968 sc->nb_ant = val & 0x3;
1970 /* read MAC address */
1971 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6);
1973 /* read default values for BBP registers */
1974 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1976 /* read Tx power for all b/g channels */
1977 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
1981 ural_bbp_init(struct ural_softc *sc)
1983 #define N(a) ((int)(sizeof (a) / sizeof ((a)[0])))
1986 /* wait for BBP to be ready */
1987 for (ntries = 0; ntries < 100; ntries++) {
1988 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
1990 if (ural_pause(sc, hz / 100))
1993 if (ntries == 100) {
1994 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1998 /* initialize BBP registers to default values */
1999 for (i = 0; i < N(ural_def_bbp); i++)
2000 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2003 /* initialize BBP registers to values stored in EEPROM */
2004 for (i = 0; i < 16; i++) {
2005 if (sc->bbp_prom[i].reg == 0xff)
2007 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2016 ural_set_txantenna(struct ural_softc *sc, int antenna)
2021 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2024 else if (antenna == 2)
2027 tx |= RAL_BBP_DIVERSITY;
2029 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2030 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2031 sc->rf_rev == RAL_RF_5222)
2032 tx |= RAL_BBP_FLIPIQ;
2034 ural_bbp_write(sc, RAL_BBP_TX, tx);
2036 /* update values in PHY_CSR5 and PHY_CSR6 */
2037 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2038 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2040 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2041 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2045 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2049 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2052 else if (antenna == 2)
2055 rx |= RAL_BBP_DIVERSITY;
2057 /* need to force no I/Q flip for RF 2525e and 2526 */
2058 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2059 rx &= ~RAL_BBP_FLIPIQ;
2061 ural_bbp_write(sc, RAL_BBP_RX, rx);
2065 ural_init_locked(struct ural_softc *sc)
2067 #define N(a) ((int)(sizeof (a) / sizeof ((a)[0])))
2068 struct ifnet *ifp = sc->sc_ifp;
2069 struct ieee80211com *ic = ifp->if_l2com;
2073 RAL_LOCK_ASSERT(sc, MA_OWNED);
2075 ural_set_testmode(sc);
2076 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
2080 /* initialize MAC registers to default values */
2081 for (i = 0; i < N(ural_def_mac); i++)
2082 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2084 /* wait for BBP and RF to wake up (this can take a long time!) */
2085 for (ntries = 0; ntries < 100; ntries++) {
2086 tmp = ural_read(sc, RAL_MAC_CSR17);
2087 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2088 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2090 if (ural_pause(sc, hz / 100))
2093 if (ntries == 100) {
2094 device_printf(sc->sc_dev,
2095 "timeout waiting for BBP/RF to wakeup\n");
2100 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2102 /* set basic rate set (will be updated later) */
2103 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2105 if (ural_bbp_init(sc) != 0)
2108 ural_set_chan(sc, ic->ic_curchan);
2110 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2111 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2113 ural_set_txantenna(sc, sc->tx_ant);
2114 ural_set_rxantenna(sc, sc->rx_ant);
2116 ural_set_macaddr(sc, IF_LLADDR(ifp));
2119 * Allocate Tx and Rx xfer queues.
2121 ural_setup_tx_list(sc);
2124 tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2125 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2126 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2127 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2128 tmp |= RAL_DROP_TODS;
2129 if (!(ifp->if_flags & IFF_PROMISC))
2130 tmp |= RAL_DROP_NOT_TO_ME;
2132 ural_write(sc, RAL_TXRX_CSR2, tmp);
2134 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2135 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2136 usbd_xfer_set_stall(sc->sc_xfer[URAL_BULK_WR]);
2137 usbd_transfer_start(sc->sc_xfer[URAL_BULK_RD]);
2140 fail: ural_stop(sc);
2145 ural_init(void *priv)
2147 struct ural_softc *sc = priv;
2148 struct ifnet *ifp = sc->sc_ifp;
2149 struct ieee80211com *ic = ifp->if_l2com;
2152 ural_init_locked(sc);
2155 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2156 ieee80211_start_all(ic); /* start all vap's */
2160 ural_stop(struct ural_softc *sc)
2162 struct ifnet *ifp = sc->sc_ifp;
2164 RAL_LOCK_ASSERT(sc, MA_OWNED);
2166 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2169 * Drain all the transfers, if not already drained:
2172 usbd_transfer_drain(sc->sc_xfer[URAL_BULK_WR]);
2173 usbd_transfer_drain(sc->sc_xfer[URAL_BULK_RD]);
2176 ural_unsetup_tx_list(sc);
2179 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2180 /* reset ASIC and BBP (but won't reset MAC registers!) */
2181 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2183 ural_pause(sc, hz / 10);
2184 ural_write(sc, RAL_MAC_CSR1, 0);
2186 ural_pause(sc, hz / 10);
2190 ural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2191 const struct ieee80211_bpf_params *params)
2193 struct ieee80211com *ic = ni->ni_ic;
2194 struct ifnet *ifp = ic->ic_ifp;
2195 struct ural_softc *sc = ifp->if_softc;
2198 /* prevent management frames from being sent if we're not ready */
2199 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2202 ieee80211_free_node(ni);
2205 if (sc->tx_nfree < RAL_TX_MINFREE) {
2206 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2209 ieee80211_free_node(ni);
2215 if (params == NULL) {
2217 * Legacy path; interpret frame contents to decide
2218 * precisely how to send the frame.
2220 if (ural_tx_mgt(sc, m, ni) != 0)
2224 * Caller supplied explicit parameters to use in
2225 * sending the frame.
2227 if (ural_tx_raw(sc, m, ni, params) != 0)
2235 ieee80211_free_node(ni);
2236 return EIO; /* XXX */
2240 ural_ratectl_start(struct ural_softc *sc, struct ieee80211_node *ni)
2242 struct ieee80211vap *vap = ni->ni_vap;
2243 struct ural_vap *uvp = URAL_VAP(vap);
2245 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2246 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2248 usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
2252 ural_ratectl_timeout(void *arg)
2254 struct ural_vap *uvp = arg;
2255 struct ieee80211vap *vap = &uvp->vap;
2256 struct ieee80211com *ic = vap->iv_ic;
2258 ieee80211_runtask(ic, &uvp->ratectl_task);
2262 ural_ratectl_task(void *arg, int pending)
2264 struct ural_vap *uvp = arg;
2265 struct ieee80211vap *vap = &uvp->vap;
2266 struct ieee80211com *ic = vap->iv_ic;
2267 struct ifnet *ifp = ic->ic_ifp;
2268 struct ural_softc *sc = ifp->if_softc;
2269 struct ieee80211_node *ni;
2273 ni = ieee80211_ref_node(vap->iv_bss);
2275 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2276 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2278 ok = sc->sta[7] + /* TX ok w/o retry */
2279 sc->sta[8]; /* TX ok w/ retry */
2280 fail = sc->sta[9]; /* TX retry-fail count */
2282 retrycnt = sc->sta[8] + fail;
2284 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2285 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2287 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2289 usb_callout_reset(&uvp->ratectl_ch, hz, ural_ratectl_timeout, uvp);
2291 ieee80211_free_node(ni);
2295 ural_pause(struct ural_softc *sc, int timeout)
2298 usb_pause_mtx(&sc->sc_mtx, timeout);