if_ural.c revision 1.3 1 /* $NetBSD: if_ural.c,v 1.3 2005/07/06 23:44:17 dyoung Exp $ */
2 /* $OpenBSD: if_ral.c,v 1.36 2005/06/20 18:54:59 damien Exp $ */
3
4 /*-
5 * Copyright (c) 2005
6 * Damien Bergamini <damien.bergamini (at) free.fr>
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 RT2500USB chipset driver
23 * http://www.ralinktech.com/
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: if_ural.c,v 1.3 2005/07/06 23:44:17 dyoung 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_radiotap.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69
70 #include <dev/usb/if_uralreg.h>
71 #include <dev/usb/if_uralvar.h>
72
73 #ifdef USB_DEBUG
74 #define URAL_DEBUG
75 #endif
76
77 #ifdef URAL_DEBUG
78 #define DPRINTF(x) do { if (ural_debug) logprintf x; } while (0)
79 #define DPRINTFN(n, x) do { if (ural_debug >= (n)) logprintf x; } while (0)
80 int ural_debug = 0;
81 #else
82 #define DPRINTF(x)
83 #define DPRINTFN(n, x)
84 #endif
85
86 /* various supported device vendors/products */
87 static const struct usb_devno ural_devs[] = {
88 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G },
89 { USB_VENDOR_ASUSTEK, USB_PRODUCT_RALINK_RT2570 },
90 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
91 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G },
92 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP },
93 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU },
94 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122 },
95 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 },
96 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI },
97 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570 },
98 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2 },
99 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG },
100 };
101
102 Static int ural_alloc_tx_list(struct ural_softc *);
103 Static void ural_free_tx_list(struct ural_softc *);
104 Static int ural_alloc_rx_list(struct ural_softc *);
105 Static void ural_free_rx_list(struct ural_softc *);
106 Static int ural_media_change(struct ifnet *);
107 Static void ural_next_scan(void *);
108 Static void ural_task(void *);
109 Static int ural_newstate(struct ieee80211com *,
110 enum ieee80211_state, int);
111 Static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
112 usbd_status);
113 Static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
114 usbd_status);
115 Static int ural_ack_rate(int);
116 Static uint16_t ural_txtime(int, int, uint32_t);
117 Static uint8_t ural_plcp_signal(int);
118 Static void ural_setup_tx_desc(struct ural_softc *,
119 struct ural_tx_desc *, uint32_t, int, int);
120 Static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
121 struct ieee80211_node *);
122 Static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
123 struct ieee80211_node *);
124 Static int ural_tx_data(struct ural_softc *, struct mbuf *,
125 struct ieee80211_node *);
126 Static void ural_start(struct ifnet *);
127 Static void ural_watchdog(struct ifnet *);
128 Static int ural_ioctl(struct ifnet *, u_long, caddr_t);
129 Static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
130 int);
131 Static uint16_t ural_read(struct ural_softc *, uint16_t);
132 Static void ural_read_multi(struct ural_softc *, uint16_t, void *,
133 int);
134 Static void ural_write(struct ural_softc *, uint16_t, uint16_t);
135 Static void ural_write_multi(struct ural_softc *, uint16_t, void *,
136 int);
137 Static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
138 Static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
139 Static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
140 Static void ural_set_chan(struct ural_softc *,
141 struct ieee80211_channel *);
142 Static void ural_disable_rf_tune(struct ural_softc *);
143 Static void ural_enable_tsf_sync(struct ural_softc *);
144 Static void ural_set_bssid(struct ural_softc *, uint8_t *);
145 Static void ural_set_macaddr(struct ural_softc *, uint8_t *);
146 Static void ural_update_promisc(struct ural_softc *);
147 Static const char *ural_get_rf(int);
148 Static void ural_read_eeprom(struct ural_softc *);
149 Static int ural_bbp_init(struct ural_softc *);
150 Static void ural_set_txantenna(struct ural_softc *, int);
151 Static void ural_set_rxantenna(struct ural_softc *, int);
152 Static int ural_init(struct ifnet *);
153 Static void ural_stop(struct ifnet *, int);
154
155 /*
156 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
157 */
158 static const struct ieee80211_rateset ural_rateset_11a =
159 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
160
161 static const struct ieee80211_rateset ural_rateset_11b =
162 { 4, { 2, 4, 11, 22 } };
163
164 static const struct ieee80211_rateset ural_rateset_11g =
165 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
166
167 /*
168 * Default values for MAC registers; values taken from the reference driver.
169 */
170 static const struct {
171 uint16_t reg;
172 uint16_t val;
173 } ural_def_mac[] = {
174 { RAL_TXRX_CSR5, 0x8c8d },
175 { RAL_TXRX_CSR6, 0x8b8a },
176 { RAL_TXRX_CSR7, 0x8687 },
177 { RAL_TXRX_CSR8, 0x0085 },
178 { RAL_MAC_CSR13, 0x1111 },
179 { RAL_MAC_CSR14, 0x1e11 },
180 { RAL_TXRX_CSR21, 0xe78f },
181 { RAL_MAC_CSR9, 0xff1d },
182 { RAL_MAC_CSR11, 0x0002 },
183 { RAL_MAC_CSR22, 0x0053 },
184 { RAL_MAC_CSR15, 0x0000 },
185 { RAL_MAC_CSR8, 0x0780 },
186 { RAL_TXRX_CSR19, 0x0000 },
187 { RAL_TXRX_CSR18, 0x005a },
188 { RAL_PHY_CSR2, 0x0000 },
189 { RAL_TXRX_CSR0, 0x1ec0 },
190 { RAL_PHY_CSR4, 0x000f }
191 };
192
193 /*
194 * Default values for BBP registers; values taken from the reference driver.
195 */
196 static const struct {
197 uint8_t reg;
198 uint8_t val;
199 } ural_def_bbp[] = {
200 { 3, 0x02 },
201 { 4, 0x19 },
202 { 14, 0x1c },
203 { 15, 0x30 },
204 { 16, 0xac },
205 { 17, 0x48 },
206 { 18, 0x18 },
207 { 19, 0xff },
208 { 20, 0x1e },
209 { 21, 0x08 },
210 { 22, 0x08 },
211 { 23, 0x08 },
212 { 24, 0x80 },
213 { 25, 0x50 },
214 { 26, 0x08 },
215 { 27, 0x23 },
216 { 30, 0x10 },
217 { 31, 0x2b },
218 { 32, 0xb9 },
219 { 34, 0x12 },
220 { 35, 0x50 },
221 { 39, 0xc4 },
222 { 40, 0x02 },
223 { 41, 0x60 },
224 { 53, 0x10 },
225 { 54, 0x18 },
226 { 56, 0x08 },
227 { 57, 0x10 },
228 { 58, 0x08 },
229 { 61, 0x60 },
230 { 62, 0x10 },
231 { 75, 0xff }
232 };
233
234 /*
235 * Default values for RF register R2 indexed by channel numbers.
236 */
237 static const uint32_t ural_rf2522_r2[] = {
238 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
239 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
240 };
241
242 static const uint32_t ural_rf2523_r2[] = {
243 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
244 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
245 };
246
247 static const uint32_t ural_rf2524_r2[] = {
248 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
249 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
250 };
251
252 static const uint32_t ural_rf2525_r2[] = {
253 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
254 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
255 };
256
257 static const uint32_t ural_rf2525_hi_r2[] = {
258 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
259 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
260 };
261
262 static const uint32_t ural_rf2525e_r2[] = {
263 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
264 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
265 };
266
267 static const uint32_t ural_rf2526_hi_r2[] = {
268 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
269 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
270 };
271
272 static const uint32_t ural_rf2526_r2[] = {
273 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
274 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
275 };
276
277 /*
278 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
279 * values taken from the reference driver.
280 */
281 static const struct {
282 uint8_t chan;
283 uint32_t r1;
284 uint32_t r2;
285 uint32_t r4;
286 } ural_rf5222[] = {
287 /* channels in the 2.4GHz band */
288 { 1, 0x08808, 0x0044d, 0x00282 },
289 { 2, 0x08808, 0x0044e, 0x00282 },
290 { 3, 0x08808, 0x0044f, 0x00282 },
291 { 4, 0x08808, 0x00460, 0x00282 },
292 { 5, 0x08808, 0x00461, 0x00282 },
293 { 6, 0x08808, 0x00462, 0x00282 },
294 { 7, 0x08808, 0x00463, 0x00282 },
295 { 8, 0x08808, 0x00464, 0x00282 },
296 { 9, 0x08808, 0x00465, 0x00282 },
297 { 10, 0x08808, 0x00466, 0x00282 },
298 { 11, 0x08808, 0x00467, 0x00282 },
299 { 12, 0x08808, 0x00468, 0x00282 },
300 { 13, 0x08808, 0x00469, 0x00282 },
301 { 14, 0x08808, 0x0046b, 0x00286 },
302
303 /* channels in the 5.2GHz band */
304 { 36, 0x08804, 0x06225, 0x00287 },
305 { 40, 0x08804, 0x06226, 0x00287 },
306 { 44, 0x08804, 0x06227, 0x00287 },
307 { 48, 0x08804, 0x06228, 0x00287 },
308 { 52, 0x08804, 0x06229, 0x00287 },
309 { 56, 0x08804, 0x0622a, 0x00287 },
310 { 60, 0x08804, 0x0622b, 0x00287 },
311 { 64, 0x08804, 0x0622c, 0x00287 },
312
313 { 100, 0x08804, 0x02200, 0x00283 },
314 { 104, 0x08804, 0x02201, 0x00283 },
315 { 108, 0x08804, 0x02202, 0x00283 },
316 { 112, 0x08804, 0x02203, 0x00283 },
317 { 116, 0x08804, 0x02204, 0x00283 },
318 { 120, 0x08804, 0x02205, 0x00283 },
319 { 124, 0x08804, 0x02206, 0x00283 },
320 { 128, 0x08804, 0x02207, 0x00283 },
321 { 132, 0x08804, 0x02208, 0x00283 },
322 { 136, 0x08804, 0x02209, 0x00283 },
323 { 140, 0x08804, 0x0220a, 0x00283 },
324
325 { 149, 0x08808, 0x02429, 0x00281 },
326 { 153, 0x08808, 0x0242b, 0x00281 },
327 { 157, 0x08808, 0x0242d, 0x00281 },
328 { 161, 0x08808, 0x0242f, 0x00281 }
329 };
330
331 USB_DECLARE_DRIVER(ural);
332
333 USB_MATCH(ural)
334 {
335 USB_MATCH_START(ural, uaa);
336
337 if (uaa->iface != NULL)
338 return UMATCH_NONE;
339
340 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
341 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
342 }
343
344 USB_ATTACH(ural)
345 {
346 USB_ATTACH_START(ural, sc, uaa);
347 struct ieee80211com *ic = &sc->sc_ic;
348 struct ifnet *ifp = &sc->sc_if;
349 usb_interface_descriptor_t *id;
350 usb_endpoint_descriptor_t *ed;
351 usbd_status error;
352 char *devinfop;
353 int i;
354
355 sc->sc_udev = uaa->device;
356
357 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
358 USB_ATTACH_SETUP;
359 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
360 usbd_devinfo_free(devinfop);
361
362 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
363 printf("%s: could not set configuration no\n",
364 USBDEVNAME(sc->sc_dev));
365 USB_ATTACH_ERROR_RETURN;
366 }
367
368 /* get the first interface handle */
369 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
370 &sc->sc_iface);
371 if (error != 0) {
372 printf("%s: could not get interface handle\n",
373 USBDEVNAME(sc->sc_dev));
374 USB_ATTACH_ERROR_RETURN;
375 }
376
377 /*
378 * Find endpoints.
379 */
380 id = usbd_get_interface_descriptor(sc->sc_iface);
381
382 sc->sc_rx_no = sc->sc_tx_no = -1;
383 for (i = 0; i < id->bNumEndpoints; i++) {
384 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
385 if (ed == NULL) {
386 printf("%s: no endpoint descriptor for iface %d\n",
387 USBDEVNAME(sc->sc_dev), i);
388 USB_ATTACH_ERROR_RETURN;
389 }
390
391 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
392 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
393 sc->sc_rx_no = ed->bEndpointAddress;
394 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
395 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
396 sc->sc_tx_no = ed->bEndpointAddress;
397 }
398 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
399 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
400 USB_ATTACH_ERROR_RETURN;
401 }
402
403 usb_init_task(&sc->sc_task, ural_task, sc);
404 callout_init(&sc->scan_ch);
405
406 /* retrieve RT2570 rev. no */
407 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
408
409 /* retrieve MAC address and various other things from EEPROM */
410 ural_read_eeprom(sc);
411
412 printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s, address %s\n",
413 USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev),
414 ether_sprintf(ic->ic_myaddr));
415
416 ic->ic_ifp = ifp;
417 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
418 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
419 ic->ic_state = IEEE80211_S_INIT;
420
421 /* set device capabilities */
422 ic->ic_caps = IEEE80211_C_MONITOR | IEEE80211_C_IBSS |
423 IEEE80211_C_HOSTAP | IEEE80211_C_SHPREAMBLE | IEEE80211_C_SHSLOT |
424 IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | IEEE80211_C_WEP;
425
426 if (sc->rf_rev == RAL_RF_5222) {
427 /* set supported .11a rates */
428 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
429
430 /* set supported .11a channels */
431 for (i = 36; i <= 64; i += 4) {
432 ic->ic_channels[i].ic_freq =
433 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
434 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
435 }
436 for (i = 100; i <= 140; i += 4) {
437 ic->ic_channels[i].ic_freq =
438 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
439 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
440 }
441 for (i = 149; i <= 161; i += 4) {
442 ic->ic_channels[i].ic_freq =
443 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
444 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
445 }
446 }
447
448 /* set supported .11b and .11g rates */
449 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
450 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
451
452 /* set supported .11b and .11g channels (1 through 14) */
453 for (i = 1; i <= 14; i++) {
454 ic->ic_channels[i].ic_freq =
455 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
456 ic->ic_channels[i].ic_flags =
457 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
458 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
459 }
460
461 ifp->if_softc = sc;
462 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
463 ifp->if_init = ural_init;
464 ifp->if_stop = ural_stop;
465 ifp->if_ioctl = ural_ioctl;
466 ifp->if_start = ural_start;
467 ifp->if_watchdog = ural_watchdog;
468 IFQ_SET_READY(&ifp->if_snd);
469 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
470
471 if_attach(ifp);
472 ieee80211_ifattach(ic);
473
474 /* override state transition machine */
475 sc->sc_newstate = ic->ic_newstate;
476 ic->ic_newstate = ural_newstate;
477 ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
478
479 #if NBPFILTER > 0
480 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
481 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
482
483 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
484 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
485 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
486
487 sc->sc_txtap_len = sizeof sc->sc_txtapu;
488 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
489 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
490 #endif
491
492 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
493 USBDEV(sc->sc_dev));
494
495 USB_ATTACH_SUCCESS_RETURN;
496 }
497
498 USB_DETACH(ural)
499 {
500 USB_DETACH_START(ural, sc);
501 struct ieee80211com *ic = &sc->sc_ic;
502 struct ifnet *ifp = &sc->sc_if;
503 int s;
504
505 s = splusb();
506
507 usb_rem_task(sc->sc_udev, &sc->sc_task);
508 callout_stop(&sc->scan_ch);
509
510 if (sc->sc_rx_pipeh != NULL) {
511 usbd_abort_pipe(sc->sc_rx_pipeh);
512 usbd_close_pipe(sc->sc_rx_pipeh);
513 }
514
515 if (sc->sc_tx_pipeh != NULL) {
516 usbd_abort_pipe(sc->sc_tx_pipeh);
517 usbd_close_pipe(sc->sc_tx_pipeh);
518 }
519
520 ural_free_rx_list(sc);
521 ural_free_tx_list(sc);
522
523 #if NBPFILTER > 0
524 bpfdetach(ifp);
525 #endif
526 ieee80211_ifdetach(ic);
527 if_detach(ifp);
528
529 splx(s);
530
531 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
532 USBDEV(sc->sc_dev));
533
534 return 0;
535 }
536
537 Static int
538 ural_alloc_tx_list(struct ural_softc *sc)
539 {
540 struct ural_tx_data *data;
541 int i, error;
542
543 sc->tx_queued = 0;
544
545 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
546 data = &sc->tx_data[i];
547
548 data->sc = sc;
549
550 data->xfer = usbd_alloc_xfer(sc->sc_udev);
551 if (data->xfer == NULL) {
552 printf("%s: could not allocate tx xfer\n",
553 USBDEVNAME(sc->sc_dev));
554 error = ENOMEM;
555 goto fail;
556 }
557
558 data->buf = usbd_alloc_buffer(data->xfer,
559 RAL_TX_DESC_SIZE + MCLBYTES);
560 if (data->buf == NULL) {
561 printf("%s: could not allocate tx buffer\n",
562 USBDEVNAME(sc->sc_dev));
563 error = ENOMEM;
564 goto fail;
565 }
566 }
567
568 return 0;
569
570 fail: ural_free_tx_list(sc);
571 return error;
572 }
573
574 Static void
575 ural_free_tx_list(struct ural_softc *sc)
576 {
577 struct ural_tx_data *data;
578 int i;
579
580 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
581 data = &sc->tx_data[i];
582
583 if (data->xfer != NULL) {
584 usbd_free_xfer(data->xfer);
585 data->xfer = NULL;
586 }
587
588 if (data->ni != NULL) {
589 ieee80211_free_node(data->ni);
590 data->ni = NULL;
591 }
592 }
593 }
594
595 Static int
596 ural_alloc_rx_list(struct ural_softc *sc)
597 {
598 struct ural_rx_data *data;
599 int i, error;
600
601 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
602 data = &sc->rx_data[i];
603
604 data->sc = sc;
605
606 data->xfer = usbd_alloc_xfer(sc->sc_udev);
607 if (data->xfer == NULL) {
608 printf("%s: could not allocate rx xfer\n",
609 USBDEVNAME(sc->sc_dev));
610 error = ENOMEM;
611 goto fail;
612 }
613
614 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
615 printf("%s: could not allocate rx buffer\n",
616 USBDEVNAME(sc->sc_dev));
617 error = ENOMEM;
618 goto fail;
619 }
620
621 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
622 if (data->m == NULL) {
623 printf("%s: could not allocate rx mbuf\n",
624 USBDEVNAME(sc->sc_dev));
625 error = ENOMEM;
626 goto fail;
627 }
628
629 MCLGET(data->m, M_DONTWAIT);
630 if (!(data->m->m_flags & M_EXT)) {
631 printf("%s: could not allocate rx mbuf cluster\n",
632 USBDEVNAME(sc->sc_dev));
633 error = ENOMEM;
634 goto fail;
635 }
636
637 data->buf = mtod(data->m, uint8_t *);
638 }
639
640 return 0;
641
642 fail: ural_free_tx_list(sc);
643 return error;
644 }
645
646 Static void
647 ural_free_rx_list(struct ural_softc *sc)
648 {
649 struct ural_rx_data *data;
650 int i;
651
652 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
653 data = &sc->rx_data[i];
654
655 if (data->xfer != NULL) {
656 usbd_free_xfer(data->xfer);
657 data->xfer = NULL;
658 }
659
660 if (data->m != NULL) {
661 m_freem(data->m);
662 data->m = NULL;
663 }
664 }
665 }
666
667 Static int
668 ural_media_change(struct ifnet *ifp)
669 {
670 int error;
671
672 error = ieee80211_media_change(ifp);
673 if (error != ENETRESET)
674 return error;
675
676 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
677 ural_init(ifp);
678
679 return 0;
680 }
681
682 /*
683 * This function is called periodically (every 200ms) during scanning to
684 * switch from one channel to another.
685 */
686 Static void
687 ural_next_scan(void *arg)
688 {
689 struct ural_softc *sc = arg;
690 struct ieee80211com *ic = &sc->sc_ic;
691
692 if (ic->ic_state == IEEE80211_S_SCAN)
693 ieee80211_next_scan(ic);
694 }
695
696 Static void
697 ural_task(void *arg)
698 {
699 struct ural_softc *sc = arg;
700 struct ieee80211com *ic = &sc->sc_ic;
701 enum ieee80211_state ostate;
702 struct mbuf *m;
703
704 ostate = ic->ic_state;
705
706 switch (sc->sc_state) {
707 case IEEE80211_S_INIT:
708 if (ostate == IEEE80211_S_RUN) {
709 /* abort TSF synchronization */
710 ural_write(sc, RAL_TXRX_CSR19, 0);
711
712 /* force tx led to stop blinking */
713 ural_write(sc, RAL_MAC_CSR20, 0);
714 }
715 break;
716
717 case IEEE80211_S_SCAN:
718 ural_set_chan(sc, ic->ic_bss->ni_chan);
719 callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
720 break;
721
722 case IEEE80211_S_AUTH:
723 ural_set_chan(sc, ic->ic_bss->ni_chan);
724 break;
725
726 case IEEE80211_S_ASSOC:
727 ural_set_chan(sc, ic->ic_bss->ni_chan);
728 break;
729
730 case IEEE80211_S_RUN:
731 ural_set_chan(sc, ic->ic_bss->ni_chan);
732
733 if (ic->ic_opmode != IEEE80211_M_MONITOR)
734 ural_set_bssid(sc, ic->ic_bss->ni_bssid);
735
736 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
737 ic->ic_opmode == IEEE80211_M_IBSS) {
738 m = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
739 if (m == NULL) {
740 printf("%s: could not allocate beacon\n",
741 USBDEVNAME(sc->sc_dev));
742 return;
743 }
744
745 if (ural_tx_bcn(sc, m, ic->ic_bss) != 0) {
746 m_freem(m);
747 printf("%s: could not transmit beacon\n",
748 USBDEVNAME(sc->sc_dev));
749 return;
750 }
751
752 /* beacon is no longer needed */
753 m_freem(m);
754 }
755
756 /* make tx led blink on tx (controlled by ASIC) */
757 ural_write(sc, RAL_MAC_CSR20, 1);
758
759 if (ic->ic_opmode != IEEE80211_M_MONITOR)
760 ural_enable_tsf_sync(sc);
761 break;
762 }
763
764 sc->sc_newstate(ic, sc->sc_state, -1);
765 }
766
767 Static int
768 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
769 {
770 struct ural_softc *sc = ic->ic_ifp->if_softc;
771
772 usb_rem_task(sc->sc_udev, &sc->sc_task);
773 callout_stop(&sc->scan_ch);
774
775 /* do it in a process context */
776 sc->sc_state = nstate;
777 usb_add_task(sc->sc_udev, &sc->sc_task);
778
779 return 0;
780 }
781
782 /* quickly determine if a given rate is CCK or OFDM */
783 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
784
785 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
786 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
787 #define RAL_SIFS 10
788
789 Static void
790 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
791 {
792 struct ural_tx_data *data = priv;
793 struct ural_softc *sc = data->sc;
794 struct ifnet *ifp = &sc->sc_if;
795 int s;
796
797 if (status != USBD_NORMAL_COMPLETION) {
798 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
799 return;
800
801 printf("%s: could not transmit buffer: %s\n",
802 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
803
804 if (status == USBD_STALLED)
805 usbd_clear_endpoint_stall(sc->sc_tx_pipeh);
806
807 ifp->if_oerrors++;
808 return;
809 }
810
811 s = splnet();
812
813 m_freem(data->m);
814 data->m = NULL;
815 ieee80211_free_node(data->ni);
816 data->ni = NULL;
817
818 sc->tx_queued--;
819 ifp->if_opackets++;
820
821 DPRINTFN(10, ("tx done\n"));
822
823 sc->sc_tx_timer = 0;
824 ifp->if_flags &= ~IFF_OACTIVE;
825 ural_start(ifp);
826
827 splx(s);
828 }
829
830 Static void
831 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
832 {
833 struct ural_rx_data *data = priv;
834 struct ural_softc *sc = data->sc;
835 struct ieee80211com *ic = &sc->sc_ic;
836 struct ifnet *ifp = &sc->sc_if;
837 struct ural_rx_desc *desc;
838 struct ieee80211_frame_min *wh;
839 struct ieee80211_node *ni;
840 struct mbuf *m;
841 int s, len;
842
843 if (status != USBD_NORMAL_COMPLETION) {
844 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
845 return;
846
847 if (status == USBD_STALLED)
848 usbd_clear_endpoint_stall(sc->sc_rx_pipeh);
849 goto skip;
850 }
851
852 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
853
854 if (len < RAL_RX_DESC_SIZE) {
855 printf("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev), len);
856 ifp->if_ierrors++;
857 goto skip;
858 }
859
860 /* rx descriptor is located at the end */
861 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
862
863 if (le32toh(desc->flags) & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
864 /*
865 * This should not happen since we did not request to receive
866 * those frames when we filled RAL_TXRX_CSR2.
867 */
868 DPRINTFN(5, ("PHY or CRC error\n"));
869 ifp->if_ierrors++;
870 goto skip;
871 }
872
873 /* finalize mbuf */
874 m = data->m;
875 m->m_pkthdr.rcvif = ifp;
876 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
877 m->m_flags |= M_HASFCS; /* hardware appends FCS */
878
879 s = splnet();
880
881 #if NBPFILTER > 0
882 if (sc->sc_drvbpf != NULL) {
883 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
884
885 tap->wr_flags = 0;
886 tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
887 tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
888 tap->wr_antenna = sc->rx_ant;
889 tap->wr_antsignal = desc->rssi;
890
891 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
892 }
893 #endif
894
895 wh = mtod(m, struct ieee80211_frame_min *);
896 ni = ieee80211_find_rxnode(ic, wh);
897
898 /* send the frame to the 802.11 layer */
899 ieee80211_input(ic, m, ni, desc->rssi, 0);
900
901 /* node is no longer needed */
902 ieee80211_free_node(ni);
903
904 splx(s);
905
906 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
907 if (data->m == NULL) {
908 printf("%s: could not allocate rx mbuf\n",
909 USBDEVNAME(sc->sc_dev));
910 return;
911 }
912
913 MCLGET(data->m, M_DONTWAIT);
914 if (!(data->m->m_flags & M_EXT)) {
915 printf("%s: could not allocate rx mbuf cluster\n",
916 USBDEVNAME(sc->sc_dev));
917 m_freem(data->m);
918 data->m = NULL;
919 return;
920 }
921
922 data->buf = mtod(data->m, uint8_t *);
923
924 DPRINTFN(15, ("rx done\n"));
925
926 skip: /* setup a new transfer */
927 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
928 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
929 usbd_transfer(xfer);
930 }
931
932 /*
933 * Return the expected ack rate for a frame transmitted at rate `rate'.
934 * XXX: this should depend on the destination node basic rate set.
935 */
936 Static int
937 ural_ack_rate(int rate)
938 {
939 switch (rate) {
940 /* CCK rates */
941 case 2:
942 return 2;
943 case 4:
944 case 11:
945 case 22:
946 return 4;
947
948 /* OFDM rates */
949 case 12:
950 case 18:
951 return 12;
952 case 24:
953 case 36:
954 return 24;
955 case 48:
956 case 72:
957 case 96:
958 case 108:
959 return 48;
960 }
961
962 /* default to 1Mbps */
963 return 2;
964 }
965
966 /*
967 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
968 * The function automatically determines the operating mode depending on the
969 * given rate. `flags' indicates whether short preamble is in use or not.
970 */
971 Static uint16_t
972 ural_txtime(int len, int rate, uint32_t flags)
973 {
974 uint16_t txtime;
975 int ceil, dbps;
976
977 if (RAL_RATE_IS_OFDM(rate)) {
978 /*
979 * OFDM TXTIME calculation.
980 * From IEEE Std 802.11a-1999, pp. 37.
981 */
982 dbps = rate * 2; /* data bits per OFDM symbol */
983
984 ceil = (16 + 8 * len + 6) / dbps;
985 if ((16 + 8 * len + 6) % dbps != 0)
986 ceil++;
987
988 txtime = 16 + 4 + 4 * ceil + 6;
989 } else {
990 /*
991 * High Rate TXTIME calculation.
992 * From IEEE Std 802.11b-1999, pp. 28.
993 */
994 ceil = (8 * len * 2) / rate;
995 if ((8 * len * 2) % rate != 0)
996 ceil++;
997
998 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
999 txtime = 72 + 24 + ceil;
1000 else
1001 txtime = 144 + 48 + ceil;
1002 }
1003
1004 return txtime;
1005 }
1006
1007 Static uint8_t
1008 ural_plcp_signal(int rate)
1009 {
1010 switch (rate) {
1011 /* CCK rates (returned values are device-dependent) */
1012 case 2: return 0x0;
1013 case 4: return 0x1;
1014 case 11: return 0x2;
1015 case 22: return 0x3;
1016
1017 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1018 case 12: return 0xb;
1019 case 18: return 0xf;
1020 case 24: return 0xa;
1021 case 36: return 0xe;
1022 case 48: return 0x9;
1023 case 72: return 0xd;
1024 case 96: return 0x8;
1025 case 108: return 0xc;
1026
1027 /* unsupported rates (should not get there) */
1028 default: return 0xff;
1029 }
1030 }
1031
1032 Static void
1033 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1034 uint32_t flags, int len, int rate)
1035 {
1036 struct ieee80211com *ic = &sc->sc_ic;
1037 uint16_t plcp_length;
1038 int remainder;
1039
1040 desc->flags = htole32(flags);
1041 desc->flags |= htole32(RAL_TX_NEWSEQ);
1042 desc->flags |= htole32(len << 16);
1043
1044 if (RAL_RATE_IS_OFDM(rate))
1045 desc->flags |= htole32(RAL_TX_OFDM);
1046
1047 desc->wme = htole16(RAL_LOGCWMAX(5) | RAL_LOGCWMIN(3) | RAL_AIFSN(2));
1048
1049 /*
1050 * Fill PLCP fields.
1051 */
1052 desc->plcp_service = 4;
1053
1054 len += 4; /* account for FCS */
1055 if (RAL_RATE_IS_OFDM(rate)) {
1056 /*
1057 * PLCP length field (LENGTH).
1058 * From IEEE Std 802.11a-1999, pp. 14.
1059 */
1060 plcp_length = len & 0xfff;
1061 desc->plcp_length = htole16((plcp_length >> 6) << 8 |
1062 (plcp_length & 0x3f));
1063 } else {
1064 /*
1065 * Long PLCP LENGTH field.
1066 * From IEEE Std 802.11b-1999, pp. 16.
1067 */
1068 plcp_length = (8 * len * 2) / rate;
1069 remainder = (8 * len * 2) % rate;
1070 if (remainder != 0) {
1071 if (rate == 22 && (rate - remainder) / 16 != 0)
1072 desc->plcp_service |= RAL_PLCP_LENGEXT;
1073 plcp_length++;
1074 }
1075 desc->plcp_length = htole16(plcp_length);
1076 }
1077
1078 desc->plcp_signal = ural_plcp_signal(rate);
1079 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1080 desc->plcp_signal |= 0x08;
1081
1082 desc->iv = 0;
1083 desc->eiv = 0;
1084 }
1085
1086 #define RAL_TX_TIMEOUT 5000
1087
1088 Static int
1089 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1090 {
1091 struct ural_tx_desc *desc;
1092 usbd_xfer_handle xfer;
1093 usbd_status error;
1094 uint8_t cmd = 0;
1095 uint8_t *buf;
1096 int xferlen, rate;
1097
1098 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4;
1099
1100 xfer = usbd_alloc_xfer(sc->sc_udev);
1101 if (xfer == NULL)
1102 return ENOMEM;
1103
1104 /* xfer length needs to be a multiple of two! */
1105 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1106
1107 buf = usbd_alloc_buffer(xfer, xferlen);
1108 if (buf == NULL) {
1109 usbd_free_xfer(xfer);
1110 return ENOMEM;
1111 }
1112
1113 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1114 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1115
1116 error = usbd_sync_transfer(xfer);
1117 if (error != 0) {
1118 usbd_free_xfer(xfer);
1119 return error;
1120 }
1121
1122 desc = (struct ural_tx_desc *)buf;
1123
1124 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1125 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1126 m0->m_pkthdr.len, rate);
1127
1128 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1129 m0->m_pkthdr.len, rate, xferlen));
1130
1131 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1132 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1133
1134 error = usbd_sync_transfer(xfer);
1135 usbd_free_xfer(xfer);
1136
1137 return error;
1138 }
1139
1140 Static int
1141 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1142 {
1143 struct ieee80211com *ic = &sc->sc_ic;
1144 struct ural_tx_desc *desc;
1145 struct ural_tx_data *data;
1146 struct ieee80211_frame *wh;
1147 uint32_t flags = 0;
1148 uint16_t dur;
1149 usbd_status error;
1150 int xferlen, rate;
1151
1152 data = &sc->tx_data[0];
1153 desc = (struct ural_tx_desc *)data->buf;
1154
1155 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4;
1156
1157 #if NBPFILTER > 0
1158 if (sc->sc_drvbpf != NULL) {
1159 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1160
1161 tap->wt_flags = 0;
1162 tap->wt_rate = rate;
1163 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1164 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1165 tap->wt_antenna = sc->tx_ant;
1166
1167 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1168 }
1169 #endif
1170
1171 data->m = m0;
1172 data->ni = ni;
1173
1174 wh = mtod(m0, struct ieee80211_frame *);
1175
1176 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1177 flags |= RAL_TX_ACK;
1178
1179 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
1180 *(uint16_t *)wh->i_dur = htole16(dur);
1181
1182 /* tell hardware to add timestamp for probe responses */
1183 if ((wh->i_fc[0] &
1184 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1185 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1186 flags |= RAL_TX_TIMESTAMP;
1187 }
1188
1189 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1190 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1191
1192 /* xfer length needs to be a multiple of two! */
1193 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1194
1195 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1196 m0->m_pkthdr.len, rate, xferlen));
1197
1198 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1199 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof);
1200
1201 error = usbd_transfer(data->xfer);
1202 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1203 m_freem(m0);
1204 return error;
1205 }
1206
1207 sc->tx_queued++;
1208
1209 return 0;
1210 }
1211
1212 Static int
1213 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1214 {
1215 struct ieee80211com *ic = &sc->sc_ic;
1216 struct ieee80211_rateset *rs;
1217 struct ural_tx_desc *desc;
1218 struct ural_tx_data *data;
1219 struct ieee80211_frame *wh;
1220 struct ieee80211_key *k;
1221 uint32_t flags = 0;
1222 uint16_t dur;
1223 usbd_status error;
1224 int xferlen, rate;
1225
1226 wh = mtod(m0, struct ieee80211_frame *);
1227
1228 /* XXX this should be reworked! */
1229 if (ic->ic_fixed_rate != -1) {
1230 if (ic->ic_curmode != IEEE80211_MODE_AUTO)
1231 rs = &ic->ic_sup_rates[ic->ic_curmode];
1232 else
1233 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
1234
1235 rate = rs->rs_rates[ic->ic_fixed_rate];
1236 } else {
1237 rs = &ni->ni_rates;
1238 rate = rs->rs_rates[ni->ni_txrate];
1239 }
1240 rate &= IEEE80211_RATE_VAL;
1241
1242 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1243 k = ieee80211_crypto_encap(ic, ni, m0);
1244 if (k == NULL) {
1245 m_freem(m0);
1246 return ENOBUFS;
1247 }
1248
1249 /* packet header may have moved, reset our local pointer */
1250 wh = mtod(m0, struct ieee80211_frame *);
1251 }
1252
1253 #if NBPFILTER > 0
1254 if (sc->sc_drvbpf != NULL) {
1255 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1256
1257 tap->wt_flags = 0;
1258 tap->wt_rate = rate;
1259 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1260 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1261 tap->wt_antenna = sc->tx_ant;
1262
1263 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1264 }
1265 #endif
1266
1267 data = &sc->tx_data[0];
1268 desc = (struct ural_tx_desc *)data->buf;
1269
1270 data->m = m0;
1271 data->ni = ni;
1272
1273 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1274 flags |= RAL_TX_ACK;
1275 flags |= RAL_TX_RETRY(7);
1276
1277 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(rate),
1278 ic->ic_flags) + RAL_SIFS;
1279 *(uint16_t *)wh->i_dur = htole16(dur);
1280 }
1281
1282 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1283 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1284
1285 /* xfer length needs to be a multiple of two! */
1286 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1287
1288 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1289 m0->m_pkthdr.len, rate, xferlen));
1290
1291 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1292 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof);
1293
1294 error = usbd_transfer(data->xfer);
1295 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1296 m_freem(m0);
1297 return error;
1298 }
1299
1300 sc->tx_queued++;
1301
1302 return 0;
1303 }
1304
1305 Static void
1306 ural_start(struct ifnet *ifp)
1307 {
1308 struct ural_softc *sc = ifp->if_softc;
1309 struct ieee80211com *ic = &sc->sc_ic;
1310 struct ether_header *eh;
1311 struct ieee80211_node *ni;
1312 struct mbuf *m0;
1313
1314 for (;;) {
1315 IF_POLL(&ic->ic_mgtq, m0);
1316 if (m0 != NULL) {
1317 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1318 ifp->if_flags |= IFF_OACTIVE;
1319 break;
1320 }
1321 IF_DEQUEUE(&ic->ic_mgtq, m0);
1322
1323 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1324 m0->m_pkthdr.rcvif = NULL;
1325 #if NBPFILTER > 0
1326 if (ic->ic_rawbpf != NULL)
1327 bpf_mtap(ic->ic_rawbpf, m0);
1328 #endif
1329 if (ural_tx_mgt(sc, m0, ni) != 0)
1330 break;
1331
1332 } else {
1333 if (ic->ic_state != IEEE80211_S_RUN)
1334 break;
1335 IFQ_DEQUEUE(&ifp->if_snd, m0);
1336 if (m0 == NULL)
1337 break;
1338 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1339 IF_PREPEND(&ifp->if_snd, m0);
1340 ifp->if_flags |= IFF_OACTIVE;
1341 break;
1342 }
1343
1344 if (m0->m_len < sizeof (struct ether_header) &&
1345 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1346 continue;
1347
1348 eh = mtod(m0, struct ether_header *);
1349 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1350 if (ni == NULL) {
1351 m_freem(m0);
1352 continue;
1353 }
1354 #if NBPFILTER > 0
1355 if (ifp->if_bpf != NULL)
1356 bpf_mtap(ifp->if_bpf, m0);
1357 #endif
1358 m0 = ieee80211_encap(ic, m0, ni);
1359 if (m0 == NULL)
1360 continue;
1361 #if NBPFILTER > 0
1362 if (ic->ic_rawbpf != NULL)
1363 bpf_mtap(ic->ic_rawbpf, m0);
1364 #endif
1365 if (ural_tx_data(sc, m0, ni) != 0) {
1366 ieee80211_free_node(ni);
1367 ifp->if_oerrors++;
1368 break;
1369 }
1370 }
1371
1372 sc->sc_tx_timer = 5;
1373 ifp->if_timer = 1;
1374 }
1375 }
1376
1377 Static void
1378 ural_watchdog(struct ifnet *ifp)
1379 {
1380 struct ural_softc *sc = ifp->if_softc;
1381 struct ieee80211com *ic = &sc->sc_ic;
1382
1383 ifp->if_timer = 0;
1384
1385 if (sc->sc_tx_timer > 0) {
1386 if (--sc->sc_tx_timer == 0) {
1387 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1388 /*ural_init(ifp); XXX needs a process context! */
1389 ifp->if_oerrors++;
1390 return;
1391 }
1392 ifp->if_timer = 1;
1393 }
1394
1395 ieee80211_watchdog(ic);
1396 }
1397
1398 Static int
1399 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1400 {
1401 struct ural_softc *sc = ifp->if_softc;
1402 struct ieee80211com *ic = &sc->sc_ic;
1403 struct ifreq *ifr;
1404 int s, error = 0;
1405
1406 s = splnet();
1407
1408 switch (cmd) {
1409 case SIOCSIFFLAGS:
1410 if (ifp->if_flags & IFF_UP) {
1411 if (ifp->if_flags & IFF_RUNNING)
1412 ural_update_promisc(sc);
1413 else
1414 ural_init(ifp);
1415 } else {
1416 if (ifp->if_flags & IFF_RUNNING)
1417 ural_stop(ifp, 1);
1418 }
1419 break;
1420
1421 case SIOCADDMULTI:
1422 case SIOCDELMULTI:
1423 ifr = (struct ifreq *)data;
1424 error = (cmd == SIOCADDMULTI) ?
1425 ether_addmulti(ifr, &sc->sc_ec) :
1426 ether_delmulti(ifr, &sc->sc_ec);
1427
1428 if (error == ENETRESET)
1429 error = 0;
1430 break;
1431
1432 case SIOCS80211CHANNEL:
1433 /*
1434 * This allows for fast channel switching in monitor mode
1435 * (used by kismet). In IBSS mode, we must explicitly reset
1436 * the interface to generate a new beacon frame.
1437 */
1438 error = ieee80211_ioctl(ic, cmd, data);
1439 if (error == ENETRESET &&
1440 ic->ic_opmode == IEEE80211_M_MONITOR) {
1441 ural_set_chan(sc, ic->ic_ibss_chan);
1442 error = 0;
1443 }
1444 break;
1445
1446 default:
1447 error = ieee80211_ioctl(ic, cmd, data);
1448 }
1449
1450 if (error == ENETRESET) {
1451 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1452 (IFF_UP | IFF_RUNNING))
1453 ural_init(ifp);
1454 error = 0;
1455 }
1456
1457 splx(s);
1458
1459 return error;
1460 }
1461
1462 Static void
1463 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1464 {
1465 usb_device_request_t req;
1466 usbd_status error;
1467
1468 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1469 req.bRequest = RAL_READ_EEPROM;
1470 USETW(req.wValue, 0);
1471 USETW(req.wIndex, addr);
1472 USETW(req.wLength, len);
1473
1474 error = usbd_do_request(sc->sc_udev, &req, buf);
1475 if (error != 0) {
1476 printf("%s: could not read EEPROM: %s\n",
1477 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1478 }
1479 }
1480
1481 Static uint16_t
1482 ural_read(struct ural_softc *sc, uint16_t reg)
1483 {
1484 usb_device_request_t req;
1485 usbd_status error;
1486 uint16_t val;
1487
1488 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1489 req.bRequest = RAL_READ_MAC;
1490 USETW(req.wValue, 0);
1491 USETW(req.wIndex, reg);
1492 USETW(req.wLength, sizeof (uint16_t));
1493
1494 error = usbd_do_request(sc->sc_udev, &req, &val);
1495 if (error != 0) {
1496 printf("%s: could not read MAC register: %s\n",
1497 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1498 return 0;
1499 }
1500
1501 return le16toh(val);
1502 }
1503
1504 Static void
1505 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1506 {
1507 usb_device_request_t req;
1508 usbd_status error;
1509
1510 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1511 req.bRequest = RAL_READ_MULTI_MAC;
1512 USETW(req.wValue, 0);
1513 USETW(req.wIndex, reg);
1514 USETW(req.wLength, len);
1515
1516 error = usbd_do_request(sc->sc_udev, &req, buf);
1517 if (error != 0) {
1518 printf("%s: could not read MAC register: %s\n",
1519 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1520 return;
1521 }
1522 }
1523
1524 Static void
1525 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1526 {
1527 usb_device_request_t req;
1528 usbd_status error;
1529
1530 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1531 req.bRequest = RAL_WRITE_MAC;
1532 USETW(req.wValue, val);
1533 USETW(req.wIndex, reg);
1534 USETW(req.wLength, 0);
1535
1536 error = usbd_do_request(sc->sc_udev, &req, NULL);
1537 if (error != 0) {
1538 printf("%s: could not write MAC register: %s\n",
1539 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1540 }
1541 }
1542
1543 Static void
1544 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1545 {
1546 usb_device_request_t req;
1547 usbd_status error;
1548
1549 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1550 req.bRequest = RAL_WRITE_MULTI_MAC;
1551 USETW(req.wValue, 0);
1552 USETW(req.wIndex, reg);
1553 USETW(req.wLength, len);
1554
1555 error = usbd_do_request(sc->sc_udev, &req, buf);
1556 if (error != 0) {
1557 printf("%s: could not write MAC register: %s\n",
1558 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1559 }
1560 }
1561
1562 Static void
1563 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1564 {
1565 uint16_t tmp;
1566 int ntries;
1567
1568 for (ntries = 0; ntries < 5; ntries++) {
1569 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1570 break;
1571 }
1572 if (ntries == 5) {
1573 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1574 return;
1575 }
1576
1577 tmp = reg << 8 | val;
1578 ural_write(sc, RAL_PHY_CSR7, tmp);
1579 }
1580
1581 Static uint8_t
1582 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1583 {
1584 uint16_t val;
1585 int ntries;
1586
1587 val = RAL_BBP_WRITE | reg << 8;
1588 ural_write(sc, RAL_PHY_CSR7, val);
1589
1590 for (ntries = 0; ntries < 5; ntries++) {
1591 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1592 break;
1593 }
1594 if (ntries == 5) {
1595 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1596 return 0;
1597 }
1598
1599 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1600 }
1601
1602 Static void
1603 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1604 {
1605 uint32_t tmp;
1606 int ntries;
1607
1608 for (ntries = 0; ntries < 5; ntries++) {
1609 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1610 break;
1611 }
1612 if (ntries == 5) {
1613 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1614 return;
1615 }
1616
1617 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1618 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1619 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1620
1621 /* remember last written value in sc */
1622 sc->rf_regs[reg] = val;
1623
1624 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1625 }
1626
1627 Static void
1628 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1629 {
1630 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1631 struct ieee80211com *ic = &sc->sc_ic;
1632 uint8_t power, tmp;
1633 u_int i, chan;
1634
1635 chan = ieee80211_chan2ieee(ic, c);
1636 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1637 return;
1638
1639 if (IEEE80211_IS_CHAN_2GHZ(c))
1640 power = min(sc->txpow[chan - 1], 31);
1641 else
1642 power = 31;
1643
1644 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1645
1646 switch (sc->rf_rev) {
1647 case RAL_RF_2522:
1648 ural_rf_write(sc, RAL_RF1, 0x00814);
1649 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1650 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1651 break;
1652
1653 case RAL_RF_2523:
1654 ural_rf_write(sc, RAL_RF1, 0x08804);
1655 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1656 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1657 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1658 break;
1659
1660 case RAL_RF_2524:
1661 ural_rf_write(sc, RAL_RF1, 0x0c808);
1662 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1663 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1664 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1665 break;
1666
1667 case RAL_RF_2525:
1668 ural_rf_write(sc, RAL_RF1, 0x08808);
1669 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1670 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1671 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1672
1673 ural_rf_write(sc, RAL_RF1, 0x08808);
1674 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1675 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1676 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1677 break;
1678
1679 case RAL_RF_2525E:
1680 ural_rf_write(sc, RAL_RF1, 0x08808);
1681 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1682 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1683 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1684 break;
1685
1686 case RAL_RF_2526:
1687 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1688 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1689 ural_rf_write(sc, RAL_RF1, 0x08804);
1690
1691 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1692 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1693 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1694 break;
1695
1696 /* dual-band RF */
1697 case RAL_RF_5222:
1698 for (i = 0; i < N(ural_rf5222); i++)
1699 if (ural_rf5222[i].chan == chan)
1700 break;
1701
1702 if (i < N(ural_rf5222)) {
1703 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1704 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1705 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1706 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1707 }
1708 break;
1709 }
1710
1711 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1712 ic->ic_state != IEEE80211_S_SCAN) {
1713 /* set Japan filter bit for channel 14 */
1714 tmp = ural_bbp_read(sc, 70);
1715
1716 tmp &= ~RAL_JAPAN_FILTER;
1717 if (chan == 14)
1718 tmp |= RAL_JAPAN_FILTER;
1719
1720 ural_bbp_write(sc, 70, tmp);
1721
1722 /* clear CRC errors */
1723 ural_read(sc, RAL_STA_CSR0);
1724
1725 DELAY(1000); /* RF needs a 1ms delay here */
1726 ural_disable_rf_tune(sc);
1727 }
1728 #undef N
1729 }
1730
1731 /*
1732 * Disable RF auto-tuning.
1733 */
1734 Static void
1735 ural_disable_rf_tune(struct ural_softc *sc)
1736 {
1737 uint32_t tmp;
1738
1739 if (sc->rf_rev != RAL_RF_2523) {
1740 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1741 ural_rf_write(sc, RAL_RF1, tmp);
1742 }
1743
1744 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1745 ural_rf_write(sc, RAL_RF3, tmp);
1746
1747 DPRINTFN(2, ("disabling RF autotune\n"));
1748 }
1749
1750 /*
1751 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1752 * synchronization.
1753 */
1754 Static void
1755 ural_enable_tsf_sync(struct ural_softc *sc)
1756 {
1757 struct ieee80211com *ic = &sc->sc_ic;
1758 uint16_t logcwmin, preload, tmp;
1759
1760 /* first, disable TSF synchronization */
1761 ural_write(sc, RAL_TXRX_CSR19, 0);
1762
1763 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1764 ural_write(sc, RAL_TXRX_CSR18, tmp);
1765
1766 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1767 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1768 tmp = logcwmin << 12 | preload;
1769 ural_write(sc, RAL_TXRX_CSR20, tmp);
1770
1771 /* finally, enable TSF synchronization */
1772 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1773 if (ic->ic_opmode == IEEE80211_M_STA)
1774 tmp |= RAL_ENABLE_TSF_SYNC(1);
1775 else
1776 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1777 ural_write(sc, RAL_TXRX_CSR19, tmp);
1778
1779 DPRINTF(("enabling TSF synchronization\n"));
1780 }
1781
1782 Static void
1783 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1784 {
1785 uint16_t tmp;
1786
1787 tmp = bssid[0] | bssid[1] << 8;
1788 ural_write(sc, RAL_MAC_CSR5, tmp);
1789
1790 tmp = bssid[2] | bssid[3] << 8;
1791 ural_write(sc, RAL_MAC_CSR6, tmp);
1792
1793 tmp = bssid[4] | bssid[5] << 8;
1794 ural_write(sc, RAL_MAC_CSR7, tmp);
1795
1796 DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
1797 }
1798
1799 Static void
1800 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1801 {
1802 uint16_t tmp;
1803
1804 tmp = addr[0] | addr[1] << 8;
1805 ural_write(sc, RAL_MAC_CSR2, tmp);
1806
1807 tmp = addr[2] | addr[3] << 8;
1808 ural_write(sc, RAL_MAC_CSR3, tmp);
1809
1810 tmp = addr[4] | addr[5] << 8;
1811 ural_write(sc, RAL_MAC_CSR4, tmp);
1812
1813 DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
1814 }
1815
1816 Static void
1817 ural_update_promisc(struct ural_softc *sc)
1818 {
1819 struct ifnet *ifp = &sc->sc_if;
1820 uint16_t tmp;
1821
1822 tmp = ural_read(sc, RAL_TXRX_CSR2);
1823
1824 tmp &= ~RAL_DROP_NOT_TO_ME;
1825 if (!(ifp->if_flags & IFF_PROMISC))
1826 tmp |= RAL_DROP_NOT_TO_ME;
1827
1828 ural_write(sc, RAL_TXRX_CSR2, tmp);
1829
1830 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1831 "entering" : "leaving"));
1832 }
1833
1834 Static const char *
1835 ural_get_rf(int rev)
1836 {
1837 switch (rev) {
1838 case RAL_RF_2522: return "RT2522";
1839 case RAL_RF_2523: return "RT2523";
1840 case RAL_RF_2524: return "RT2524";
1841 case RAL_RF_2525: return "RT2525";
1842 case RAL_RF_2525E: return "RT2525e";
1843 case RAL_RF_2526: return "RT2526";
1844 case RAL_RF_5222: return "RT5222";
1845 default: return "unknown";
1846 }
1847 }
1848
1849 Static void
1850 ural_read_eeprom(struct ural_softc *sc)
1851 {
1852 struct ieee80211com *ic = &sc->sc_ic;
1853 uint16_t val;
1854
1855 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1856 val = le16toh(val);
1857 sc->rf_rev = (val >> 11) & 0x7;
1858 sc->hw_radio = (val >> 10) & 0x1;
1859 sc->led_mode = (val >> 6) & 0x7;
1860 sc->rx_ant = (val >> 4) & 0x3;
1861 sc->tx_ant = (val >> 2) & 0x3;
1862 sc->nb_ant = val & 0x3;
1863
1864 /* read MAC address */
1865 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1866
1867 /* read default values for BBP registers */
1868 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1869
1870 /* read Tx power for all b/g channels */
1871 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
1872 }
1873
1874 Static int
1875 ural_bbp_init(struct ural_softc *sc)
1876 {
1877 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1878 int i, ntries;
1879
1880 /* wait for BBP to be ready */
1881 for (ntries = 0; ntries < 100; ntries++) {
1882 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
1883 break;
1884 DELAY(1000);
1885 }
1886 if (ntries == 100) {
1887 printf("%s: timeout waiting for BBP\n", USBDEVNAME(sc->sc_dev));
1888 return EIO;
1889 }
1890
1891 /* initialize BBP registers to default values */
1892 for (i = 0; i < N(ural_def_bbp); i++)
1893 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
1894
1895 #if 0
1896 /* initialize BBP registers to values stored in EEPROM */
1897 for (i = 0; i < 16; i++) {
1898 if (sc->bbp_prom[i].reg == 0xff)
1899 continue;
1900 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1901 }
1902 #endif
1903
1904 return 0;
1905 #undef N
1906 }
1907
1908 Static void
1909 ural_set_txantenna(struct ural_softc *sc, int antenna)
1910 {
1911 uint16_t tmp;
1912 uint8_t tx;
1913
1914 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
1915 if (antenna == 1)
1916 tx |= RAL_BBP_ANTA;
1917 else if (antenna == 2)
1918 tx |= RAL_BBP_ANTB;
1919 else
1920 tx |= RAL_BBP_DIVERSITY;
1921
1922 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
1923 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
1924 sc->rf_rev == RAL_RF_5222)
1925 tx |= RAL_BBP_FLIPIQ;
1926
1927 ural_bbp_write(sc, RAL_BBP_TX, tx);
1928
1929 /* update flags in PHY_CSR5 and PHY_CSR6 too */
1930 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
1931 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
1932
1933 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
1934 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
1935 }
1936
1937 Static void
1938 ural_set_rxantenna(struct ural_softc *sc, int antenna)
1939 {
1940 uint8_t rx;
1941
1942 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
1943 if (antenna == 1)
1944 rx |= RAL_BBP_ANTA;
1945 else if (antenna == 2)
1946 rx |= RAL_BBP_ANTB;
1947 else
1948 rx |= RAL_BBP_DIVERSITY;
1949
1950 /* need to force no I/Q flip for RF 2525e and 2526 */
1951 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
1952 rx &= ~RAL_BBP_FLIPIQ;
1953
1954 ural_bbp_write(sc, RAL_BBP_RX, rx);
1955 }
1956
1957 Static int
1958 ural_init(struct ifnet *ifp)
1959 {
1960 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1961 struct ural_softc *sc = ifp->if_softc;
1962 struct ieee80211com *ic = &sc->sc_ic;
1963 struct ieee80211_key *wk;
1964 struct ural_rx_data *data;
1965 uint16_t sta[11], tmp;
1966 usbd_status error;
1967 int i, ntries;
1968
1969 ural_stop(ifp, 0);
1970
1971 /* initialize MAC registers to default values */
1972 for (i = 0; i < N(ural_def_mac); i++)
1973 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
1974
1975 /* wait for BBP and RF to wake up (this can take a long time!) */
1976 for (ntries = 0; ntries < 100; ntries++) {
1977 tmp = ural_read(sc, RAL_MAC_CSR17);
1978 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
1979 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
1980 break;
1981 DELAY(1000);
1982 }
1983 if (ntries == 100) {
1984 printf("%s: timeout waiting for BBP/RF to wakeup\n",
1985 USBDEVNAME(sc->sc_dev));
1986 error = EIO;
1987 goto fail;
1988 }
1989
1990 /* we're ready! */
1991 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
1992
1993 /* set supported basic rates (1, 2, 6, 12, 24) */
1994 ural_write(sc, RAL_TXRX_CSR11, 0x153);
1995
1996 error = ural_bbp_init(sc);
1997 if (error != 0)
1998 goto fail;
1999
2000 /* set default BSS channel */
2001 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2002 ural_set_chan(sc, ic->ic_bss->ni_chan);
2003
2004 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2005 ural_read_multi(sc, RAL_STA_CSR0, sta, sizeof sta);
2006
2007 /* set default sensitivity */
2008 ural_bbp_write(sc, 17, 0x48);
2009
2010 ural_set_txantenna(sc, 1);
2011 ural_set_rxantenna(sc, 1);
2012
2013 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2014 ural_set_macaddr(sc, ic->ic_myaddr);
2015
2016 /*
2017 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
2018 */
2019 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2020 wk = &ic->ic_nw_keys[i];
2021 ural_write_multi(sc, RAL_SEC_CSR0 + i * IEEE80211_KEYBUF_SIZE,
2022 wk->wk_key, IEEE80211_KEYBUF_SIZE);
2023 }
2024
2025 /*
2026 * Open Tx and Rx USB bulk pipes.
2027 */
2028 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2029 &sc->sc_tx_pipeh);
2030 if (error != 0) {
2031 printf("%s: could not open Tx pipe: %s\n",
2032 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2033 goto fail;
2034 }
2035
2036 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2037 &sc->sc_rx_pipeh);
2038 if (error != 0) {
2039 printf("%s: could not open Rx pipe: %s\n",
2040 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2041 goto fail;
2042 }
2043
2044 /*
2045 * Allocate Tx and Rx xfer queues.
2046 */
2047 error = ural_alloc_tx_list(sc);
2048 if (error != 0) {
2049 printf("%s: could not allocate Tx list\n",
2050 USBDEVNAME(sc->sc_dev));
2051 goto fail;
2052 }
2053
2054 error = ural_alloc_rx_list(sc);
2055 if (error != 0) {
2056 printf("%s: could not allocate Rx list\n",
2057 USBDEVNAME(sc->sc_dev));
2058 goto fail;
2059 }
2060
2061 /*
2062 * Start up the receive pipe.
2063 */
2064 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2065 data = &sc->rx_data[i];
2066
2067 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2068 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2069 usbd_transfer(data->xfer);
2070 }
2071
2072 /* kick Rx */
2073 tmp = RAL_DROP_PHY_ERROR | RAL_DROP_CRC_ERROR;
2074 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2075 tmp |= RAL_DROP_CTL | RAL_DROP_VERSION_ERROR;
2076 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2077 tmp |= RAL_DROP_TODS;
2078 if (!(ifp->if_flags & IFF_PROMISC))
2079 tmp |= RAL_DROP_NOT_TO_ME;
2080 }
2081 ural_write(sc, RAL_TXRX_CSR2, tmp);
2082
2083 ifp->if_flags &= ~IFF_OACTIVE;
2084 ifp->if_flags |= IFF_RUNNING;
2085
2086 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2087 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2088 else
2089 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2090
2091 return 0;
2092
2093 fail: ural_stop(ifp, 1);
2094 return error;
2095 #undef N
2096 }
2097
2098 Static void
2099 ural_stop(struct ifnet *ifp, int disable)
2100 {
2101 struct ural_softc *sc = ifp->if_softc;
2102 struct ieee80211com *ic = &sc->sc_ic;
2103
2104 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2105
2106 /* disable Rx */
2107 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2108
2109 /* reset ASIC and BBP (but won't reset MAC registers!) */
2110 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2111 ural_write(sc, RAL_MAC_CSR1, 0);
2112
2113 sc->sc_tx_timer = 0;
2114 ifp->if_timer = 0;
2115 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2116
2117 if (sc->sc_rx_pipeh != NULL) {
2118 usbd_abort_pipe(sc->sc_rx_pipeh);
2119 usbd_close_pipe(sc->sc_rx_pipeh);
2120 sc->sc_rx_pipeh = NULL;
2121 }
2122
2123 if (sc->sc_tx_pipeh != NULL) {
2124 usbd_abort_pipe(sc->sc_tx_pipeh);
2125 usbd_close_pipe(sc->sc_tx_pipeh);
2126 sc->sc_tx_pipeh = NULL;
2127 }
2128
2129 ural_free_rx_list(sc);
2130 ural_free_tx_list(sc);
2131 }
2132
2133 int
2134 ural_activate(device_ptr_t self, enum devact act)
2135 {
2136 struct ural_softc *sc = (struct ural_softc *)self;
2137
2138 switch (act) {
2139 case DVACT_ACTIVATE:
2140 return EOPNOTSUPP;
2141 break;
2142
2143 case DVACT_DEACTIVATE:
2144 if_deactivate(&sc->sc_if);
2145 break;
2146 }
2147
2148 return 0;
2149 }
2150