1 /* $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.10.2.8 2006/07/08 07:48:43 maxim Exp $ */
2 /* $DragonFly: src/sys/dev/netif/ural/if_ural.c,v 1.22 2007/11/05 19:09:43 hasso Exp $ */
5 * Copyright (c) 2005, 2006
6 * Damien Bergamini <damien.bergamini@free.fr>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Ralink Technology RT2500USB chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/sysctl.h>
38 #include <net/ethernet.h>
40 #include <net/if_arp.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/ifq_var.h>
45 #include <netproto/802_11/ieee80211_var.h>
46 #include <netproto/802_11/ieee80211_radiotap.h>
47 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
49 #include <bus/usb/usb.h>
50 #include <bus/usb/usbdi.h>
51 #include <bus/usb/usbdi_util.h>
53 #include <dev/netif/ural/if_uralreg.h>
54 #include <dev/netif/ural/if_uralvar.h>
57 #define DPRINTF(x) do { if (uraldebug > 0) kprintf x; } while (0)
58 #define DPRINTFN(n, x) do { if (uraldebug >= (n)) kprintf x; } while (0)
60 SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
61 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &uraldebug, 0,
65 #define DPRINTFN(n, x)
68 #define URAL_RSSI(rssi) \
69 ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
70 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
72 /* various supported device vendors/products */
73 static const struct usb_devno ural_devs[] = {
74 { USB_DEVICE(0x0411, 0x005e) }, /* Melco WLI-U2-KG54-YB */
75 { USB_DEVICE(0x0411, 0x0066) }, /* Melco WLI-U2-KG54 */
76 { USB_DEVICE(0x0411, 0x0067) }, /* Melco WLI-U2-KG54-AI */
77 { USB_DEVICE(0x0411, 0x008b) }, /* Melco Nintendo Wi-Fi */
78 { USB_DEVICE(0x050d, 0x7050) }, /* Belkin Components F5D7050 */
79 { USB_DEVICE(0x06f8, 0xe000) }, /* Guillemot HWGUSB254 */
80 { USB_DEVICE(0x0769, 0x11f3) }, /* Surecom RT2570 */
81 { USB_DEVICE(0x0b05, 0x1706) }, /* Ralink (XXX) RT2500USB */
82 { USB_DEVICE(0x0b05, 0x1707) }, /* Asus WL167G */
83 { USB_DEVICE(0x0db0, 0x6861) }, /* MSI RT2570 */
84 { USB_DEVICE(0x0db0, 0x6865) }, /* MSI RT2570 */
85 { USB_DEVICE(0x0db0, 0x6869) }, /* MSI RT2570 */
86 { USB_DEVICE(0x0eb0, 0x9020) }, /* Nova Technology NV-902W */
87 { USB_DEVICE(0x0f88, 0x3012) }, /* VTech RT2570 */
88 { USB_DEVICE(0x1044, 0x8007) }, /* GIGABYTE GN-WBKG */
89 { USB_DEVICE(0x114b, 0x0110) }, /* Sphairon UB801R */
90 { USB_DEVICE(0x148f, 0x1706) }, /* Ralink RT2570 */
91 { USB_DEVICE(0x148f, 0x2570) }, /* Ralink RT2570 */
92 { USB_DEVICE(0x148f, 0x9020) }, /* Ralink RT2570 */
93 { USB_DEVICE(0x14b2, 0x3c02) }, /* Conceptronic C54RU */
94 { USB_DEVICE(0x1737, 0x000d) }, /* Linksys WUSB54G */
95 { USB_DEVICE(0x1737, 0x0011) }, /* Linksys WUSB54GP */
96 { USB_DEVICE(0x1737, 0x001a) }, /* Linksys HU200TS */
97 { USB_DEVICE(0x2001, 0x3c00) }, /* D-Link DWL-G122 */
98 { USB_DEVICE(0x5a57, 0x0260) }, /* Zinwell RT2570 */
101 MODULE_DEPEND(ural, wlan, 1, 1, 1);
103 static int ural_alloc_tx_list(struct ural_softc *);
104 static void ural_free_tx_list(struct ural_softc *);
105 static int ural_alloc_rx_list(struct ural_softc *);
106 static void ural_free_rx_list(struct ural_softc *);
107 static int ural_media_change(struct ifnet *);
108 static void ural_next_scan(void *);
109 static void ural_task(void *);
110 static int ural_newstate(struct ieee80211com *,
111 enum ieee80211_state, int);
112 static int ural_rxrate(struct ural_rx_desc *);
113 static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
115 static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
117 static uint8_t ural_plcp_signal(int);
118 static void ural_setup_tx_desc(struct ural_softc *,
119 struct ural_tx_desc *, uint32_t, int, int);
120 static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
121 struct ieee80211_node *);
122 static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
123 struct ieee80211_node *);
124 static int ural_tx_data(struct ural_softc *, struct mbuf *,
125 struct ieee80211_node *);
126 static void ural_start(struct ifnet *);
127 static void ural_watchdog(struct ifnet *);
128 static int ural_reset(struct ifnet *);
129 static int ural_ioctl(struct ifnet *, u_long, caddr_t,
131 static void ural_set_testmode(struct ural_softc *);
132 static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
134 static uint16_t ural_read(struct ural_softc *, uint16_t);
135 static void ural_read_multi(struct ural_softc *, uint16_t, void *,
137 static void ural_write(struct ural_softc *, uint16_t, uint16_t);
138 static void ural_write_multi(struct ural_softc *, uint16_t, void *,
140 static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
141 static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
142 static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
143 static void ural_set_chan(struct ural_softc *,
144 struct ieee80211_channel *);
145 static void ural_disable_rf_tune(struct ural_softc *);
146 static void ural_enable_tsf_sync(struct ural_softc *);
147 static void ural_update_slot(struct ifnet *);
148 static void ural_set_txpreamble(struct ural_softc *);
149 static void ural_set_basicrates(struct ural_softc *);
150 static void ural_set_bssid(struct ural_softc *, uint8_t *);
151 static void ural_set_macaddr(struct ural_softc *, uint8_t *);
152 static void ural_update_promisc(struct ural_softc *);
153 static const char *ural_get_rf(int);
154 static void ural_read_eeprom(struct ural_softc *);
155 static int ural_bbp_init(struct ural_softc *);
156 static void ural_set_txantenna(struct ural_softc *, int);
157 static void ural_set_rxantenna(struct ural_softc *, int);
158 static void ural_init(void *);
159 static void ural_stop(struct ural_softc *);
160 static void ural_stats(struct ieee80211com *,
161 struct ieee80211_node *,
162 struct ieee80211_ratectl_stats *);
163 static void ural_stats_update(usbd_xfer_handle,
164 usbd_private_handle, usbd_status);
165 static void ural_stats_timeout(void *);
166 static void ural_ratectl_change(struct ieee80211com *ic, u_int,
170 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
172 static const struct ieee80211_rateset ural_rateset_11a =
173 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
175 static const struct ieee80211_rateset ural_rateset_11b =
176 { 4, { 2, 4, 11, 22 } };
178 static const struct ieee80211_rateset ural_rateset_11g =
179 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
182 * Default values for MAC registers; values taken from the reference driver.
184 static const struct {
188 { RAL_TXRX_CSR5, 0x8c8d },
189 { RAL_TXRX_CSR6, 0x8b8a },
190 { RAL_TXRX_CSR7, 0x8687 },
191 { RAL_TXRX_CSR8, 0x0085 },
192 { RAL_MAC_CSR13, 0x1111 },
193 { RAL_MAC_CSR14, 0x1e11 },
194 { RAL_TXRX_CSR21, 0xe78f },
195 { RAL_MAC_CSR9, 0xff1d },
196 { RAL_MAC_CSR11, 0x0002 },
197 { RAL_MAC_CSR22, 0x0053 },
198 { RAL_MAC_CSR15, 0x0000 },
199 { RAL_MAC_CSR8, 0x0780 },
200 { RAL_TXRX_CSR19, 0x0000 },
201 { RAL_TXRX_CSR18, 0x005a },
202 { RAL_PHY_CSR2, 0x0000 },
203 { RAL_TXRX_CSR0, 0x1ec0 },
204 { RAL_PHY_CSR4, 0x000f }
208 * Default values for BBP registers; values taken from the reference driver.
210 static const struct {
249 * Default values for RF register R2 indexed by channel numbers.
251 static const uint32_t ural_rf2522_r2[] = {
252 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
253 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
256 static const uint32_t ural_rf2523_r2[] = {
257 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
258 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
261 static const uint32_t ural_rf2524_r2[] = {
262 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
263 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
266 static const uint32_t ural_rf2525_r2[] = {
267 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
268 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
271 static const uint32_t ural_rf2525_hi_r2[] = {
272 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
273 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
276 static const uint32_t ural_rf2525e_r2[] = {
277 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
278 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
281 static const uint32_t ural_rf2526_hi_r2[] = {
282 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
283 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
286 static const uint32_t ural_rf2526_r2[] = {
287 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
288 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
292 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
293 * values taken from the reference driver.
295 static const struct {
301 { 1, 0x08808, 0x0044d, 0x00282 },
302 { 2, 0x08808, 0x0044e, 0x00282 },
303 { 3, 0x08808, 0x0044f, 0x00282 },
304 { 4, 0x08808, 0x00460, 0x00282 },
305 { 5, 0x08808, 0x00461, 0x00282 },
306 { 6, 0x08808, 0x00462, 0x00282 },
307 { 7, 0x08808, 0x00463, 0x00282 },
308 { 8, 0x08808, 0x00464, 0x00282 },
309 { 9, 0x08808, 0x00465, 0x00282 },
310 { 10, 0x08808, 0x00466, 0x00282 },
311 { 11, 0x08808, 0x00467, 0x00282 },
312 { 12, 0x08808, 0x00468, 0x00282 },
313 { 13, 0x08808, 0x00469, 0x00282 },
314 { 14, 0x08808, 0x0046b, 0x00286 },
316 { 36, 0x08804, 0x06225, 0x00287 },
317 { 40, 0x08804, 0x06226, 0x00287 },
318 { 44, 0x08804, 0x06227, 0x00287 },
319 { 48, 0x08804, 0x06228, 0x00287 },
320 { 52, 0x08804, 0x06229, 0x00287 },
321 { 56, 0x08804, 0x0622a, 0x00287 },
322 { 60, 0x08804, 0x0622b, 0x00287 },
323 { 64, 0x08804, 0x0622c, 0x00287 },
325 { 100, 0x08804, 0x02200, 0x00283 },
326 { 104, 0x08804, 0x02201, 0x00283 },
327 { 108, 0x08804, 0x02202, 0x00283 },
328 { 112, 0x08804, 0x02203, 0x00283 },
329 { 116, 0x08804, 0x02204, 0x00283 },
330 { 120, 0x08804, 0x02205, 0x00283 },
331 { 124, 0x08804, 0x02206, 0x00283 },
332 { 128, 0x08804, 0x02207, 0x00283 },
333 { 132, 0x08804, 0x02208, 0x00283 },
334 { 136, 0x08804, 0x02209, 0x00283 },
335 { 140, 0x08804, 0x0220a, 0x00283 },
337 { 149, 0x08808, 0x02429, 0x00281 },
338 { 153, 0x08808, 0x0242b, 0x00281 },
339 { 157, 0x08808, 0x0242d, 0x00281 },
340 { 161, 0x08808, 0x0242f, 0x00281 }
343 static device_probe_t ural_match;
344 static device_attach_t ural_attach;
345 static device_detach_t ural_detach;
347 static devclass_t ural_devclass;
349 static kobj_method_t ural_methods[] = {
350 DEVMETHOD(device_probe, ural_match),
351 DEVMETHOD(device_attach, ural_attach),
352 DEVMETHOD(device_detach, ural_detach),
356 static driver_t ural_driver = {
359 sizeof(struct ural_softc)
362 MODULE_DEPEND(ural, usb, 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;
389 sc->sc_udev = uaa->device;
390 sc->sc_tx_retries = 7; /* TODO tunable/sysctl */
392 usbd_devinfo(sc->sc_udev, 0, devinfo);
394 device_set_desc_copy(self, devinfo);
396 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
397 kprintf("%s: could not set configuration no\n",
398 device_get_nameunit(sc->sc_dev));
402 /* get the first interface handle */
403 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
406 kprintf("%s: could not get interface handle\n",
407 device_get_nameunit(sc->sc_dev));
414 id = usbd_get_interface_descriptor(sc->sc_iface);
416 sc->sc_rx_no = sc->sc_tx_no = -1;
417 for (i = 0; i < id->bNumEndpoints; i++) {
418 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
420 kprintf("%s: no endpoint descriptor for %d\n",
421 device_get_nameunit(sc->sc_dev), i);
425 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
426 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
427 sc->sc_rx_no = ed->bEndpointAddress;
428 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
429 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
430 sc->sc_tx_no = ed->bEndpointAddress;
432 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
433 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
437 usb_init_task(&sc->sc_task, ural_task, sc);
438 callout_init(&sc->scan_ch);
439 callout_init(&sc->stats_ch);
441 /* retrieve RT2570 rev. no */
442 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
444 /* retrieve MAC address and various other things from EEPROM */
445 ural_read_eeprom(sc);
447 kprintf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
448 device_get_nameunit(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
452 if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
453 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
454 ifp->if_init = ural_init;
455 ifp->if_ioctl = ural_ioctl;
456 ifp->if_start = ural_start;
457 ifp->if_watchdog = ural_watchdog;
458 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
459 ifq_set_ready(&ifp->if_snd);
461 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
462 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
463 ic->ic_ratectl.rc_st_valid_stats =
464 IEEE80211_RATECTL_STATS_PKT_NORETRY |
465 IEEE80211_RATECTL_STATS_PKT_OK |
466 IEEE80211_RATECTL_STATS_PKT_ERR |
467 IEEE80211_RATECTL_STATS_RETRIES;
468 ic->ic_ratectl.rc_st_stats = ural_stats;
469 ic->ic_ratectl.rc_st_change = ural_ratectl_change;
471 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
472 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
473 ic->ic_state = IEEE80211_S_INIT;
475 /* set device capabilities */
477 IEEE80211_C_IBSS | /* IBSS mode supported */
478 IEEE80211_C_MONITOR | /* monitor mode supported */
479 IEEE80211_C_HOSTAP | /* HostAp mode supported */
480 IEEE80211_C_TXPMGT | /* tx power management */
481 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
482 IEEE80211_C_SHSLOT | /* short slot time supported */
483 IEEE80211_C_WPA; /* 802.11i */
485 if (sc->rf_rev == RAL_RF_5222) {
486 /* set supported .11a rates */
487 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
489 /* set supported .11a channels */
490 for (i = 36; i <= 64; i += 4) {
491 ic->ic_channels[i].ic_freq =
492 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
493 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
495 for (i = 100; i <= 140; i += 4) {
496 ic->ic_channels[i].ic_freq =
497 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
498 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
500 for (i = 149; i <= 161; i += 4) {
501 ic->ic_channels[i].ic_freq =
502 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
503 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
507 /* set supported .11b and .11g rates */
508 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
509 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
511 /* set supported .11b and .11g channels (1 through 14) */
512 for (i = 1; i <= 14; i++) {
513 ic->ic_channels[i].ic_freq =
514 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
515 ic->ic_channels[i].ic_flags =
516 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
517 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
520 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
522 ieee80211_ifattach(ic);
523 ic->ic_reset = ural_reset;
524 /* enable s/w bmiss handling in sta mode */
525 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
527 /* override state transition machine */
528 sc->sc_newstate = ic->ic_newstate;
529 ic->ic_newstate = ural_newstate;
530 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
532 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
533 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
535 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
536 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
537 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
539 sc->sc_txtap_len = sizeof sc->sc_txtapu;
540 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
541 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
544 ieee80211_announce(ic);
550 ural_detach(device_t self)
552 struct ural_softc *sc = device_get_softc(self);
553 struct ieee80211com *ic = &sc->sc_ic;
554 struct ifnet *ifp = &ic->ic_if;
561 callout_stop(&sc->scan_ch);
562 callout_stop(&sc->stats_ch);
564 lwkt_serialize_enter(ifp->if_serializer);
566 lwkt_serialize_exit(ifp->if_serializer);
568 usb_rem_task(sc->sc_udev, &sc->sc_task);
571 ieee80211_ifdetach(ic);
575 KKASSERT(sc->stats_xfer == NULL);
576 KKASSERT(sc->sc_rx_pipeh == NULL);
577 KKASSERT(sc->sc_tx_pipeh == NULL);
581 * Make sure TX/RX list is empty
583 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
584 struct ural_tx_data *data = &sc->tx_data[i];
586 KKASSERT(data->xfer == NULL);
587 KKASSERT(data->ni == NULL);
588 KKASSERT(data->m == NULL);
590 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
591 struct ural_rx_data *data = &sc->rx_data[i];
593 KKASSERT(data->xfer == NULL);
594 KKASSERT(data->m == NULL);
602 ural_alloc_tx_list(struct ural_softc *sc)
608 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
609 struct ural_tx_data *data = &sc->tx_data[i];
613 data->xfer = usbd_alloc_xfer(sc->sc_udev);
614 if (data->xfer == NULL) {
615 kprintf("%s: could not allocate tx xfer\n",
616 device_get_nameunit(sc->sc_dev));
620 data->buf = usbd_alloc_buffer(data->xfer,
621 RAL_TX_DESC_SIZE + MCLBYTES);
622 if (data->buf == NULL) {
623 kprintf("%s: could not allocate tx buffer\n",
624 device_get_nameunit(sc->sc_dev));
632 ural_free_tx_list(struct ural_softc *sc)
636 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
637 struct ural_tx_data *data = &sc->tx_data[i];
639 if (data->xfer != NULL) {
640 usbd_free_xfer(data->xfer);
644 if (data->ni != NULL) {
645 ieee80211_free_node(data->ni);
648 if (data->m != NULL) {
657 ural_alloc_rx_list(struct ural_softc *sc)
661 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
662 struct ural_rx_data *data = &sc->rx_data[i];
666 data->xfer = usbd_alloc_xfer(sc->sc_udev);
667 if (data->xfer == NULL) {
668 kprintf("%s: could not allocate rx xfer\n",
669 device_get_nameunit(sc->sc_dev));
673 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
674 kprintf("%s: could not allocate rx buffer\n",
675 device_get_nameunit(sc->sc_dev));
679 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
680 if (data->m == NULL) {
681 kprintf("%s: could not allocate rx mbuf\n",
682 device_get_nameunit(sc->sc_dev));
686 data->buf = mtod(data->m, uint8_t *);
692 ural_free_rx_list(struct ural_softc *sc)
696 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
697 struct ural_rx_data *data = &sc->rx_data[i];
699 if (data->xfer != NULL) {
700 usbd_free_xfer(data->xfer);
704 if (data->m != NULL) {
712 ural_media_change(struct ifnet *ifp)
714 struct ural_softc *sc = ifp->if_softc;
717 error = ieee80211_media_change(ifp);
718 if (error != ENETRESET)
721 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
728 * This function is called periodically (every 200ms) during scanning to
729 * switch from one channel to another.
732 ural_next_scan(void *arg)
734 struct ural_softc *sc = arg;
735 struct ieee80211com *ic = &sc->sc_ic;
736 struct ifnet *ifp = &ic->ic_if;
743 if (ic->ic_state == IEEE80211_S_SCAN) {
744 lwkt_serialize_enter(ifp->if_serializer);
745 ieee80211_next_scan(ic);
746 lwkt_serialize_exit(ifp->if_serializer);
753 ural_task(void *xarg)
755 struct ural_softc *sc = xarg;
756 struct ieee80211com *ic = &sc->sc_ic;
757 struct ifnet *ifp = &ic->ic_if;
758 enum ieee80211_state nstate;
759 struct ieee80211_node *ni;
768 nstate = sc->sc_state;
771 KASSERT(nstate != IEEE80211_S_INIT,
772 ("->INIT state transition should not be defered\n"));
773 ural_set_chan(sc, ic->ic_curchan);
775 switch (sc->sc_state) {
776 case IEEE80211_S_RUN:
779 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
780 ural_update_slot(&ic->ic_if);
781 ural_set_txpreamble(sc);
782 ural_set_basicrates(sc);
783 ural_set_bssid(sc, ni->ni_bssid);
786 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
787 ic->ic_opmode == IEEE80211_M_IBSS) {
788 lwkt_serialize_enter(ifp->if_serializer);
789 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
790 lwkt_serialize_exit(ifp->if_serializer);
793 kprintf("%s: could not allocate beacon\n",
794 device_get_nameunit(sc->sc_dev));
799 if (ural_tx_bcn(sc, m, ni) != 0) {
800 kprintf("%s: could not send beacon\n",
801 device_get_nameunit(sc->sc_dev));
807 /* make tx led blink on tx (controlled by ASIC) */
808 ural_write(sc, RAL_MAC_CSR20, 1);
810 if (ic->ic_opmode != IEEE80211_M_MONITOR)
811 ural_enable_tsf_sync(sc);
813 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
814 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
816 callout_reset(&sc->stats_ch, 4 * hz / 5,
817 ural_stats_timeout, sc);
820 case IEEE80211_S_SCAN:
821 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
828 lwkt_serialize_enter(ifp->if_serializer);
829 ieee80211_ratectl_newstate(ic, sc->sc_state);
830 sc->sc_newstate(ic, sc->sc_state, arg);
831 lwkt_serialize_exit(ifp->if_serializer);
837 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
839 struct ifnet *ifp = &ic->ic_if;
840 struct ural_softc *sc = ifp->if_softc;
842 ASSERT_SERIALIZED(ifp->if_serializer);
846 callout_stop(&sc->scan_ch);
847 callout_stop(&sc->stats_ch);
849 /* do it in a process context */
850 sc->sc_state = nstate;
853 lwkt_serialize_exit(ifp->if_serializer);
854 usb_rem_task(sc->sc_udev, &sc->sc_task);
856 if (nstate == IEEE80211_S_INIT) {
857 lwkt_serialize_enter(ifp->if_serializer);
858 ieee80211_ratectl_newstate(ic, nstate);
859 sc->sc_newstate(ic, nstate, arg);
861 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
862 lwkt_serialize_enter(ifp->if_serializer);
869 /* quickly determine if a given rate is CCK or OFDM */
870 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
872 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
874 #define RAL_RXTX_TURNAROUND 5 /* us */
877 * This function is only used by the Rx radiotap code.
880 ural_rxrate(struct ural_rx_desc *desc)
882 if (le32toh(desc->flags) & RAL_RX_OFDM) {
883 /* reverse function of ural_plcp_signal */
884 switch (desc->rate) {
892 case 0xc: return 108;
895 if (desc->rate == 10)
897 if (desc->rate == 20)
899 if (desc->rate == 55)
901 if (desc->rate == 110)
904 return 2; /* should not get there */
908 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
910 struct ural_tx_data *data = priv;
911 struct ural_softc *sc = data->sc;
912 struct ieee80211_node *ni;
913 struct ifnet *ifp = &sc->sc_ic.ic_if;
920 if (status != USBD_NORMAL_COMPLETION) {
921 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
926 kprintf("%s: could not transmit buffer: %s\n",
927 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
929 if (status == USBD_STALLED)
930 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
945 DPRINTFN(10, ("tx done\n"));
948 ifp->if_flags &= ~IFF_OACTIVE;
950 lwkt_serialize_enter(ifp->if_serializer);
951 ieee80211_free_node(ni);
953 lwkt_serialize_exit(ifp->if_serializer);
959 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
961 struct ural_rx_data *data = priv;
962 struct ural_softc *sc = data->sc;
963 struct ieee80211com *ic = &sc->sc_ic;
964 struct ifnet *ifp = &ic->ic_if;
965 struct ural_rx_desc *desc;
966 struct ieee80211_frame *wh;
967 struct ieee80211_node *ni;
968 struct mbuf *mnew, *m;
976 if (status != USBD_NORMAL_COMPLETION) {
977 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
982 if (status == USBD_STALLED)
983 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
987 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
989 if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
990 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
996 /* rx descriptor is located at the end */
997 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
999 if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
1000 (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
1002 * This should not happen since we did not request to receive
1003 * those frames when we filled RAL_TXRX_CSR2.
1005 DPRINTFN(5, ("PHY or CRC error\n"));
1010 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1020 lwkt_serialize_enter(ifp->if_serializer);
1023 m->m_pkthdr.rcvif = ifp;
1024 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
1026 if (sc->sc_drvbpf != NULL) {
1027 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
1029 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; /* h/w leaves FCS */
1030 tap->wr_rate = ural_rxrate(desc);
1031 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1032 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1033 tap->wr_antenna = sc->rx_ant;
1034 tap->wr_antsignal = URAL_RSSI(desc->rssi);
1036 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1039 /* trim CRC here so WEP can find its own CRC at the end of packet. */
1040 m_adj(m, -IEEE80211_CRC_LEN);
1042 wh = mtod(m, struct ieee80211_frame *);
1043 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1045 /* send the frame to the 802.11 layer */
1046 ieee80211_input(ic, m, ni, URAL_RSSI(desc->rssi), 0);
1048 /* node is no longer needed */
1049 ieee80211_free_node(ni);
1051 lwkt_serialize_exit(ifp->if_serializer);
1054 data->buf = mtod(data->m, uint8_t *);
1056 DPRINTFN(15, ("rx done\n"));
1058 skip: /* setup a new transfer */
1059 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
1060 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
1061 usbd_transfer(xfer);
1067 ural_plcp_signal(int rate)
1070 /* CCK rates (returned values are device-dependent) */
1073 case 11: return 0x2;
1074 case 22: return 0x3;
1076 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1077 case 12: return 0xb;
1078 case 18: return 0xf;
1079 case 24: return 0xa;
1080 case 36: return 0xe;
1081 case 48: return 0x9;
1082 case 72: return 0xd;
1083 case 96: return 0x8;
1084 case 108: return 0xc;
1086 /* unsupported rates (should not get there) */
1087 default: return 0xff;
1092 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1093 uint32_t flags, int len, int rate)
1095 struct ieee80211com *ic = &sc->sc_ic;
1096 uint16_t plcp_length;
1099 desc->flags = htole32(flags);
1100 desc->flags |= htole32(RAL_TX_NEWSEQ);
1101 desc->flags |= htole32(len << 16);
1103 desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
1104 desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
1106 /* setup PLCP fields */
1107 desc->plcp_signal = ural_plcp_signal(rate);
1108 desc->plcp_service = 4;
1110 len += IEEE80211_CRC_LEN;
1111 if (RAL_RATE_IS_OFDM(rate)) {
1112 desc->flags |= htole32(RAL_TX_OFDM);
1114 plcp_length = len & 0xfff;
1115 desc->plcp_length_hi = plcp_length >> 6;
1116 desc->plcp_length_lo = plcp_length & 0x3f;
1118 plcp_length = (16 * len + rate - 1) / rate;
1120 remainder = (16 * len) % 22;
1121 if (remainder != 0 && remainder < 7)
1122 desc->plcp_service |= RAL_PLCP_LENGEXT;
1124 desc->plcp_length_hi = plcp_length >> 8;
1125 desc->plcp_length_lo = plcp_length & 0xff;
1127 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1128 desc->plcp_signal |= 0x08;
1135 #define RAL_TX_TIMEOUT 5000
1138 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1140 struct ural_tx_desc *desc;
1141 usbd_xfer_handle xfer;
1147 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1149 xfer = usbd_alloc_xfer(sc->sc_udev);
1153 /* xfer length needs to be a multiple of two! */
1154 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1156 buf = usbd_alloc_buffer(xfer, xferlen);
1158 usbd_free_xfer(xfer);
1162 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1163 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1165 error = usbd_sync_transfer(xfer);
1167 usbd_free_xfer(xfer);
1171 desc = (struct ural_tx_desc *)buf;
1173 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1174 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1175 m0->m_pkthdr.len, rate);
1177 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1178 m0->m_pkthdr.len, rate, xferlen));
1180 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1181 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1183 error = usbd_sync_transfer(xfer);
1184 usbd_free_xfer(xfer);
1190 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1192 struct ieee80211com *ic = &sc->sc_ic;
1193 struct ifnet *ifp = &ic->ic_if;
1194 struct ural_tx_desc *desc;
1195 struct ural_tx_data *data;
1196 struct ieee80211_frame *wh;
1202 data = &sc->tx_data[0];
1203 desc = (struct ural_tx_desc *)data->buf;
1205 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1210 wh = mtod(m0, struct ieee80211_frame *);
1212 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1213 flags |= RAL_TX_ACK;
1215 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1217 *(uint16_t *)wh->i_dur = htole16(dur);
1219 /* tell hardware to add timestamp for probe responses */
1220 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1221 IEEE80211_FC0_TYPE_MGT &&
1222 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1223 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1224 flags |= RAL_TX_TIMESTAMP;
1227 if (sc->sc_drvbpf != NULL) {
1228 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1231 tap->wt_rate = rate;
1232 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1233 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1234 tap->wt_antenna = sc->tx_ant;
1236 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1239 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1240 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1242 /* align end on a 2-bytes boundary */
1243 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1246 * No space left in the last URB to store the extra 2 bytes, force
1247 * sending of another URB.
1249 if ((xferlen % 64) == 0)
1252 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1253 m0->m_pkthdr.len, rate, xferlen));
1255 lwkt_serialize_exit(ifp->if_serializer);
1257 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1258 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1261 error = usbd_transfer(data->xfer);
1262 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1271 lwkt_serialize_enter(ifp->if_serializer);
1276 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1278 struct ieee80211com *ic = &sc->sc_ic;
1279 struct ifnet *ifp = &ic->ic_if;
1280 struct ural_tx_desc *desc;
1281 struct ural_tx_data *data;
1282 struct ieee80211_frame *wh;
1283 struct ieee80211_key *k;
1287 int xferlen, rate, rate_idx;
1289 wh = mtod(m0, struct ieee80211_frame *);
1291 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rate_idx, 1);
1292 rate = IEEE80211_RS_RATE(&ni->ni_rates, rate_idx);
1294 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1295 k = ieee80211_crypto_encap(ic, ni, m0);
1301 /* packet header may have moved, reset our local pointer */
1302 wh = mtod(m0, struct ieee80211_frame *);
1305 data = &sc->tx_data[0];
1306 desc = (struct ural_tx_desc *)data->buf;
1311 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1312 flags |= RAL_TX_ACK;
1313 flags |= RAL_TX_RETRY(sc->sc_tx_retries);
1315 dur = ieee80211_txtime(ni, RAL_ACK_SIZE,
1316 ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1318 *(uint16_t *)wh->i_dur = htole16(dur);
1321 if (sc->sc_drvbpf != NULL) {
1322 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1325 tap->wt_rate = rate;
1326 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1327 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1328 tap->wt_antenna = sc->tx_ant;
1330 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1333 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1334 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1336 /* align end on a 2-bytes boundary */
1337 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1340 * No space left in the last URB to store the extra 2 bytes, force
1341 * sending of another URB.
1343 if ((xferlen % 64) == 0)
1346 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1347 m0->m_pkthdr.len, rate, xferlen));
1349 lwkt_serialize_exit(ifp->if_serializer);
1351 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1352 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
1355 error = usbd_transfer(data->xfer);
1356 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1365 lwkt_serialize_enter(ifp->if_serializer);
1370 ural_start(struct ifnet *ifp)
1372 struct ural_softc *sc = ifp->if_softc;
1373 struct ieee80211com *ic = &sc->sc_ic;
1375 ASSERT_SERIALIZED(ifp->if_serializer);
1382 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
1388 struct ieee80211_node *ni;
1391 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1392 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1393 ifp->if_flags |= IFF_OACTIVE;
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)
1413 m0 = ifq_poll(&ifp->if_snd);
1416 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1417 ifp->if_flags |= IFF_OACTIVE;
1421 ifq_dequeue(&ifp->if_snd, m0);
1423 if (m0->m_len < sizeof (struct ether_header)) {
1424 m0 = m_pullup(m0, sizeof (struct ether_header));
1431 eh = mtod(m0, struct ether_header *);
1432 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1439 m0 = ieee80211_encap(ic, m0, ni);
1441 ieee80211_free_node(ni);
1445 if (ic->ic_rawbpf != NULL)
1446 bpf_mtap(ic->ic_rawbpf, m0);
1448 if (ural_tx_data(sc, m0, ni) != 0) {
1449 ieee80211_free_node(ni);
1455 sc->sc_tx_timer = 5;
1463 ural_watchdog(struct ifnet *ifp)
1465 struct ural_softc *sc = ifp->if_softc;
1466 struct ieee80211com *ic = &sc->sc_ic;
1468 ASSERT_SERIALIZED(ifp->if_serializer);
1474 if (sc->sc_tx_timer > 0) {
1475 if (--sc->sc_tx_timer == 0) {
1476 device_printf(sc->sc_dev, "device timeout\n");
1477 /*ural_init(sc); XXX needs a process context! */
1485 ieee80211_watchdog(ic);
1491 * This function allows for fast channel switching in monitor mode (used by
1492 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1493 * generate a new beacon frame.
1496 ural_reset(struct ifnet *ifp)
1498 struct ural_softc *sc = ifp->if_softc;
1499 struct ieee80211com *ic = &sc->sc_ic;
1501 ASSERT_SERIALIZED(ifp->if_serializer);
1503 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1508 lwkt_serialize_exit(ifp->if_serializer);
1509 ural_set_chan(sc, ic->ic_curchan);
1510 lwkt_serialize_enter(ifp->if_serializer);
1518 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1520 struct ural_softc *sc = ifp->if_softc;
1521 struct ieee80211com *ic = &sc->sc_ic;
1524 ASSERT_SERIALIZED(ifp->if_serializer);
1530 if (ifp->if_flags & IFF_UP) {
1531 if (ifp->if_flags & IFF_RUNNING) {
1532 lwkt_serialize_exit(ifp->if_serializer);
1533 ural_update_promisc(sc);
1534 lwkt_serialize_enter(ifp->if_serializer);
1539 if (ifp->if_flags & IFF_RUNNING)
1545 error = ieee80211_ioctl(ic, cmd, data, cr);
1548 if (error == ENETRESET) {
1549 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1550 (IFF_UP | IFF_RUNNING) &&
1551 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1561 ural_set_testmode(struct ural_softc *sc)
1563 usb_device_request_t req;
1566 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1567 req.bRequest = RAL_VENDOR_REQUEST;
1568 USETW(req.wValue, 4);
1569 USETW(req.wIndex, 1);
1570 USETW(req.wLength, 0);
1572 error = usbd_do_request(sc->sc_udev, &req, NULL);
1574 kprintf("%s: could not set test mode: %s\n",
1575 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1580 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1582 usb_device_request_t req;
1585 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1586 req.bRequest = RAL_READ_EEPROM;
1587 USETW(req.wValue, 0);
1588 USETW(req.wIndex, addr);
1589 USETW(req.wLength, len);
1591 error = usbd_do_request(sc->sc_udev, &req, buf);
1593 kprintf("%s: could not read EEPROM: %s\n",
1594 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1599 ural_read(struct ural_softc *sc, uint16_t reg)
1601 usb_device_request_t req;
1605 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1606 req.bRequest = RAL_READ_MAC;
1607 USETW(req.wValue, 0);
1608 USETW(req.wIndex, reg);
1609 USETW(req.wLength, sizeof (uint16_t));
1611 error = usbd_do_request(sc->sc_udev, &req, &val);
1613 kprintf("%s: could not read MAC register: %s\n",
1614 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1618 return le16toh(val);
1622 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1624 usb_device_request_t req;
1627 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1628 req.bRequest = RAL_READ_MULTI_MAC;
1629 USETW(req.wValue, 0);
1630 USETW(req.wIndex, reg);
1631 USETW(req.wLength, len);
1633 error = usbd_do_request(sc->sc_udev, &req, buf);
1635 kprintf("%s: could not read MAC register: %s\n",
1636 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1641 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1643 usb_device_request_t req;
1646 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1647 req.bRequest = RAL_WRITE_MAC;
1648 USETW(req.wValue, val);
1649 USETW(req.wIndex, reg);
1650 USETW(req.wLength, 0);
1652 error = usbd_do_request(sc->sc_udev, &req, NULL);
1654 kprintf("%s: could not write MAC register: %s\n",
1655 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1660 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1662 usb_device_request_t req;
1665 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1666 req.bRequest = RAL_WRITE_MULTI_MAC;
1667 USETW(req.wValue, 0);
1668 USETW(req.wIndex, reg);
1669 USETW(req.wLength, len);
1671 error = usbd_do_request(sc->sc_udev, &req, buf);
1673 kprintf("%s: could not write MAC register: %s\n",
1674 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1679 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1684 for (ntries = 0; ntries < 5; ntries++) {
1685 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1689 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1693 tmp = reg << 8 | val;
1694 ural_write(sc, RAL_PHY_CSR7, tmp);
1698 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1703 val = RAL_BBP_WRITE | reg << 8;
1704 ural_write(sc, RAL_PHY_CSR7, val);
1706 for (ntries = 0; ntries < 5; ntries++) {
1707 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1711 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1715 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1719 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1724 for (ntries = 0; ntries < 5; ntries++) {
1725 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1729 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1733 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1734 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1735 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1737 /* remember last written value in sc */
1738 sc->rf_regs[reg] = val;
1740 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1744 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1746 struct ieee80211com *ic = &sc->sc_ic;
1750 chan = ieee80211_chan2ieee(ic, c);
1751 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1754 if (IEEE80211_IS_CHAN_2GHZ(c))
1755 power = min(sc->txpow[chan - 1], 31);
1759 /* adjust txpower using ifconfig settings */
1760 power -= (100 - ic->ic_txpowlimit) / 8;
1762 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1764 switch (sc->rf_rev) {
1766 ural_rf_write(sc, RAL_RF1, 0x00814);
1767 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1768 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1772 ural_rf_write(sc, RAL_RF1, 0x08804);
1773 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1774 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1775 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1779 ural_rf_write(sc, RAL_RF1, 0x0c808);
1780 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1781 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1782 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1786 ural_rf_write(sc, RAL_RF1, 0x08808);
1787 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1788 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1789 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1791 ural_rf_write(sc, RAL_RF1, 0x08808);
1792 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1793 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1794 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1798 ural_rf_write(sc, RAL_RF1, 0x08808);
1799 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1800 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1801 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1805 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1806 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1807 ural_rf_write(sc, RAL_RF1, 0x08804);
1809 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1810 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1811 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1816 for (i = 0; ural_rf5222[i].chan != chan; i++)
1819 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1820 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1821 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1822 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1826 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1827 ic->ic_state != IEEE80211_S_SCAN) {
1828 /* set Japan filter bit for channel 14 */
1829 tmp = ural_bbp_read(sc, 70);
1831 tmp &= ~RAL_JAPAN_FILTER;
1833 tmp |= RAL_JAPAN_FILTER;
1835 ural_bbp_write(sc, 70, tmp);
1837 /* clear CRC errors */
1838 ural_read(sc, RAL_STA_CSR0);
1841 ural_disable_rf_tune(sc);
1844 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1845 : IEEE80211_DUR_SIFS;
1849 * Disable RF auto-tuning.
1852 ural_disable_rf_tune(struct ural_softc *sc)
1856 if (sc->rf_rev != RAL_RF_2523) {
1857 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1858 ural_rf_write(sc, RAL_RF1, tmp);
1861 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1862 ural_rf_write(sc, RAL_RF3, tmp);
1864 DPRINTFN(2, ("disabling RF autotune\n"));
1868 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1872 ural_enable_tsf_sync(struct ural_softc *sc)
1874 struct ieee80211com *ic = &sc->sc_ic;
1875 uint16_t logcwmin, preload, tmp;
1877 /* first, disable TSF synchronization */
1878 ural_write(sc, RAL_TXRX_CSR19, 0);
1880 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1881 ural_write(sc, RAL_TXRX_CSR18, tmp);
1883 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1884 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1885 tmp = logcwmin << 12 | preload;
1886 ural_write(sc, RAL_TXRX_CSR20, tmp);
1888 /* finally, enable TSF synchronization */
1889 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1890 if (ic->ic_opmode == IEEE80211_M_STA)
1891 tmp |= RAL_ENABLE_TSF_SYNC(1);
1893 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1894 ural_write(sc, RAL_TXRX_CSR19, tmp);
1896 DPRINTF(("enabling TSF synchronization\n"));
1900 ural_update_slot(struct ifnet *ifp)
1902 struct ural_softc *sc = ifp->if_softc;
1903 struct ieee80211com *ic = &sc->sc_ic;
1904 uint16_t slottime, sifs, eifs;
1906 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1909 * These settings may sound a bit inconsistent but this is what the
1910 * reference driver does.
1912 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1913 sifs = 16 - RAL_RXTX_TURNAROUND;
1916 sifs = 10 - RAL_RXTX_TURNAROUND;
1920 ural_write(sc, RAL_MAC_CSR10, slottime);
1921 ural_write(sc, RAL_MAC_CSR11, sifs);
1922 ural_write(sc, RAL_MAC_CSR12, eifs);
1926 ural_set_txpreamble(struct ural_softc *sc)
1930 tmp = ural_read(sc, RAL_TXRX_CSR10);
1932 tmp &= ~RAL_SHORT_PREAMBLE;
1933 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1934 tmp |= RAL_SHORT_PREAMBLE;
1936 ural_write(sc, RAL_TXRX_CSR10, tmp);
1940 ural_set_basicrates(struct ural_softc *sc)
1942 struct ieee80211com *ic = &sc->sc_ic;
1944 /* update basic rate set */
1945 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1946 /* 11b basic rates: 1, 2Mbps */
1947 ural_write(sc, RAL_TXRX_CSR11, 0x3);
1948 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1949 /* 11a basic rates: 6, 12, 24Mbps */
1950 ural_write(sc, RAL_TXRX_CSR11, 0x150);
1952 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1953 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
1958 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1962 tmp = bssid[0] | bssid[1] << 8;
1963 ural_write(sc, RAL_MAC_CSR5, tmp);
1965 tmp = bssid[2] | bssid[3] << 8;
1966 ural_write(sc, RAL_MAC_CSR6, tmp);
1968 tmp = bssid[4] | bssid[5] << 8;
1969 ural_write(sc, RAL_MAC_CSR7, tmp);
1971 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
1975 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1979 tmp = addr[0] | addr[1] << 8;
1980 ural_write(sc, RAL_MAC_CSR2, tmp);
1982 tmp = addr[2] | addr[3] << 8;
1983 ural_write(sc, RAL_MAC_CSR3, tmp);
1985 tmp = addr[4] | addr[5] << 8;
1986 ural_write(sc, RAL_MAC_CSR4, tmp);
1988 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
1992 ural_update_promisc(struct ural_softc *sc)
1994 struct ifnet *ifp = &sc->sc_ic.ic_if;
1997 tmp = ural_read(sc, RAL_TXRX_CSR2);
1999 tmp &= ~RAL_DROP_NOT_TO_ME;
2000 if (!(ifp->if_flags & IFF_PROMISC))
2001 tmp |= RAL_DROP_NOT_TO_ME;
2003 ural_write(sc, RAL_TXRX_CSR2, tmp);
2005 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2006 "entering" : "leaving"));
2010 ural_get_rf(int rev)
2013 case RAL_RF_2522: return "RT2522";
2014 case RAL_RF_2523: return "RT2523";
2015 case RAL_RF_2524: return "RT2524";
2016 case RAL_RF_2525: return "RT2525";
2017 case RAL_RF_2525E: return "RT2525e";
2018 case RAL_RF_2526: return "RT2526";
2019 case RAL_RF_5222: return "RT5222";
2020 default: return "unknown";
2025 ural_read_eeprom(struct ural_softc *sc)
2027 struct ieee80211com *ic = &sc->sc_ic;
2030 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
2032 sc->rf_rev = (val >> 11) & 0x7;
2033 sc->hw_radio = (val >> 10) & 0x1;
2034 sc->led_mode = (val >> 6) & 0x7;
2035 sc->rx_ant = (val >> 4) & 0x3;
2036 sc->tx_ant = (val >> 2) & 0x3;
2037 sc->nb_ant = val & 0x3;
2039 /* read MAC address */
2040 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
2042 /* read default values for BBP registers */
2043 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2045 /* read Tx power for all b/g channels */
2046 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
2050 ural_bbp_init(struct ural_softc *sc)
2052 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2055 /* wait for BBP to be ready */
2056 for (ntries = 0; ntries < 100; ntries++) {
2057 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
2061 if (ntries == 100) {
2062 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2066 /* initialize BBP registers to default values */
2067 for (i = 0; i < N(ural_def_bbp); i++)
2068 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
2071 /* initialize BBP registers to values stored in EEPROM */
2072 for (i = 0; i < 16; i++) {
2073 if (sc->bbp_prom[i].reg == 0xff)
2075 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2084 ural_set_txantenna(struct ural_softc *sc, int antenna)
2089 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
2092 else if (antenna == 2)
2095 tx |= RAL_BBP_DIVERSITY;
2097 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2098 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
2099 sc->rf_rev == RAL_RF_5222)
2100 tx |= RAL_BBP_FLIPIQ;
2102 ural_bbp_write(sc, RAL_BBP_TX, tx);
2104 /* update values in PHY_CSR5 and PHY_CSR6 */
2105 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
2106 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
2108 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
2109 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
2113 ural_set_rxantenna(struct ural_softc *sc, int antenna)
2117 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
2120 else if (antenna == 2)
2123 rx |= RAL_BBP_DIVERSITY;
2125 /* need to force no I/Q flip for RF 2525e and 2526 */
2126 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
2127 rx &= ~RAL_BBP_FLIPIQ;
2129 ural_bbp_write(sc, RAL_BBP_RX, rx);
2133 ural_init(void *priv)
2135 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2136 struct ural_softc *sc = priv;
2137 struct ieee80211com *ic = &sc->sc_ic;
2138 struct ifnet *ifp = &ic->ic_if;
2139 struct ural_rx_data *data;
2141 usbd_status usb_err;
2142 int i, ntries, error;
2144 ASSERT_SERIALIZED(ifp->if_serializer);
2148 lwkt_serialize_exit(ifp->if_serializer);
2149 ural_set_testmode(sc);
2150 ural_write(sc, 0x308, 0x00f0); /* XXX magic */
2151 lwkt_serialize_enter(ifp->if_serializer);
2156 lwkt_serialize_exit(ifp->if_serializer);
2158 /* initialize MAC registers to default values */
2159 for (i = 0; i < N(ural_def_mac); i++)
2160 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
2162 /* wait for BBP and RF to wake up (this can take a long time!) */
2163 for (ntries = 0; ntries < 100; ntries++) {
2164 tmp = ural_read(sc, RAL_MAC_CSR17);
2165 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
2166 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
2170 if (ntries == 100) {
2171 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
2172 device_get_nameunit(sc->sc_dev));
2178 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
2180 /* set basic rate set (will be updated later) */
2181 ural_write(sc, RAL_TXRX_CSR11, 0x15f);
2183 error = ural_bbp_init(sc);
2187 /* set default BSS channel */
2188 ural_set_chan(sc, ic->ic_curchan);
2190 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2191 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
2193 ural_set_txantenna(sc, sc->tx_ant);
2194 ural_set_rxantenna(sc, sc->rx_ant);
2196 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2197 ural_set_macaddr(sc, ic->ic_myaddr);
2200 * Allocate xfer for AMRR statistics requests.
2202 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
2203 if (sc->stats_xfer == NULL) {
2204 kprintf("%s: could not allocate AMRR xfer\n",
2205 device_get_nameunit(sc->sc_dev));
2211 * Open Tx and Rx USB bulk pipes.
2213 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2216 kprintf("%s: could not open Tx pipe: %s\n",
2217 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2222 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2225 kprintf("%s: could not open Rx pipe: %s\n",
2226 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
2232 * Allocate Tx and Rx xfer queues.
2234 error = ural_alloc_tx_list(sc);
2236 kprintf("%s: could not allocate Tx list\n",
2237 device_get_nameunit(sc->sc_dev));
2241 error = ural_alloc_rx_list(sc);
2243 kprintf("%s: could not allocate Rx list\n",
2244 device_get_nameunit(sc->sc_dev));
2249 * Start up the receive pipe.
2251 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2252 data = &sc->rx_data[i];
2254 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2255 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2256 usbd_transfer(data->xfer);
2260 tmp = RAL_DROP_PHY | RAL_DROP_CRC;
2261 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2262 tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION;
2263 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2264 tmp |= RAL_DROP_TODS;
2265 if (!(ifp->if_flags & IFF_PROMISC))
2266 tmp |= RAL_DROP_NOT_TO_ME;
2268 ural_write(sc, RAL_TXRX_CSR2, tmp);
2270 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2271 ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta));
2273 lwkt_serialize_enter(ifp->if_serializer);
2277 ifp->if_flags &= ~IFF_OACTIVE;
2278 ifp->if_flags |= IFF_RUNNING;
2280 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2281 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2282 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2284 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2293 ural_stop(struct ural_softc *sc)
2295 struct ieee80211com *ic = &sc->sc_ic;
2296 struct ifnet *ifp = &ic->ic_if;
2298 ASSERT_SERIALIZED(ifp->if_serializer);
2302 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2305 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2307 sc->sc_tx_timer = 0;
2310 lwkt_serialize_exit(ifp->if_serializer);
2313 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2315 /* reset ASIC and BBP (but won't reset MAC registers!) */
2316 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2317 ural_write(sc, RAL_MAC_CSR1, 0);
2319 if (sc->stats_xfer != NULL) {
2320 usbd_free_xfer(sc->stats_xfer);
2321 sc->stats_xfer = NULL;
2324 if (sc->sc_rx_pipeh != NULL) {
2325 usbd_abort_pipe(sc->sc_rx_pipeh);
2326 usbd_close_pipe(sc->sc_rx_pipeh);
2327 sc->sc_rx_pipeh = NULL;
2330 if (sc->sc_tx_pipeh != NULL) {
2331 usbd_abort_pipe(sc->sc_tx_pipeh);
2332 usbd_close_pipe(sc->sc_tx_pipeh);
2333 sc->sc_tx_pipeh = NULL;
2336 lwkt_serialize_enter(ifp->if_serializer);
2338 ural_free_rx_list(sc);
2339 ural_free_tx_list(sc);
2345 ural_stats_timeout(void *arg)
2347 struct ural_softc *sc = (struct ural_softc *)arg;
2348 usb_device_request_t req;
2356 * Asynchronously read statistic registers (cleared by read).
2358 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2359 req.bRequest = RAL_READ_MULTI_MAC;
2360 USETW(req.wValue, 0);
2361 USETW(req.wIndex, RAL_STA_CSR0);
2362 USETW(req.wLength, sizeof(sc->sta));
2364 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2365 USBD_DEFAULT_TIMEOUT, &req,
2366 sc->sta, sizeof(sc->sta), 0,
2368 usbd_transfer(sc->stats_xfer);
2374 ural_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2377 struct ural_softc *sc = (struct ural_softc *)priv;
2378 struct ifnet *ifp = &sc->sc_ic.ic_if;
2379 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2381 if (status != USBD_NORMAL_COMPLETION) {
2382 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2383 "cancelling automatic rate control\n");
2389 /* count TX retry-fail as Tx errors */
2390 ifp->if_oerrors += sc->sta[RAL_TX_PKT_FAIL];
2392 stats->stats_pkt_ok += sc->sta[RAL_TX_PKT_NO_RETRY] +
2393 sc->sta[RAL_TX_PKT_ONE_RETRY] +
2394 sc->sta[RAL_TX_PKT_MULTI_RETRY];
2396 stats->stats_pkt_err += sc->sta[RAL_TX_PKT_FAIL];
2398 stats->stats_pkt_noretry += sc->sta[RAL_TX_PKT_NO_RETRY];
2400 stats->stats_retries += sc->sta[RAL_TX_PKT_ONE_RETRY];
2403 * XXX Estimated average:
2404 * Actual number of retries for each packet should belong to
2405 * [2, sc->sc_tx_retries]
2407 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY] *
2408 ((2 + sc->sc_tx_retries) / 2);
2410 stats->stats_retries += sc->sta[RAL_TX_PKT_MULTI_RETRY];
2412 stats->stats_retries += sc->sta[RAL_TX_PKT_FAIL] * sc->sc_tx_retries;
2414 callout_reset(&sc->stats_ch, 4 * hz / 5, ural_stats_timeout, sc);
2420 ural_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2421 struct ieee80211_ratectl_stats *stats)
2423 struct ifnet *ifp = &ic->ic_if;
2424 struct ural_softc *sc = ifp->if_softc;
2426 ASSERT_SERIALIZED(ifp->if_serializer);
2428 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2429 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2433 ural_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2435 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2436 struct ieee80211_onoe_param *oparam;
2438 if (st->rc_st_param != NULL) {
2439 kfree(st->rc_st_param, M_DEVBUF);
2440 st->rc_st_param = NULL;
2444 case IEEE80211_RATECTL_ONOE:
2445 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2447 IEEE80211_ONOE_PARAM_SETUP(oparam);
2448 oparam->onoe_raise = 20;
2450 st->rc_st_param = oparam;
2452 case IEEE80211_RATECTL_NONE:
2453 /* This could only happen during detaching */
2456 panic("unknown rate control algo %u\n", nrc);
2460 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);