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