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