if_ural.c revision 1.2 1 /* $NetBSD: if_ural.c,v 1.2 2005/07/04 17:46:31 drochner 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.2 2005/07/04 17:46:31 drochner 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 return ENOBUFS;
1246
1247 /* packet header may have moved, reset our local pointer */
1248 wh = mtod(m0, struct ieee80211_frame *);
1249 }
1250
1251 #if NBPFILTER > 0
1252 if (sc->sc_drvbpf != NULL) {
1253 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1254
1255 tap->wt_flags = 0;
1256 tap->wt_rate = rate;
1257 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1258 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1259 tap->wt_antenna = sc->tx_ant;
1260
1261 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1262 }
1263 #endif
1264
1265 data = &sc->tx_data[0];
1266 desc = (struct ural_tx_desc *)data->buf;
1267
1268 data->m = m0;
1269 data->ni = ni;
1270
1271 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1272 flags |= RAL_TX_ACK;
1273 flags |= RAL_TX_RETRY(7);
1274
1275 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(rate),
1276 ic->ic_flags) + RAL_SIFS;
1277 *(uint16_t *)wh->i_dur = htole16(dur);
1278 }
1279
1280 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1281 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1282
1283 /* xfer length needs to be a multiple of two! */
1284 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1285
1286 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1287 m0->m_pkthdr.len, rate, xferlen));
1288
1289 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1290 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof);
1291
1292 error = usbd_transfer(data->xfer);
1293 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1294 m_freem(m0);
1295 return error;
1296 }
1297
1298 sc->tx_queued++;
1299
1300 return 0;
1301 }
1302
1303 Static void
1304 ural_start(struct ifnet *ifp)
1305 {
1306 struct ural_softc *sc = ifp->if_softc;
1307 struct ieee80211com *ic = &sc->sc_ic;
1308 struct ether_header *eh;
1309 struct ieee80211_node *ni;
1310 struct mbuf *m0;
1311
1312 for (;;) {
1313 IF_POLL(&ic->ic_mgtq, m0);
1314 if (m0 != NULL) {
1315 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1316 ifp->if_flags |= IFF_OACTIVE;
1317 break;
1318 }
1319 IF_DEQUEUE(&ic->ic_mgtq, m0);
1320
1321 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1322 m0->m_pkthdr.rcvif = NULL;
1323 #if NBPFILTER > 0
1324 if (ic->ic_rawbpf != NULL)
1325 bpf_mtap(ic->ic_rawbpf, m0);
1326 #endif
1327 if (ural_tx_mgt(sc, m0, ni) != 0)
1328 break;
1329
1330 } else {
1331 if (ic->ic_state != IEEE80211_S_RUN)
1332 break;
1333 IFQ_DEQUEUE(&ifp->if_snd, m0);
1334 if (m0 == NULL)
1335 break;
1336 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1337 IF_PREPEND(&ifp->if_snd, m0);
1338 ifp->if_flags |= IFF_OACTIVE;
1339 break;
1340 }
1341
1342 if (m0->m_len < sizeof (struct ether_header) &&
1343 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1344 continue;
1345
1346 eh = mtod(m0, struct ether_header *);
1347 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1348 if (ni == NULL) {
1349 m_freem(m0);
1350 continue;
1351 }
1352 #if NBPFILTER > 0
1353 if (ifp->if_bpf != NULL)
1354 bpf_mtap(ifp->if_bpf, m0);
1355 #endif
1356 m0 = ieee80211_encap(ic, m0, ni);
1357 if (m0 == NULL)
1358 continue;
1359 #if NBPFILTER > 0
1360 if (ic->ic_rawbpf != NULL)
1361 bpf_mtap(ic->ic_rawbpf, m0);
1362 #endif
1363 if (ural_tx_data(sc, m0, ni) != 0) {
1364 ieee80211_free_node(ni);
1365 ifp->if_oerrors++;
1366 break;
1367 }
1368 }
1369
1370 sc->sc_tx_timer = 5;
1371 ifp->if_timer = 1;
1372 }
1373 }
1374
1375 Static void
1376 ural_watchdog(struct ifnet *ifp)
1377 {
1378 struct ural_softc *sc = ifp->if_softc;
1379 struct ieee80211com *ic = &sc->sc_ic;
1380
1381 ifp->if_timer = 0;
1382
1383 if (sc->sc_tx_timer > 0) {
1384 if (--sc->sc_tx_timer == 0) {
1385 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1386 /*ural_init(ifp); XXX needs a process context! */
1387 ifp->if_oerrors++;
1388 return;
1389 }
1390 ifp->if_timer = 1;
1391 }
1392
1393 ieee80211_watchdog(ic);
1394 }
1395
1396 Static int
1397 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1398 {
1399 struct ural_softc *sc = ifp->if_softc;
1400 struct ieee80211com *ic = &sc->sc_ic;
1401 struct ifreq *ifr;
1402 int s, error = 0;
1403
1404 s = splnet();
1405
1406 switch (cmd) {
1407 case SIOCSIFFLAGS:
1408 if (ifp->if_flags & IFF_UP) {
1409 if (ifp->if_flags & IFF_RUNNING)
1410 ural_update_promisc(sc);
1411 else
1412 ural_init(ifp);
1413 } else {
1414 if (ifp->if_flags & IFF_RUNNING)
1415 ural_stop(ifp, 1);
1416 }
1417 break;
1418
1419 case SIOCADDMULTI:
1420 case SIOCDELMULTI:
1421 ifr = (struct ifreq *)data;
1422 error = (cmd == SIOCADDMULTI) ?
1423 ether_addmulti(ifr, &sc->sc_ec) :
1424 ether_delmulti(ifr, &sc->sc_ec);
1425
1426 if (error == ENETRESET)
1427 error = 0;
1428 break;
1429
1430 case SIOCS80211CHANNEL:
1431 /*
1432 * This allows for fast channel switching in monitor mode
1433 * (used by kismet). In IBSS mode, we must explicitly reset
1434 * the interface to generate a new beacon frame.
1435 */
1436 error = ieee80211_ioctl(ic, cmd, data);
1437 if (error == ENETRESET &&
1438 ic->ic_opmode == IEEE80211_M_MONITOR) {
1439 ural_set_chan(sc, ic->ic_ibss_chan);
1440 error = 0;
1441 }
1442 break;
1443
1444 default:
1445 error = ieee80211_ioctl(ic, cmd, data);
1446 }
1447
1448 if (error == ENETRESET) {
1449 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1450 (IFF_UP | IFF_RUNNING))
1451 ural_init(ifp);
1452 error = 0;
1453 }
1454
1455 splx(s);
1456
1457 return error;
1458 }
1459
1460 Static void
1461 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1462 {
1463 usb_device_request_t req;
1464 usbd_status error;
1465
1466 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1467 req.bRequest = RAL_READ_EEPROM;
1468 USETW(req.wValue, 0);
1469 USETW(req.wIndex, addr);
1470 USETW(req.wLength, len);
1471
1472 error = usbd_do_request(sc->sc_udev, &req, buf);
1473 if (error != 0) {
1474 printf("%s: could not read EEPROM: %s\n",
1475 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1476 }
1477 }
1478
1479 Static uint16_t
1480 ural_read(struct ural_softc *sc, uint16_t reg)
1481 {
1482 usb_device_request_t req;
1483 usbd_status error;
1484 uint16_t val;
1485
1486 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1487 req.bRequest = RAL_READ_MAC;
1488 USETW(req.wValue, 0);
1489 USETW(req.wIndex, reg);
1490 USETW(req.wLength, sizeof (uint16_t));
1491
1492 error = usbd_do_request(sc->sc_udev, &req, &val);
1493 if (error != 0) {
1494 printf("%s: could not read MAC register: %s\n",
1495 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1496 return 0;
1497 }
1498
1499 return le16toh(val);
1500 }
1501
1502 Static void
1503 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1504 {
1505 usb_device_request_t req;
1506 usbd_status error;
1507
1508 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1509 req.bRequest = RAL_READ_MULTI_MAC;
1510 USETW(req.wValue, 0);
1511 USETW(req.wIndex, reg);
1512 USETW(req.wLength, len);
1513
1514 error = usbd_do_request(sc->sc_udev, &req, buf);
1515 if (error != 0) {
1516 printf("%s: could not read MAC register: %s\n",
1517 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1518 return;
1519 }
1520 }
1521
1522 Static void
1523 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1524 {
1525 usb_device_request_t req;
1526 usbd_status error;
1527
1528 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1529 req.bRequest = RAL_WRITE_MAC;
1530 USETW(req.wValue, val);
1531 USETW(req.wIndex, reg);
1532 USETW(req.wLength, 0);
1533
1534 error = usbd_do_request(sc->sc_udev, &req, NULL);
1535 if (error != 0) {
1536 printf("%s: could not write MAC register: %s\n",
1537 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1538 }
1539 }
1540
1541 Static void
1542 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1543 {
1544 usb_device_request_t req;
1545 usbd_status error;
1546
1547 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1548 req.bRequest = RAL_WRITE_MULTI_MAC;
1549 USETW(req.wValue, 0);
1550 USETW(req.wIndex, reg);
1551 USETW(req.wLength, len);
1552
1553 error = usbd_do_request(sc->sc_udev, &req, buf);
1554 if (error != 0) {
1555 printf("%s: could not write MAC register: %s\n",
1556 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1557 }
1558 }
1559
1560 Static void
1561 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1562 {
1563 uint16_t tmp;
1564 int ntries;
1565
1566 for (ntries = 0; ntries < 5; ntries++) {
1567 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1568 break;
1569 }
1570 if (ntries == 5) {
1571 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1572 return;
1573 }
1574
1575 tmp = reg << 8 | val;
1576 ural_write(sc, RAL_PHY_CSR7, tmp);
1577 }
1578
1579 Static uint8_t
1580 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1581 {
1582 uint16_t val;
1583 int ntries;
1584
1585 val = RAL_BBP_WRITE | reg << 8;
1586 ural_write(sc, RAL_PHY_CSR7, val);
1587
1588 for (ntries = 0; ntries < 5; ntries++) {
1589 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1590 break;
1591 }
1592 if (ntries == 5) {
1593 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1594 return 0;
1595 }
1596
1597 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1598 }
1599
1600 Static void
1601 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1602 {
1603 uint32_t tmp;
1604 int ntries;
1605
1606 for (ntries = 0; ntries < 5; ntries++) {
1607 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1608 break;
1609 }
1610 if (ntries == 5) {
1611 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1612 return;
1613 }
1614
1615 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1616 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1617 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1618
1619 /* remember last written value in sc */
1620 sc->rf_regs[reg] = val;
1621
1622 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1623 }
1624
1625 Static void
1626 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1627 {
1628 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1629 struct ieee80211com *ic = &sc->sc_ic;
1630 uint8_t power, tmp;
1631 u_int i, chan;
1632
1633 chan = ieee80211_chan2ieee(ic, c);
1634 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1635 return;
1636
1637 if (IEEE80211_IS_CHAN_2GHZ(c))
1638 power = min(sc->txpow[chan - 1], 31);
1639 else
1640 power = 31;
1641
1642 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1643
1644 switch (sc->rf_rev) {
1645 case RAL_RF_2522:
1646 ural_rf_write(sc, RAL_RF1, 0x00814);
1647 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1648 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1649 break;
1650
1651 case RAL_RF_2523:
1652 ural_rf_write(sc, RAL_RF1, 0x08804);
1653 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1654 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1655 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1656 break;
1657
1658 case RAL_RF_2524:
1659 ural_rf_write(sc, RAL_RF1, 0x0c808);
1660 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1661 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1662 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1663 break;
1664
1665 case RAL_RF_2525:
1666 ural_rf_write(sc, RAL_RF1, 0x08808);
1667 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1668 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1669 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1670
1671 ural_rf_write(sc, RAL_RF1, 0x08808);
1672 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1673 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1674 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1675 break;
1676
1677 case RAL_RF_2525E:
1678 ural_rf_write(sc, RAL_RF1, 0x08808);
1679 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1680 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1681 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1682 break;
1683
1684 case RAL_RF_2526:
1685 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1686 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1687 ural_rf_write(sc, RAL_RF1, 0x08804);
1688
1689 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1690 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1691 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1692 break;
1693
1694 /* dual-band RF */
1695 case RAL_RF_5222:
1696 for (i = 0; i < N(ural_rf5222); i++)
1697 if (ural_rf5222[i].chan == chan)
1698 break;
1699
1700 if (i < N(ural_rf5222)) {
1701 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1702 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1703 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1704 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1705 }
1706 break;
1707 }
1708
1709 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1710 ic->ic_state != IEEE80211_S_SCAN) {
1711 /* set Japan filter bit for channel 14 */
1712 tmp = ural_bbp_read(sc, 70);
1713
1714 tmp &= ~RAL_JAPAN_FILTER;
1715 if (chan == 14)
1716 tmp |= RAL_JAPAN_FILTER;
1717
1718 ural_bbp_write(sc, 70, tmp);
1719
1720 /* clear CRC errors */
1721 ural_read(sc, RAL_STA_CSR0);
1722
1723 DELAY(1000); /* RF needs a 1ms delay here */
1724 ural_disable_rf_tune(sc);
1725 }
1726 #undef N
1727 }
1728
1729 /*
1730 * Disable RF auto-tuning.
1731 */
1732 Static void
1733 ural_disable_rf_tune(struct ural_softc *sc)
1734 {
1735 uint32_t tmp;
1736
1737 if (sc->rf_rev != RAL_RF_2523) {
1738 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1739 ural_rf_write(sc, RAL_RF1, tmp);
1740 }
1741
1742 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1743 ural_rf_write(sc, RAL_RF3, tmp);
1744
1745 DPRINTFN(2, ("disabling RF autotune\n"));
1746 }
1747
1748 /*
1749 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1750 * synchronization.
1751 */
1752 Static void
1753 ural_enable_tsf_sync(struct ural_softc *sc)
1754 {
1755 struct ieee80211com *ic = &sc->sc_ic;
1756 uint16_t logcwmin, preload, tmp;
1757
1758 /* first, disable TSF synchronization */
1759 ural_write(sc, RAL_TXRX_CSR19, 0);
1760
1761 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1762 ural_write(sc, RAL_TXRX_CSR18, tmp);
1763
1764 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1765 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1766 tmp = logcwmin << 12 | preload;
1767 ural_write(sc, RAL_TXRX_CSR20, tmp);
1768
1769 /* finally, enable TSF synchronization */
1770 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1771 if (ic->ic_opmode == IEEE80211_M_STA)
1772 tmp |= RAL_ENABLE_TSF_SYNC(1);
1773 else
1774 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1775 ural_write(sc, RAL_TXRX_CSR19, tmp);
1776
1777 DPRINTF(("enabling TSF synchronization\n"));
1778 }
1779
1780 Static void
1781 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1782 {
1783 uint16_t tmp;
1784
1785 tmp = bssid[0] | bssid[1] << 8;
1786 ural_write(sc, RAL_MAC_CSR5, tmp);
1787
1788 tmp = bssid[2] | bssid[3] << 8;
1789 ural_write(sc, RAL_MAC_CSR6, tmp);
1790
1791 tmp = bssid[4] | bssid[5] << 8;
1792 ural_write(sc, RAL_MAC_CSR7, tmp);
1793
1794 DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
1795 }
1796
1797 Static void
1798 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1799 {
1800 uint16_t tmp;
1801
1802 tmp = addr[0] | addr[1] << 8;
1803 ural_write(sc, RAL_MAC_CSR2, tmp);
1804
1805 tmp = addr[2] | addr[3] << 8;
1806 ural_write(sc, RAL_MAC_CSR3, tmp);
1807
1808 tmp = addr[4] | addr[5] << 8;
1809 ural_write(sc, RAL_MAC_CSR4, tmp);
1810
1811 DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
1812 }
1813
1814 Static void
1815 ural_update_promisc(struct ural_softc *sc)
1816 {
1817 struct ifnet *ifp = &sc->sc_if;
1818 uint16_t tmp;
1819
1820 tmp = ural_read(sc, RAL_TXRX_CSR2);
1821
1822 tmp &= ~RAL_DROP_NOT_TO_ME;
1823 if (!(ifp->if_flags & IFF_PROMISC))
1824 tmp |= RAL_DROP_NOT_TO_ME;
1825
1826 ural_write(sc, RAL_TXRX_CSR2, tmp);
1827
1828 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1829 "entering" : "leaving"));
1830 }
1831
1832 Static const char *
1833 ural_get_rf(int rev)
1834 {
1835 switch (rev) {
1836 case RAL_RF_2522: return "RT2522";
1837 case RAL_RF_2523: return "RT2523";
1838 case RAL_RF_2524: return "RT2524";
1839 case RAL_RF_2525: return "RT2525";
1840 case RAL_RF_2525E: return "RT2525e";
1841 case RAL_RF_2526: return "RT2526";
1842 case RAL_RF_5222: return "RT5222";
1843 default: return "unknown";
1844 }
1845 }
1846
1847 Static void
1848 ural_read_eeprom(struct ural_softc *sc)
1849 {
1850 struct ieee80211com *ic = &sc->sc_ic;
1851 uint16_t val;
1852
1853 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1854 val = le16toh(val);
1855 sc->rf_rev = (val >> 11) & 0x7;
1856 sc->hw_radio = (val >> 10) & 0x1;
1857 sc->led_mode = (val >> 6) & 0x7;
1858 sc->rx_ant = (val >> 4) & 0x3;
1859 sc->tx_ant = (val >> 2) & 0x3;
1860 sc->nb_ant = val & 0x3;
1861
1862 /* read MAC address */
1863 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1864
1865 /* read default values for BBP registers */
1866 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1867
1868 /* read Tx power for all b/g channels */
1869 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
1870 }
1871
1872 Static int
1873 ural_bbp_init(struct ural_softc *sc)
1874 {
1875 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1876 int i, ntries;
1877
1878 /* wait for BBP to be ready */
1879 for (ntries = 0; ntries < 100; ntries++) {
1880 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
1881 break;
1882 DELAY(1000);
1883 }
1884 if (ntries == 100) {
1885 printf("%s: timeout waiting for BBP\n", USBDEVNAME(sc->sc_dev));
1886 return EIO;
1887 }
1888
1889 /* initialize BBP registers to default values */
1890 for (i = 0; i < N(ural_def_bbp); i++)
1891 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
1892
1893 #if 0
1894 /* initialize BBP registers to values stored in EEPROM */
1895 for (i = 0; i < 16; i++) {
1896 if (sc->bbp_prom[i].reg == 0xff)
1897 continue;
1898 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1899 }
1900 #endif
1901
1902 return 0;
1903 #undef N
1904 }
1905
1906 Static void
1907 ural_set_txantenna(struct ural_softc *sc, int antenna)
1908 {
1909 uint16_t tmp;
1910 uint8_t tx;
1911
1912 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
1913 if (antenna == 1)
1914 tx |= RAL_BBP_ANTA;
1915 else if (antenna == 2)
1916 tx |= RAL_BBP_ANTB;
1917 else
1918 tx |= RAL_BBP_DIVERSITY;
1919
1920 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
1921 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
1922 sc->rf_rev == RAL_RF_5222)
1923 tx |= RAL_BBP_FLIPIQ;
1924
1925 ural_bbp_write(sc, RAL_BBP_TX, tx);
1926
1927 /* update flags in PHY_CSR5 and PHY_CSR6 too */
1928 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
1929 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
1930
1931 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
1932 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
1933 }
1934
1935 Static void
1936 ural_set_rxantenna(struct ural_softc *sc, int antenna)
1937 {
1938 uint8_t rx;
1939
1940 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
1941 if (antenna == 1)
1942 rx |= RAL_BBP_ANTA;
1943 else if (antenna == 2)
1944 rx |= RAL_BBP_ANTB;
1945 else
1946 rx |= RAL_BBP_DIVERSITY;
1947
1948 /* need to force no I/Q flip for RF 2525e and 2526 */
1949 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
1950 rx &= ~RAL_BBP_FLIPIQ;
1951
1952 ural_bbp_write(sc, RAL_BBP_RX, rx);
1953 }
1954
1955 Static int
1956 ural_init(struct ifnet *ifp)
1957 {
1958 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1959 struct ural_softc *sc = ifp->if_softc;
1960 struct ieee80211com *ic = &sc->sc_ic;
1961 struct ieee80211_key *wk;
1962 struct ural_rx_data *data;
1963 uint16_t sta[11], tmp;
1964 usbd_status error;
1965 int i, ntries;
1966
1967 ural_stop(ifp, 0);
1968
1969 /* initialize MAC registers to default values */
1970 for (i = 0; i < N(ural_def_mac); i++)
1971 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
1972
1973 /* wait for BBP and RF to wake up (this can take a long time!) */
1974 for (ntries = 0; ntries < 100; ntries++) {
1975 tmp = ural_read(sc, RAL_MAC_CSR17);
1976 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
1977 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
1978 break;
1979 DELAY(1000);
1980 }
1981 if (ntries == 100) {
1982 printf("%s: timeout waiting for BBP/RF to wakeup\n",
1983 USBDEVNAME(sc->sc_dev));
1984 error = EIO;
1985 goto fail;
1986 }
1987
1988 /* we're ready! */
1989 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
1990
1991 /* set supported basic rates (1, 2, 6, 12, 24) */
1992 ural_write(sc, RAL_TXRX_CSR11, 0x153);
1993
1994 error = ural_bbp_init(sc);
1995 if (error != 0)
1996 goto fail;
1997
1998 /* set default BSS channel */
1999 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2000 ural_set_chan(sc, ic->ic_bss->ni_chan);
2001
2002 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2003 ural_read_multi(sc, RAL_STA_CSR0, sta, sizeof sta);
2004
2005 /* set default sensitivity */
2006 ural_bbp_write(sc, 17, 0x48);
2007
2008 ural_set_txantenna(sc, 1);
2009 ural_set_rxantenna(sc, 1);
2010
2011 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2012 ural_set_macaddr(sc, ic->ic_myaddr);
2013
2014 /*
2015 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
2016 */
2017 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2018 wk = &ic->ic_nw_keys[i];
2019 ural_write_multi(sc, RAL_SEC_CSR0 + i * IEEE80211_KEYBUF_SIZE,
2020 wk->wk_key, IEEE80211_KEYBUF_SIZE);
2021 }
2022
2023 /*
2024 * Open Tx and Rx USB bulk pipes.
2025 */
2026 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2027 &sc->sc_tx_pipeh);
2028 if (error != 0) {
2029 printf("%s: could not open Tx pipe: %s\n",
2030 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2031 goto fail;
2032 }
2033
2034 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2035 &sc->sc_rx_pipeh);
2036 if (error != 0) {
2037 printf("%s: could not open Rx pipe: %s\n",
2038 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2039 goto fail;
2040 }
2041
2042 /*
2043 * Allocate Tx and Rx xfer queues.
2044 */
2045 error = ural_alloc_tx_list(sc);
2046 if (error != 0) {
2047 printf("%s: could not allocate Tx list\n",
2048 USBDEVNAME(sc->sc_dev));
2049 goto fail;
2050 }
2051
2052 error = ural_alloc_rx_list(sc);
2053 if (error != 0) {
2054 printf("%s: could not allocate Rx list\n",
2055 USBDEVNAME(sc->sc_dev));
2056 goto fail;
2057 }
2058
2059 /*
2060 * Start up the receive pipe.
2061 */
2062 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2063 data = &sc->rx_data[i];
2064
2065 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2066 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2067 usbd_transfer(data->xfer);
2068 }
2069
2070 /* kick Rx */
2071 tmp = RAL_DROP_PHY_ERROR | RAL_DROP_CRC_ERROR;
2072 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2073 tmp |= RAL_DROP_CTL | RAL_DROP_VERSION_ERROR;
2074 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2075 tmp |= RAL_DROP_TODS;
2076 if (!(ifp->if_flags & IFF_PROMISC))
2077 tmp |= RAL_DROP_NOT_TO_ME;
2078 }
2079 ural_write(sc, RAL_TXRX_CSR2, tmp);
2080
2081 ifp->if_flags &= ~IFF_OACTIVE;
2082 ifp->if_flags |= IFF_RUNNING;
2083
2084 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2085 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2086 else
2087 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2088
2089 return 0;
2090
2091 fail: ural_stop(ifp, 1);
2092 return error;
2093 #undef N
2094 }
2095
2096 Static void
2097 ural_stop(struct ifnet *ifp, int disable)
2098 {
2099 struct ural_softc *sc = ifp->if_softc;
2100 struct ieee80211com *ic = &sc->sc_ic;
2101
2102 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2103
2104 /* disable Rx */
2105 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2106
2107 /* reset ASIC and BBP (but won't reset MAC registers!) */
2108 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2109 ural_write(sc, RAL_MAC_CSR1, 0);
2110
2111 sc->sc_tx_timer = 0;
2112 ifp->if_timer = 0;
2113 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2114
2115 if (sc->sc_rx_pipeh != NULL) {
2116 usbd_abort_pipe(sc->sc_rx_pipeh);
2117 usbd_close_pipe(sc->sc_rx_pipeh);
2118 sc->sc_rx_pipeh = NULL;
2119 }
2120
2121 if (sc->sc_tx_pipeh != NULL) {
2122 usbd_abort_pipe(sc->sc_tx_pipeh);
2123 usbd_close_pipe(sc->sc_tx_pipeh);
2124 sc->sc_tx_pipeh = NULL;
2125 }
2126
2127 ural_free_rx_list(sc);
2128 ural_free_tx_list(sc);
2129 }
2130
2131 int
2132 ural_activate(device_ptr_t self, enum devact act)
2133 {
2134 struct ural_softc *sc = (struct ural_softc *)self;
2135
2136 switch (act) {
2137 case DVACT_ACTIVATE:
2138 return EOPNOTSUPP;
2139 break;
2140
2141 case DVACT_DEACTIVATE:
2142 if_deactivate(&sc->sc_if);
2143 break;
2144 }
2145
2146 return 0;
2147 }
2148