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