if_rum.c revision 1.6.4.4 1 1.1 joerg /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 1.6.4.4 itohy /* $NetBSD: if_rum.c,v 1.6.4.4 2007/06/18 13:42:31 itohy Exp $ */
3 1.1 joerg
4 1.1 joerg /*-
5 1.1 joerg * Copyright (c) 2005, 2006 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 joerg * Copyright (c) 2006 Niall O'Higgins <niallo (at) openbsd.org>
7 1.1 joerg *
8 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
9 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
10 1.1 joerg * copyright notice and this permission notice appear in all copies.
11 1.1 joerg *
12 1.1 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 joerg */
20 1.1 joerg
21 1.1 joerg /*-
22 1.1 joerg * Ralink Technology RT2501USB/RT2601USB chipset driver
23 1.1 joerg * http://www.ralinktech.com/
24 1.1 joerg */
25 1.1 joerg
26 1.2 xtraeme #include <sys/cdefs.h>
27 1.6.4.4 itohy __KERNEL_RCSID(0, "$NetBSD: if_rum.c,v 1.6.4.4 2007/06/18 13:42:31 itohy Exp $");
28 1.2 xtraeme
29 1.1 joerg #include "bpfilter.h"
30 1.1 joerg
31 1.1 joerg #include <sys/param.h>
32 1.1 joerg #include <sys/sockio.h>
33 1.1 joerg #include <sys/sysctl.h>
34 1.1 joerg #include <sys/mbuf.h>
35 1.1 joerg #include <sys/kernel.h>
36 1.1 joerg #include <sys/socket.h>
37 1.1 joerg #include <sys/systm.h>
38 1.1 joerg #include <sys/malloc.h>
39 1.1 joerg #include <sys/conf.h>
40 1.1 joerg #include <sys/device.h>
41 1.1 joerg
42 1.1 joerg #include <machine/bus.h>
43 1.1 joerg #include <machine/endian.h>
44 1.1 joerg #include <machine/intr.h>
45 1.1 joerg
46 1.1 joerg #if NBPFILTER > 0
47 1.1 joerg #include <net/bpf.h>
48 1.1 joerg #endif
49 1.1 joerg #include <net/if.h>
50 1.1 joerg #include <net/if_arp.h>
51 1.1 joerg #include <net/if_dl.h>
52 1.1 joerg #include <net/if_ether.h>
53 1.1 joerg #include <net/if_media.h>
54 1.1 joerg #include <net/if_types.h>
55 1.1 joerg
56 1.1 joerg #include <netinet/in.h>
57 1.1 joerg #include <netinet/in_systm.h>
58 1.1 joerg #include <netinet/in_var.h>
59 1.1 joerg #include <netinet/ip.h>
60 1.1 joerg
61 1.1 joerg #include <net80211/ieee80211_netbsd.h>
62 1.1 joerg #include <net80211/ieee80211_var.h>
63 1.1 joerg #include <net80211/ieee80211_amrr.h>
64 1.1 joerg #include <net80211/ieee80211_radiotap.h>
65 1.1 joerg
66 1.1 joerg #include <dev/firmload.h>
67 1.1 joerg
68 1.1 joerg #include <dev/usb/usb.h>
69 1.1 joerg #include <dev/usb/usbdi.h>
70 1.1 joerg #include <dev/usb/usbdi_util.h>
71 1.1 joerg #include <dev/usb/usbdevs.h>
72 1.6.4.2 itohy #include <dev/usb/usb_ethersubr.h>
73 1.1 joerg
74 1.1 joerg #include <dev/usb/if_rumreg.h>
75 1.1 joerg #include <dev/usb/if_rumvar.h>
76 1.1 joerg
77 1.1 joerg #ifdef USB_DEBUG
78 1.1 joerg #define RUM_DEBUG
79 1.1 joerg #endif
80 1.1 joerg
81 1.1 joerg #ifdef RUM_DEBUG
82 1.1 joerg #define DPRINTF(x) do { if (rum_debug) logprintf x; } while (0)
83 1.1 joerg #define DPRINTFN(n, x) do { if (rum_debug >= (n)) logprintf x; } while (0)
84 1.1 joerg int rum_debug = 0;
85 1.1 joerg #else
86 1.1 joerg #define DPRINTF(x)
87 1.1 joerg #define DPRINTFN(n, x)
88 1.1 joerg #endif
89 1.1 joerg
90 1.1 joerg /* various supported device vendors/products */
91 1.1 joerg static const struct usb_devno rum_devs[] = {
92 1.1 joerg { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573 },
93 1.1 joerg { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
94 1.1 joerg { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
95 1.1 joerg { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
96 1.1 joerg { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
97 1.1 joerg { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
98 1.1 joerg { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
99 1.1 joerg { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
100 1.1 joerg { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
101 1.1 joerg { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
102 1.1 joerg { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
103 1.1 joerg { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
104 1.1 joerg { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
105 1.4 elad { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
106 1.1 joerg { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
107 1.1 joerg { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
108 1.1 joerg { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
109 1.4 elad { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
110 1.1 joerg { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
111 1.1 joerg { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
112 1.1 joerg { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
113 1.1 joerg { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
114 1.3 christos { USB_VENDOR_RALINK_2, USB_PRODUCT_RALINK_2_RT2573 },
115 1.1 joerg { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
116 1.1 joerg { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
117 1.1 joerg { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
118 1.1 joerg { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
119 1.1 joerg };
120 1.1 joerg
121 1.1 joerg Static int rum_attachhook(void *);
122 1.6.4.2 itohy #if 0
123 1.1 joerg Static int rum_alloc_tx_list(struct rum_softc *);
124 1.6.4.2 itohy #endif
125 1.1 joerg Static void rum_free_tx_list(struct rum_softc *);
126 1.6.4.2 itohy #if 0
127 1.1 joerg Static int rum_alloc_rx_list(struct rum_softc *);
128 1.6.4.2 itohy #endif
129 1.1 joerg Static void rum_free_rx_list(struct rum_softc *);
130 1.1 joerg Static int rum_media_change(struct ifnet *);
131 1.1 joerg Static void rum_next_scan(void *);
132 1.1 joerg Static void rum_task(void *);
133 1.1 joerg Static int rum_newstate(struct ieee80211com *,
134 1.1 joerg enum ieee80211_state, int);
135 1.1 joerg Static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
136 1.1 joerg usbd_status);
137 1.1 joerg Static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
138 1.1 joerg usbd_status);
139 1.1 joerg #if NBPFILTER > 0
140 1.1 joerg Static uint8_t rum_rxrate(struct rum_rx_desc *);
141 1.1 joerg #endif
142 1.1 joerg Static int rum_ack_rate(struct ieee80211com *, int);
143 1.1 joerg Static uint16_t rum_txtime(int, int, uint32_t);
144 1.1 joerg Static uint8_t rum_plcp_signal(int);
145 1.1 joerg Static void rum_setup_tx_desc(struct rum_softc *,
146 1.1 joerg struct rum_tx_desc *, uint32_t, uint16_t, int,
147 1.1 joerg int);
148 1.1 joerg Static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
149 1.1 joerg struct ieee80211_node *);
150 1.1 joerg Static int rum_tx_data(struct rum_softc *, struct mbuf *,
151 1.1 joerg struct ieee80211_node *);
152 1.1 joerg Static void rum_start(struct ifnet *);
153 1.1 joerg Static void rum_watchdog(struct ifnet *);
154 1.6.4.3 itohy Static int rum_ioctl(struct ifnet *, u_long, usb_ioctlarg_t);
155 1.1 joerg Static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
156 1.1 joerg int);
157 1.1 joerg Static uint32_t rum_read(struct rum_softc *, uint16_t);
158 1.1 joerg Static void rum_read_multi(struct rum_softc *, uint16_t, void *,
159 1.1 joerg int);
160 1.1 joerg Static void rum_write(struct rum_softc *, uint16_t, uint32_t);
161 1.1 joerg Static void rum_write_multi(struct rum_softc *, uint16_t, void *,
162 1.1 joerg size_t);
163 1.1 joerg Static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
164 1.1 joerg Static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
165 1.1 joerg Static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
166 1.1 joerg Static void rum_select_antenna(struct rum_softc *);
167 1.1 joerg Static void rum_enable_mrr(struct rum_softc *);
168 1.1 joerg Static void rum_set_txpreamble(struct rum_softc *);
169 1.1 joerg Static void rum_set_basicrates(struct rum_softc *);
170 1.1 joerg Static void rum_select_band(struct rum_softc *,
171 1.1 joerg struct ieee80211_channel *);
172 1.1 joerg Static void rum_set_chan(struct rum_softc *,
173 1.1 joerg struct ieee80211_channel *);
174 1.1 joerg Static void rum_enable_tsf_sync(struct rum_softc *);
175 1.1 joerg Static void rum_update_slot(struct rum_softc *);
176 1.1 joerg Static void rum_set_bssid(struct rum_softc *, const uint8_t *);
177 1.1 joerg Static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
178 1.1 joerg Static void rum_update_promisc(struct rum_softc *);
179 1.1 joerg Static const char *rum_get_rf(int);
180 1.1 joerg Static void rum_read_eeprom(struct rum_softc *);
181 1.1 joerg Static int rum_bbp_init(struct rum_softc *);
182 1.1 joerg Static int rum_init(struct ifnet *);
183 1.1 joerg Static void rum_stop(struct ifnet *, int);
184 1.1 joerg Static int rum_load_microcode(struct rum_softc *, const u_char *,
185 1.1 joerg size_t);
186 1.1 joerg Static int rum_prepare_beacon(struct rum_softc *);
187 1.1 joerg Static void rum_amrr_start(struct rum_softc *,
188 1.1 joerg struct ieee80211_node *);
189 1.1 joerg Static void rum_amrr_timeout(void *);
190 1.1 joerg Static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
191 1.1 joerg usbd_status status);
192 1.1 joerg
193 1.1 joerg /*
194 1.1 joerg * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
195 1.1 joerg */
196 1.1 joerg static const struct ieee80211_rateset rum_rateset_11a =
197 1.1 joerg { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
198 1.1 joerg
199 1.1 joerg static const struct ieee80211_rateset rum_rateset_11b =
200 1.1 joerg { 4, { 2, 4, 11, 22 } };
201 1.1 joerg
202 1.1 joerg static const struct ieee80211_rateset rum_rateset_11g =
203 1.1 joerg { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
204 1.1 joerg
205 1.1 joerg static const struct {
206 1.1 joerg uint32_t reg;
207 1.1 joerg uint32_t val;
208 1.1 joerg } rum_def_mac[] = {
209 1.1 joerg RT2573_DEF_MAC
210 1.1 joerg };
211 1.1 joerg
212 1.1 joerg static const struct {
213 1.1 joerg uint8_t reg;
214 1.1 joerg uint8_t val;
215 1.1 joerg } rum_def_bbp[] = {
216 1.1 joerg RT2573_DEF_BBP
217 1.1 joerg };
218 1.1 joerg
219 1.1 joerg static const struct rfprog {
220 1.1 joerg uint8_t chan;
221 1.1 joerg uint32_t r1, r2, r3, r4;
222 1.1 joerg } rum_rf5226[] = {
223 1.1 joerg RT2573_RF5226
224 1.1 joerg }, rum_rf5225[] = {
225 1.1 joerg RT2573_RF5225
226 1.1 joerg };
227 1.1 joerg
228 1.1 joerg USB_DECLARE_DRIVER(rum);
229 1.1 joerg
230 1.1 joerg USB_MATCH(rum)
231 1.1 joerg {
232 1.1 joerg USB_MATCH_START(rum, uaa);
233 1.1 joerg
234 1.6.4.4 itohy #ifndef USB_USE_IFATTACH
235 1.1 joerg if (uaa->iface != NULL)
236 1.1 joerg return UMATCH_NONE;
237 1.6.4.4 itohy #endif /* USB_USE_IFATTACH */
238 1.1 joerg
239 1.1 joerg return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
240 1.1 joerg UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
241 1.1 joerg }
242 1.1 joerg
243 1.1 joerg Static int
244 1.1 joerg rum_attachhook(void *xsc)
245 1.1 joerg {
246 1.1 joerg struct rum_softc *sc = xsc;
247 1.1 joerg firmware_handle_t fwh;
248 1.1 joerg const char *name = "rum-rt2573";
249 1.1 joerg u_char *ucode;
250 1.1 joerg size_t size;
251 1.1 joerg int error;
252 1.1 joerg
253 1.1 joerg if ((error = firmware_open("rum", name, &fwh)) != 0) {
254 1.1 joerg printf("%s: failed loadfirmware of file %s (error %d)\n",
255 1.1 joerg USBDEVNAME(sc->sc_dev), name, error);
256 1.1 joerg return error;
257 1.1 joerg }
258 1.1 joerg size = firmware_get_size(fwh);
259 1.1 joerg ucode = firmware_malloc(size);
260 1.1 joerg if (ucode == NULL) {
261 1.1 joerg printf("%s: failed to allocate firmware memory\n",
262 1.1 joerg USBDEVNAME(sc->sc_dev));
263 1.1 joerg firmware_close(fwh);
264 1.1 joerg return ENOMEM;;
265 1.1 joerg }
266 1.1 joerg error = firmware_read(fwh, 0, ucode, size);
267 1.1 joerg firmware_close(fwh);
268 1.1 joerg if (error != 0) {
269 1.1 joerg printf("%s: failed to read firmware (error %d)\n",
270 1.1 joerg USBDEVNAME(sc->sc_dev), error);
271 1.1 joerg firmware_free(ucode, 0);
272 1.1 joerg return error;
273 1.1 joerg }
274 1.1 joerg
275 1.1 joerg if (rum_load_microcode(sc, ucode, size) != 0) {
276 1.1 joerg printf("%s: could not load 8051 microcode\n",
277 1.1 joerg USBDEVNAME(sc->sc_dev));
278 1.1 joerg firmware_free(ucode, 0);
279 1.1 joerg return ENXIO;
280 1.1 joerg }
281 1.1 joerg
282 1.1 joerg firmware_free(ucode, 0);
283 1.1 joerg sc->sc_flags |= RT2573_FWLOADED;
284 1.1 joerg
285 1.1 joerg return 0;
286 1.1 joerg }
287 1.1 joerg
288 1.1 joerg USB_ATTACH(rum)
289 1.1 joerg {
290 1.1 joerg USB_ATTACH_START(rum, sc, uaa);
291 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
292 1.1 joerg struct ifnet *ifp = &sc->sc_if;
293 1.1 joerg usb_interface_descriptor_t *id;
294 1.1 joerg usb_endpoint_descriptor_t *ed;
295 1.1 joerg usbd_status error;
296 1.1 joerg char *devinfop;
297 1.1 joerg int i, ntries;
298 1.1 joerg uint32_t tmp;
299 1.1 joerg
300 1.1 joerg sc->sc_udev = uaa->device;
301 1.1 joerg sc->sc_flags = 0;
302 1.1 joerg
303 1.1 joerg devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
304 1.1 joerg USB_ATTACH_SETUP;
305 1.1 joerg printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
306 1.1 joerg usbd_devinfo_free(devinfop);
307 1.1 joerg
308 1.1 joerg if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
309 1.1 joerg printf("%s: could not set configuration no\n",
310 1.1 joerg USBDEVNAME(sc->sc_dev));
311 1.1 joerg USB_ATTACH_ERROR_RETURN;
312 1.1 joerg }
313 1.1 joerg
314 1.1 joerg /* get the first interface handle */
315 1.1 joerg error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
316 1.1 joerg &sc->sc_iface);
317 1.1 joerg if (error != 0) {
318 1.1 joerg printf("%s: could not get interface handle\n",
319 1.1 joerg USBDEVNAME(sc->sc_dev));
320 1.1 joerg USB_ATTACH_ERROR_RETURN;
321 1.1 joerg }
322 1.1 joerg
323 1.1 joerg /*
324 1.1 joerg * Find endpoints.
325 1.1 joerg */
326 1.1 joerg id = usbd_get_interface_descriptor(sc->sc_iface);
327 1.1 joerg
328 1.1 joerg sc->sc_rx_no = sc->sc_tx_no = -1;
329 1.1 joerg for (i = 0; i < id->bNumEndpoints; i++) {
330 1.1 joerg ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
331 1.1 joerg if (ed == NULL) {
332 1.1 joerg printf("%s: no endpoint descriptor for iface %d\n",
333 1.1 joerg USBDEVNAME(sc->sc_dev), i);
334 1.1 joerg USB_ATTACH_ERROR_RETURN;
335 1.1 joerg }
336 1.1 joerg
337 1.1 joerg if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
338 1.1 joerg UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
339 1.1 joerg sc->sc_rx_no = ed->bEndpointAddress;
340 1.1 joerg else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
341 1.1 joerg UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
342 1.1 joerg sc->sc_tx_no = ed->bEndpointAddress;
343 1.1 joerg }
344 1.1 joerg if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
345 1.1 joerg printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
346 1.1 joerg USB_ATTACH_ERROR_RETURN;
347 1.1 joerg }
348 1.1 joerg
349 1.1 joerg usb_init_task(&sc->sc_task, rum_task, sc);
350 1.1 joerg callout_init(&sc->scan_ch);
351 1.1 joerg
352 1.1 joerg sc->amrr.amrr_min_success_threshold = 1;
353 1.1 joerg sc->amrr.amrr_max_success_threshold = 10;
354 1.1 joerg callout_init(&sc->amrr_ch);
355 1.1 joerg
356 1.1 joerg /* retrieve RT2573 rev. no */
357 1.6.4.1 itohy tmp = 0; /* XXX shut up warning */
358 1.1 joerg for (ntries = 0; ntries < 1000; ntries++) {
359 1.1 joerg if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
360 1.1 joerg break;
361 1.1 joerg DELAY(1000);
362 1.1 joerg }
363 1.1 joerg if (ntries == 1000) {
364 1.1 joerg printf("%s: timeout waiting for chip to settle\n",
365 1.1 joerg USBDEVNAME(sc->sc_dev));
366 1.1 joerg USB_ATTACH_ERROR_RETURN;
367 1.1 joerg }
368 1.1 joerg
369 1.1 joerg /* retrieve MAC address and various other things from EEPROM */
370 1.1 joerg rum_read_eeprom(sc);
371 1.1 joerg
372 1.1 joerg printf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
373 1.1 joerg USBDEVNAME(sc->sc_dev), sc->macbbp_rev, tmp,
374 1.1 joerg rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr));
375 1.1 joerg
376 1.1 joerg ic->ic_ifp = ifp;
377 1.1 joerg ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
378 1.1 joerg ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
379 1.1 joerg ic->ic_state = IEEE80211_S_INIT;
380 1.1 joerg
381 1.1 joerg /* set device capabilities */
382 1.1 joerg ic->ic_caps =
383 1.1 joerg IEEE80211_C_IBSS | /* IBSS mode supported */
384 1.1 joerg IEEE80211_C_MONITOR | /* monitor mode supported */
385 1.1 joerg IEEE80211_C_HOSTAP | /* HostAp mode supported */
386 1.1 joerg IEEE80211_C_TXPMGT | /* tx power management */
387 1.1 joerg IEEE80211_C_SHPREAMBLE | /* short preamble supported */
388 1.1 joerg IEEE80211_C_SHSLOT | /* short slot time supported */
389 1.1 joerg IEEE80211_C_WPA; /* 802.11i */
390 1.1 joerg
391 1.1 joerg if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
392 1.1 joerg /* set supported .11a rates */
393 1.1 joerg ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
394 1.1 joerg
395 1.1 joerg /* set supported .11a channels */
396 1.1 joerg for (i = 34; i <= 46; i += 4) {
397 1.1 joerg ic->ic_channels[i].ic_freq =
398 1.1 joerg ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
399 1.1 joerg ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
400 1.1 joerg }
401 1.1 joerg for (i = 36; i <= 64; i += 4) {
402 1.1 joerg ic->ic_channels[i].ic_freq =
403 1.1 joerg ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
404 1.1 joerg ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
405 1.1 joerg }
406 1.1 joerg for (i = 100; i <= 140; i += 4) {
407 1.1 joerg ic->ic_channels[i].ic_freq =
408 1.1 joerg ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
409 1.1 joerg ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
410 1.1 joerg }
411 1.1 joerg for (i = 149; i <= 165; i += 4) {
412 1.1 joerg ic->ic_channels[i].ic_freq =
413 1.1 joerg ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
414 1.1 joerg ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
415 1.1 joerg }
416 1.1 joerg }
417 1.1 joerg
418 1.1 joerg /* set supported .11b and .11g rates */
419 1.1 joerg ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
420 1.1 joerg ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
421 1.1 joerg
422 1.1 joerg /* set supported .11b and .11g channels (1 through 14) */
423 1.1 joerg for (i = 1; i <= 14; i++) {
424 1.1 joerg ic->ic_channels[i].ic_freq =
425 1.1 joerg ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
426 1.1 joerg ic->ic_channels[i].ic_flags =
427 1.1 joerg IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
428 1.1 joerg IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
429 1.1 joerg }
430 1.1 joerg
431 1.1 joerg ifp->if_softc = sc;
432 1.1 joerg ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
433 1.1 joerg ifp->if_init = rum_init;
434 1.1 joerg ifp->if_ioctl = rum_ioctl;
435 1.1 joerg ifp->if_start = rum_start;
436 1.1 joerg ifp->if_watchdog = rum_watchdog;
437 1.1 joerg IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
438 1.1 joerg IFQ_SET_READY(&ifp->if_snd);
439 1.1 joerg memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
440 1.1 joerg
441 1.1 joerg if_attach(ifp);
442 1.1 joerg ieee80211_ifattach(ic);
443 1.1 joerg
444 1.1 joerg /* override state transition machine */
445 1.1 joerg sc->sc_newstate = ic->ic_newstate;
446 1.1 joerg ic->ic_newstate = rum_newstate;
447 1.1 joerg ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
448 1.1 joerg
449 1.1 joerg #if NBPFILTER > 0
450 1.1 joerg bpfattach2(ifp, DLT_IEEE802_11_RADIO,
451 1.1 joerg sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, &sc->sc_drvbpf);
452 1.1 joerg
453 1.1 joerg sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
454 1.1 joerg sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
455 1.1 joerg sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
456 1.1 joerg
457 1.1 joerg sc->sc_txtap_len = sizeof sc->sc_txtapu;
458 1.1 joerg sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
459 1.1 joerg sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
460 1.1 joerg #endif
461 1.1 joerg
462 1.1 joerg ieee80211_announce(ic);
463 1.1 joerg
464 1.1 joerg usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
465 1.1 joerg USBDEV(sc->sc_dev));
466 1.1 joerg
467 1.1 joerg USB_ATTACH_SUCCESS_RETURN;
468 1.1 joerg }
469 1.1 joerg
470 1.1 joerg USB_DETACH(rum)
471 1.1 joerg {
472 1.1 joerg USB_DETACH_START(rum, sc);
473 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
474 1.1 joerg struct ifnet *ifp = &sc->sc_if;
475 1.1 joerg int s;
476 1.1 joerg
477 1.1 joerg s = splusb();
478 1.1 joerg
479 1.1 joerg rum_stop(ifp, 1);
480 1.1 joerg usb_rem_task(sc->sc_udev, &sc->sc_task);
481 1.1 joerg callout_stop(&sc->scan_ch);
482 1.1 joerg callout_stop(&sc->amrr_ch);
483 1.1 joerg
484 1.6.4.2 itohy if (sc->sc_rx_pipeh != NULL)
485 1.6.4.2 itohy usbd_abort_pipe(sc->sc_rx_pipeh);
486 1.6.4.2 itohy
487 1.6.4.2 itohy if (sc->sc_tx_pipeh != NULL)
488 1.6.4.2 itohy usbd_abort_pipe(sc->sc_tx_pipeh);
489 1.6.4.2 itohy
490 1.6.4.2 itohy rum_free_rx_list(sc);
491 1.6.4.2 itohy rum_free_tx_list(sc);
492 1.6.4.2 itohy
493 1.1 joerg if (sc->amrr_xfer != NULL) {
494 1.1 joerg usbd_free_xfer(sc->amrr_xfer);
495 1.1 joerg sc->amrr_xfer = NULL;
496 1.1 joerg }
497 1.1 joerg
498 1.6.4.2 itohy if (sc->sc_rx_pipeh != NULL)
499 1.1 joerg usbd_close_pipe(sc->sc_rx_pipeh);
500 1.1 joerg
501 1.6.4.2 itohy if (sc->sc_tx_pipeh != NULL)
502 1.1 joerg usbd_close_pipe(sc->sc_tx_pipeh);
503 1.1 joerg
504 1.1 joerg #if NBPFILTER > 0
505 1.1 joerg bpfdetach(ifp);
506 1.1 joerg #endif
507 1.1 joerg ieee80211_ifdetach(ic); /* free all nodes */
508 1.1 joerg if_detach(ifp);
509 1.1 joerg
510 1.1 joerg splx(s);
511 1.1 joerg
512 1.1 joerg usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
513 1.1 joerg USBDEV(sc->sc_dev));
514 1.1 joerg
515 1.1 joerg return 0;
516 1.1 joerg }
517 1.1 joerg
518 1.1 joerg Static void
519 1.1 joerg rum_free_tx_list(struct rum_softc *sc)
520 1.1 joerg {
521 1.1 joerg int i;
522 1.1 joerg
523 1.6.4.2 itohy usb_ether_tx_list_free(sc->tx_data, RT2573_TX_LIST_COUNT);
524 1.1 joerg
525 1.6.4.2 itohy for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
526 1.6.4.2 itohy if (sc->tx_ni[i] != NULL) {
527 1.6.4.2 itohy ieee80211_free_node(sc->tx_ni[i]);
528 1.6.4.2 itohy sc->tx_ni[i] = NULL;
529 1.1 joerg }
530 1.1 joerg }
531 1.1 joerg }
532 1.1 joerg
533 1.1 joerg Static void
534 1.1 joerg rum_free_rx_list(struct rum_softc *sc)
535 1.1 joerg {
536 1.1 joerg
537 1.6.4.2 itohy usb_ether_rx_list_free(sc->rx_data, RT2573_RX_LIST_COUNT);
538 1.1 joerg }
539 1.1 joerg
540 1.1 joerg Static int
541 1.1 joerg rum_media_change(struct ifnet *ifp)
542 1.1 joerg {
543 1.1 joerg int error;
544 1.1 joerg
545 1.1 joerg error = ieee80211_media_change(ifp);
546 1.1 joerg if (error != ENETRESET)
547 1.1 joerg return error;
548 1.1 joerg
549 1.1 joerg if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
550 1.1 joerg rum_init(ifp);
551 1.1 joerg
552 1.1 joerg return 0;
553 1.1 joerg }
554 1.1 joerg
555 1.1 joerg /*
556 1.1 joerg * This function is called periodically (every 200ms) during scanning to
557 1.1 joerg * switch from one channel to another.
558 1.1 joerg */
559 1.1 joerg Static void
560 1.1 joerg rum_next_scan(void *arg)
561 1.1 joerg {
562 1.1 joerg struct rum_softc *sc = arg;
563 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
564 1.1 joerg
565 1.1 joerg if (ic->ic_state == IEEE80211_S_SCAN)
566 1.1 joerg ieee80211_next_scan(ic);
567 1.1 joerg }
568 1.1 joerg
569 1.1 joerg Static void
570 1.1 joerg rum_task(void *arg)
571 1.1 joerg {
572 1.1 joerg struct rum_softc *sc = arg;
573 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
574 1.1 joerg enum ieee80211_state ostate;
575 1.1 joerg struct ieee80211_node *ni;
576 1.1 joerg uint32_t tmp;
577 1.1 joerg
578 1.1 joerg ostate = ic->ic_state;
579 1.1 joerg
580 1.1 joerg switch (sc->sc_state) {
581 1.1 joerg case IEEE80211_S_INIT:
582 1.1 joerg if (ostate == IEEE80211_S_RUN) {
583 1.1 joerg /* abort TSF synchronization */
584 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR9);
585 1.1 joerg rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
586 1.1 joerg }
587 1.1 joerg break;
588 1.1 joerg
589 1.1 joerg case IEEE80211_S_SCAN:
590 1.1 joerg rum_set_chan(sc, ic->ic_curchan);
591 1.1 joerg callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
592 1.1 joerg break;
593 1.1 joerg
594 1.1 joerg case IEEE80211_S_AUTH:
595 1.1 joerg rum_set_chan(sc, ic->ic_curchan);
596 1.1 joerg break;
597 1.1 joerg
598 1.1 joerg case IEEE80211_S_ASSOC:
599 1.1 joerg rum_set_chan(sc, ic->ic_curchan);
600 1.1 joerg break;
601 1.1 joerg
602 1.1 joerg case IEEE80211_S_RUN:
603 1.1 joerg rum_set_chan(sc, ic->ic_curchan);
604 1.1 joerg
605 1.1 joerg ni = ic->ic_bss;
606 1.1 joerg
607 1.1 joerg if (ic->ic_opmode != IEEE80211_M_MONITOR) {
608 1.1 joerg rum_update_slot(sc);
609 1.1 joerg rum_enable_mrr(sc);
610 1.1 joerg rum_set_txpreamble(sc);
611 1.1 joerg rum_set_basicrates(sc);
612 1.1 joerg rum_set_bssid(sc, ni->ni_bssid);
613 1.1 joerg }
614 1.1 joerg
615 1.1 joerg if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
616 1.1 joerg ic->ic_opmode == IEEE80211_M_IBSS)
617 1.1 joerg rum_prepare_beacon(sc);
618 1.1 joerg
619 1.1 joerg if (ic->ic_opmode != IEEE80211_M_MONITOR)
620 1.1 joerg rum_enable_tsf_sync(sc);
621 1.1 joerg
622 1.1 joerg /* enable automatic rate adaptation in STA mode */
623 1.1 joerg if (ic->ic_opmode == IEEE80211_M_STA &&
624 1.1 joerg ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
625 1.1 joerg rum_amrr_start(sc, ni);
626 1.1 joerg
627 1.1 joerg break;
628 1.1 joerg }
629 1.1 joerg
630 1.1 joerg sc->sc_newstate(ic, sc->sc_state, -1);
631 1.1 joerg }
632 1.1 joerg
633 1.1 joerg Static int
634 1.1 joerg rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
635 1.1 joerg {
636 1.1 joerg struct rum_softc *sc = ic->ic_ifp->if_softc;
637 1.1 joerg
638 1.1 joerg usb_rem_task(sc->sc_udev, &sc->sc_task);
639 1.1 joerg callout_stop(&sc->scan_ch);
640 1.1 joerg callout_stop(&sc->amrr_ch);
641 1.1 joerg
642 1.1 joerg /* do it in a process context */
643 1.1 joerg sc->sc_state = nstate;
644 1.1 joerg usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
645 1.1 joerg
646 1.1 joerg return 0;
647 1.1 joerg }
648 1.1 joerg
649 1.1 joerg /* quickly determine if a given rate is CCK or OFDM */
650 1.1 joerg #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
651 1.1 joerg
652 1.1 joerg #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
653 1.1 joerg #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
654 1.1 joerg
655 1.1 joerg Static void
656 1.1 joerg rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
657 1.1 joerg {
658 1.6.4.2 itohy struct ue_chain *data = priv;
659 1.6.4.2 itohy struct rum_softc *sc = (void *)data->ue_dev;
660 1.1 joerg struct ifnet *ifp = &sc->sc_if;
661 1.1 joerg int s;
662 1.1 joerg
663 1.6.4.2 itohy usbd_unmap_buffer(xfer);
664 1.6.4.2 itohy
665 1.1 joerg if (status != USBD_NORMAL_COMPLETION) {
666 1.1 joerg if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
667 1.1 joerg return;
668 1.1 joerg
669 1.1 joerg printf("%s: could not transmit buffer: %s\n",
670 1.1 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(status));
671 1.1 joerg
672 1.1 joerg if (status == USBD_STALLED)
673 1.1 joerg usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
674 1.1 joerg
675 1.1 joerg ifp->if_oerrors++;
676 1.1 joerg return;
677 1.1 joerg }
678 1.1 joerg
679 1.1 joerg s = splnet();
680 1.1 joerg
681 1.6.4.2 itohy m_freem(data->ue_mbuf);
682 1.6.4.2 itohy data->ue_mbuf = NULL;
683 1.6.4.2 itohy ieee80211_free_node(sc->tx_ni[data->ue_idx]);
684 1.6.4.2 itohy sc->tx_ni[data->ue_idx] = NULL;
685 1.1 joerg
686 1.1 joerg sc->tx_queued--;
687 1.1 joerg ifp->if_opackets++;
688 1.1 joerg
689 1.1 joerg DPRINTFN(10, ("tx done\n"));
690 1.1 joerg
691 1.1 joerg sc->sc_tx_timer = 0;
692 1.1 joerg ifp->if_flags &= ~IFF_OACTIVE;
693 1.1 joerg rum_start(ifp);
694 1.1 joerg
695 1.1 joerg splx(s);
696 1.1 joerg }
697 1.1 joerg
698 1.1 joerg Static void
699 1.1 joerg rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
700 1.1 joerg {
701 1.6.4.2 itohy struct ue_chain *data = priv;
702 1.6.4.2 itohy struct rum_softc *sc = (void *)data->ue_dev;
703 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
704 1.1 joerg struct ifnet *ifp = &sc->sc_if;
705 1.1 joerg struct rum_rx_desc *desc;
706 1.1 joerg struct ieee80211_frame *wh;
707 1.1 joerg struct ieee80211_node *ni;
708 1.6.4.2 itohy struct mbuf *m;
709 1.1 joerg int s, len;
710 1.1 joerg
711 1.6.4.2 itohy usbd_unmap_buffer(xfer);
712 1.6.4.2 itohy
713 1.1 joerg if (status != USBD_NORMAL_COMPLETION) {
714 1.1 joerg if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
715 1.1 joerg return;
716 1.1 joerg
717 1.1 joerg if (status == USBD_STALLED)
718 1.1 joerg usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
719 1.1 joerg goto skip;
720 1.1 joerg }
721 1.1 joerg
722 1.1 joerg usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
723 1.1 joerg
724 1.1 joerg if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
725 1.1 joerg DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
726 1.1 joerg len));
727 1.1 joerg ifp->if_ierrors++;
728 1.1 joerg goto skip;
729 1.1 joerg }
730 1.1 joerg
731 1.6.4.2 itohy m = data->ue_mbuf;
732 1.6.4.2 itohy desc = mtod(m, struct rum_rx_desc *);
733 1.1 joerg
734 1.6.4.2 itohy if (UGETDW((u_int8_t *)&desc->flags) & RT2573_RX_CRC_ERROR) {
735 1.1 joerg /*
736 1.1 joerg * This should not happen since we did not request to receive
737 1.1 joerg * those frames when we filled RT2573_TXRX_CSR0.
738 1.1 joerg */
739 1.1 joerg DPRINTFN(5, ("CRC error\n"));
740 1.1 joerg ifp->if_ierrors++;
741 1.1 joerg goto skip;
742 1.1 joerg }
743 1.1 joerg
744 1.6.4.2 itohy /*
745 1.6.4.2 itohy * Allocate new mbuf cluster for the next transfer.
746 1.6.4.2 itohy * If that failed, discard current packet and recycle the mbuf.
747 1.6.4.2 itohy */
748 1.6.4.2 itohy if ((data->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
749 1.6.4.2 itohy printf("%s: no memory for rx list -- packet dropped!\n",
750 1.1 joerg USBDEVNAME(sc->sc_dev));
751 1.1 joerg ifp->if_ierrors++;
752 1.6.4.2 itohy data->ue_mbuf = usb_ether_newbuf(m);
753 1.1 joerg goto skip;
754 1.1 joerg }
755 1.1 joerg
756 1.1 joerg /* finalize mbuf */
757 1.1 joerg m->m_pkthdr.rcvif = ifp;
758 1.6.4.3 itohy m->m_data = (void *)(desc + 1);
759 1.6.4.2 itohy m->m_pkthdr.len = m->m_len = UGETW((u_int8_t *)&desc->flags + 2);
760 1.1 joerg
761 1.1 joerg s = splnet();
762 1.1 joerg
763 1.1 joerg #if NBPFILTER > 0
764 1.1 joerg if (sc->sc_drvbpf != NULL) {
765 1.1 joerg struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
766 1.1 joerg
767 1.1 joerg tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
768 1.1 joerg tap->wr_rate = rum_rxrate(desc);
769 1.1 joerg tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
770 1.1 joerg tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
771 1.1 joerg tap->wr_antenna = sc->rx_ant;
772 1.1 joerg tap->wr_antsignal = desc->rssi;
773 1.1 joerg
774 1.1 joerg bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
775 1.1 joerg }
776 1.1 joerg #endif
777 1.1 joerg
778 1.1 joerg wh = mtod(m, struct ieee80211_frame *);
779 1.1 joerg ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
780 1.1 joerg
781 1.1 joerg /* send the frame to the 802.11 layer */
782 1.1 joerg ieee80211_input(ic, m, ni, desc->rssi, 0);
783 1.1 joerg
784 1.1 joerg /* node is no longer needed */
785 1.1 joerg ieee80211_free_node(ni);
786 1.1 joerg
787 1.1 joerg splx(s);
788 1.1 joerg
789 1.1 joerg DPRINTFN(15, ("rx done\n"));
790 1.1 joerg
791 1.1 joerg skip: /* setup a new transfer */
792 1.6.4.2 itohy (void)usbd_map_buffer_mbuf(xfer, data->ue_mbuf);
793 1.6.4.2 itohy usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, NULL /* XXX buf */, MCLBYTES,
794 1.1 joerg USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
795 1.1 joerg usbd_transfer(xfer);
796 1.1 joerg }
797 1.1 joerg
798 1.1 joerg /*
799 1.1 joerg * This function is only used by the Rx radiotap code. It returns the rate at
800 1.1 joerg * which a given frame was received.
801 1.1 joerg */
802 1.1 joerg #if NBPFILTER > 0
803 1.1 joerg Static uint8_t
804 1.1 joerg rum_rxrate(struct rum_rx_desc *desc)
805 1.1 joerg {
806 1.1 joerg if (le32toh(desc->flags) & RT2573_RX_OFDM) {
807 1.1 joerg /* reverse function of rum_plcp_signal */
808 1.1 joerg switch (desc->rate) {
809 1.1 joerg case 0xb: return 12;
810 1.1 joerg case 0xf: return 18;
811 1.1 joerg case 0xa: return 24;
812 1.1 joerg case 0xe: return 36;
813 1.1 joerg case 0x9: return 48;
814 1.1 joerg case 0xd: return 72;
815 1.1 joerg case 0x8: return 96;
816 1.1 joerg case 0xc: return 108;
817 1.1 joerg }
818 1.1 joerg } else {
819 1.1 joerg if (desc->rate == 10)
820 1.1 joerg return 2;
821 1.1 joerg if (desc->rate == 20)
822 1.1 joerg return 4;
823 1.1 joerg if (desc->rate == 55)
824 1.1 joerg return 11;
825 1.1 joerg if (desc->rate == 110)
826 1.1 joerg return 22;
827 1.1 joerg }
828 1.1 joerg return 2; /* should not get there */
829 1.1 joerg }
830 1.1 joerg #endif
831 1.1 joerg
832 1.1 joerg /*
833 1.1 joerg * Return the expected ack rate for a frame transmitted at rate `rate'.
834 1.1 joerg * XXX: this should depend on the destination node basic rate set.
835 1.1 joerg */
836 1.1 joerg Static int
837 1.1 joerg rum_ack_rate(struct ieee80211com *ic, int rate)
838 1.1 joerg {
839 1.1 joerg switch (rate) {
840 1.1 joerg /* CCK rates */
841 1.1 joerg case 2:
842 1.1 joerg return 2;
843 1.1 joerg case 4:
844 1.1 joerg case 11:
845 1.1 joerg case 22:
846 1.1 joerg return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
847 1.1 joerg
848 1.1 joerg /* OFDM rates */
849 1.1 joerg case 12:
850 1.1 joerg case 18:
851 1.1 joerg return 12;
852 1.1 joerg case 24:
853 1.1 joerg case 36:
854 1.1 joerg return 24;
855 1.1 joerg case 48:
856 1.1 joerg case 72:
857 1.1 joerg case 96:
858 1.1 joerg case 108:
859 1.1 joerg return 48;
860 1.1 joerg }
861 1.1 joerg
862 1.1 joerg /* default to 1Mbps */
863 1.1 joerg return 2;
864 1.1 joerg }
865 1.1 joerg
866 1.1 joerg /*
867 1.1 joerg * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
868 1.1 joerg * The function automatically determines the operating mode depending on the
869 1.1 joerg * given rate. `flags' indicates whether short preamble is in use or not.
870 1.1 joerg */
871 1.1 joerg Static uint16_t
872 1.1 joerg rum_txtime(int len, int rate, uint32_t flags)
873 1.1 joerg {
874 1.1 joerg uint16_t txtime;
875 1.1 joerg
876 1.1 joerg if (RUM_RATE_IS_OFDM(rate)) {
877 1.1 joerg /* IEEE Std 802.11a-1999, pp. 37 */
878 1.1 joerg txtime = (8 + 4 * len + 3 + rate - 1) / rate;
879 1.1 joerg txtime = 16 + 4 + 4 * txtime + 6;
880 1.1 joerg } else {
881 1.1 joerg /* IEEE Std 802.11b-1999, pp. 28 */
882 1.1 joerg txtime = (16 * len + rate - 1) / rate;
883 1.1 joerg if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
884 1.1 joerg txtime += 72 + 24;
885 1.1 joerg else
886 1.1 joerg txtime += 144 + 48;
887 1.1 joerg }
888 1.1 joerg return txtime;
889 1.1 joerg }
890 1.1 joerg
891 1.1 joerg Static uint8_t
892 1.1 joerg rum_plcp_signal(int rate)
893 1.1 joerg {
894 1.1 joerg switch (rate) {
895 1.1 joerg /* CCK rates (returned values are device-dependent) */
896 1.1 joerg case 2: return 0x0;
897 1.1 joerg case 4: return 0x1;
898 1.1 joerg case 11: return 0x2;
899 1.1 joerg case 22: return 0x3;
900 1.1 joerg
901 1.1 joerg /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
902 1.1 joerg case 12: return 0xb;
903 1.1 joerg case 18: return 0xf;
904 1.1 joerg case 24: return 0xa;
905 1.1 joerg case 36: return 0xe;
906 1.1 joerg case 48: return 0x9;
907 1.1 joerg case 72: return 0xd;
908 1.1 joerg case 96: return 0x8;
909 1.1 joerg case 108: return 0xc;
910 1.1 joerg
911 1.1 joerg /* unsupported rates (should not get there) */
912 1.1 joerg default: return 0xff;
913 1.1 joerg }
914 1.1 joerg }
915 1.1 joerg
916 1.1 joerg Static void
917 1.1 joerg rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
918 1.1 joerg uint32_t flags, uint16_t xflags, int len, int rate)
919 1.1 joerg {
920 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
921 1.1 joerg uint16_t plcp_length;
922 1.1 joerg int remainder;
923 1.1 joerg
924 1.1 joerg desc->flags = htole32(flags);
925 1.1 joerg desc->flags |= htole32(RT2573_TX_VALID);
926 1.1 joerg desc->flags |= htole32(len << 16);
927 1.1 joerg
928 1.1 joerg desc->xflags = htole16(xflags);
929 1.1 joerg
930 1.1 joerg desc->wme = htole16(
931 1.1 joerg RT2573_QID(0) |
932 1.1 joerg RT2573_AIFSN(2) |
933 1.1 joerg RT2573_LOGCWMIN(4) |
934 1.1 joerg RT2573_LOGCWMAX(10));
935 1.1 joerg
936 1.1 joerg /* setup PLCP fields */
937 1.1 joerg desc->plcp_signal = rum_plcp_signal(rate);
938 1.1 joerg desc->plcp_service = 4;
939 1.1 joerg
940 1.1 joerg len += IEEE80211_CRC_LEN;
941 1.1 joerg if (RUM_RATE_IS_OFDM(rate)) {
942 1.1 joerg desc->flags |= htole32(RT2573_TX_OFDM);
943 1.1 joerg
944 1.1 joerg plcp_length = len & 0xfff;
945 1.1 joerg desc->plcp_length_hi = plcp_length >> 6;
946 1.1 joerg desc->plcp_length_lo = plcp_length & 0x3f;
947 1.1 joerg } else {
948 1.1 joerg plcp_length = (16 * len + rate - 1) / rate;
949 1.1 joerg if (rate == 22) {
950 1.1 joerg remainder = (16 * len) % 22;
951 1.1 joerg if (remainder != 0 && remainder < 7)
952 1.1 joerg desc->plcp_service |= RT2573_PLCP_LENGEXT;
953 1.1 joerg }
954 1.1 joerg desc->plcp_length_hi = plcp_length >> 8;
955 1.1 joerg desc->plcp_length_lo = plcp_length & 0xff;
956 1.1 joerg
957 1.1 joerg if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
958 1.1 joerg desc->plcp_signal |= 0x08;
959 1.1 joerg }
960 1.1 joerg }
961 1.1 joerg
962 1.1 joerg #define RUM_TX_TIMEOUT 5000
963 1.1 joerg
964 1.1 joerg Static int
965 1.1 joerg rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
966 1.1 joerg {
967 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
968 1.1 joerg struct rum_tx_desc *desc;
969 1.6.4.2 itohy struct ue_chain *data;
970 1.1 joerg struct ieee80211_frame *wh;
971 1.1 joerg uint32_t flags = 0;
972 1.1 joerg uint16_t dur;
973 1.1 joerg usbd_status error;
974 1.1 joerg int xferlen, rate;
975 1.6.4.2 itohy int ret;
976 1.1 joerg
977 1.1 joerg data = &sc->tx_data[0];
978 1.1 joerg
979 1.1 joerg rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
980 1.1 joerg
981 1.6.4.2 itohy data->ue_mbuf = m0;
982 1.6.4.2 itohy sc->tx_ni[data->ue_idx] = ni;
983 1.1 joerg
984 1.1 joerg wh = mtod(m0, struct ieee80211_frame *);
985 1.1 joerg
986 1.1 joerg if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
987 1.1 joerg flags |= RT2573_TX_ACK;
988 1.1 joerg
989 1.1 joerg dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
990 1.1 joerg ic->ic_flags) + sc->sifs;
991 1.1 joerg *(uint16_t *)wh->i_dur = htole16(dur);
992 1.1 joerg
993 1.1 joerg /* tell hardware to set timestamp in probe responses */
994 1.1 joerg if ((wh->i_fc[0] &
995 1.1 joerg (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
996 1.1 joerg (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
997 1.1 joerg flags |= RT2573_TX_TIMESTAMP;
998 1.1 joerg }
999 1.1 joerg
1000 1.1 joerg #if NBPFILTER > 0
1001 1.1 joerg if (sc->sc_drvbpf != NULL) {
1002 1.1 joerg struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1003 1.1 joerg
1004 1.1 joerg tap->wt_flags = 0;
1005 1.1 joerg tap->wt_rate = rate;
1006 1.1 joerg tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1007 1.1 joerg tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1008 1.1 joerg tap->wt_antenna = sc->tx_ant;
1009 1.1 joerg
1010 1.1 joerg bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1011 1.1 joerg }
1012 1.1 joerg #endif
1013 1.1 joerg
1014 1.6.4.2 itohy /* Prepend Tx descriptor */
1015 1.6.4.2 itohy M_PREPEND(m0, RT2573_TX_DESC_SIZE, M_DONTWAIT);
1016 1.6.4.2 itohy if (m0 != NULL)
1017 1.6.4.2 itohy m0 = m_pullup(m0, RT2573_TX_DESC_SIZE); /* just in case */
1018 1.6.4.2 itohy if (m0 == NULL) {
1019 1.6.4.2 itohy return ENOBUFS;
1020 1.6.4.2 itohy }
1021 1.6.4.2 itohy desc = mtod(m0, struct rum_tx_desc *);
1022 1.6.4.2 itohy
1023 1.1 joerg rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1024 1.1 joerg
1025 1.1 joerg /* align end on a 4-bytes boundary */
1026 1.1 joerg xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1027 1.1 joerg
1028 1.1 joerg /*
1029 1.1 joerg * No space left in the last URB to store the extra 4 bytes, force
1030 1.1 joerg * sending of another URB.
1031 1.1 joerg */
1032 1.1 joerg if ((xferlen % 64) == 0)
1033 1.1 joerg xferlen += 4;
1034 1.1 joerg
1035 1.6.4.2 itohy if (m0->m_pkthdr.len != xferlen) {
1036 1.6.4.2 itohy m_copyback(m0, xferlen - 1, 1, ""); /* expand mbuf chain */
1037 1.6.4.2 itohy if (m0->m_pkthdr.len != xferlen) {
1038 1.6.4.2 itohy m_freem(m0);
1039 1.6.4.2 itohy return ENOBUFS;
1040 1.6.4.2 itohy }
1041 1.6.4.2 itohy }
1042 1.6.4.2 itohy
1043 1.6.4.2 itohy ret = usb_ether_map_tx_buffer_mbuf(data, m0);
1044 1.6.4.2 itohy if (ret) {
1045 1.6.4.2 itohy m_freem(m0);
1046 1.6.4.2 itohy return ret;
1047 1.6.4.2 itohy }
1048 1.6.4.2 itohy
1049 1.5 wiz DPRINTFN(10, ("sending msg frame len=%lu rate=%u xfer len=%u\n",
1050 1.6 wiz (long unsigned int)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE,
1051 1.6 wiz rate, xferlen));
1052 1.1 joerg
1053 1.6.4.2 itohy usbd_setup_xfer(data->ue_xfer, sc->sc_tx_pipeh, data, NULL /* XXX buf */, xferlen,
1054 1.1 joerg USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1055 1.1 joerg
1056 1.6.4.2 itohy error = usbd_transfer(data->ue_xfer);
1057 1.1 joerg if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1058 1.6.4.2 itohy data->ue_mbuf = NULL;
1059 1.1 joerg m_freem(m0);
1060 1.1 joerg return error;
1061 1.1 joerg }
1062 1.1 joerg
1063 1.1 joerg sc->tx_queued++;
1064 1.1 joerg
1065 1.1 joerg return 0;
1066 1.1 joerg }
1067 1.1 joerg
1068 1.1 joerg Static int
1069 1.1 joerg rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1070 1.1 joerg {
1071 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1072 1.1 joerg struct rum_tx_desc *desc;
1073 1.6.4.2 itohy struct ue_chain *data;
1074 1.1 joerg struct ieee80211_frame *wh;
1075 1.1 joerg struct ieee80211_key *k;
1076 1.1 joerg uint32_t flags = 0;
1077 1.1 joerg uint16_t dur;
1078 1.1 joerg usbd_status error;
1079 1.1 joerg int xferlen, rate;
1080 1.6.4.2 itohy int ret;
1081 1.1 joerg
1082 1.1 joerg wh = mtod(m0, struct ieee80211_frame *);
1083 1.1 joerg
1084 1.1 joerg if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1085 1.1 joerg rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
1086 1.1 joerg else
1087 1.1 joerg rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1088 1.1 joerg rate &= IEEE80211_RATE_VAL;
1089 1.1 joerg
1090 1.1 joerg if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1091 1.1 joerg k = ieee80211_crypto_encap(ic, ni, m0);
1092 1.1 joerg if (k == NULL) {
1093 1.1 joerg m_freem(m0);
1094 1.1 joerg return ENOBUFS;
1095 1.1 joerg }
1096 1.1 joerg
1097 1.1 joerg /* packet header may have moved, reset our local pointer */
1098 1.1 joerg wh = mtod(m0, struct ieee80211_frame *);
1099 1.1 joerg }
1100 1.1 joerg
1101 1.1 joerg data = &sc->tx_data[0];
1102 1.1 joerg
1103 1.6.4.2 itohy data->ue_mbuf = m0;
1104 1.6.4.2 itohy sc->tx_ni[data->ue_idx] = ni;
1105 1.1 joerg
1106 1.1 joerg if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1107 1.1 joerg flags |= RT2573_TX_ACK;
1108 1.1 joerg
1109 1.1 joerg dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1110 1.1 joerg ic->ic_flags) + sc->sifs;
1111 1.1 joerg *(uint16_t *)wh->i_dur = htole16(dur);
1112 1.1 joerg }
1113 1.1 joerg
1114 1.1 joerg #if NBPFILTER > 0
1115 1.1 joerg if (sc->sc_drvbpf != NULL) {
1116 1.1 joerg struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1117 1.1 joerg
1118 1.1 joerg tap->wt_flags = 0;
1119 1.1 joerg tap->wt_rate = rate;
1120 1.1 joerg tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1121 1.1 joerg tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1122 1.1 joerg tap->wt_antenna = sc->tx_ant;
1123 1.1 joerg
1124 1.1 joerg bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1125 1.1 joerg }
1126 1.1 joerg #endif
1127 1.1 joerg
1128 1.6.4.2 itohy /* Prepend Tx descriptor */
1129 1.6.4.2 itohy M_PREPEND(m0, RT2573_TX_DESC_SIZE, M_DONTWAIT);
1130 1.6.4.2 itohy if (m0 != NULL)
1131 1.6.4.2 itohy m0 = m_pullup(m0, RT2573_TX_DESC_SIZE); /* just in case */
1132 1.6.4.2 itohy if (m0 == NULL) {
1133 1.6.4.2 itohy return ENOBUFS;
1134 1.6.4.2 itohy }
1135 1.6.4.2 itohy desc = mtod(m0, struct rum_tx_desc *);
1136 1.6.4.2 itohy
1137 1.1 joerg rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1138 1.1 joerg
1139 1.1 joerg /* align end on a 4-bytes boundary */
1140 1.1 joerg xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1141 1.1 joerg
1142 1.1 joerg /*
1143 1.1 joerg * No space left in the last URB to store the extra 4 bytes, force
1144 1.1 joerg * sending of another URB.
1145 1.1 joerg */
1146 1.1 joerg if ((xferlen % 64) == 0)
1147 1.1 joerg xferlen += 4;
1148 1.1 joerg
1149 1.6.4.2 itohy if (m0->m_pkthdr.len != xferlen) {
1150 1.6.4.2 itohy m_copyback(m0, xferlen - 1, 1, ""); /* expand mbuf chain */
1151 1.6.4.2 itohy if (m0->m_pkthdr.len != xferlen) {
1152 1.6.4.2 itohy m_freem(m0);
1153 1.6.4.2 itohy return ENOBUFS;
1154 1.6.4.2 itohy }
1155 1.6.4.2 itohy }
1156 1.6.4.2 itohy
1157 1.6.4.2 itohy ret = usb_ether_map_tx_buffer_mbuf(data, m0);
1158 1.6.4.2 itohy if (ret) {
1159 1.6.4.2 itohy m_freem(m0);
1160 1.6.4.2 itohy return ret;
1161 1.6.4.2 itohy }
1162 1.6.4.2 itohy
1163 1.5 wiz DPRINTFN(10, ("sending data frame len=%lu rate=%u xfer len=%u\n",
1164 1.6 wiz (long unsigned int)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE,
1165 1.6 wiz rate, xferlen));
1166 1.1 joerg
1167 1.6.4.2 itohy usbd_setup_xfer(data->ue_xfer, sc->sc_tx_pipeh, data, NULL /* XXX buf */, xferlen,
1168 1.1 joerg USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1169 1.1 joerg
1170 1.6.4.2 itohy error = usbd_transfer(data->ue_xfer);
1171 1.1 joerg if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1172 1.6.4.2 itohy data->ue_mbuf = NULL;
1173 1.1 joerg m_freem(m0);
1174 1.1 joerg return error;
1175 1.1 joerg }
1176 1.1 joerg
1177 1.1 joerg sc->tx_queued++;
1178 1.1 joerg
1179 1.1 joerg return 0;
1180 1.1 joerg }
1181 1.1 joerg
1182 1.1 joerg Static void
1183 1.1 joerg rum_start(struct ifnet *ifp)
1184 1.1 joerg {
1185 1.1 joerg struct rum_softc *sc = ifp->if_softc;
1186 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1187 1.1 joerg struct ether_header *eh;
1188 1.1 joerg struct ieee80211_node *ni;
1189 1.1 joerg struct mbuf *m0;
1190 1.1 joerg
1191 1.1 joerg for (;;) {
1192 1.1 joerg IF_POLL(&ic->ic_mgtq, m0);
1193 1.1 joerg if (m0 != NULL) {
1194 1.1 joerg if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1195 1.1 joerg ifp->if_flags |= IFF_OACTIVE;
1196 1.1 joerg break;
1197 1.1 joerg }
1198 1.1 joerg IF_DEQUEUE(&ic->ic_mgtq, m0);
1199 1.1 joerg
1200 1.1 joerg ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1201 1.1 joerg m0->m_pkthdr.rcvif = NULL;
1202 1.1 joerg #if NBPFILTER > 0
1203 1.1 joerg if (ic->ic_rawbpf != NULL)
1204 1.1 joerg bpf_mtap(ic->ic_rawbpf, m0);
1205 1.1 joerg #endif
1206 1.1 joerg if (rum_tx_mgt(sc, m0, ni) != 0)
1207 1.1 joerg break;
1208 1.1 joerg
1209 1.1 joerg } else {
1210 1.1 joerg if (ic->ic_state != IEEE80211_S_RUN)
1211 1.1 joerg break;
1212 1.1 joerg IFQ_POLL(&ifp->if_snd, m0);
1213 1.1 joerg if (m0 == NULL)
1214 1.1 joerg break;
1215 1.1 joerg if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1216 1.1 joerg ifp->if_flags |= IFF_OACTIVE;
1217 1.1 joerg break;
1218 1.1 joerg }
1219 1.1 joerg IFQ_DEQUEUE(&ifp->if_snd, m0);
1220 1.1 joerg if (m0->m_len < sizeof(struct ether_header) &&
1221 1.1 joerg !(m0 = m_pullup(m0, sizeof(struct ether_header))))
1222 1.1 joerg continue;
1223 1.1 joerg
1224 1.1 joerg eh = mtod(m0, struct ether_header *);
1225 1.1 joerg ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1226 1.1 joerg if (ni == NULL) {
1227 1.1 joerg m_freem(m0);
1228 1.1 joerg continue;
1229 1.1 joerg }
1230 1.1 joerg #if NBPFILTER > 0
1231 1.1 joerg if (ifp->if_bpf != NULL)
1232 1.1 joerg bpf_mtap(ifp->if_bpf, m0);
1233 1.1 joerg #endif
1234 1.1 joerg m0 = ieee80211_encap(ic, m0, ni);
1235 1.1 joerg if (m0 == NULL) {
1236 1.1 joerg ieee80211_free_node(ni);
1237 1.1 joerg continue;
1238 1.1 joerg }
1239 1.1 joerg #if NBPFILTER > 0
1240 1.1 joerg if (ic->ic_rawbpf != NULL)
1241 1.1 joerg bpf_mtap(ic->ic_rawbpf, m0);
1242 1.1 joerg #endif
1243 1.1 joerg if (rum_tx_data(sc, m0, ni) != 0) {
1244 1.1 joerg ieee80211_free_node(ni);
1245 1.1 joerg ifp->if_oerrors++;
1246 1.1 joerg break;
1247 1.1 joerg }
1248 1.1 joerg }
1249 1.1 joerg
1250 1.1 joerg sc->sc_tx_timer = 5;
1251 1.1 joerg ifp->if_timer = 1;
1252 1.1 joerg }
1253 1.1 joerg }
1254 1.1 joerg
1255 1.1 joerg Static void
1256 1.1 joerg rum_watchdog(struct ifnet *ifp)
1257 1.1 joerg {
1258 1.1 joerg struct rum_softc *sc = ifp->if_softc;
1259 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1260 1.1 joerg
1261 1.1 joerg ifp->if_timer = 0;
1262 1.1 joerg
1263 1.1 joerg if (sc->sc_tx_timer > 0) {
1264 1.1 joerg if (--sc->sc_tx_timer == 0) {
1265 1.1 joerg printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1266 1.1 joerg /*rum_init(ifp); XXX needs a process context! */
1267 1.1 joerg ifp->if_oerrors++;
1268 1.1 joerg return;
1269 1.1 joerg }
1270 1.1 joerg ifp->if_timer = 1;
1271 1.1 joerg }
1272 1.1 joerg
1273 1.1 joerg ieee80211_watchdog(ic);
1274 1.1 joerg }
1275 1.1 joerg
1276 1.1 joerg Static int
1277 1.6.4.3 itohy rum_ioctl(struct ifnet *ifp, u_long cmd, usb_ioctlarg_t data)
1278 1.1 joerg {
1279 1.1 joerg struct rum_softc *sc = ifp->if_softc;
1280 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1281 1.1 joerg int s, error = 0;
1282 1.1 joerg
1283 1.1 joerg s = splnet();
1284 1.1 joerg
1285 1.1 joerg switch (cmd) {
1286 1.1 joerg case SIOCSIFFLAGS:
1287 1.1 joerg if (ifp->if_flags & IFF_UP) {
1288 1.1 joerg if (ifp->if_flags & IFF_RUNNING)
1289 1.1 joerg rum_update_promisc(sc);
1290 1.1 joerg else
1291 1.1 joerg rum_init(ifp);
1292 1.1 joerg } else {
1293 1.1 joerg if (ifp->if_flags & IFF_RUNNING)
1294 1.1 joerg rum_stop(ifp, 1);
1295 1.1 joerg }
1296 1.1 joerg break;
1297 1.1 joerg
1298 1.1 joerg default:
1299 1.1 joerg error = ieee80211_ioctl(ic, cmd, data);
1300 1.1 joerg }
1301 1.1 joerg
1302 1.1 joerg if (error == ENETRESET) {
1303 1.1 joerg if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1304 1.1 joerg (IFF_UP | IFF_RUNNING))
1305 1.1 joerg rum_init(ifp);
1306 1.1 joerg error = 0;
1307 1.1 joerg }
1308 1.1 joerg
1309 1.1 joerg splx(s);
1310 1.1 joerg
1311 1.1 joerg return error;
1312 1.1 joerg }
1313 1.1 joerg
1314 1.1 joerg Static void
1315 1.1 joerg rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1316 1.1 joerg {
1317 1.1 joerg usb_device_request_t req;
1318 1.1 joerg usbd_status error;
1319 1.1 joerg
1320 1.1 joerg req.bmRequestType = UT_READ_VENDOR_DEVICE;
1321 1.1 joerg req.bRequest = RT2573_READ_EEPROM;
1322 1.1 joerg USETW(req.wValue, 0);
1323 1.1 joerg USETW(req.wIndex, addr);
1324 1.1 joerg USETW(req.wLength, len);
1325 1.1 joerg
1326 1.1 joerg error = usbd_do_request(sc->sc_udev, &req, buf);
1327 1.1 joerg if (error != 0) {
1328 1.1 joerg printf("%s: could not read EEPROM: %s\n",
1329 1.1 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1330 1.1 joerg }
1331 1.1 joerg }
1332 1.1 joerg
1333 1.1 joerg Static uint32_t
1334 1.1 joerg rum_read(struct rum_softc *sc, uint16_t reg)
1335 1.1 joerg {
1336 1.1 joerg uint32_t val;
1337 1.1 joerg
1338 1.1 joerg rum_read_multi(sc, reg, &val, sizeof val);
1339 1.1 joerg
1340 1.1 joerg return le32toh(val);
1341 1.1 joerg }
1342 1.1 joerg
1343 1.1 joerg Static void
1344 1.1 joerg rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1345 1.1 joerg {
1346 1.1 joerg usb_device_request_t req;
1347 1.1 joerg usbd_status error;
1348 1.1 joerg
1349 1.1 joerg req.bmRequestType = UT_READ_VENDOR_DEVICE;
1350 1.1 joerg req.bRequest = RT2573_READ_MULTI_MAC;
1351 1.1 joerg USETW(req.wValue, 0);
1352 1.1 joerg USETW(req.wIndex, reg);
1353 1.1 joerg USETW(req.wLength, len);
1354 1.1 joerg
1355 1.1 joerg error = usbd_do_request(sc->sc_udev, &req, buf);
1356 1.1 joerg if (error != 0) {
1357 1.1 joerg printf("%s: could not multi read MAC register: %s\n",
1358 1.1 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1359 1.1 joerg }
1360 1.1 joerg }
1361 1.1 joerg
1362 1.1 joerg Static void
1363 1.1 joerg rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1364 1.1 joerg {
1365 1.1 joerg uint32_t tmp = htole32(val);
1366 1.1 joerg
1367 1.1 joerg rum_write_multi(sc, reg, &tmp, sizeof tmp);
1368 1.1 joerg }
1369 1.1 joerg
1370 1.1 joerg Static void
1371 1.1 joerg rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1372 1.1 joerg {
1373 1.1 joerg usb_device_request_t req;
1374 1.1 joerg usbd_status error;
1375 1.1 joerg
1376 1.1 joerg req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1377 1.1 joerg req.bRequest = RT2573_WRITE_MULTI_MAC;
1378 1.1 joerg USETW(req.wValue, 0);
1379 1.1 joerg USETW(req.wIndex, reg);
1380 1.1 joerg USETW(req.wLength, len);
1381 1.1 joerg
1382 1.1 joerg error = usbd_do_request(sc->sc_udev, &req, buf);
1383 1.1 joerg if (error != 0) {
1384 1.1 joerg printf("%s: could not multi write MAC register: %s\n",
1385 1.1 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1386 1.1 joerg }
1387 1.1 joerg }
1388 1.1 joerg
1389 1.1 joerg Static void
1390 1.1 joerg rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1391 1.1 joerg {
1392 1.1 joerg uint32_t tmp;
1393 1.1 joerg int ntries;
1394 1.1 joerg
1395 1.1 joerg for (ntries = 0; ntries < 5; ntries++) {
1396 1.1 joerg if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1397 1.1 joerg break;
1398 1.1 joerg }
1399 1.1 joerg if (ntries == 5) {
1400 1.1 joerg printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1401 1.1 joerg return;
1402 1.1 joerg }
1403 1.1 joerg
1404 1.1 joerg tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1405 1.1 joerg rum_write(sc, RT2573_PHY_CSR3, tmp);
1406 1.1 joerg }
1407 1.1 joerg
1408 1.1 joerg Static uint8_t
1409 1.1 joerg rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1410 1.1 joerg {
1411 1.1 joerg uint32_t val;
1412 1.1 joerg int ntries;
1413 1.1 joerg
1414 1.1 joerg for (ntries = 0; ntries < 5; ntries++) {
1415 1.1 joerg if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1416 1.1 joerg break;
1417 1.1 joerg }
1418 1.1 joerg if (ntries == 5) {
1419 1.1 joerg printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1420 1.1 joerg return 0;
1421 1.1 joerg }
1422 1.1 joerg
1423 1.1 joerg val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1424 1.1 joerg rum_write(sc, RT2573_PHY_CSR3, val);
1425 1.1 joerg
1426 1.1 joerg for (ntries = 0; ntries < 100; ntries++) {
1427 1.1 joerg val = rum_read(sc, RT2573_PHY_CSR3);
1428 1.1 joerg if (!(val & RT2573_BBP_BUSY))
1429 1.1 joerg return val & 0xff;
1430 1.1 joerg DELAY(1);
1431 1.1 joerg }
1432 1.1 joerg
1433 1.1 joerg printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1434 1.1 joerg return 0;
1435 1.1 joerg }
1436 1.1 joerg
1437 1.1 joerg Static void
1438 1.1 joerg rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1439 1.1 joerg {
1440 1.1 joerg uint32_t tmp;
1441 1.1 joerg int ntries;
1442 1.1 joerg
1443 1.1 joerg for (ntries = 0; ntries < 5; ntries++) {
1444 1.1 joerg if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1445 1.1 joerg break;
1446 1.1 joerg }
1447 1.1 joerg if (ntries == 5) {
1448 1.1 joerg printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1449 1.1 joerg return;
1450 1.1 joerg }
1451 1.1 joerg
1452 1.1 joerg tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1453 1.1 joerg (reg & 3);
1454 1.1 joerg rum_write(sc, RT2573_PHY_CSR4, tmp);
1455 1.1 joerg
1456 1.1 joerg /* remember last written value in sc */
1457 1.1 joerg sc->rf_regs[reg] = val;
1458 1.1 joerg
1459 1.1 joerg DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1460 1.1 joerg }
1461 1.1 joerg
1462 1.1 joerg Static void
1463 1.1 joerg rum_select_antenna(struct rum_softc *sc)
1464 1.1 joerg {
1465 1.1 joerg uint8_t bbp4, bbp77;
1466 1.1 joerg uint32_t tmp;
1467 1.1 joerg
1468 1.1 joerg bbp4 = rum_bbp_read(sc, 4);
1469 1.1 joerg bbp77 = rum_bbp_read(sc, 77);
1470 1.1 joerg
1471 1.1 joerg /* TBD */
1472 1.1 joerg
1473 1.1 joerg /* make sure Rx is disabled before switching antenna */
1474 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR0);
1475 1.1 joerg rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1476 1.1 joerg
1477 1.1 joerg rum_bbp_write(sc, 4, bbp4);
1478 1.1 joerg rum_bbp_write(sc, 77, bbp77);
1479 1.1 joerg
1480 1.1 joerg rum_write(sc, RT2573_TXRX_CSR0, tmp);
1481 1.1 joerg }
1482 1.1 joerg
1483 1.1 joerg /*
1484 1.1 joerg * Enable multi-rate retries for frames sent at OFDM rates.
1485 1.1 joerg * In 802.11b/g mode, allow fallback to CCK rates.
1486 1.1 joerg */
1487 1.1 joerg Static void
1488 1.1 joerg rum_enable_mrr(struct rum_softc *sc)
1489 1.1 joerg {
1490 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1491 1.1 joerg uint32_t tmp;
1492 1.1 joerg
1493 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR4);
1494 1.1 joerg
1495 1.1 joerg tmp &= ~RT2573_MRR_CCK_FALLBACK;
1496 1.1 joerg if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1497 1.1 joerg tmp |= RT2573_MRR_CCK_FALLBACK;
1498 1.1 joerg tmp |= RT2573_MRR_ENABLED;
1499 1.1 joerg
1500 1.1 joerg rum_write(sc, RT2573_TXRX_CSR4, tmp);
1501 1.1 joerg }
1502 1.1 joerg
1503 1.1 joerg Static void
1504 1.1 joerg rum_set_txpreamble(struct rum_softc *sc)
1505 1.1 joerg {
1506 1.1 joerg uint32_t tmp;
1507 1.1 joerg
1508 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR4);
1509 1.1 joerg
1510 1.1 joerg tmp &= ~RT2573_SHORT_PREAMBLE;
1511 1.1 joerg if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1512 1.1 joerg tmp |= RT2573_SHORT_PREAMBLE;
1513 1.1 joerg
1514 1.1 joerg rum_write(sc, RT2573_TXRX_CSR4, tmp);
1515 1.1 joerg }
1516 1.1 joerg
1517 1.1 joerg Static void
1518 1.1 joerg rum_set_basicrates(struct rum_softc *sc)
1519 1.1 joerg {
1520 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1521 1.1 joerg
1522 1.1 joerg /* update basic rate set */
1523 1.1 joerg if (ic->ic_curmode == IEEE80211_MODE_11B) {
1524 1.1 joerg /* 11b basic rates: 1, 2Mbps */
1525 1.1 joerg rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1526 1.1 joerg } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1527 1.1 joerg /* 11a basic rates: 6, 12, 24Mbps */
1528 1.1 joerg rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1529 1.1 joerg } else {
1530 1.1 joerg /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1531 1.1 joerg rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1532 1.1 joerg }
1533 1.1 joerg }
1534 1.1 joerg
1535 1.1 joerg /*
1536 1.1 joerg * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1537 1.1 joerg * driver.
1538 1.1 joerg */
1539 1.1 joerg Static void
1540 1.1 joerg rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1541 1.1 joerg {
1542 1.1 joerg uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1543 1.1 joerg uint32_t tmp;
1544 1.1 joerg
1545 1.1 joerg /* update all BBP registers that depend on the band */
1546 1.1 joerg bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1547 1.1 joerg bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1548 1.1 joerg if (IEEE80211_IS_CHAN_5GHZ(c)) {
1549 1.1 joerg bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1550 1.1 joerg bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1551 1.1 joerg }
1552 1.1 joerg if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1553 1.1 joerg (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1554 1.1 joerg bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1555 1.1 joerg }
1556 1.1 joerg
1557 1.1 joerg sc->bbp17 = bbp17;
1558 1.1 joerg rum_bbp_write(sc, 17, bbp17);
1559 1.1 joerg rum_bbp_write(sc, 96, bbp96);
1560 1.1 joerg rum_bbp_write(sc, 104, bbp104);
1561 1.1 joerg
1562 1.1 joerg if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1563 1.1 joerg (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1564 1.1 joerg rum_bbp_write(sc, 75, 0x80);
1565 1.1 joerg rum_bbp_write(sc, 86, 0x80);
1566 1.1 joerg rum_bbp_write(sc, 88, 0x80);
1567 1.1 joerg }
1568 1.1 joerg
1569 1.1 joerg rum_bbp_write(sc, 35, bbp35);
1570 1.1 joerg rum_bbp_write(sc, 97, bbp97);
1571 1.1 joerg rum_bbp_write(sc, 98, bbp98);
1572 1.1 joerg
1573 1.1 joerg tmp = rum_read(sc, RT2573_PHY_CSR0);
1574 1.1 joerg tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1575 1.1 joerg if (IEEE80211_IS_CHAN_2GHZ(c))
1576 1.1 joerg tmp |= RT2573_PA_PE_2GHZ;
1577 1.1 joerg else
1578 1.1 joerg tmp |= RT2573_PA_PE_5GHZ;
1579 1.1 joerg rum_write(sc, RT2573_PHY_CSR0, tmp);
1580 1.1 joerg
1581 1.1 joerg /* 802.11a uses a 16 microseconds short interframe space */
1582 1.1 joerg sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1583 1.1 joerg }
1584 1.1 joerg
1585 1.1 joerg Static void
1586 1.1 joerg rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1587 1.1 joerg {
1588 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1589 1.1 joerg const struct rfprog *rfprog;
1590 1.1 joerg uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1591 1.1 joerg int8_t power;
1592 1.1 joerg u_int i, chan;
1593 1.1 joerg
1594 1.1 joerg chan = ieee80211_chan2ieee(ic, c);
1595 1.1 joerg if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1596 1.1 joerg return;
1597 1.1 joerg
1598 1.1 joerg /* select the appropriate RF settings based on what EEPROM says */
1599 1.1 joerg rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1600 1.1 joerg sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1601 1.1 joerg
1602 1.1 joerg /* find the settings for this channel (we know it exists) */
1603 1.1 joerg for (i = 0; rfprog[i].chan != chan; i++);
1604 1.1 joerg
1605 1.1 joerg power = sc->txpow[i];
1606 1.1 joerg if (power < 0) {
1607 1.1 joerg bbp94 += power;
1608 1.1 joerg power = 0;
1609 1.1 joerg } else if (power > 31) {
1610 1.1 joerg bbp94 += power - 31;
1611 1.1 joerg power = 31;
1612 1.1 joerg }
1613 1.1 joerg
1614 1.1 joerg /*
1615 1.1 joerg * If we are switching from the 2GHz band to the 5GHz band or
1616 1.1 joerg * vice-versa, BBP registers need to be reprogrammed.
1617 1.1 joerg */
1618 1.1 joerg if (c->ic_flags != ic->ic_curchan->ic_flags) {
1619 1.1 joerg rum_select_band(sc, c);
1620 1.1 joerg rum_select_antenna(sc);
1621 1.1 joerg }
1622 1.1 joerg ic->ic_curchan = c;
1623 1.1 joerg
1624 1.1 joerg rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1625 1.1 joerg rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1626 1.1 joerg rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1627 1.1 joerg rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1628 1.1 joerg
1629 1.1 joerg rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1630 1.1 joerg rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1631 1.1 joerg rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1632 1.1 joerg rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1633 1.1 joerg
1634 1.1 joerg rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1635 1.1 joerg rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1636 1.1 joerg rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1637 1.1 joerg rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1638 1.1 joerg
1639 1.1 joerg DELAY(10);
1640 1.1 joerg
1641 1.1 joerg /* enable smart mode for MIMO-capable RFs */
1642 1.1 joerg bbp3 = rum_bbp_read(sc, 3);
1643 1.1 joerg
1644 1.1 joerg bbp3 &= ~RT2573_SMART_MODE;
1645 1.1 joerg if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1646 1.1 joerg bbp3 |= RT2573_SMART_MODE;
1647 1.1 joerg
1648 1.1 joerg rum_bbp_write(sc, 3, bbp3);
1649 1.1 joerg
1650 1.1 joerg if (bbp94 != RT2573_BBPR94_DEFAULT)
1651 1.1 joerg rum_bbp_write(sc, 94, bbp94);
1652 1.1 joerg }
1653 1.1 joerg
1654 1.1 joerg /*
1655 1.1 joerg * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1656 1.1 joerg * and HostAP operating modes.
1657 1.1 joerg */
1658 1.1 joerg Static void
1659 1.1 joerg rum_enable_tsf_sync(struct rum_softc *sc)
1660 1.1 joerg {
1661 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1662 1.1 joerg uint32_t tmp;
1663 1.1 joerg
1664 1.1 joerg if (ic->ic_opmode != IEEE80211_M_STA) {
1665 1.1 joerg /*
1666 1.1 joerg * Change default 16ms TBTT adjustment to 8ms.
1667 1.1 joerg * Must be done before enabling beacon generation.
1668 1.1 joerg */
1669 1.1 joerg rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1670 1.1 joerg }
1671 1.1 joerg
1672 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1673 1.1 joerg
1674 1.1 joerg /* set beacon interval (in 1/16ms unit) */
1675 1.1 joerg tmp |= ic->ic_bss->ni_intval * 16;
1676 1.1 joerg
1677 1.1 joerg tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1678 1.1 joerg if (ic->ic_opmode == IEEE80211_M_STA)
1679 1.1 joerg tmp |= RT2573_TSF_MODE(1);
1680 1.1 joerg else
1681 1.1 joerg tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1682 1.1 joerg
1683 1.1 joerg rum_write(sc, RT2573_TXRX_CSR9, tmp);
1684 1.1 joerg }
1685 1.1 joerg
1686 1.1 joerg Static void
1687 1.1 joerg rum_update_slot(struct rum_softc *sc)
1688 1.1 joerg {
1689 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1690 1.1 joerg uint8_t slottime;
1691 1.1 joerg uint32_t tmp;
1692 1.1 joerg
1693 1.1 joerg slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1694 1.1 joerg
1695 1.1 joerg tmp = rum_read(sc, RT2573_MAC_CSR9);
1696 1.1 joerg tmp = (tmp & ~0xff) | slottime;
1697 1.1 joerg rum_write(sc, RT2573_MAC_CSR9, tmp);
1698 1.1 joerg
1699 1.1 joerg DPRINTF(("setting slot time to %uus\n", slottime));
1700 1.1 joerg }
1701 1.1 joerg
1702 1.1 joerg Static void
1703 1.1 joerg rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1704 1.1 joerg {
1705 1.1 joerg uint32_t tmp;
1706 1.1 joerg
1707 1.1 joerg tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1708 1.1 joerg rum_write(sc, RT2573_MAC_CSR4, tmp);
1709 1.1 joerg
1710 1.1 joerg tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1711 1.1 joerg rum_write(sc, RT2573_MAC_CSR5, tmp);
1712 1.1 joerg }
1713 1.1 joerg
1714 1.1 joerg Static void
1715 1.1 joerg rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1716 1.1 joerg {
1717 1.1 joerg uint32_t tmp;
1718 1.1 joerg
1719 1.1 joerg tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1720 1.1 joerg rum_write(sc, RT2573_MAC_CSR2, tmp);
1721 1.1 joerg
1722 1.1 joerg tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1723 1.1 joerg rum_write(sc, RT2573_MAC_CSR3, tmp);
1724 1.1 joerg }
1725 1.1 joerg
1726 1.1 joerg Static void
1727 1.1 joerg rum_update_promisc(struct rum_softc *sc)
1728 1.1 joerg {
1729 1.1 joerg struct ifnet *ifp = sc->sc_ic.ic_ifp;
1730 1.1 joerg uint32_t tmp;
1731 1.1 joerg
1732 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR0);
1733 1.1 joerg
1734 1.1 joerg tmp &= ~RT2573_DROP_NOT_TO_ME;
1735 1.1 joerg if (!(ifp->if_flags & IFF_PROMISC))
1736 1.1 joerg tmp |= RT2573_DROP_NOT_TO_ME;
1737 1.1 joerg
1738 1.1 joerg rum_write(sc, RT2573_TXRX_CSR0, tmp);
1739 1.1 joerg
1740 1.1 joerg DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1741 1.1 joerg "entering" : "leaving"));
1742 1.1 joerg }
1743 1.1 joerg
1744 1.1 joerg Static const char *
1745 1.1 joerg rum_get_rf(int rev)
1746 1.1 joerg {
1747 1.1 joerg switch (rev) {
1748 1.1 joerg case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1749 1.1 joerg case RT2573_RF_2528: return "RT2528";
1750 1.1 joerg case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1751 1.1 joerg case RT2573_RF_5226: return "RT5226";
1752 1.1 joerg default: return "unknown";
1753 1.1 joerg }
1754 1.1 joerg }
1755 1.1 joerg
1756 1.1 joerg Static void
1757 1.1 joerg rum_read_eeprom(struct rum_softc *sc)
1758 1.1 joerg {
1759 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1760 1.1 joerg uint16_t val;
1761 1.1 joerg #ifdef RUM_DEBUG
1762 1.1 joerg int i;
1763 1.1 joerg #endif
1764 1.1 joerg
1765 1.1 joerg /* read MAC/BBP type */
1766 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1767 1.1 joerg sc->macbbp_rev = le16toh(val);
1768 1.1 joerg
1769 1.1 joerg /* read MAC address */
1770 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1771 1.1 joerg
1772 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1773 1.1 joerg val = le16toh(val);
1774 1.1 joerg sc->rf_rev = (val >> 11) & 0x1f;
1775 1.1 joerg sc->hw_radio = (val >> 10) & 0x1;
1776 1.1 joerg sc->rx_ant = (val >> 4) & 0x3;
1777 1.1 joerg sc->tx_ant = (val >> 2) & 0x3;
1778 1.1 joerg sc->nb_ant = val & 0x3;
1779 1.1 joerg
1780 1.1 joerg DPRINTF(("RF revision=%d\n", sc->rf_rev));
1781 1.1 joerg
1782 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1783 1.1 joerg val = le16toh(val);
1784 1.1 joerg sc->ext_5ghz_lna = (val >> 6) & 0x1;
1785 1.1 joerg sc->ext_2ghz_lna = (val >> 4) & 0x1;
1786 1.1 joerg
1787 1.1 joerg DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1788 1.1 joerg sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1789 1.1 joerg
1790 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1791 1.1 joerg val = le16toh(val);
1792 1.1 joerg if ((val & 0xff) != 0xff)
1793 1.1 joerg sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1794 1.1 joerg
1795 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1796 1.1 joerg val = le16toh(val);
1797 1.1 joerg if ((val & 0xff) != 0xff)
1798 1.1 joerg sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1799 1.1 joerg
1800 1.1 joerg DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1801 1.1 joerg sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1802 1.1 joerg
1803 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1804 1.1 joerg val = le16toh(val);
1805 1.1 joerg if ((val & 0xff) != 0xff)
1806 1.1 joerg sc->rffreq = val & 0xff;
1807 1.1 joerg
1808 1.1 joerg DPRINTF(("RF freq=%d\n", sc->rffreq));
1809 1.1 joerg
1810 1.1 joerg /* read Tx power for all a/b/g channels */
1811 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1812 1.1 joerg /* XXX default Tx power for 802.11a channels */
1813 1.1 joerg memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1814 1.1 joerg #ifdef RUM_DEBUG
1815 1.1 joerg for (i = 0; i < 14; i++)
1816 1.1 joerg DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1817 1.1 joerg #endif
1818 1.1 joerg
1819 1.1 joerg /* read default values for BBP registers */
1820 1.1 joerg rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1821 1.1 joerg #ifdef RUM_DEBUG
1822 1.1 joerg for (i = 0; i < 14; i++) {
1823 1.1 joerg if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1824 1.1 joerg continue;
1825 1.1 joerg DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1826 1.1 joerg sc->bbp_prom[i].val));
1827 1.1 joerg }
1828 1.1 joerg #endif
1829 1.1 joerg }
1830 1.1 joerg
1831 1.1 joerg Static int
1832 1.1 joerg rum_bbp_init(struct rum_softc *sc)
1833 1.1 joerg {
1834 1.1 joerg #define N(a) (sizeof (a) / sizeof ((a)[0]))
1835 1.1 joerg int i, ntries;
1836 1.1 joerg uint8_t val;
1837 1.1 joerg
1838 1.1 joerg /* wait for BBP to be ready */
1839 1.1 joerg for (ntries = 0; ntries < 100; ntries++) {
1840 1.1 joerg val = rum_bbp_read(sc, 0);
1841 1.1 joerg if (val != 0 && val != 0xff)
1842 1.1 joerg break;
1843 1.1 joerg DELAY(1000);
1844 1.1 joerg }
1845 1.1 joerg if (ntries == 100) {
1846 1.1 joerg printf("%s: timeout waiting for BBP\n",
1847 1.1 joerg USBDEVNAME(sc->sc_dev));
1848 1.1 joerg return EIO;
1849 1.1 joerg }
1850 1.1 joerg
1851 1.1 joerg /* initialize BBP registers to default values */
1852 1.1 joerg for (i = 0; i < N(rum_def_bbp); i++)
1853 1.1 joerg rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1854 1.1 joerg
1855 1.1 joerg /* write vendor-specific BBP values (from EEPROM) */
1856 1.1 joerg for (i = 0; i < 16; i++) {
1857 1.1 joerg if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1858 1.1 joerg continue;
1859 1.1 joerg rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1860 1.1 joerg }
1861 1.1 joerg
1862 1.1 joerg return 0;
1863 1.1 joerg #undef N
1864 1.1 joerg }
1865 1.1 joerg
1866 1.1 joerg Static int
1867 1.1 joerg rum_init(struct ifnet *ifp)
1868 1.1 joerg {
1869 1.1 joerg #define N(a) (sizeof (a) / sizeof ((a)[0]))
1870 1.1 joerg struct rum_softc *sc = ifp->if_softc;
1871 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
1872 1.6.4.2 itohy struct ue_chain *data;
1873 1.1 joerg uint32_t tmp;
1874 1.6.4.2 itohy usbd_status uerror;
1875 1.6.4.2 itohy int error;
1876 1.1 joerg int i, ntries;
1877 1.1 joerg
1878 1.1 joerg if ((sc->sc_flags & RT2573_FWLOADED) == 0) {
1879 1.6.4.2 itohy if (rum_attachhook(sc)) {
1880 1.6.4.2 itohy error = EIO;
1881 1.1 joerg goto fail;
1882 1.6.4.2 itohy }
1883 1.1 joerg }
1884 1.1 joerg
1885 1.1 joerg rum_stop(ifp, 0);
1886 1.1 joerg
1887 1.1 joerg /* initialize MAC registers to default values */
1888 1.1 joerg for (i = 0; i < N(rum_def_mac); i++)
1889 1.1 joerg rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1890 1.1 joerg
1891 1.1 joerg /* set host ready */
1892 1.1 joerg rum_write(sc, RT2573_MAC_CSR1, 3);
1893 1.1 joerg rum_write(sc, RT2573_MAC_CSR1, 0);
1894 1.1 joerg
1895 1.1 joerg /* wait for BBP/RF to wakeup */
1896 1.1 joerg for (ntries = 0; ntries < 1000; ntries++) {
1897 1.1 joerg if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1898 1.1 joerg break;
1899 1.1 joerg rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1900 1.1 joerg DELAY(1000);
1901 1.1 joerg }
1902 1.1 joerg if (ntries == 1000) {
1903 1.1 joerg printf("%s: timeout waiting for BBP/RF to wakeup\n",
1904 1.1 joerg USBDEVNAME(sc->sc_dev));
1905 1.6.4.2 itohy error = EIO;
1906 1.1 joerg goto fail;
1907 1.1 joerg }
1908 1.1 joerg
1909 1.1 joerg if ((error = rum_bbp_init(sc)) != 0)
1910 1.1 joerg goto fail;
1911 1.1 joerg
1912 1.1 joerg /* select default channel */
1913 1.1 joerg rum_select_band(sc, ic->ic_curchan);
1914 1.1 joerg rum_select_antenna(sc);
1915 1.1 joerg rum_set_chan(sc, ic->ic_curchan);
1916 1.1 joerg
1917 1.1 joerg /* clear STA registers */
1918 1.1 joerg rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1919 1.1 joerg
1920 1.1 joerg IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1921 1.1 joerg rum_set_macaddr(sc, ic->ic_myaddr);
1922 1.1 joerg
1923 1.1 joerg /* initialize ASIC */
1924 1.1 joerg rum_write(sc, RT2573_MAC_CSR1, 4);
1925 1.1 joerg
1926 1.1 joerg /*
1927 1.1 joerg * Allocate xfer for AMRR statistics requests.
1928 1.1 joerg */
1929 1.6.4.1 itohy sc->amrr_xfer = usbd_alloc_default_xfer(sc->sc_udev);
1930 1.1 joerg if (sc->amrr_xfer == NULL) {
1931 1.1 joerg printf("%s: could not allocate AMRR xfer\n",
1932 1.1 joerg USBDEVNAME(sc->sc_dev));
1933 1.1 joerg goto fail;
1934 1.1 joerg }
1935 1.1 joerg
1936 1.1 joerg /*
1937 1.1 joerg * Open Tx and Rx USB bulk pipes.
1938 1.1 joerg */
1939 1.6.4.2 itohy uerror = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1940 1.1 joerg &sc->sc_tx_pipeh);
1941 1.6.4.2 itohy if (uerror != USBD_NORMAL_COMPLETION) {
1942 1.1 joerg printf("%s: could not open Tx pipe: %s\n",
1943 1.6.4.2 itohy USBDEVNAME(sc->sc_dev), usbd_errstr(uerror));
1944 1.6.4.2 itohy error = EIO;
1945 1.1 joerg goto fail;
1946 1.1 joerg }
1947 1.1 joerg
1948 1.6.4.2 itohy uerror = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1949 1.1 joerg &sc->sc_rx_pipeh);
1950 1.6.4.2 itohy if (uerror != USBD_NORMAL_COMPLETION) {
1951 1.1 joerg printf("%s: could not open Rx pipe: %s\n",
1952 1.6.4.2 itohy USBDEVNAME(sc->sc_dev), usbd_errstr(uerror));
1953 1.6.4.2 itohy error = EIO;
1954 1.1 joerg goto fail;
1955 1.1 joerg }
1956 1.1 joerg
1957 1.1 joerg /*
1958 1.1 joerg * Allocate Tx and Rx xfer queues.
1959 1.1 joerg */
1960 1.6.4.2 itohy sc->tx_queued = 0;
1961 1.6.4.2 itohy error = usb_ether_tx_list_init(USBDEV(sc->sc_dev),
1962 1.6.4.2 itohy sc->tx_data, RT2573_TX_LIST_COUNT,
1963 1.6.4.2 itohy sc->sc_udev, sc->sc_tx_pipeh, NULL);
1964 1.1 joerg if (error != 0) {
1965 1.1 joerg printf("%s: could not allocate Tx list\n",
1966 1.1 joerg USBDEVNAME(sc->sc_dev));
1967 1.1 joerg goto fail;
1968 1.1 joerg }
1969 1.1 joerg
1970 1.6.4.2 itohy error = usb_ether_rx_list_init(USBDEV(sc->sc_dev),
1971 1.6.4.2 itohy sc->rx_data, RT2573_RX_LIST_COUNT,
1972 1.6.4.2 itohy sc->sc_udev, sc->sc_rx_pipeh);
1973 1.1 joerg if (error != 0) {
1974 1.1 joerg printf("%s: could not allocate Rx list\n",
1975 1.1 joerg USBDEVNAME(sc->sc_dev));
1976 1.1 joerg goto fail;
1977 1.1 joerg }
1978 1.1 joerg
1979 1.1 joerg /*
1980 1.1 joerg * Start up the receive pipe.
1981 1.1 joerg */
1982 1.1 joerg for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1983 1.1 joerg data = &sc->rx_data[i];
1984 1.1 joerg
1985 1.6.4.2 itohy (void)usbd_map_buffer_mbuf(data->ue_xfer, data->ue_mbuf);
1986 1.6.4.2 itohy usbd_setup_xfer(data->ue_xfer, sc->sc_rx_pipeh, data, NULL /* XXX buf */,
1987 1.1 joerg MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
1988 1.6.4.2 itohy usbd_transfer(data->ue_xfer);
1989 1.1 joerg }
1990 1.1 joerg
1991 1.1 joerg /* update Rx filter */
1992 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
1993 1.1 joerg
1994 1.1 joerg tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
1995 1.1 joerg if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1996 1.1 joerg tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
1997 1.1 joerg RT2573_DROP_ACKCTS;
1998 1.1 joerg if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1999 1.1 joerg tmp |= RT2573_DROP_TODS;
2000 1.1 joerg if (!(ifp->if_flags & IFF_PROMISC))
2001 1.1 joerg tmp |= RT2573_DROP_NOT_TO_ME;
2002 1.1 joerg }
2003 1.1 joerg rum_write(sc, RT2573_TXRX_CSR0, tmp);
2004 1.1 joerg
2005 1.1 joerg ifp->if_flags &= ~IFF_OACTIVE;
2006 1.1 joerg ifp->if_flags |= IFF_RUNNING;
2007 1.1 joerg
2008 1.1 joerg if (ic->ic_opmode == IEEE80211_M_MONITOR)
2009 1.1 joerg ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2010 1.1 joerg else
2011 1.1 joerg ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2012 1.1 joerg
2013 1.1 joerg return 0;
2014 1.1 joerg
2015 1.1 joerg fail: rum_stop(ifp, 1);
2016 1.1 joerg return error;
2017 1.1 joerg #undef N
2018 1.1 joerg }
2019 1.1 joerg
2020 1.1 joerg Static void
2021 1.1 joerg rum_stop(struct ifnet *ifp, int disable)
2022 1.1 joerg {
2023 1.1 joerg struct rum_softc *sc = ifp->if_softc;
2024 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
2025 1.1 joerg uint32_t tmp;
2026 1.1 joerg
2027 1.1 joerg ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2028 1.1 joerg
2029 1.1 joerg sc->sc_tx_timer = 0;
2030 1.1 joerg ifp->if_timer = 0;
2031 1.1 joerg ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2032 1.1 joerg
2033 1.1 joerg /* disable Rx */
2034 1.1 joerg tmp = rum_read(sc, RT2573_TXRX_CSR0);
2035 1.1 joerg rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2036 1.1 joerg
2037 1.1 joerg /* reset ASIC */
2038 1.1 joerg rum_write(sc, RT2573_MAC_CSR1, 3);
2039 1.1 joerg rum_write(sc, RT2573_MAC_CSR1, 0);
2040 1.1 joerg
2041 1.6.4.2 itohy /* Stop transfers. */
2042 1.6.4.2 itohy if (sc->sc_rx_pipeh != NULL)
2043 1.1 joerg usbd_abort_pipe(sc->sc_rx_pipeh);
2044 1.6.4.2 itohy if (sc->sc_tx_pipeh != NULL)
2045 1.6.4.2 itohy usbd_abort_pipe(sc->sc_tx_pipeh);
2046 1.6.4.2 itohy
2047 1.6.4.2 itohy /* Free RX/TX resources. */
2048 1.6.4.2 itohy rum_free_rx_list(sc);
2049 1.6.4.2 itohy rum_free_tx_list(sc);
2050 1.6.4.2 itohy
2051 1.6.4.2 itohy /* Close pipes. */
2052 1.6.4.2 itohy if (sc->sc_rx_pipeh != NULL) {
2053 1.1 joerg usbd_close_pipe(sc->sc_rx_pipeh);
2054 1.1 joerg sc->sc_rx_pipeh = NULL;
2055 1.1 joerg }
2056 1.1 joerg
2057 1.1 joerg if (sc->sc_tx_pipeh != NULL) {
2058 1.1 joerg usbd_close_pipe(sc->sc_tx_pipeh);
2059 1.1 joerg sc->sc_tx_pipeh = NULL;
2060 1.1 joerg }
2061 1.1 joerg }
2062 1.1 joerg
2063 1.1 joerg Static int
2064 1.1 joerg rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2065 1.1 joerg {
2066 1.1 joerg usb_device_request_t req;
2067 1.1 joerg uint16_t reg = RT2573_MCU_CODE_BASE;
2068 1.1 joerg usbd_status error;
2069 1.1 joerg
2070 1.1 joerg /* copy firmware image into NIC */
2071 1.1 joerg for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2072 1.1 joerg rum_write(sc, reg, UGETDW(ucode));
2073 1.1 joerg
2074 1.1 joerg req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2075 1.1 joerg req.bRequest = RT2573_MCU_CNTL;
2076 1.1 joerg USETW(req.wValue, RT2573_MCU_RUN);
2077 1.1 joerg USETW(req.wIndex, 0);
2078 1.1 joerg USETW(req.wLength, 0);
2079 1.1 joerg
2080 1.1 joerg error = usbd_do_request(sc->sc_udev, &req, NULL);
2081 1.1 joerg if (error != 0) {
2082 1.1 joerg printf("%s: could not run firmware: %s\n",
2083 1.1 joerg USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2084 1.1 joerg }
2085 1.1 joerg return error;
2086 1.1 joerg }
2087 1.1 joerg
2088 1.1 joerg Static int
2089 1.1 joerg rum_prepare_beacon(struct rum_softc *sc)
2090 1.1 joerg {
2091 1.1 joerg struct ieee80211com *ic = &sc->sc_ic;
2092 1.1 joerg struct rum_tx_desc desc;
2093 1.1 joerg struct mbuf *m0;
2094 1.1 joerg int rate;
2095 1.1 joerg
2096 1.1 joerg m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2097 1.1 joerg if (m0 == NULL) {
2098 1.1 joerg printf("%s: could not allocate beacon frame\n",
2099 1.1 joerg sc->sc_dev.dv_xname);
2100 1.1 joerg return ENOBUFS;
2101 1.1 joerg }
2102 1.1 joerg
2103 1.1 joerg /* send beacons at the lowest available rate */
2104 1.1 joerg rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2105 1.1 joerg
2106 1.1 joerg rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2107 1.1 joerg m0->m_pkthdr.len, rate);
2108 1.1 joerg
2109 1.1 joerg /* copy the first 24 bytes of Tx descriptor into NIC memory */
2110 1.1 joerg rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2111 1.1 joerg
2112 1.1 joerg /* copy beacon header and payload into NIC memory */
2113 1.1 joerg rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2114 1.1 joerg m0->m_pkthdr.len);
2115 1.1 joerg
2116 1.1 joerg m_freem(m0);
2117 1.1 joerg
2118 1.1 joerg return 0;
2119 1.1 joerg }
2120 1.1 joerg
2121 1.1 joerg Static void
2122 1.1 joerg rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2123 1.1 joerg {
2124 1.1 joerg int i;
2125 1.1 joerg
2126 1.1 joerg /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2127 1.1 joerg rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2128 1.1 joerg
2129 1.1 joerg ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2130 1.1 joerg
2131 1.1 joerg /* set rate to some reasonable initial value */
2132 1.1 joerg for (i = ni->ni_rates.rs_nrates - 1;
2133 1.1 joerg i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2134 1.1 joerg i--);
2135 1.1 joerg ni->ni_txrate = i;
2136 1.1 joerg
2137 1.1 joerg callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2138 1.1 joerg }
2139 1.1 joerg
2140 1.1 joerg Static void
2141 1.1 joerg rum_amrr_timeout(void *arg)
2142 1.1 joerg {
2143 1.1 joerg struct rum_softc *sc = arg;
2144 1.1 joerg usb_device_request_t req;
2145 1.1 joerg int s;
2146 1.1 joerg
2147 1.1 joerg s = splusb();
2148 1.1 joerg
2149 1.1 joerg /*
2150 1.1 joerg * Asynchronously read statistic registers (cleared by read).
2151 1.1 joerg */
2152 1.1 joerg req.bmRequestType = UT_READ_VENDOR_DEVICE;
2153 1.1 joerg req.bRequest = RT2573_READ_MULTI_MAC;
2154 1.1 joerg USETW(req.wValue, 0);
2155 1.1 joerg USETW(req.wIndex, RT2573_STA_CSR0);
2156 1.1 joerg USETW(req.wLength, sizeof sc->sta);
2157 1.1 joerg
2158 1.1 joerg usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2159 1.1 joerg USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2160 1.1 joerg rum_amrr_update);
2161 1.1 joerg (void)usbd_transfer(sc->amrr_xfer);
2162 1.1 joerg
2163 1.1 joerg splx(s);
2164 1.1 joerg }
2165 1.1 joerg
2166 1.1 joerg Static void
2167 1.1 joerg rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2168 1.1 joerg usbd_status status)
2169 1.1 joerg {
2170 1.1 joerg struct rum_softc *sc = (struct rum_softc *)priv;
2171 1.1 joerg struct ifnet *ifp = sc->sc_ic.ic_ifp;
2172 1.1 joerg
2173 1.1 joerg if (status != USBD_NORMAL_COMPLETION) {
2174 1.1 joerg printf("%s: could not retrieve Tx statistics - cancelling "
2175 1.1 joerg "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2176 1.1 joerg return;
2177 1.1 joerg }
2178 1.1 joerg
2179 1.1 joerg /* count TX retry-fail as Tx errors */
2180 1.1 joerg ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2181 1.1 joerg
2182 1.1 joerg sc->amn.amn_retrycnt =
2183 1.1 joerg (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2184 1.1 joerg (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2185 1.1 joerg (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2186 1.1 joerg
2187 1.1 joerg sc->amn.amn_txcnt =
2188 1.1 joerg sc->amn.amn_retrycnt +
2189 1.1 joerg (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2190 1.1 joerg
2191 1.1 joerg ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2192 1.1 joerg
2193 1.1 joerg callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2194 1.1 joerg }
2195 1.1 joerg
2196 1.1 joerg int
2197 1.1 joerg rum_activate(device_ptr_t self, enum devact act)
2198 1.1 joerg {
2199 1.1 joerg switch (act) {
2200 1.1 joerg case DVACT_ACTIVATE:
2201 1.1 joerg return EOPNOTSUPP;
2202 1.1 joerg
2203 1.1 joerg case DVACT_DEACTIVATE:
2204 1.1 joerg /*if_deactivate(&sc->sc_ic.ic_if);*/
2205 1.1 joerg break;
2206 1.1 joerg }
2207 1.1 joerg
2208 1.1 joerg return 0;
2209 1.1 joerg }
2210