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