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