if_zyd.c revision 1.7.2.2 1 1.7.2.2 mjf /* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */
2 1.7.2.2 mjf /* $NetBSD: if_zyd.c,v 1.7.2.2 2007/07/11 20:08:35 mjf Exp $ */
3 1.7.2.2 mjf
4 1.7.2.2 mjf /*-
5 1.7.2.2 mjf * Copyright (c) 2006 by Damien Bergamini <damien.bergamini (at) free.fr>
6 1.7.2.2 mjf * Copyright (c) 2006 by Florian Stoehr <ich (at) florian-stoehr.de>
7 1.7.2.2 mjf *
8 1.7.2.2 mjf * Permission to use, copy, modify, and distribute this software for any
9 1.7.2.2 mjf * purpose with or without fee is hereby granted, provided that the above
10 1.7.2.2 mjf * copyright notice and this permission notice appear in all copies.
11 1.7.2.2 mjf *
12 1.7.2.2 mjf * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.7.2.2 mjf * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.7.2.2 mjf * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.7.2.2 mjf * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.7.2.2 mjf * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.7.2.2 mjf * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.7.2.2 mjf * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.7.2.2 mjf */
20 1.7.2.2 mjf
21 1.7.2.2 mjf /*
22 1.7.2.2 mjf * ZyDAS ZD1211/ZD1211B USB WLAN driver.
23 1.7.2.2 mjf */
24 1.7.2.2 mjf #include <sys/cdefs.h>
25 1.7.2.2 mjf __KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.7.2.2 2007/07/11 20:08:35 mjf Exp $");
26 1.7.2.2 mjf
27 1.7.2.2 mjf #include "bpfilter.h"
28 1.7.2.2 mjf
29 1.7.2.2 mjf #include <sys/param.h>
30 1.7.2.2 mjf #include <sys/sockio.h>
31 1.7.2.2 mjf #include <sys/proc.h>
32 1.7.2.2 mjf #include <sys/mbuf.h>
33 1.7.2.2 mjf #include <sys/kernel.h>
34 1.7.2.2 mjf #include <sys/socket.h>
35 1.7.2.2 mjf #include <sys/systm.h>
36 1.7.2.2 mjf #include <sys/malloc.h>
37 1.7.2.2 mjf #include <sys/conf.h>
38 1.7.2.2 mjf #include <sys/device.h>
39 1.7.2.2 mjf
40 1.7.2.2 mjf #include <machine/bus.h>
41 1.7.2.2 mjf #include <machine/endian.h>
42 1.7.2.2 mjf
43 1.7.2.2 mjf #if NBPFILTER > 0
44 1.7.2.2 mjf #include <net/bpf.h>
45 1.7.2.2 mjf #endif
46 1.7.2.2 mjf #include <net/if.h>
47 1.7.2.2 mjf #include <net/if_arp.h>
48 1.7.2.2 mjf #include <net/if_dl.h>
49 1.7.2.2 mjf #include <net/if_ether.h>
50 1.7.2.2 mjf #include <net/if_media.h>
51 1.7.2.2 mjf #include <net/if_types.h>
52 1.7.2.2 mjf
53 1.7.2.2 mjf #include <netinet/in.h>
54 1.7.2.2 mjf #include <netinet/in_systm.h>
55 1.7.2.2 mjf #include <netinet/in_var.h>
56 1.7.2.2 mjf #include <netinet/ip.h>
57 1.7.2.2 mjf
58 1.7.2.2 mjf #include <net80211/ieee80211_netbsd.h>
59 1.7.2.2 mjf #include <net80211/ieee80211_var.h>
60 1.7.2.2 mjf #include <net80211/ieee80211_amrr.h>
61 1.7.2.2 mjf #include <net80211/ieee80211_radiotap.h>
62 1.7.2.2 mjf
63 1.7.2.2 mjf #include <dev/firmload.h>
64 1.7.2.2 mjf
65 1.7.2.2 mjf #include <dev/usb/usb.h>
66 1.7.2.2 mjf #include <dev/usb/usbdi.h>
67 1.7.2.2 mjf #include <dev/usb/usbdi_util.h>
68 1.7.2.2 mjf #include <dev/usb/usbdevs.h>
69 1.7.2.2 mjf
70 1.7.2.2 mjf #include <dev/usb/if_zydreg.h>
71 1.7.2.2 mjf
72 1.7.2.2 mjf #ifdef USB_DEBUG
73 1.7.2.2 mjf #define ZYD_DEBUG
74 1.7.2.2 mjf #endif
75 1.7.2.2 mjf
76 1.7.2.2 mjf #ifdef ZYD_DEBUG
77 1.7.2.2 mjf #define DPRINTF(x) do { if (zyddebug > 0) printf x; } while (0)
78 1.7.2.2 mjf #define DPRINTFN(n, x) do { if (zyddebug > (n)) printf x; } while (0)
79 1.7.2.2 mjf int zyddebug = 0;
80 1.7.2.2 mjf #else
81 1.7.2.2 mjf #define DPRINTF(x)
82 1.7.2.2 mjf #define DPRINTFN(n, x)
83 1.7.2.2 mjf #endif
84 1.7.2.2 mjf
85 1.7.2.2 mjf static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
86 1.7.2.2 mjf static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
87 1.7.2.2 mjf
88 1.7.2.2 mjf /* various supported device vendors/products */
89 1.7.2.2 mjf #define ZYD_ZD1211_DEV(v, p) \
90 1.7.2.2 mjf { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
91 1.7.2.2 mjf #define ZYD_ZD1211B_DEV(v, p) \
92 1.7.2.2 mjf { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
93 1.7.2.2 mjf static const struct zyd_type {
94 1.7.2.2 mjf struct usb_devno dev;
95 1.7.2.2 mjf uint8_t rev;
96 1.7.2.2 mjf #define ZYD_ZD1211 0
97 1.7.2.2 mjf #define ZYD_ZD1211B 1
98 1.7.2.2 mjf } zyd_devs[] = {
99 1.7.2.2 mjf ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
100 1.7.2.2 mjf ZYD_ZD1211_DEV(ABOCOM, WL54),
101 1.7.2.2 mjf ZYD_ZD1211_DEV(ASUSTEK, WL159G),
102 1.7.2.2 mjf ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
103 1.7.2.2 mjf ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
104 1.7.2.2 mjf ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
105 1.7.2.2 mjf ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
106 1.7.2.2 mjf ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
107 1.7.2.2 mjf ZYD_ZD1211_DEV(SAGEM, XG760A),
108 1.7.2.2 mjf ZYD_ZD1211_DEV(SENAO, NUB8301),
109 1.7.2.2 mjf ZYD_ZD1211_DEV(SITECOMEU, WL113),
110 1.7.2.2 mjf ZYD_ZD1211_DEV(SWEEX, ZD1211),
111 1.7.2.2 mjf ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
112 1.7.2.2 mjf ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
113 1.7.2.2 mjf ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
114 1.7.2.2 mjf ZYD_ZD1211_DEV(TWINMOS, G240),
115 1.7.2.2 mjf ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
116 1.7.2.2 mjf ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
117 1.7.2.2 mjf ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
118 1.7.2.2 mjf ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
119 1.7.2.2 mjf ZYD_ZD1211_DEV(ZCOM, ZD1211),
120 1.7.2.2 mjf ZYD_ZD1211_DEV(ZYDAS, ZD1211),
121 1.7.2.2 mjf ZYD_ZD1211_DEV(ZYXEL, AG225H),
122 1.7.2.2 mjf ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
123 1.7.2.2 mjf
124 1.7.2.2 mjf ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
125 1.7.2.2 mjf ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
126 1.7.2.2 mjf ZYD_ZD1211B_DEV(ASUSTEK, A9T_WIFI),
127 1.7.2.2 mjf ZYD_ZD1211B_DEV(BELKIN, F5D7050C),
128 1.7.2.2 mjf ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
129 1.7.2.2 mjf ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
130 1.7.2.2 mjf ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
131 1.7.2.2 mjf ZYD_ZD1211B_DEV(MELCO, KG54L),
132 1.7.2.2 mjf ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
133 1.7.2.2 mjf ZYD_ZD1211B_DEV(SAGEM, XG76NA),
134 1.7.2.2 mjf ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
135 1.7.2.2 mjf ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
136 1.7.2.2 mjf #if 0 /* Shall we needs? */
137 1.7.2.2 mjf ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_1),
138 1.7.2.2 mjf ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_2),
139 1.7.2.2 mjf ZYD_ZD1211B_DEV(UNKNOWN2, ZD1211B),
140 1.7.2.2 mjf ZYD_ZD1211B_DEV(UNKNOWN3, ZD1211B),
141 1.7.2.2 mjf #endif
142 1.7.2.2 mjf ZYD_ZD1211B_DEV(USR, USR5423),
143 1.7.2.2 mjf ZYD_ZD1211B_DEV(VTECH, ZD1211B),
144 1.7.2.2 mjf ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
145 1.7.2.2 mjf ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
146 1.7.2.2 mjf ZYD_ZD1211B_DEV(ZYXEL, M202),
147 1.7.2.2 mjf ZYD_ZD1211B_DEV(ZYXEL, G220V2),
148 1.7.2.2 mjf };
149 1.7.2.2 mjf #define zyd_lookup(v, p) \
150 1.7.2.2 mjf ((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
151 1.7.2.2 mjf
152 1.7.2.2 mjf USB_DECLARE_DRIVER(zyd);
153 1.7.2.2 mjf
154 1.7.2.2 mjf Static int zyd_attachhook(void *);
155 1.7.2.2 mjf Static int zyd_complete_attach(struct zyd_softc *);
156 1.7.2.2 mjf Static int zyd_open_pipes(struct zyd_softc *);
157 1.7.2.2 mjf Static void zyd_close_pipes(struct zyd_softc *);
158 1.7.2.2 mjf Static int zyd_alloc_tx_list(struct zyd_softc *);
159 1.7.2.2 mjf Static void zyd_free_tx_list(struct zyd_softc *);
160 1.7.2.2 mjf Static int zyd_alloc_rx_list(struct zyd_softc *);
161 1.7.2.2 mjf Static void zyd_free_rx_list(struct zyd_softc *);
162 1.7.2.2 mjf Static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *);
163 1.7.2.2 mjf Static int zyd_media_change(struct ifnet *);
164 1.7.2.2 mjf Static void zyd_next_scan(void *);
165 1.7.2.2 mjf Static void zyd_task(void *);
166 1.7.2.2 mjf Static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
167 1.7.2.2 mjf Static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
168 1.7.2.2 mjf void *, int, u_int);
169 1.7.2.2 mjf Static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
170 1.7.2.2 mjf Static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
171 1.7.2.2 mjf Static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
172 1.7.2.2 mjf Static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
173 1.7.2.2 mjf Static int zyd_rfwrite(struct zyd_softc *, uint32_t);
174 1.7.2.2 mjf Static void zyd_lock_phy(struct zyd_softc *);
175 1.7.2.2 mjf Static void zyd_unlock_phy(struct zyd_softc *);
176 1.7.2.2 mjf Static int zyd_rfmd_init(struct zyd_rf *);
177 1.7.2.2 mjf Static int zyd_rfmd_switch_radio(struct zyd_rf *, int);
178 1.7.2.2 mjf Static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
179 1.7.2.2 mjf Static int zyd_al2230_init(struct zyd_rf *);
180 1.7.2.2 mjf Static int zyd_al2230_switch_radio(struct zyd_rf *, int);
181 1.7.2.2 mjf Static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
182 1.7.2.2 mjf Static int zyd_al2230_init_b(struct zyd_rf *);
183 1.7.2.2 mjf Static int zyd_al7230B_init(struct zyd_rf *);
184 1.7.2.2 mjf Static int zyd_al7230B_switch_radio(struct zyd_rf *, int);
185 1.7.2.2 mjf Static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
186 1.7.2.2 mjf Static int zyd_al2210_init(struct zyd_rf *);
187 1.7.2.2 mjf Static int zyd_al2210_switch_radio(struct zyd_rf *, int);
188 1.7.2.2 mjf Static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
189 1.7.2.2 mjf Static int zyd_gct_init(struct zyd_rf *);
190 1.7.2.2 mjf Static int zyd_gct_switch_radio(struct zyd_rf *, int);
191 1.7.2.2 mjf Static int zyd_gct_set_channel(struct zyd_rf *, uint8_t);
192 1.7.2.2 mjf Static int zyd_maxim_init(struct zyd_rf *);
193 1.7.2.2 mjf Static int zyd_maxim_switch_radio(struct zyd_rf *, int);
194 1.7.2.2 mjf Static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
195 1.7.2.2 mjf Static int zyd_maxim2_init(struct zyd_rf *);
196 1.7.2.2 mjf Static int zyd_maxim2_switch_radio(struct zyd_rf *, int);
197 1.7.2.2 mjf Static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
198 1.7.2.2 mjf Static int zyd_rf_attach(struct zyd_softc *, uint8_t);
199 1.7.2.2 mjf Static const char *zyd_rf_name(uint8_t);
200 1.7.2.2 mjf Static int zyd_hw_init(struct zyd_softc *);
201 1.7.2.2 mjf Static int zyd_read_eeprom(struct zyd_softc *);
202 1.7.2.2 mjf Static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
203 1.7.2.2 mjf Static int zyd_set_bssid(struct zyd_softc *, const uint8_t *);
204 1.7.2.2 mjf Static int zyd_switch_radio(struct zyd_softc *, int);
205 1.7.2.2 mjf Static void zyd_set_led(struct zyd_softc *, int, int);
206 1.7.2.2 mjf Static int zyd_set_rxfilter(struct zyd_softc *);
207 1.7.2.2 mjf Static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
208 1.7.2.2 mjf Static int zyd_set_beacon_interval(struct zyd_softc *, int);
209 1.7.2.2 mjf Static uint8_t zyd_plcp_signal(int);
210 1.7.2.2 mjf Static void zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
211 1.7.2.2 mjf Static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
212 1.7.2.2 mjf Static void zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
213 1.7.2.2 mjf Static void zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
214 1.7.2.2 mjf Static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
215 1.7.2.2 mjf struct ieee80211_node *);
216 1.7.2.2 mjf Static int zyd_tx_data(struct zyd_softc *, struct mbuf *,
217 1.7.2.2 mjf struct ieee80211_node *);
218 1.7.2.2 mjf Static void zyd_start(struct ifnet *);
219 1.7.2.2 mjf Static void zyd_watchdog(struct ifnet *);
220 1.7.2.2 mjf Static int zyd_ioctl(struct ifnet *, u_long, void *);
221 1.7.2.2 mjf Static int zyd_init(struct ifnet *);
222 1.7.2.2 mjf Static void zyd_stop(struct ifnet *, int);
223 1.7.2.2 mjf Static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
224 1.7.2.2 mjf Static void zyd_iter_func(void *, struct ieee80211_node *);
225 1.7.2.2 mjf Static void zyd_amrr_timeout(void *);
226 1.7.2.2 mjf Static void zyd_newassoc(struct ieee80211_node *, int);
227 1.7.2.2 mjf
228 1.7.2.2 mjf static const struct ieee80211_rateset zyd_rateset_11b =
229 1.7.2.2 mjf { 4, { 2, 4, 11, 22 } };
230 1.7.2.2 mjf
231 1.7.2.2 mjf static const struct ieee80211_rateset zyd_rateset_11g =
232 1.7.2.2 mjf { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
233 1.7.2.2 mjf
234 1.7.2.2 mjf USB_MATCH(zyd)
235 1.7.2.2 mjf {
236 1.7.2.2 mjf USB_MATCH_START(zyd, uaa);
237 1.7.2.2 mjf
238 1.7.2.2 mjf return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
239 1.7.2.2 mjf UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
240 1.7.2.2 mjf }
241 1.7.2.2 mjf
242 1.7.2.2 mjf Static int
243 1.7.2.2 mjf zyd_attachhook(void *xsc)
244 1.7.2.2 mjf {
245 1.7.2.2 mjf struct zyd_softc *sc = xsc;
246 1.7.2.2 mjf firmware_handle_t fwh;
247 1.7.2.2 mjf const char *fwname;
248 1.7.2.2 mjf u_char *fw;
249 1.7.2.2 mjf size_t size;
250 1.7.2.2 mjf int error;
251 1.7.2.2 mjf
252 1.7.2.2 mjf fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b";
253 1.7.2.2 mjf if ((error = firmware_open("zyd", fwname, &fwh)) != 0) {
254 1.7.2.2 mjf printf("%s: failed to open firmware %s (error=%d)\n",
255 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), fwname, error);
256 1.7.2.2 mjf return error;
257 1.7.2.2 mjf }
258 1.7.2.2 mjf size = firmware_get_size(fwh);
259 1.7.2.2 mjf fw = firmware_malloc(size);
260 1.7.2.2 mjf if (fw == NULL) {
261 1.7.2.2 mjf printf("%s: failed to allocate firmware memory\n",
262 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
263 1.7.2.2 mjf firmware_close(fwh);
264 1.7.2.2 mjf return ENOMEM;;
265 1.7.2.2 mjf }
266 1.7.2.2 mjf error = firmware_read(fwh, 0, fw, size);
267 1.7.2.2 mjf firmware_close(fwh);
268 1.7.2.2 mjf if (error != 0) {
269 1.7.2.2 mjf printf("%s: failed to read firmware (error %d)\n",
270 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), error);
271 1.7.2.2 mjf firmware_free(fw, 0);
272 1.7.2.2 mjf return error;
273 1.7.2.2 mjf }
274 1.7.2.2 mjf
275 1.7.2.2 mjf error = zyd_loadfirmware(sc, fw, size);
276 1.7.2.2 mjf if (error != 0) {
277 1.7.2.2 mjf printf("%s: could not load firmware (error=%d)\n",
278 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), error);
279 1.7.2.2 mjf firmware_free(fw, 0);
280 1.7.2.2 mjf return ENXIO;
281 1.7.2.2 mjf }
282 1.7.2.2 mjf
283 1.7.2.2 mjf firmware_free(fw, 0);
284 1.7.2.2 mjf sc->sc_flags |= ZD1211_FWLOADED;
285 1.7.2.2 mjf
286 1.7.2.2 mjf /* complete the attach process */
287 1.7.2.2 mjf if ((error = zyd_complete_attach(sc)) == 0)
288 1.7.2.2 mjf sc->attached = 1;
289 1.7.2.2 mjf return error;
290 1.7.2.2 mjf }
291 1.7.2.2 mjf
292 1.7.2.2 mjf USB_ATTACH(zyd)
293 1.7.2.2 mjf {
294 1.7.2.2 mjf USB_ATTACH_START(zyd, sc, uaa);
295 1.7.2.2 mjf char *devinfop;
296 1.7.2.2 mjf usb_device_descriptor_t* ddesc;
297 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
298 1.7.2.2 mjf
299 1.7.2.2 mjf sc->sc_udev = uaa->device;
300 1.7.2.2 mjf sc->sc_flags = 0;
301 1.7.2.2 mjf
302 1.7.2.2 mjf devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
303 1.7.2.2 mjf USB_ATTACH_SETUP;
304 1.7.2.2 mjf printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
305 1.7.2.2 mjf usbd_devinfo_free(devinfop);
306 1.7.2.2 mjf
307 1.7.2.2 mjf sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
308 1.7.2.2 mjf
309 1.7.2.2 mjf ddesc = usbd_get_device_descriptor(sc->sc_udev);
310 1.7.2.2 mjf if (UGETW(ddesc->bcdDevice) < 0x4330) {
311 1.7.2.2 mjf printf("%s: device version mismatch: 0x%x "
312 1.7.2.2 mjf "(only >= 43.30 supported)\n", USBDEVNAME(sc->sc_dev),
313 1.7.2.2 mjf UGETW(ddesc->bcdDevice));
314 1.7.2.2 mjf USB_ATTACH_ERROR_RETURN;
315 1.7.2.2 mjf }
316 1.7.2.2 mjf
317 1.7.2.2 mjf ifp->if_softc = sc;
318 1.7.2.2 mjf ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
319 1.7.2.2 mjf ifp->if_init = zyd_init;
320 1.7.2.2 mjf ifp->if_ioctl = zyd_ioctl;
321 1.7.2.2 mjf ifp->if_start = zyd_start;
322 1.7.2.2 mjf ifp->if_watchdog = zyd_watchdog;
323 1.7.2.2 mjf IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
324 1.7.2.2 mjf IFQ_SET_READY(&ifp->if_snd);
325 1.7.2.2 mjf memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
326 1.7.2.2 mjf
327 1.7.2.2 mjf if_attach(ifp);
328 1.7.2.2 mjf /* XXXX: alloc temporarily until the layer2 can be configured. */
329 1.7.2.2 mjf if_alloc_sadl(ifp);
330 1.7.2.2 mjf
331 1.7.2.2 mjf SIMPLEQ_INIT(&sc->sc_rqh);
332 1.7.2.2 mjf
333 1.7.2.2 mjf USB_ATTACH_SUCCESS_RETURN;
334 1.7.2.2 mjf }
335 1.7.2.2 mjf
336 1.7.2.2 mjf Static int
337 1.7.2.2 mjf zyd_complete_attach(struct zyd_softc *sc)
338 1.7.2.2 mjf {
339 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
340 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
341 1.7.2.2 mjf usbd_status error;
342 1.7.2.2 mjf int i;
343 1.7.2.2 mjf
344 1.7.2.2 mjf usb_init_task(&sc->sc_task, zyd_task, sc);
345 1.7.2.2 mjf usb_callout_init(sc->sc_scan_ch);
346 1.7.2.2 mjf
347 1.7.2.2 mjf sc->amrr.amrr_min_success_threshold = 1;
348 1.7.2.2 mjf sc->amrr.amrr_max_success_threshold = 10;
349 1.7.2.2 mjf usb_callout_init(sc->sc_amrr_ch);
350 1.7.2.2 mjf
351 1.7.2.2 mjf error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
352 1.7.2.2 mjf if (error != 0) {
353 1.7.2.2 mjf printf("%s: setting config no failed\n",
354 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
355 1.7.2.2 mjf goto fail;
356 1.7.2.2 mjf }
357 1.7.2.2 mjf
358 1.7.2.2 mjf error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
359 1.7.2.2 mjf &sc->sc_iface);
360 1.7.2.2 mjf if (error != 0) {
361 1.7.2.2 mjf printf("%s: getting interface handle failed\n",
362 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
363 1.7.2.2 mjf goto fail;
364 1.7.2.2 mjf }
365 1.7.2.2 mjf
366 1.7.2.2 mjf if ((error = zyd_open_pipes(sc)) != 0) {
367 1.7.2.2 mjf printf("%s: could not open pipes\n", USBDEVNAME(sc->sc_dev));
368 1.7.2.2 mjf goto fail;
369 1.7.2.2 mjf }
370 1.7.2.2 mjf
371 1.7.2.2 mjf if ((error = zyd_read_eeprom(sc)) != 0) {
372 1.7.2.2 mjf printf("%s: could not read EEPROM\n", USBDEVNAME(sc->sc_dev));
373 1.7.2.2 mjf goto fail;
374 1.7.2.2 mjf }
375 1.7.2.2 mjf
376 1.7.2.2 mjf if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
377 1.7.2.2 mjf printf("%s: could not attach RF\n", USBDEVNAME(sc->sc_dev));
378 1.7.2.2 mjf goto fail;
379 1.7.2.2 mjf }
380 1.7.2.2 mjf
381 1.7.2.2 mjf if ((error = zyd_hw_init(sc)) != 0) {
382 1.7.2.2 mjf printf("%s: hardware initialization failed\n",
383 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
384 1.7.2.2 mjf goto fail;
385 1.7.2.2 mjf }
386 1.7.2.2 mjf
387 1.7.2.2 mjf printf("%s: HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
388 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), (sc->mac_rev == ZYD_ZD1211) ? "": "B",
389 1.7.2.2 mjf sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
390 1.7.2.2 mjf sc->pa_rev, ether_sprintf(ic->ic_myaddr));
391 1.7.2.2 mjf
392 1.7.2.2 mjf ic->ic_ifp = ifp;
393 1.7.2.2 mjf ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
394 1.7.2.2 mjf ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
395 1.7.2.2 mjf ic->ic_state = IEEE80211_S_INIT;
396 1.7.2.2 mjf
397 1.7.2.2 mjf /* set device capabilities */
398 1.7.2.2 mjf ic->ic_caps =
399 1.7.2.2 mjf IEEE80211_C_MONITOR | /* monitor mode supported */
400 1.7.2.2 mjf IEEE80211_C_TXPMGT | /* tx power management */
401 1.7.2.2 mjf IEEE80211_C_SHPREAMBLE | /* short preamble supported */
402 1.7.2.2 mjf IEEE80211_C_WEP; /* s/w WEP */
403 1.7.2.2 mjf
404 1.7.2.2 mjf /* set supported .11b and .11g rates */
405 1.7.2.2 mjf ic->ic_sup_rates[IEEE80211_MODE_11B] = zyd_rateset_11b;
406 1.7.2.2 mjf ic->ic_sup_rates[IEEE80211_MODE_11G] = zyd_rateset_11g;
407 1.7.2.2 mjf
408 1.7.2.2 mjf /* set supported .11b and .11g channels (1 through 14) */
409 1.7.2.2 mjf for (i = 1; i <= 14; i++) {
410 1.7.2.2 mjf ic->ic_channels[i].ic_freq =
411 1.7.2.2 mjf ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
412 1.7.2.2 mjf ic->ic_channels[i].ic_flags =
413 1.7.2.2 mjf IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
414 1.7.2.2 mjf IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
415 1.7.2.2 mjf }
416 1.7.2.2 mjf
417 1.7.2.2 mjf if_free_sadl(ifp);
418 1.7.2.2 mjf ieee80211_ifattach(ic);
419 1.7.2.2 mjf ic->ic_node_alloc = zyd_node_alloc;
420 1.7.2.2 mjf ic->ic_newassoc = zyd_newassoc;
421 1.7.2.2 mjf
422 1.7.2.2 mjf /* override state transition machine */
423 1.7.2.2 mjf sc->sc_newstate = ic->ic_newstate;
424 1.7.2.2 mjf ic->ic_newstate = zyd_newstate;
425 1.7.2.2 mjf ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status);
426 1.7.2.2 mjf
427 1.7.2.2 mjf #if NBPFILTER > 0
428 1.7.2.2 mjf bpfattach2(ifp, DLT_IEEE802_11_RADIO,
429 1.7.2.2 mjf sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
430 1.7.2.2 mjf &sc->sc_drvbpf);
431 1.7.2.2 mjf
432 1.7.2.2 mjf sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
433 1.7.2.2 mjf sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
434 1.7.2.2 mjf sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
435 1.7.2.2 mjf
436 1.7.2.2 mjf sc->sc_txtap_len = sizeof sc->sc_txtapu;
437 1.7.2.2 mjf sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
438 1.7.2.2 mjf sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
439 1.7.2.2 mjf #endif
440 1.7.2.2 mjf
441 1.7.2.2 mjf ieee80211_announce(ic);
442 1.7.2.2 mjf
443 1.7.2.2 mjf usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
444 1.7.2.2 mjf USBDEV(sc->sc_dev));
445 1.7.2.2 mjf
446 1.7.2.2 mjf fail: return error;
447 1.7.2.2 mjf }
448 1.7.2.2 mjf
449 1.7.2.2 mjf USB_DETACH(zyd)
450 1.7.2.2 mjf {
451 1.7.2.2 mjf USB_DETACH_START(zyd, sc);
452 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
453 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
454 1.7.2.2 mjf int s;
455 1.7.2.2 mjf
456 1.7.2.2 mjf if (!sc->attached) {
457 1.7.2.2 mjf if_free_sadl(ifp);
458 1.7.2.2 mjf if_detach(ifp);
459 1.7.2.2 mjf return 0;
460 1.7.2.2 mjf }
461 1.7.2.2 mjf
462 1.7.2.2 mjf s = splusb();
463 1.7.2.2 mjf
464 1.7.2.2 mjf zyd_stop(ifp, 1);
465 1.7.2.2 mjf usb_rem_task(sc->sc_udev, &sc->sc_task);
466 1.7.2.2 mjf usb_uncallout(sc->sc_scan_ch, zyd_next_scan, sc);
467 1.7.2.2 mjf usb_uncallout(sc->sc_amrr_ch, zyd_amrr_timeout, sc);
468 1.7.2.2 mjf
469 1.7.2.2 mjf zyd_close_pipes(sc);
470 1.7.2.2 mjf
471 1.7.2.2 mjf zyd_free_rx_list(sc);
472 1.7.2.2 mjf zyd_free_tx_list(sc);
473 1.7.2.2 mjf
474 1.7.2.2 mjf sc->attached = 0;
475 1.7.2.2 mjf
476 1.7.2.2 mjf #if NBPFILTER > 0
477 1.7.2.2 mjf bpfdetach(ifp);
478 1.7.2.2 mjf #endif
479 1.7.2.2 mjf ieee80211_ifdetach(ic);
480 1.7.2.2 mjf if_detach(ifp);
481 1.7.2.2 mjf
482 1.7.2.2 mjf splx(s);
483 1.7.2.2 mjf
484 1.7.2.2 mjf usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
485 1.7.2.2 mjf USBDEV(sc->sc_dev));
486 1.7.2.2 mjf
487 1.7.2.2 mjf return 0;
488 1.7.2.2 mjf }
489 1.7.2.2 mjf
490 1.7.2.2 mjf Static int
491 1.7.2.2 mjf zyd_open_pipes(struct zyd_softc *sc)
492 1.7.2.2 mjf {
493 1.7.2.2 mjf usb_endpoint_descriptor_t *edesc;
494 1.7.2.2 mjf int isize;
495 1.7.2.2 mjf usbd_status error;
496 1.7.2.2 mjf
497 1.7.2.2 mjf /* interrupt in */
498 1.7.2.2 mjf edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
499 1.7.2.2 mjf if (edesc == NULL)
500 1.7.2.2 mjf return EINVAL;
501 1.7.2.2 mjf
502 1.7.2.2 mjf isize = UGETW(edesc->wMaxPacketSize);
503 1.7.2.2 mjf if (isize == 0) /* should not happen */
504 1.7.2.2 mjf return EINVAL;
505 1.7.2.2 mjf
506 1.7.2.2 mjf sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
507 1.7.2.2 mjf if (sc->ibuf == NULL)
508 1.7.2.2 mjf return ENOMEM;
509 1.7.2.2 mjf
510 1.7.2.2 mjf error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
511 1.7.2.2 mjf &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
512 1.7.2.2 mjf USBD_DEFAULT_INTERVAL);
513 1.7.2.2 mjf if (error != 0) {
514 1.7.2.2 mjf printf("%s: open rx intr pipe failed: %s\n",
515 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(error));
516 1.7.2.2 mjf goto fail;
517 1.7.2.2 mjf }
518 1.7.2.2 mjf
519 1.7.2.2 mjf /* interrupt out (not necessarily an interrupt pipe) */
520 1.7.2.2 mjf error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
521 1.7.2.2 mjf &sc->zyd_ep[ZYD_ENDPT_IOUT]);
522 1.7.2.2 mjf if (error != 0) {
523 1.7.2.2 mjf printf("%s: open tx intr pipe failed: %s\n",
524 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(error));
525 1.7.2.2 mjf goto fail;
526 1.7.2.2 mjf }
527 1.7.2.2 mjf
528 1.7.2.2 mjf /* bulk in */
529 1.7.2.2 mjf error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
530 1.7.2.2 mjf &sc->zyd_ep[ZYD_ENDPT_BIN]);
531 1.7.2.2 mjf if (error != 0) {
532 1.7.2.2 mjf printf("%s: open rx pipe failed: %s\n",
533 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(error));
534 1.7.2.2 mjf goto fail;
535 1.7.2.2 mjf }
536 1.7.2.2 mjf
537 1.7.2.2 mjf /* bulk out */
538 1.7.2.2 mjf error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
539 1.7.2.2 mjf &sc->zyd_ep[ZYD_ENDPT_BOUT]);
540 1.7.2.2 mjf if (error != 0) {
541 1.7.2.2 mjf printf("%s: open tx pipe failed: %s\n",
542 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(error));
543 1.7.2.2 mjf goto fail;
544 1.7.2.2 mjf }
545 1.7.2.2 mjf
546 1.7.2.2 mjf return 0;
547 1.7.2.2 mjf
548 1.7.2.2 mjf fail: zyd_close_pipes(sc);
549 1.7.2.2 mjf return error;
550 1.7.2.2 mjf }
551 1.7.2.2 mjf
552 1.7.2.2 mjf Static void
553 1.7.2.2 mjf zyd_close_pipes(struct zyd_softc *sc)
554 1.7.2.2 mjf {
555 1.7.2.2 mjf int i;
556 1.7.2.2 mjf
557 1.7.2.2 mjf for (i = 0; i < ZYD_ENDPT_CNT; i++) {
558 1.7.2.2 mjf if (sc->zyd_ep[i] != NULL) {
559 1.7.2.2 mjf usbd_abort_pipe(sc->zyd_ep[i]);
560 1.7.2.2 mjf usbd_close_pipe(sc->zyd_ep[i]);
561 1.7.2.2 mjf sc->zyd_ep[i] = NULL;
562 1.7.2.2 mjf }
563 1.7.2.2 mjf }
564 1.7.2.2 mjf if (sc->ibuf != NULL) {
565 1.7.2.2 mjf free(sc->ibuf, M_USBDEV);
566 1.7.2.2 mjf sc->ibuf = NULL;
567 1.7.2.2 mjf }
568 1.7.2.2 mjf }
569 1.7.2.2 mjf
570 1.7.2.2 mjf Static int
571 1.7.2.2 mjf zyd_alloc_tx_list(struct zyd_softc *sc)
572 1.7.2.2 mjf {
573 1.7.2.2 mjf int i, error;
574 1.7.2.2 mjf
575 1.7.2.2 mjf sc->tx_queued = 0;
576 1.7.2.2 mjf
577 1.7.2.2 mjf for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
578 1.7.2.2 mjf struct zyd_tx_data *data = &sc->tx_data[i];
579 1.7.2.2 mjf
580 1.7.2.2 mjf data->sc = sc; /* backpointer for callbacks */
581 1.7.2.2 mjf
582 1.7.2.2 mjf data->xfer = usbd_alloc_xfer(sc->sc_udev);
583 1.7.2.2 mjf if (data->xfer == NULL) {
584 1.7.2.2 mjf printf("%s: could not allocate tx xfer\n",
585 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
586 1.7.2.2 mjf error = ENOMEM;
587 1.7.2.2 mjf goto fail;
588 1.7.2.2 mjf }
589 1.7.2.2 mjf data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
590 1.7.2.2 mjf if (data->buf == NULL) {
591 1.7.2.2 mjf printf("%s: could not allocate tx buffer\n",
592 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
593 1.7.2.2 mjf error = ENOMEM;
594 1.7.2.2 mjf goto fail;
595 1.7.2.2 mjf }
596 1.7.2.2 mjf
597 1.7.2.2 mjf /* clear Tx descriptor */
598 1.7.2.2 mjf bzero(data->buf, sizeof (struct zyd_tx_desc));
599 1.7.2.2 mjf }
600 1.7.2.2 mjf return 0;
601 1.7.2.2 mjf
602 1.7.2.2 mjf fail: zyd_free_tx_list(sc);
603 1.7.2.2 mjf return error;
604 1.7.2.2 mjf }
605 1.7.2.2 mjf
606 1.7.2.2 mjf Static void
607 1.7.2.2 mjf zyd_free_tx_list(struct zyd_softc *sc)
608 1.7.2.2 mjf {
609 1.7.2.2 mjf int i;
610 1.7.2.2 mjf
611 1.7.2.2 mjf for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
612 1.7.2.2 mjf struct zyd_tx_data *data = &sc->tx_data[i];
613 1.7.2.2 mjf
614 1.7.2.2 mjf if (data->xfer != NULL) {
615 1.7.2.2 mjf usbd_free_xfer(data->xfer);
616 1.7.2.2 mjf data->xfer = NULL;
617 1.7.2.2 mjf }
618 1.7.2.2 mjf if (data->ni != NULL) {
619 1.7.2.2 mjf ieee80211_free_node(data->ni);
620 1.7.2.2 mjf data->ni = NULL;
621 1.7.2.2 mjf }
622 1.7.2.2 mjf }
623 1.7.2.2 mjf }
624 1.7.2.2 mjf
625 1.7.2.2 mjf Static int
626 1.7.2.2 mjf zyd_alloc_rx_list(struct zyd_softc *sc)
627 1.7.2.2 mjf {
628 1.7.2.2 mjf int i, error;
629 1.7.2.2 mjf
630 1.7.2.2 mjf for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
631 1.7.2.2 mjf struct zyd_rx_data *data = &sc->rx_data[i];
632 1.7.2.2 mjf
633 1.7.2.2 mjf data->sc = sc; /* backpointer for callbacks */
634 1.7.2.2 mjf
635 1.7.2.2 mjf data->xfer = usbd_alloc_xfer(sc->sc_udev);
636 1.7.2.2 mjf if (data->xfer == NULL) {
637 1.7.2.2 mjf printf("%s: could not allocate rx xfer\n",
638 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
639 1.7.2.2 mjf error = ENOMEM;
640 1.7.2.2 mjf goto fail;
641 1.7.2.2 mjf }
642 1.7.2.2 mjf data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
643 1.7.2.2 mjf if (data->buf == NULL) {
644 1.7.2.2 mjf printf("%s: could not allocate rx buffer\n",
645 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
646 1.7.2.2 mjf error = ENOMEM;
647 1.7.2.2 mjf goto fail;
648 1.7.2.2 mjf }
649 1.7.2.2 mjf }
650 1.7.2.2 mjf return 0;
651 1.7.2.2 mjf
652 1.7.2.2 mjf fail: zyd_free_rx_list(sc);
653 1.7.2.2 mjf return error;
654 1.7.2.2 mjf }
655 1.7.2.2 mjf
656 1.7.2.2 mjf Static void
657 1.7.2.2 mjf zyd_free_rx_list(struct zyd_softc *sc)
658 1.7.2.2 mjf {
659 1.7.2.2 mjf int i;
660 1.7.2.2 mjf
661 1.7.2.2 mjf for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
662 1.7.2.2 mjf struct zyd_rx_data *data = &sc->rx_data[i];
663 1.7.2.2 mjf
664 1.7.2.2 mjf if (data->xfer != NULL) {
665 1.7.2.2 mjf usbd_free_xfer(data->xfer);
666 1.7.2.2 mjf data->xfer = NULL;
667 1.7.2.2 mjf }
668 1.7.2.2 mjf }
669 1.7.2.2 mjf }
670 1.7.2.2 mjf
671 1.7.2.2 mjf /* ARGUSED */
672 1.7.2.2 mjf Static struct ieee80211_node *
673 1.7.2.2 mjf zyd_node_alloc(struct ieee80211_node_table *nt __unused)
674 1.7.2.2 mjf {
675 1.7.2.2 mjf struct zyd_node *zn;
676 1.7.2.2 mjf
677 1.7.2.2 mjf zn = malloc(sizeof (struct zyd_node), M_DEVBUF, M_NOWAIT);
678 1.7.2.2 mjf if (zn != NULL)
679 1.7.2.2 mjf bzero(zn, sizeof (struct zyd_node));
680 1.7.2.2 mjf return (struct ieee80211_node *)zn;
681 1.7.2.2 mjf }
682 1.7.2.2 mjf
683 1.7.2.2 mjf Static int
684 1.7.2.2 mjf zyd_media_change(struct ifnet *ifp)
685 1.7.2.2 mjf {
686 1.7.2.2 mjf int error;
687 1.7.2.2 mjf
688 1.7.2.2 mjf error = ieee80211_media_change(ifp);
689 1.7.2.2 mjf if (error != ENETRESET)
690 1.7.2.2 mjf return error;
691 1.7.2.2 mjf
692 1.7.2.2 mjf if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
693 1.7.2.2 mjf zyd_init(ifp);
694 1.7.2.2 mjf
695 1.7.2.2 mjf return 0;
696 1.7.2.2 mjf }
697 1.7.2.2 mjf
698 1.7.2.2 mjf /*
699 1.7.2.2 mjf * This function is called periodically (every 200ms) during scanning to
700 1.7.2.2 mjf * switch from one channel to another.
701 1.7.2.2 mjf */
702 1.7.2.2 mjf Static void
703 1.7.2.2 mjf zyd_next_scan(void *arg)
704 1.7.2.2 mjf {
705 1.7.2.2 mjf struct zyd_softc *sc = arg;
706 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
707 1.7.2.2 mjf
708 1.7.2.2 mjf if (ic->ic_state == IEEE80211_S_SCAN)
709 1.7.2.2 mjf ieee80211_next_scan(ic);
710 1.7.2.2 mjf }
711 1.7.2.2 mjf
712 1.7.2.2 mjf Static void
713 1.7.2.2 mjf zyd_task(void *arg)
714 1.7.2.2 mjf {
715 1.7.2.2 mjf struct zyd_softc *sc = arg;
716 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
717 1.7.2.2 mjf enum ieee80211_state ostate;
718 1.7.2.2 mjf
719 1.7.2.2 mjf ostate = ic->ic_state;
720 1.7.2.2 mjf
721 1.7.2.2 mjf switch (sc->sc_state) {
722 1.7.2.2 mjf case IEEE80211_S_INIT:
723 1.7.2.2 mjf if (ostate == IEEE80211_S_RUN) {
724 1.7.2.2 mjf /* turn link LED off */
725 1.7.2.2 mjf zyd_set_led(sc, ZYD_LED1, 0);
726 1.7.2.2 mjf
727 1.7.2.2 mjf /* stop data LED from blinking */
728 1.7.2.2 mjf zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
729 1.7.2.2 mjf }
730 1.7.2.2 mjf break;
731 1.7.2.2 mjf
732 1.7.2.2 mjf case IEEE80211_S_SCAN:
733 1.7.2.2 mjf zyd_set_chan(sc, ic->ic_curchan);
734 1.7.2.2 mjf usb_callout(sc->sc_scan_ch, hz / 5, zyd_next_scan, sc);
735 1.7.2.2 mjf break;
736 1.7.2.2 mjf
737 1.7.2.2 mjf case IEEE80211_S_AUTH:
738 1.7.2.2 mjf case IEEE80211_S_ASSOC:
739 1.7.2.2 mjf zyd_set_chan(sc, ic->ic_curchan);
740 1.7.2.2 mjf break;
741 1.7.2.2 mjf
742 1.7.2.2 mjf case IEEE80211_S_RUN:
743 1.7.2.2 mjf {
744 1.7.2.2 mjf struct ieee80211_node *ni = ic->ic_bss;
745 1.7.2.2 mjf
746 1.7.2.2 mjf zyd_set_chan(sc, ic->ic_curchan);
747 1.7.2.2 mjf
748 1.7.2.2 mjf if (ic->ic_opmode != IEEE80211_M_MONITOR) {
749 1.7.2.2 mjf /* turn link LED on */
750 1.7.2.2 mjf zyd_set_led(sc, ZYD_LED1, 1);
751 1.7.2.2 mjf
752 1.7.2.2 mjf /* make data LED blink upon Tx */
753 1.7.2.2 mjf zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
754 1.7.2.2 mjf
755 1.7.2.2 mjf zyd_set_bssid(sc, ni->ni_bssid);
756 1.7.2.2 mjf }
757 1.7.2.2 mjf
758 1.7.2.2 mjf if (ic->ic_opmode == IEEE80211_M_STA) {
759 1.7.2.2 mjf /* fake a join to init the tx rate */
760 1.7.2.2 mjf zyd_newassoc(ni, 1);
761 1.7.2.2 mjf }
762 1.7.2.2 mjf
763 1.7.2.2 mjf /* start automatic rate control timer */
764 1.7.2.2 mjf if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
765 1.7.2.2 mjf usb_callout(sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
766 1.7.2.2 mjf
767 1.7.2.2 mjf break;
768 1.7.2.2 mjf }
769 1.7.2.2 mjf }
770 1.7.2.2 mjf
771 1.7.2.2 mjf sc->sc_newstate(ic, sc->sc_state, -1);
772 1.7.2.2 mjf }
773 1.7.2.2 mjf
774 1.7.2.2 mjf Static int
775 1.7.2.2 mjf zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
776 1.7.2.2 mjf {
777 1.7.2.2 mjf struct zyd_softc *sc = ic->ic_ifp->if_softc;
778 1.7.2.2 mjf
779 1.7.2.2 mjf usb_rem_task(sc->sc_udev, &sc->sc_task);
780 1.7.2.2 mjf usb_uncallout(sc->sc_scan_ch, zyd_next_scan, sc);
781 1.7.2.2 mjf usb_uncallout(sc->sc_amrr_ch, zyd_amrr_timeout, sc);
782 1.7.2.2 mjf
783 1.7.2.2 mjf /* do it in a process context */
784 1.7.2.2 mjf sc->sc_state = nstate;
785 1.7.2.2 mjf usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
786 1.7.2.2 mjf
787 1.7.2.2 mjf return 0;
788 1.7.2.2 mjf }
789 1.7.2.2 mjf
790 1.7.2.2 mjf Static int
791 1.7.2.2 mjf zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
792 1.7.2.2 mjf void *odata, int olen, u_int flags)
793 1.7.2.2 mjf {
794 1.7.2.2 mjf usbd_xfer_handle xfer;
795 1.7.2.2 mjf struct zyd_cmd cmd;
796 1.7.2.2 mjf struct rq rq;
797 1.7.2.2 mjf uint16_t xferflags;
798 1.7.2.2 mjf usbd_status error;
799 1.7.2.2 mjf int s = 0;
800 1.7.2.2 mjf
801 1.7.2.2 mjf if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
802 1.7.2.2 mjf return ENOMEM;
803 1.7.2.2 mjf
804 1.7.2.2 mjf cmd.code = htole16(code);
805 1.7.2.2 mjf bcopy(idata, cmd.data, ilen);
806 1.7.2.2 mjf
807 1.7.2.2 mjf xferflags = USBD_FORCE_SHORT_XFER;
808 1.7.2.2 mjf if (!(flags & ZYD_CMD_FLAG_READ))
809 1.7.2.2 mjf xferflags |= USBD_SYNCHRONOUS;
810 1.7.2.2 mjf else {
811 1.7.2.2 mjf s = splusb();
812 1.7.2.2 mjf rq.idata = idata;
813 1.7.2.2 mjf rq.odata = odata;
814 1.7.2.2 mjf rq.len = olen / sizeof (struct zyd_pair);
815 1.7.2.2 mjf SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
816 1.7.2.2 mjf }
817 1.7.2.2 mjf
818 1.7.2.2 mjf usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
819 1.7.2.2 mjf sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
820 1.7.2.2 mjf error = usbd_transfer(xfer);
821 1.7.2.2 mjf if (error != USBD_IN_PROGRESS && error != 0) {
822 1.7.2.2 mjf if (flags & ZYD_CMD_FLAG_READ)
823 1.7.2.2 mjf splx(s);
824 1.7.2.2 mjf printf("%s: could not send command (error=%s)\n",
825 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(error));
826 1.7.2.2 mjf (void)usbd_free_xfer(xfer);
827 1.7.2.2 mjf return EIO;
828 1.7.2.2 mjf }
829 1.7.2.2 mjf if (!(flags & ZYD_CMD_FLAG_READ)) {
830 1.7.2.2 mjf (void)usbd_free_xfer(xfer);
831 1.7.2.2 mjf return 0; /* write: don't wait for reply */
832 1.7.2.2 mjf }
833 1.7.2.2 mjf /* wait at most one second for command reply */
834 1.7.2.2 mjf error = tsleep(odata, PCATCH, "zydcmd", hz);
835 1.7.2.2 mjf if (error == EWOULDBLOCK)
836 1.7.2.2 mjf printf("%s: zyd_read sleep timeout\n", USBDEVNAME(sc->sc_dev));
837 1.7.2.2 mjf SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
838 1.7.2.2 mjf splx(s);
839 1.7.2.2 mjf
840 1.7.2.2 mjf (void)usbd_free_xfer(xfer);
841 1.7.2.2 mjf return error;
842 1.7.2.2 mjf }
843 1.7.2.2 mjf
844 1.7.2.2 mjf Static int
845 1.7.2.2 mjf zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
846 1.7.2.2 mjf {
847 1.7.2.2 mjf struct zyd_pair tmp;
848 1.7.2.2 mjf int error;
849 1.7.2.2 mjf
850 1.7.2.2 mjf reg = htole16(reg);
851 1.7.2.2 mjf error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof reg, &tmp, sizeof tmp,
852 1.7.2.2 mjf ZYD_CMD_FLAG_READ);
853 1.7.2.2 mjf if (error == 0)
854 1.7.2.2 mjf *val = le16toh(tmp.val);
855 1.7.2.2 mjf return error;
856 1.7.2.2 mjf }
857 1.7.2.2 mjf
858 1.7.2.2 mjf Static int
859 1.7.2.2 mjf zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
860 1.7.2.2 mjf {
861 1.7.2.2 mjf struct zyd_pair tmp[2];
862 1.7.2.2 mjf uint16_t regs[2];
863 1.7.2.2 mjf int error;
864 1.7.2.2 mjf
865 1.7.2.2 mjf regs[0] = htole16(ZYD_REG32_HI(reg));
866 1.7.2.2 mjf regs[1] = htole16(ZYD_REG32_LO(reg));
867 1.7.2.2 mjf error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
868 1.7.2.2 mjf ZYD_CMD_FLAG_READ);
869 1.7.2.2 mjf if (error == 0)
870 1.7.2.2 mjf *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
871 1.7.2.2 mjf return error;
872 1.7.2.2 mjf }
873 1.7.2.2 mjf
874 1.7.2.2 mjf Static int
875 1.7.2.2 mjf zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
876 1.7.2.2 mjf {
877 1.7.2.2 mjf struct zyd_pair pair;
878 1.7.2.2 mjf
879 1.7.2.2 mjf pair.reg = htole16(reg);
880 1.7.2.2 mjf pair.val = htole16(val);
881 1.7.2.2 mjf
882 1.7.2.2 mjf return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
883 1.7.2.2 mjf }
884 1.7.2.2 mjf
885 1.7.2.2 mjf Static int
886 1.7.2.2 mjf zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
887 1.7.2.2 mjf {
888 1.7.2.2 mjf struct zyd_pair pair[2];
889 1.7.2.2 mjf
890 1.7.2.2 mjf pair[0].reg = htole16(ZYD_REG32_HI(reg));
891 1.7.2.2 mjf pair[0].val = htole16(val >> 16);
892 1.7.2.2 mjf pair[1].reg = htole16(ZYD_REG32_LO(reg));
893 1.7.2.2 mjf pair[1].val = htole16(val & 0xffff);
894 1.7.2.2 mjf
895 1.7.2.2 mjf return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
896 1.7.2.2 mjf }
897 1.7.2.2 mjf
898 1.7.2.2 mjf Static int
899 1.7.2.2 mjf zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
900 1.7.2.2 mjf {
901 1.7.2.2 mjf struct zyd_rf *rf = &sc->sc_rf;
902 1.7.2.2 mjf struct zyd_rfwrite req;
903 1.7.2.2 mjf uint16_t cr203;
904 1.7.2.2 mjf int i;
905 1.7.2.2 mjf
906 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &cr203);
907 1.7.2.2 mjf cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
908 1.7.2.2 mjf
909 1.7.2.2 mjf req.code = htole16(2);
910 1.7.2.2 mjf req.width = htole16(rf->width);
911 1.7.2.2 mjf for (i = 0; i < rf->width; i++) {
912 1.7.2.2 mjf req.bit[i] = htole16(cr203);
913 1.7.2.2 mjf if (val & (1 << (rf->width - 1 - i)))
914 1.7.2.2 mjf req.bit[i] |= htole16(ZYD_RF_DATA);
915 1.7.2.2 mjf }
916 1.7.2.2 mjf return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
917 1.7.2.2 mjf }
918 1.7.2.2 mjf
919 1.7.2.2 mjf Static void
920 1.7.2.2 mjf zyd_lock_phy(struct zyd_softc *sc)
921 1.7.2.2 mjf {
922 1.7.2.2 mjf uint32_t tmp;
923 1.7.2.2 mjf
924 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
925 1.7.2.2 mjf tmp &= ~ZYD_UNLOCK_PHY_REGS;
926 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
927 1.7.2.2 mjf }
928 1.7.2.2 mjf
929 1.7.2.2 mjf Static void
930 1.7.2.2 mjf zyd_unlock_phy(struct zyd_softc *sc)
931 1.7.2.2 mjf {
932 1.7.2.2 mjf uint32_t tmp;
933 1.7.2.2 mjf
934 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
935 1.7.2.2 mjf tmp |= ZYD_UNLOCK_PHY_REGS;
936 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
937 1.7.2.2 mjf }
938 1.7.2.2 mjf
939 1.7.2.2 mjf /*
940 1.7.2.2 mjf * RFMD RF methods.
941 1.7.2.2 mjf */
942 1.7.2.2 mjf Static int
943 1.7.2.2 mjf zyd_rfmd_init(struct zyd_rf *rf)
944 1.7.2.2 mjf {
945 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
946 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
947 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
948 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_RFMD_RF;
949 1.7.2.2 mjf int i, error;
950 1.7.2.2 mjf
951 1.7.2.2 mjf /* init RF-dependent PHY registers */
952 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
953 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
954 1.7.2.2 mjf if (error != 0)
955 1.7.2.2 mjf return error;
956 1.7.2.2 mjf }
957 1.7.2.2 mjf
958 1.7.2.2 mjf /* init RFMD radio */
959 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
960 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
961 1.7.2.2 mjf return error;
962 1.7.2.2 mjf }
963 1.7.2.2 mjf return 0;
964 1.7.2.2 mjf #undef N
965 1.7.2.2 mjf }
966 1.7.2.2 mjf
967 1.7.2.2 mjf Static int
968 1.7.2.2 mjf zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
969 1.7.2.2 mjf {
970 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
971 1.7.2.2 mjf
972 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
973 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
974 1.7.2.2 mjf
975 1.7.2.2 mjf return 0;
976 1.7.2.2 mjf }
977 1.7.2.2 mjf
978 1.7.2.2 mjf Static int
979 1.7.2.2 mjf zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
980 1.7.2.2 mjf {
981 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
982 1.7.2.2 mjf static const struct {
983 1.7.2.2 mjf uint32_t r1, r2;
984 1.7.2.2 mjf } rfprog[] = ZYD_RFMD_CHANTABLE;
985 1.7.2.2 mjf
986 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
987 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
988 1.7.2.2 mjf
989 1.7.2.2 mjf return 0;
990 1.7.2.2 mjf }
991 1.7.2.2 mjf
992 1.7.2.2 mjf /*
993 1.7.2.2 mjf * AL2230 RF methods.
994 1.7.2.2 mjf */
995 1.7.2.2 mjf Static int
996 1.7.2.2 mjf zyd_al2230_init(struct zyd_rf *rf)
997 1.7.2.2 mjf {
998 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
999 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1000 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1001 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_AL2230_RF;
1002 1.7.2.2 mjf int i, error;
1003 1.7.2.2 mjf
1004 1.7.2.2 mjf /* init RF-dependent PHY registers */
1005 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1006 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1007 1.7.2.2 mjf if (error != 0)
1008 1.7.2.2 mjf return error;
1009 1.7.2.2 mjf }
1010 1.7.2.2 mjf
1011 1.7.2.2 mjf /* init AL2230 radio */
1012 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1013 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1014 1.7.2.2 mjf return error;
1015 1.7.2.2 mjf }
1016 1.7.2.2 mjf return 0;
1017 1.7.2.2 mjf #undef N
1018 1.7.2.2 mjf }
1019 1.7.2.2 mjf
1020 1.7.2.2 mjf Static int
1021 1.7.2.2 mjf zyd_al2230_init_b(struct zyd_rf *rf)
1022 1.7.2.2 mjf {
1023 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1024 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1025 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1026 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_AL2230_RF_B;
1027 1.7.2.2 mjf int i, error;
1028 1.7.2.2 mjf
1029 1.7.2.2 mjf /* init RF-dependent PHY registers */
1030 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1031 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1032 1.7.2.2 mjf if (error != 0)
1033 1.7.2.2 mjf return error;
1034 1.7.2.2 mjf }
1035 1.7.2.2 mjf
1036 1.7.2.2 mjf /* init AL2230 radio */
1037 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1038 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1039 1.7.2.2 mjf return error;
1040 1.7.2.2 mjf }
1041 1.7.2.2 mjf return 0;
1042 1.7.2.2 mjf #undef N
1043 1.7.2.2 mjf }
1044 1.7.2.2 mjf
1045 1.7.2.2 mjf Static int
1046 1.7.2.2 mjf zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1047 1.7.2.2 mjf {
1048 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1049 1.7.2.2 mjf int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1050 1.7.2.2 mjf
1051 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1052 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1053 1.7.2.2 mjf
1054 1.7.2.2 mjf return 0;
1055 1.7.2.2 mjf }
1056 1.7.2.2 mjf
1057 1.7.2.2 mjf Static int
1058 1.7.2.2 mjf zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1059 1.7.2.2 mjf {
1060 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1061 1.7.2.2 mjf static const struct {
1062 1.7.2.2 mjf uint32_t r1, r2, r3;
1063 1.7.2.2 mjf } rfprog[] = ZYD_AL2230_CHANTABLE;
1064 1.7.2.2 mjf
1065 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1066 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1067 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1068 1.7.2.2 mjf
1069 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR138, 0x28);
1070 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, 0x06);
1071 1.7.2.2 mjf
1072 1.7.2.2 mjf return 0;
1073 1.7.2.2 mjf }
1074 1.7.2.2 mjf
1075 1.7.2.2 mjf /*
1076 1.7.2.2 mjf * AL7230B RF methods.
1077 1.7.2.2 mjf */
1078 1.7.2.2 mjf Static int
1079 1.7.2.2 mjf zyd_al7230B_init(struct zyd_rf *rf)
1080 1.7.2.2 mjf {
1081 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1082 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1083 1.7.2.2 mjf static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1084 1.7.2.2 mjf static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1085 1.7.2.2 mjf static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1086 1.7.2.2 mjf static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1087 1.7.2.2 mjf static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1088 1.7.2.2 mjf int i, error;
1089 1.7.2.2 mjf
1090 1.7.2.2 mjf /* for AL7230B, PHY and RF need to be initialized in "phases" */
1091 1.7.2.2 mjf
1092 1.7.2.2 mjf /* init RF-dependent PHY registers, part one */
1093 1.7.2.2 mjf for (i = 0; i < N(phyini_1); i++) {
1094 1.7.2.2 mjf error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1095 1.7.2.2 mjf if (error != 0)
1096 1.7.2.2 mjf return error;
1097 1.7.2.2 mjf }
1098 1.7.2.2 mjf /* init AL7230B radio, part one */
1099 1.7.2.2 mjf for (i = 0; i < N(rfini_1); i++) {
1100 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1101 1.7.2.2 mjf return error;
1102 1.7.2.2 mjf }
1103 1.7.2.2 mjf /* init RF-dependent PHY registers, part two */
1104 1.7.2.2 mjf for (i = 0; i < N(phyini_2); i++) {
1105 1.7.2.2 mjf error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1106 1.7.2.2 mjf if (error != 0)
1107 1.7.2.2 mjf return error;
1108 1.7.2.2 mjf }
1109 1.7.2.2 mjf /* init AL7230B radio, part two */
1110 1.7.2.2 mjf for (i = 0; i < N(rfini_2); i++) {
1111 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1112 1.7.2.2 mjf return error;
1113 1.7.2.2 mjf }
1114 1.7.2.2 mjf /* init RF-dependent PHY registers, part three */
1115 1.7.2.2 mjf for (i = 0; i < N(phyini_3); i++) {
1116 1.7.2.2 mjf error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1117 1.7.2.2 mjf if (error != 0)
1118 1.7.2.2 mjf return error;
1119 1.7.2.2 mjf }
1120 1.7.2.2 mjf
1121 1.7.2.2 mjf return 0;
1122 1.7.2.2 mjf #undef N
1123 1.7.2.2 mjf }
1124 1.7.2.2 mjf
1125 1.7.2.2 mjf Static int
1126 1.7.2.2 mjf zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1127 1.7.2.2 mjf {
1128 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1129 1.7.2.2 mjf
1130 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1131 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1132 1.7.2.2 mjf
1133 1.7.2.2 mjf return 0;
1134 1.7.2.2 mjf }
1135 1.7.2.2 mjf
1136 1.7.2.2 mjf Static int
1137 1.7.2.2 mjf zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1138 1.7.2.2 mjf {
1139 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1140 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1141 1.7.2.2 mjf static const struct {
1142 1.7.2.2 mjf uint32_t r1, r2;
1143 1.7.2.2 mjf } rfprog[] = ZYD_AL7230B_CHANTABLE;
1144 1.7.2.2 mjf static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1145 1.7.2.2 mjf int i, error;
1146 1.7.2.2 mjf
1147 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR240, 0x57);
1148 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1149 1.7.2.2 mjf
1150 1.7.2.2 mjf for (i = 0; i < N(rfsc); i++) {
1151 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1152 1.7.2.2 mjf return error;
1153 1.7.2.2 mjf }
1154 1.7.2.2 mjf
1155 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR128, 0x14);
1156 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR129, 0x12);
1157 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR130, 0x10);
1158 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR38, 0x38);
1159 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR136, 0xdf);
1160 1.7.2.2 mjf
1161 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1162 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1163 1.7.2.2 mjf (void)zyd_rfwrite(sc, 0x3c9000);
1164 1.7.2.2 mjf
1165 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1166 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, 0x06);
1167 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR240, 0x08);
1168 1.7.2.2 mjf
1169 1.7.2.2 mjf return 0;
1170 1.7.2.2 mjf #undef N
1171 1.7.2.2 mjf }
1172 1.7.2.2 mjf
1173 1.7.2.2 mjf /*
1174 1.7.2.2 mjf * AL2210 RF methods.
1175 1.7.2.2 mjf */
1176 1.7.2.2 mjf Static int
1177 1.7.2.2 mjf zyd_al2210_init(struct zyd_rf *rf)
1178 1.7.2.2 mjf {
1179 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1180 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1181 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1182 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_AL2210_RF;
1183 1.7.2.2 mjf uint32_t tmp;
1184 1.7.2.2 mjf int i, error;
1185 1.7.2.2 mjf
1186 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR18, 2);
1187 1.7.2.2 mjf
1188 1.7.2.2 mjf /* init RF-dependent PHY registers */
1189 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1190 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1191 1.7.2.2 mjf if (error != 0)
1192 1.7.2.2 mjf return error;
1193 1.7.2.2 mjf }
1194 1.7.2.2 mjf /* init AL2210 radio */
1195 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1196 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1197 1.7.2.2 mjf return error;
1198 1.7.2.2 mjf }
1199 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1200 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1201 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1202 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1203 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1204 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1205 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1206 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR18, 3);
1207 1.7.2.2 mjf
1208 1.7.2.2 mjf return 0;
1209 1.7.2.2 mjf #undef N
1210 1.7.2.2 mjf }
1211 1.7.2.2 mjf
1212 1.7.2.2 mjf Static int
1213 1.7.2.2 mjf zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1214 1.7.2.2 mjf {
1215 1.7.2.2 mjf /* vendor driver does nothing for this RF chip */
1216 1.7.2.2 mjf
1217 1.7.2.2 mjf return 0;
1218 1.7.2.2 mjf }
1219 1.7.2.2 mjf
1220 1.7.2.2 mjf Static int
1221 1.7.2.2 mjf zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1222 1.7.2.2 mjf {
1223 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1224 1.7.2.2 mjf static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1225 1.7.2.2 mjf uint32_t tmp;
1226 1.7.2.2 mjf
1227 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR18, 2);
1228 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1229 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1230 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1231 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1232 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1233 1.7.2.2 mjf
1234 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1235 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1236 1.7.2.2 mjf
1237 1.7.2.2 mjf /* actually set the channel */
1238 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1239 1.7.2.2 mjf
1240 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR18, 3);
1241 1.7.2.2 mjf
1242 1.7.2.2 mjf return 0;
1243 1.7.2.2 mjf }
1244 1.7.2.2 mjf
1245 1.7.2.2 mjf /*
1246 1.7.2.2 mjf * GCT RF methods.
1247 1.7.2.2 mjf */
1248 1.7.2.2 mjf Static int
1249 1.7.2.2 mjf zyd_gct_init(struct zyd_rf *rf)
1250 1.7.2.2 mjf {
1251 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1252 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1253 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1254 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_GCT_RF;
1255 1.7.2.2 mjf int i, error;
1256 1.7.2.2 mjf
1257 1.7.2.2 mjf /* init RF-dependent PHY registers */
1258 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1259 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1260 1.7.2.2 mjf if (error != 0)
1261 1.7.2.2 mjf return error;
1262 1.7.2.2 mjf }
1263 1.7.2.2 mjf /* init cgt radio */
1264 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1265 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1266 1.7.2.2 mjf return error;
1267 1.7.2.2 mjf }
1268 1.7.2.2 mjf return 0;
1269 1.7.2.2 mjf #undef N
1270 1.7.2.2 mjf }
1271 1.7.2.2 mjf
1272 1.7.2.2 mjf Static int
1273 1.7.2.2 mjf zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1274 1.7.2.2 mjf {
1275 1.7.2.2 mjf /* vendor driver does nothing for this RF chip */
1276 1.7.2.2 mjf
1277 1.7.2.2 mjf return 0;
1278 1.7.2.2 mjf }
1279 1.7.2.2 mjf
1280 1.7.2.2 mjf Static int
1281 1.7.2.2 mjf zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1282 1.7.2.2 mjf {
1283 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1284 1.7.2.2 mjf static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1285 1.7.2.2 mjf
1286 1.7.2.2 mjf (void)zyd_rfwrite(sc, 0x1c0000);
1287 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1288 1.7.2.2 mjf (void)zyd_rfwrite(sc, 0x1c0008);
1289 1.7.2.2 mjf
1290 1.7.2.2 mjf return 0;
1291 1.7.2.2 mjf }
1292 1.7.2.2 mjf
1293 1.7.2.2 mjf /*
1294 1.7.2.2 mjf * Maxim RF methods.
1295 1.7.2.2 mjf */
1296 1.7.2.2 mjf Static int
1297 1.7.2.2 mjf zyd_maxim_init(struct zyd_rf *rf)
1298 1.7.2.2 mjf {
1299 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1300 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1301 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1302 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_MAXIM_RF;
1303 1.7.2.2 mjf uint16_t tmp;
1304 1.7.2.2 mjf int i, error;
1305 1.7.2.2 mjf
1306 1.7.2.2 mjf /* init RF-dependent PHY registers */
1307 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1308 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1309 1.7.2.2 mjf if (error != 0)
1310 1.7.2.2 mjf return error;
1311 1.7.2.2 mjf }
1312 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1313 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1314 1.7.2.2 mjf
1315 1.7.2.2 mjf /* init maxim radio */
1316 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1317 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1318 1.7.2.2 mjf return error;
1319 1.7.2.2 mjf }
1320 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1321 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1322 1.7.2.2 mjf
1323 1.7.2.2 mjf return 0;
1324 1.7.2.2 mjf #undef N
1325 1.7.2.2 mjf }
1326 1.7.2.2 mjf
1327 1.7.2.2 mjf Static int
1328 1.7.2.2 mjf zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1329 1.7.2.2 mjf {
1330 1.7.2.2 mjf /* vendor driver does nothing for this RF chip */
1331 1.7.2.2 mjf
1332 1.7.2.2 mjf return 0;
1333 1.7.2.2 mjf }
1334 1.7.2.2 mjf
1335 1.7.2.2 mjf Static int
1336 1.7.2.2 mjf zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1337 1.7.2.2 mjf {
1338 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1339 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1340 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1341 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_MAXIM_RF;
1342 1.7.2.2 mjf static const struct {
1343 1.7.2.2 mjf uint32_t r1, r2;
1344 1.7.2.2 mjf } rfprog[] = ZYD_MAXIM_CHANTABLE;
1345 1.7.2.2 mjf uint16_t tmp;
1346 1.7.2.2 mjf int i, error;
1347 1.7.2.2 mjf
1348 1.7.2.2 mjf /*
1349 1.7.2.2 mjf * Do the same as we do when initializing it, except for the channel
1350 1.7.2.2 mjf * values coming from the two channel tables.
1351 1.7.2.2 mjf */
1352 1.7.2.2 mjf
1353 1.7.2.2 mjf /* init RF-dependent PHY registers */
1354 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1355 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1356 1.7.2.2 mjf if (error != 0)
1357 1.7.2.2 mjf return error;
1358 1.7.2.2 mjf }
1359 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1360 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1361 1.7.2.2 mjf
1362 1.7.2.2 mjf /* first two values taken from the chantables */
1363 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1364 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1365 1.7.2.2 mjf
1366 1.7.2.2 mjf /* init maxim radio - skipping the two first values */
1367 1.7.2.2 mjf for (i = 2; i < N(rfini); i++) {
1368 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1369 1.7.2.2 mjf return error;
1370 1.7.2.2 mjf }
1371 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1372 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1373 1.7.2.2 mjf
1374 1.7.2.2 mjf return 0;
1375 1.7.2.2 mjf #undef N
1376 1.7.2.2 mjf }
1377 1.7.2.2 mjf
1378 1.7.2.2 mjf /*
1379 1.7.2.2 mjf * Maxim2 RF methods.
1380 1.7.2.2 mjf */
1381 1.7.2.2 mjf Static int
1382 1.7.2.2 mjf zyd_maxim2_init(struct zyd_rf *rf)
1383 1.7.2.2 mjf {
1384 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1385 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1386 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1387 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1388 1.7.2.2 mjf uint16_t tmp;
1389 1.7.2.2 mjf int i, error;
1390 1.7.2.2 mjf
1391 1.7.2.2 mjf /* init RF-dependent PHY registers */
1392 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1393 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1394 1.7.2.2 mjf if (error != 0)
1395 1.7.2.2 mjf return error;
1396 1.7.2.2 mjf }
1397 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1398 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1399 1.7.2.2 mjf
1400 1.7.2.2 mjf /* init maxim2 radio */
1401 1.7.2.2 mjf for (i = 0; i < N(rfini); i++) {
1402 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1403 1.7.2.2 mjf return error;
1404 1.7.2.2 mjf }
1405 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1406 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1407 1.7.2.2 mjf
1408 1.7.2.2 mjf return 0;
1409 1.7.2.2 mjf #undef N
1410 1.7.2.2 mjf }
1411 1.7.2.2 mjf
1412 1.7.2.2 mjf Static int
1413 1.7.2.2 mjf zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1414 1.7.2.2 mjf {
1415 1.7.2.2 mjf /* vendor driver does nothing for this RF chip */
1416 1.7.2.2 mjf
1417 1.7.2.2 mjf return 0;
1418 1.7.2.2 mjf }
1419 1.7.2.2 mjf
1420 1.7.2.2 mjf Static int
1421 1.7.2.2 mjf zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1422 1.7.2.2 mjf {
1423 1.7.2.2 mjf #define N(a) (sizeof (a) / sizeof ((a)[0]))
1424 1.7.2.2 mjf struct zyd_softc *sc = rf->rf_sc;
1425 1.7.2.2 mjf static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1426 1.7.2.2 mjf static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1427 1.7.2.2 mjf static const struct {
1428 1.7.2.2 mjf uint32_t r1, r2;
1429 1.7.2.2 mjf } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1430 1.7.2.2 mjf uint16_t tmp;
1431 1.7.2.2 mjf int i, error;
1432 1.7.2.2 mjf
1433 1.7.2.2 mjf /*
1434 1.7.2.2 mjf * Do the same as we do when initializing it, except for the channel
1435 1.7.2.2 mjf * values coming from the two channel tables.
1436 1.7.2.2 mjf */
1437 1.7.2.2 mjf
1438 1.7.2.2 mjf /* init RF-dependent PHY registers */
1439 1.7.2.2 mjf for (i = 0; i < N(phyini); i++) {
1440 1.7.2.2 mjf error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1441 1.7.2.2 mjf if (error != 0)
1442 1.7.2.2 mjf return error;
1443 1.7.2.2 mjf }
1444 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1445 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1446 1.7.2.2 mjf
1447 1.7.2.2 mjf /* first two values taken from the chantables */
1448 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1449 1.7.2.2 mjf (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1450 1.7.2.2 mjf
1451 1.7.2.2 mjf /* init maxim2 radio - skipping the two first values */
1452 1.7.2.2 mjf for (i = 2; i < N(rfini); i++) {
1453 1.7.2.2 mjf if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1454 1.7.2.2 mjf return error;
1455 1.7.2.2 mjf }
1456 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_CR203, &tmp);
1457 1.7.2.2 mjf (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1458 1.7.2.2 mjf
1459 1.7.2.2 mjf return 0;
1460 1.7.2.2 mjf #undef N
1461 1.7.2.2 mjf }
1462 1.7.2.2 mjf
1463 1.7.2.2 mjf Static int
1464 1.7.2.2 mjf zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1465 1.7.2.2 mjf {
1466 1.7.2.2 mjf struct zyd_rf *rf = &sc->sc_rf;
1467 1.7.2.2 mjf
1468 1.7.2.2 mjf rf->rf_sc = sc;
1469 1.7.2.2 mjf
1470 1.7.2.2 mjf switch (type) {
1471 1.7.2.2 mjf case ZYD_RF_RFMD:
1472 1.7.2.2 mjf rf->init = zyd_rfmd_init;
1473 1.7.2.2 mjf rf->switch_radio = zyd_rfmd_switch_radio;
1474 1.7.2.2 mjf rf->set_channel = zyd_rfmd_set_channel;
1475 1.7.2.2 mjf rf->width = 24; /* 24-bit RF values */
1476 1.7.2.2 mjf break;
1477 1.7.2.2 mjf case ZYD_RF_AL2230:
1478 1.7.2.2 mjf if (sc->mac_rev == ZYD_ZD1211B)
1479 1.7.2.2 mjf rf->init = zyd_al2230_init_b;
1480 1.7.2.2 mjf else
1481 1.7.2.2 mjf rf->init = zyd_al2230_init;
1482 1.7.2.2 mjf rf->switch_radio = zyd_al2230_switch_radio;
1483 1.7.2.2 mjf rf->set_channel = zyd_al2230_set_channel;
1484 1.7.2.2 mjf rf->width = 24; /* 24-bit RF values */
1485 1.7.2.2 mjf break;
1486 1.7.2.2 mjf case ZYD_RF_AL7230B:
1487 1.7.2.2 mjf rf->init = zyd_al7230B_init;
1488 1.7.2.2 mjf rf->switch_radio = zyd_al7230B_switch_radio;
1489 1.7.2.2 mjf rf->set_channel = zyd_al7230B_set_channel;
1490 1.7.2.2 mjf rf->width = 24; /* 24-bit RF values */
1491 1.7.2.2 mjf break;
1492 1.7.2.2 mjf case ZYD_RF_AL2210:
1493 1.7.2.2 mjf rf->init = zyd_al2210_init;
1494 1.7.2.2 mjf rf->switch_radio = zyd_al2210_switch_radio;
1495 1.7.2.2 mjf rf->set_channel = zyd_al2210_set_channel;
1496 1.7.2.2 mjf rf->width = 24; /* 24-bit RF values */
1497 1.7.2.2 mjf break;
1498 1.7.2.2 mjf case ZYD_RF_GCT:
1499 1.7.2.2 mjf rf->init = zyd_gct_init;
1500 1.7.2.2 mjf rf->switch_radio = zyd_gct_switch_radio;
1501 1.7.2.2 mjf rf->set_channel = zyd_gct_set_channel;
1502 1.7.2.2 mjf rf->width = 21; /* 21-bit RF values */
1503 1.7.2.2 mjf break;
1504 1.7.2.2 mjf case ZYD_RF_MAXIM_NEW:
1505 1.7.2.2 mjf rf->init = zyd_maxim_init;
1506 1.7.2.2 mjf rf->switch_radio = zyd_maxim_switch_radio;
1507 1.7.2.2 mjf rf->set_channel = zyd_maxim_set_channel;
1508 1.7.2.2 mjf rf->width = 18; /* 18-bit RF values */
1509 1.7.2.2 mjf break;
1510 1.7.2.2 mjf case ZYD_RF_MAXIM_NEW2:
1511 1.7.2.2 mjf rf->init = zyd_maxim2_init;
1512 1.7.2.2 mjf rf->switch_radio = zyd_maxim2_switch_radio;
1513 1.7.2.2 mjf rf->set_channel = zyd_maxim2_set_channel;
1514 1.7.2.2 mjf rf->width = 18; /* 18-bit RF values */
1515 1.7.2.2 mjf break;
1516 1.7.2.2 mjf default:
1517 1.7.2.2 mjf printf("%s: sorry, radio \"%s\" is not supported yet\n",
1518 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), zyd_rf_name(type));
1519 1.7.2.2 mjf return EINVAL;
1520 1.7.2.2 mjf }
1521 1.7.2.2 mjf return 0;
1522 1.7.2.2 mjf }
1523 1.7.2.2 mjf
1524 1.7.2.2 mjf Static const char *
1525 1.7.2.2 mjf zyd_rf_name(uint8_t type)
1526 1.7.2.2 mjf {
1527 1.7.2.2 mjf static const char * const zyd_rfs[] = {
1528 1.7.2.2 mjf "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1529 1.7.2.2 mjf "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1530 1.7.2.2 mjf "PV2000", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1531 1.7.2.2 mjf "PHILIPS"
1532 1.7.2.2 mjf };
1533 1.7.2.2 mjf
1534 1.7.2.2 mjf return zyd_rfs[(type > 15) ? 0 : type];
1535 1.7.2.2 mjf }
1536 1.7.2.2 mjf
1537 1.7.2.2 mjf Static int
1538 1.7.2.2 mjf zyd_hw_init(struct zyd_softc *sc)
1539 1.7.2.2 mjf {
1540 1.7.2.2 mjf struct zyd_rf *rf = &sc->sc_rf;
1541 1.7.2.2 mjf const struct zyd_phy_pair *phyp;
1542 1.7.2.2 mjf int error;
1543 1.7.2.2 mjf
1544 1.7.2.2 mjf /* specify that the plug and play is finished */
1545 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1546 1.7.2.2 mjf
1547 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1548 1.7.2.2 mjf DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1549 1.7.2.2 mjf
1550 1.7.2.2 mjf /* retrieve firmware revision number */
1551 1.7.2.2 mjf (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1552 1.7.2.2 mjf
1553 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1554 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1555 1.7.2.2 mjf
1556 1.7.2.2 mjf /* disable interrupts */
1557 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1558 1.7.2.2 mjf
1559 1.7.2.2 mjf /* PHY init */
1560 1.7.2.2 mjf zyd_lock_phy(sc);
1561 1.7.2.2 mjf phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1562 1.7.2.2 mjf for (; phyp->reg != 0; phyp++) {
1563 1.7.2.2 mjf if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1564 1.7.2.2 mjf goto fail;
1565 1.7.2.2 mjf }
1566 1.7.2.2 mjf zyd_unlock_phy(sc);
1567 1.7.2.2 mjf
1568 1.7.2.2 mjf /* HMAC init */
1569 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1570 1.7.2.2 mjf zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1571 1.7.2.2 mjf
1572 1.7.2.2 mjf if (sc->mac_rev == ZYD_ZD1211) {
1573 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1574 1.7.2.2 mjf } else {
1575 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
1576 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1577 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1578 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1579 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1580 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1581 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1582 1.7.2.2 mjf zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1583 1.7.2.2 mjf }
1584 1.7.2.2 mjf
1585 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1586 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1587 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1588 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1589 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1590 1.7.2.2 mjf zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1591 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1592 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1593 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1594 1.7.2.2 mjf zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1595 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1596 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1597 1.7.2.2 mjf zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1598 1.7.2.2 mjf zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1599 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1600 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1601 1.7.2.2 mjf zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1602 1.7.2.2 mjf
1603 1.7.2.2 mjf /* RF chip init */
1604 1.7.2.2 mjf zyd_lock_phy(sc);
1605 1.7.2.2 mjf error = (*rf->init)(rf);
1606 1.7.2.2 mjf zyd_unlock_phy(sc);
1607 1.7.2.2 mjf if (error != 0) {
1608 1.7.2.2 mjf printf("%s: radio initialization failed\n",
1609 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
1610 1.7.2.2 mjf goto fail;
1611 1.7.2.2 mjf }
1612 1.7.2.2 mjf
1613 1.7.2.2 mjf /* init beacon interval to 100ms */
1614 1.7.2.2 mjf if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1615 1.7.2.2 mjf goto fail;
1616 1.7.2.2 mjf
1617 1.7.2.2 mjf fail: return error;
1618 1.7.2.2 mjf }
1619 1.7.2.2 mjf
1620 1.7.2.2 mjf Static int
1621 1.7.2.2 mjf zyd_read_eeprom(struct zyd_softc *sc)
1622 1.7.2.2 mjf {
1623 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
1624 1.7.2.2 mjf uint32_t tmp;
1625 1.7.2.2 mjf uint16_t val;
1626 1.7.2.2 mjf int i;
1627 1.7.2.2 mjf
1628 1.7.2.2 mjf /* read MAC address */
1629 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1630 1.7.2.2 mjf ic->ic_myaddr[0] = tmp & 0xff;
1631 1.7.2.2 mjf ic->ic_myaddr[1] = tmp >> 8;
1632 1.7.2.2 mjf ic->ic_myaddr[2] = tmp >> 16;
1633 1.7.2.2 mjf ic->ic_myaddr[3] = tmp >> 24;
1634 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1635 1.7.2.2 mjf ic->ic_myaddr[4] = tmp & 0xff;
1636 1.7.2.2 mjf ic->ic_myaddr[5] = tmp >> 8;
1637 1.7.2.2 mjf
1638 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1639 1.7.2.2 mjf sc->rf_rev = tmp & 0x0f;
1640 1.7.2.2 mjf sc->pa_rev = (tmp >> 16) & 0x0f;
1641 1.7.2.2 mjf
1642 1.7.2.2 mjf /* read regulatory domain (currently unused) */
1643 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1644 1.7.2.2 mjf sc->regdomain = tmp >> 16;
1645 1.7.2.2 mjf DPRINTF(("regulatory domain %x\n", sc->regdomain));
1646 1.7.2.2 mjf
1647 1.7.2.2 mjf /* read Tx power calibration tables */
1648 1.7.2.2 mjf for (i = 0; i < 7; i++) {
1649 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1650 1.7.2.2 mjf sc->pwr_cal[i * 2] = val >> 8;
1651 1.7.2.2 mjf sc->pwr_cal[i * 2 + 1] = val & 0xff;
1652 1.7.2.2 mjf
1653 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1654 1.7.2.2 mjf sc->pwr_int[i * 2] = val >> 8;
1655 1.7.2.2 mjf sc->pwr_int[i * 2 + 1] = val & 0xff;
1656 1.7.2.2 mjf
1657 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1658 1.7.2.2 mjf sc->ofdm36_cal[i * 2] = val >> 8;
1659 1.7.2.2 mjf sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1660 1.7.2.2 mjf
1661 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1662 1.7.2.2 mjf sc->ofdm48_cal[i * 2] = val >> 8;
1663 1.7.2.2 mjf sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1664 1.7.2.2 mjf
1665 1.7.2.2 mjf (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1666 1.7.2.2 mjf sc->ofdm54_cal[i * 2] = val >> 8;
1667 1.7.2.2 mjf sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1668 1.7.2.2 mjf }
1669 1.7.2.2 mjf return 0;
1670 1.7.2.2 mjf }
1671 1.7.2.2 mjf
1672 1.7.2.2 mjf Static int
1673 1.7.2.2 mjf zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1674 1.7.2.2 mjf {
1675 1.7.2.2 mjf uint32_t tmp;
1676 1.7.2.2 mjf
1677 1.7.2.2 mjf tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1678 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1679 1.7.2.2 mjf
1680 1.7.2.2 mjf tmp = addr[5] << 8 | addr[4];
1681 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1682 1.7.2.2 mjf
1683 1.7.2.2 mjf return 0;
1684 1.7.2.2 mjf }
1685 1.7.2.2 mjf
1686 1.7.2.2 mjf Static int
1687 1.7.2.2 mjf zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1688 1.7.2.2 mjf {
1689 1.7.2.2 mjf uint32_t tmp;
1690 1.7.2.2 mjf
1691 1.7.2.2 mjf tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1692 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1693 1.7.2.2 mjf
1694 1.7.2.2 mjf tmp = addr[5] << 8 | addr[4];
1695 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1696 1.7.2.2 mjf
1697 1.7.2.2 mjf return 0;
1698 1.7.2.2 mjf }
1699 1.7.2.2 mjf
1700 1.7.2.2 mjf Static int
1701 1.7.2.2 mjf zyd_switch_radio(struct zyd_softc *sc, int on)
1702 1.7.2.2 mjf {
1703 1.7.2.2 mjf struct zyd_rf *rf = &sc->sc_rf;
1704 1.7.2.2 mjf int error;
1705 1.7.2.2 mjf
1706 1.7.2.2 mjf zyd_lock_phy(sc);
1707 1.7.2.2 mjf error = (*rf->switch_radio)(rf, on);
1708 1.7.2.2 mjf zyd_unlock_phy(sc);
1709 1.7.2.2 mjf
1710 1.7.2.2 mjf return error;
1711 1.7.2.2 mjf }
1712 1.7.2.2 mjf
1713 1.7.2.2 mjf Static void
1714 1.7.2.2 mjf zyd_set_led(struct zyd_softc *sc, int which, int on)
1715 1.7.2.2 mjf {
1716 1.7.2.2 mjf uint32_t tmp;
1717 1.7.2.2 mjf
1718 1.7.2.2 mjf (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1719 1.7.2.2 mjf tmp &= ~which;
1720 1.7.2.2 mjf if (on)
1721 1.7.2.2 mjf tmp |= which;
1722 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1723 1.7.2.2 mjf }
1724 1.7.2.2 mjf
1725 1.7.2.2 mjf Static int
1726 1.7.2.2 mjf zyd_set_rxfilter(struct zyd_softc *sc)
1727 1.7.2.2 mjf {
1728 1.7.2.2 mjf uint32_t rxfilter;
1729 1.7.2.2 mjf
1730 1.7.2.2 mjf switch (sc->sc_ic.ic_opmode) {
1731 1.7.2.2 mjf case IEEE80211_M_STA:
1732 1.7.2.2 mjf rxfilter = ZYD_FILTER_BSS;
1733 1.7.2.2 mjf break;
1734 1.7.2.2 mjf case IEEE80211_M_IBSS:
1735 1.7.2.2 mjf case IEEE80211_M_HOSTAP:
1736 1.7.2.2 mjf rxfilter = ZYD_FILTER_HOSTAP;
1737 1.7.2.2 mjf break;
1738 1.7.2.2 mjf case IEEE80211_M_MONITOR:
1739 1.7.2.2 mjf rxfilter = ZYD_FILTER_MONITOR;
1740 1.7.2.2 mjf break;
1741 1.7.2.2 mjf default:
1742 1.7.2.2 mjf /* should not get there */
1743 1.7.2.2 mjf return EINVAL;
1744 1.7.2.2 mjf }
1745 1.7.2.2 mjf return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1746 1.7.2.2 mjf }
1747 1.7.2.2 mjf
1748 1.7.2.2 mjf Static void
1749 1.7.2.2 mjf zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1750 1.7.2.2 mjf {
1751 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
1752 1.7.2.2 mjf struct zyd_rf *rf = &sc->sc_rf;
1753 1.7.2.2 mjf u_int chan;
1754 1.7.2.2 mjf
1755 1.7.2.2 mjf chan = ieee80211_chan2ieee(ic, c);
1756 1.7.2.2 mjf if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1757 1.7.2.2 mjf return;
1758 1.7.2.2 mjf
1759 1.7.2.2 mjf zyd_lock_phy(sc);
1760 1.7.2.2 mjf
1761 1.7.2.2 mjf (*rf->set_channel)(rf, chan);
1762 1.7.2.2 mjf
1763 1.7.2.2 mjf /* update Tx power */
1764 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1765 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1766 1.7.2.2 mjf
1767 1.7.2.2 mjf if (sc->mac_rev == ZYD_ZD1211B) {
1768 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1769 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1770 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1771 1.7.2.2 mjf
1772 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR69, 0x28);
1773 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR69, 0x2a);
1774 1.7.2.2 mjf }
1775 1.7.2.2 mjf
1776 1.7.2.2 mjf zyd_unlock_phy(sc);
1777 1.7.2.2 mjf }
1778 1.7.2.2 mjf
1779 1.7.2.2 mjf Static int
1780 1.7.2.2 mjf zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1781 1.7.2.2 mjf {
1782 1.7.2.2 mjf /* XXX this is probably broken.. */
1783 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1784 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1);
1785 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval);
1786 1.7.2.2 mjf
1787 1.7.2.2 mjf return 0;
1788 1.7.2.2 mjf }
1789 1.7.2.2 mjf
1790 1.7.2.2 mjf Static uint8_t
1791 1.7.2.2 mjf zyd_plcp_signal(int rate)
1792 1.7.2.2 mjf {
1793 1.7.2.2 mjf switch (rate) {
1794 1.7.2.2 mjf /* CCK rates (returned values are device-dependent) */
1795 1.7.2.2 mjf case 2: return 0x0;
1796 1.7.2.2 mjf case 4: return 0x1;
1797 1.7.2.2 mjf case 11: return 0x2;
1798 1.7.2.2 mjf case 22: return 0x3;
1799 1.7.2.2 mjf
1800 1.7.2.2 mjf /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1801 1.7.2.2 mjf case 12: return 0xb;
1802 1.7.2.2 mjf case 18: return 0xf;
1803 1.7.2.2 mjf case 24: return 0xa;
1804 1.7.2.2 mjf case 36: return 0xe;
1805 1.7.2.2 mjf case 48: return 0x9;
1806 1.7.2.2 mjf case 72: return 0xd;
1807 1.7.2.2 mjf case 96: return 0x8;
1808 1.7.2.2 mjf case 108: return 0xc;
1809 1.7.2.2 mjf
1810 1.7.2.2 mjf /* unsupported rates (should not get there) */
1811 1.7.2.2 mjf default: return 0xff;
1812 1.7.2.2 mjf }
1813 1.7.2.2 mjf }
1814 1.7.2.2 mjf
1815 1.7.2.2 mjf Static void
1816 1.7.2.2 mjf zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1817 1.7.2.2 mjf {
1818 1.7.2.2 mjf struct zyd_softc *sc = (struct zyd_softc *)priv;
1819 1.7.2.2 mjf struct zyd_cmd *cmd;
1820 1.7.2.2 mjf uint32_t datalen;
1821 1.7.2.2 mjf
1822 1.7.2.2 mjf if (status != USBD_NORMAL_COMPLETION) {
1823 1.7.2.2 mjf if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1824 1.7.2.2 mjf return;
1825 1.7.2.2 mjf
1826 1.7.2.2 mjf if (status == USBD_STALLED) {
1827 1.7.2.2 mjf usbd_clear_endpoint_stall_async(
1828 1.7.2.2 mjf sc->zyd_ep[ZYD_ENDPT_IIN]);
1829 1.7.2.2 mjf }
1830 1.7.2.2 mjf return;
1831 1.7.2.2 mjf }
1832 1.7.2.2 mjf
1833 1.7.2.2 mjf cmd = (struct zyd_cmd *)sc->ibuf;
1834 1.7.2.2 mjf
1835 1.7.2.2 mjf if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1836 1.7.2.2 mjf struct zyd_notif_retry *retry =
1837 1.7.2.2 mjf (struct zyd_notif_retry *)cmd->data;
1838 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
1839 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
1840 1.7.2.2 mjf struct ieee80211_node *ni;
1841 1.7.2.2 mjf
1842 1.7.2.2 mjf DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
1843 1.7.2.2 mjf le16toh(retry->rate), ether_sprintf(retry->macaddr),
1844 1.7.2.2 mjf le16toh(retry->count) & 0xff, le16toh(retry->count)));
1845 1.7.2.2 mjf
1846 1.7.2.2 mjf /*
1847 1.7.2.2 mjf * Find the node to which the packet was sent and update its
1848 1.7.2.2 mjf * retry statistics. In BSS mode, this node is the AP we're
1849 1.7.2.2 mjf * associated to so no lookup is actually needed.
1850 1.7.2.2 mjf */
1851 1.7.2.2 mjf if (ic->ic_opmode != IEEE80211_M_STA) {
1852 1.7.2.2 mjf ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr);
1853 1.7.2.2 mjf if (ni == NULL)
1854 1.7.2.2 mjf return; /* just ignore */
1855 1.7.2.2 mjf } else
1856 1.7.2.2 mjf ni = ic->ic_bss;
1857 1.7.2.2 mjf
1858 1.7.2.2 mjf ((struct zyd_node *)ni)->amn.amn_retrycnt++;
1859 1.7.2.2 mjf
1860 1.7.2.2 mjf if (le16toh(retry->count) & 0x100)
1861 1.7.2.2 mjf ifp->if_oerrors++; /* too many retries */
1862 1.7.2.2 mjf
1863 1.7.2.2 mjf } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
1864 1.7.2.2 mjf struct rq *rqp;
1865 1.7.2.2 mjf
1866 1.7.2.2 mjf if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1867 1.7.2.2 mjf return; /* HMAC interrupt */
1868 1.7.2.2 mjf
1869 1.7.2.2 mjf usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
1870 1.7.2.2 mjf datalen -= sizeof(cmd->code);
1871 1.7.2.2 mjf datalen -= 2; /* XXX: padding? */
1872 1.7.2.2 mjf
1873 1.7.2.2 mjf SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
1874 1.7.2.2 mjf int i;
1875 1.7.2.2 mjf
1876 1.7.2.2 mjf if (sizeof(struct zyd_pair) * rqp->len != datalen)
1877 1.7.2.2 mjf continue;
1878 1.7.2.2 mjf for (i = 0; i < rqp->len; i++) {
1879 1.7.2.2 mjf if (*(((const uint16_t *)rqp->idata) + i) !=
1880 1.7.2.2 mjf (((struct zyd_pair *)cmd->data) + i)->reg)
1881 1.7.2.2 mjf break;
1882 1.7.2.2 mjf }
1883 1.7.2.2 mjf if (i != rqp->len)
1884 1.7.2.2 mjf continue;
1885 1.7.2.2 mjf
1886 1.7.2.2 mjf /* copy answer into caller-supplied buffer */
1887 1.7.2.2 mjf bcopy(cmd->data, rqp->odata,
1888 1.7.2.2 mjf sizeof(struct zyd_pair) * rqp->len);
1889 1.7.2.2 mjf wakeup(rqp->odata); /* wakeup caller */
1890 1.7.2.2 mjf
1891 1.7.2.2 mjf return;
1892 1.7.2.2 mjf }
1893 1.7.2.2 mjf return; /* unexpected IORD notification */
1894 1.7.2.2 mjf } else {
1895 1.7.2.2 mjf printf("%s: unknown notification %x\n", USBDEVNAME(sc->sc_dev),
1896 1.7.2.2 mjf le16toh(cmd->code));
1897 1.7.2.2 mjf }
1898 1.7.2.2 mjf }
1899 1.7.2.2 mjf
1900 1.7.2.2 mjf Static void
1901 1.7.2.2 mjf zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1902 1.7.2.2 mjf {
1903 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
1904 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
1905 1.7.2.2 mjf struct ieee80211_node *ni;
1906 1.7.2.2 mjf struct ieee80211_frame *wh;
1907 1.7.2.2 mjf const struct zyd_plcphdr *plcp;
1908 1.7.2.2 mjf const struct zyd_rx_stat *stat;
1909 1.7.2.2 mjf struct mbuf *m;
1910 1.7.2.2 mjf int rlen, s;
1911 1.7.2.2 mjf
1912 1.7.2.2 mjf if (len < ZYD_MIN_FRAGSZ) {
1913 1.7.2.2 mjf printf("%s: frame too short (length=%d)\n",
1914 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), len);
1915 1.7.2.2 mjf ifp->if_ierrors++;
1916 1.7.2.2 mjf return;
1917 1.7.2.2 mjf }
1918 1.7.2.2 mjf
1919 1.7.2.2 mjf plcp = (const struct zyd_plcphdr *)buf;
1920 1.7.2.2 mjf stat = (const struct zyd_rx_stat *)
1921 1.7.2.2 mjf (buf + len - sizeof (struct zyd_rx_stat));
1922 1.7.2.2 mjf
1923 1.7.2.2 mjf if (stat->flags & ZYD_RX_ERROR) {
1924 1.7.2.2 mjf DPRINTF(("%s: RX status indicated error (%x)\n",
1925 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), stat->flags));
1926 1.7.2.2 mjf ifp->if_ierrors++;
1927 1.7.2.2 mjf return;
1928 1.7.2.2 mjf }
1929 1.7.2.2 mjf
1930 1.7.2.2 mjf /* compute actual frame length */
1931 1.7.2.2 mjf rlen = len - sizeof (struct zyd_plcphdr) -
1932 1.7.2.2 mjf sizeof (struct zyd_rx_stat) - IEEE80211_CRC_LEN;
1933 1.7.2.2 mjf
1934 1.7.2.2 mjf /* allocate a mbuf to store the frame */
1935 1.7.2.2 mjf MGETHDR(m, M_DONTWAIT, MT_DATA);
1936 1.7.2.2 mjf if (m == NULL) {
1937 1.7.2.2 mjf printf("%s: could not allocate rx mbuf\n",
1938 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
1939 1.7.2.2 mjf ifp->if_ierrors++;
1940 1.7.2.2 mjf return;
1941 1.7.2.2 mjf }
1942 1.7.2.2 mjf if (rlen > MHLEN) {
1943 1.7.2.2 mjf MCLGET(m, M_DONTWAIT);
1944 1.7.2.2 mjf if (!(m->m_flags & M_EXT)) {
1945 1.7.2.2 mjf printf("%s: could not allocate rx mbuf cluster\n",
1946 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
1947 1.7.2.2 mjf m_freem(m);
1948 1.7.2.2 mjf ifp->if_ierrors++;
1949 1.7.2.2 mjf return;
1950 1.7.2.2 mjf }
1951 1.7.2.2 mjf }
1952 1.7.2.2 mjf m->m_pkthdr.rcvif = ifp;
1953 1.7.2.2 mjf m->m_pkthdr.len = m->m_len = rlen;
1954 1.7.2.2 mjf bcopy((const uint8_t *)(plcp + 1), mtod(m, uint8_t *), rlen);
1955 1.7.2.2 mjf
1956 1.7.2.2 mjf s = splnet();
1957 1.7.2.2 mjf
1958 1.7.2.2 mjf #if NBPFILTER > 0
1959 1.7.2.2 mjf if (sc->sc_drvbpf != NULL) {
1960 1.7.2.2 mjf struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1961 1.7.2.2 mjf static const uint8_t rates[] = {
1962 1.7.2.2 mjf /* reverse function of zyd_plcp_signal() */
1963 1.7.2.2 mjf 2, 4, 11, 22, 0, 0, 0, 0,
1964 1.7.2.2 mjf 96, 48, 24, 12, 108, 72, 36, 18
1965 1.7.2.2 mjf };
1966 1.7.2.2 mjf
1967 1.7.2.2 mjf tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1968 1.7.2.2 mjf tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1969 1.7.2.2 mjf tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1970 1.7.2.2 mjf tap->wr_rssi = stat->rssi;
1971 1.7.2.2 mjf tap->wr_rate = rates[plcp->signal & 0xf];
1972 1.7.2.2 mjf
1973 1.7.2.2 mjf bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1974 1.7.2.2 mjf }
1975 1.7.2.2 mjf #endif
1976 1.7.2.2 mjf
1977 1.7.2.2 mjf wh = mtod(m, struct ieee80211_frame *);
1978 1.7.2.2 mjf ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1979 1.7.2.2 mjf ieee80211_input(ic, m, ni, stat->rssi, 0);
1980 1.7.2.2 mjf
1981 1.7.2.2 mjf /* node is no longer needed */
1982 1.7.2.2 mjf ieee80211_free_node(ni);
1983 1.7.2.2 mjf
1984 1.7.2.2 mjf splx(s);
1985 1.7.2.2 mjf }
1986 1.7.2.2 mjf
1987 1.7.2.2 mjf Static void
1988 1.7.2.2 mjf zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1989 1.7.2.2 mjf {
1990 1.7.2.2 mjf struct zyd_rx_data *data = priv;
1991 1.7.2.2 mjf struct zyd_softc *sc = data->sc;
1992 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
1993 1.7.2.2 mjf const struct zyd_rx_desc *desc;
1994 1.7.2.2 mjf int len;
1995 1.7.2.2 mjf
1996 1.7.2.2 mjf if (status != USBD_NORMAL_COMPLETION) {
1997 1.7.2.2 mjf if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1998 1.7.2.2 mjf return;
1999 1.7.2.2 mjf
2000 1.7.2.2 mjf if (status == USBD_STALLED)
2001 1.7.2.2 mjf usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2002 1.7.2.2 mjf
2003 1.7.2.2 mjf goto skip;
2004 1.7.2.2 mjf }
2005 1.7.2.2 mjf usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2006 1.7.2.2 mjf
2007 1.7.2.2 mjf if (len < ZYD_MIN_RXBUFSZ) {
2008 1.7.2.2 mjf printf("%s: xfer too short (length=%d)\n",
2009 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), len);
2010 1.7.2.2 mjf ifp->if_ierrors++;
2011 1.7.2.2 mjf goto skip;
2012 1.7.2.2 mjf }
2013 1.7.2.2 mjf
2014 1.7.2.2 mjf desc = (const struct zyd_rx_desc *)
2015 1.7.2.2 mjf (data->buf + len - sizeof (struct zyd_rx_desc));
2016 1.7.2.2 mjf
2017 1.7.2.2 mjf if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2018 1.7.2.2 mjf const uint8_t *p = data->buf, *end = p + len;
2019 1.7.2.2 mjf int i;
2020 1.7.2.2 mjf
2021 1.7.2.2 mjf DPRINTFN(3, ("received multi-frame transfer\n"));
2022 1.7.2.2 mjf
2023 1.7.2.2 mjf for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2024 1.7.2.2 mjf const uint16_t len16 = UGETW(desc->len[i]);
2025 1.7.2.2 mjf
2026 1.7.2.2 mjf if (len16 == 0 || p + len16 > end)
2027 1.7.2.2 mjf break;
2028 1.7.2.2 mjf
2029 1.7.2.2 mjf zyd_rx_data(sc, p, len16);
2030 1.7.2.2 mjf /* next frame is aligned on a 32-bit boundary */
2031 1.7.2.2 mjf p += (len16 + 3) & ~3;
2032 1.7.2.2 mjf }
2033 1.7.2.2 mjf } else {
2034 1.7.2.2 mjf DPRINTFN(3, ("received single-frame transfer\n"));
2035 1.7.2.2 mjf
2036 1.7.2.2 mjf zyd_rx_data(sc, data->buf, len);
2037 1.7.2.2 mjf }
2038 1.7.2.2 mjf
2039 1.7.2.2 mjf skip: /* setup a new transfer */
2040 1.7.2.2 mjf usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
2041 1.7.2.2 mjf ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2042 1.7.2.2 mjf USBD_NO_TIMEOUT, zyd_rxeof);
2043 1.7.2.2 mjf (void)usbd_transfer(xfer);
2044 1.7.2.2 mjf }
2045 1.7.2.2 mjf
2046 1.7.2.2 mjf Static int
2047 1.7.2.2 mjf zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2048 1.7.2.2 mjf {
2049 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2050 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
2051 1.7.2.2 mjf struct zyd_tx_desc *desc;
2052 1.7.2.2 mjf struct zyd_tx_data *data;
2053 1.7.2.2 mjf struct ieee80211_frame *wh;
2054 1.7.2.2 mjf int xferlen, totlen, rate;
2055 1.7.2.2 mjf uint16_t pktlen;
2056 1.7.2.2 mjf usbd_status error;
2057 1.7.2.2 mjf
2058 1.7.2.2 mjf data = &sc->tx_data[0];
2059 1.7.2.2 mjf desc = (struct zyd_tx_desc *)data->buf;
2060 1.7.2.2 mjf
2061 1.7.2.2 mjf rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2062 1.7.2.2 mjf
2063 1.7.2.2 mjf data->ni = ni;
2064 1.7.2.2 mjf
2065 1.7.2.2 mjf wh = mtod(m0, struct ieee80211_frame *);
2066 1.7.2.2 mjf
2067 1.7.2.2 mjf xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
2068 1.7.2.2 mjf totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2069 1.7.2.2 mjf
2070 1.7.2.2 mjf /* fill Tx descriptor */
2071 1.7.2.2 mjf desc->len = htole16(totlen);
2072 1.7.2.2 mjf
2073 1.7.2.2 mjf desc->flags = ZYD_TX_FLAG_BACKOFF;
2074 1.7.2.2 mjf if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2075 1.7.2.2 mjf /* multicast frames are not sent at OFDM rates in 802.11b/g */
2076 1.7.2.2 mjf if (totlen > ic->ic_rtsthreshold) {
2077 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_RTS;
2078 1.7.2.2 mjf } else if (ZYD_RATE_IS_OFDM(rate) &&
2079 1.7.2.2 mjf (ic->ic_flags & IEEE80211_F_USEPROT)) {
2080 1.7.2.2 mjf if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2081 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2082 1.7.2.2 mjf else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2083 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_RTS;
2084 1.7.2.2 mjf }
2085 1.7.2.2 mjf } else
2086 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_MULTICAST;
2087 1.7.2.2 mjf
2088 1.7.2.2 mjf if ((wh->i_fc[0] &
2089 1.7.2.2 mjf (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2090 1.7.2.2 mjf (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2091 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2092 1.7.2.2 mjf
2093 1.7.2.2 mjf desc->phy = zyd_plcp_signal(rate);
2094 1.7.2.2 mjf if (ZYD_RATE_IS_OFDM(rate)) {
2095 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_OFDM;
2096 1.7.2.2 mjf if (ic->ic_curmode == IEEE80211_MODE_11A)
2097 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_5GHZ;
2098 1.7.2.2 mjf } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2099 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2100 1.7.2.2 mjf
2101 1.7.2.2 mjf /* actual transmit length (XXX why +10?) */
2102 1.7.2.2 mjf pktlen = sizeof (struct zyd_tx_desc) + 10;
2103 1.7.2.2 mjf if (sc->mac_rev == ZYD_ZD1211)
2104 1.7.2.2 mjf pktlen += totlen;
2105 1.7.2.2 mjf desc->pktlen = htole16(pktlen);
2106 1.7.2.2 mjf
2107 1.7.2.2 mjf desc->plcp_length = (16 * totlen + rate - 1) / rate;
2108 1.7.2.2 mjf desc->plcp_service = 0;
2109 1.7.2.2 mjf if (rate == 22) {
2110 1.7.2.2 mjf const int remainder = (16 * totlen) % 22;
2111 1.7.2.2 mjf if (remainder != 0 && remainder < 7)
2112 1.7.2.2 mjf desc->plcp_service |= ZYD_PLCP_LENGEXT;
2113 1.7.2.2 mjf }
2114 1.7.2.2 mjf
2115 1.7.2.2 mjf #if NBPFILTER > 0
2116 1.7.2.2 mjf if (sc->sc_drvbpf != NULL) {
2117 1.7.2.2 mjf struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2118 1.7.2.2 mjf
2119 1.7.2.2 mjf tap->wt_flags = 0;
2120 1.7.2.2 mjf tap->wt_rate = rate;
2121 1.7.2.2 mjf tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2122 1.7.2.2 mjf tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2123 1.7.2.2 mjf
2124 1.7.2.2 mjf bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2125 1.7.2.2 mjf }
2126 1.7.2.2 mjf #endif
2127 1.7.2.2 mjf
2128 1.7.2.2 mjf m_copydata(m0, 0, m0->m_pkthdr.len,
2129 1.7.2.2 mjf data->buf + sizeof (struct zyd_tx_desc));
2130 1.7.2.2 mjf
2131 1.7.2.2 mjf DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
2132 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2133 1.7.2.2 mjf
2134 1.7.2.2 mjf m_freem(m0); /* mbuf no longer needed */
2135 1.7.2.2 mjf
2136 1.7.2.2 mjf usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2137 1.7.2.2 mjf data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2138 1.7.2.2 mjf ZYD_TX_TIMEOUT, zyd_txeof);
2139 1.7.2.2 mjf error = usbd_transfer(data->xfer);
2140 1.7.2.2 mjf if (error != USBD_IN_PROGRESS && error != 0) {
2141 1.7.2.2 mjf ifp->if_oerrors++;
2142 1.7.2.2 mjf return EIO;
2143 1.7.2.2 mjf }
2144 1.7.2.2 mjf sc->tx_queued++;
2145 1.7.2.2 mjf
2146 1.7.2.2 mjf return 0;
2147 1.7.2.2 mjf }
2148 1.7.2.2 mjf
2149 1.7.2.2 mjf Static void
2150 1.7.2.2 mjf zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2151 1.7.2.2 mjf {
2152 1.7.2.2 mjf struct zyd_tx_data *data = priv;
2153 1.7.2.2 mjf struct zyd_softc *sc = data->sc;
2154 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
2155 1.7.2.2 mjf int s;
2156 1.7.2.2 mjf
2157 1.7.2.2 mjf if (status != USBD_NORMAL_COMPLETION) {
2158 1.7.2.2 mjf if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2159 1.7.2.2 mjf return;
2160 1.7.2.2 mjf
2161 1.7.2.2 mjf printf("%s: could not transmit buffer: %s\n",
2162 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), usbd_errstr(status));
2163 1.7.2.2 mjf
2164 1.7.2.2 mjf if (status == USBD_STALLED) {
2165 1.7.2.2 mjf usbd_clear_endpoint_stall_async(
2166 1.7.2.2 mjf sc->zyd_ep[ZYD_ENDPT_BOUT]);
2167 1.7.2.2 mjf }
2168 1.7.2.2 mjf ifp->if_oerrors++;
2169 1.7.2.2 mjf return;
2170 1.7.2.2 mjf }
2171 1.7.2.2 mjf
2172 1.7.2.2 mjf s = splnet();
2173 1.7.2.2 mjf
2174 1.7.2.2 mjf /* update rate control statistics */
2175 1.7.2.2 mjf ((struct zyd_node *)data->ni)->amn.amn_txcnt++;
2176 1.7.2.2 mjf
2177 1.7.2.2 mjf ieee80211_free_node(data->ni);
2178 1.7.2.2 mjf data->ni = NULL;
2179 1.7.2.2 mjf
2180 1.7.2.2 mjf sc->tx_queued--;
2181 1.7.2.2 mjf ifp->if_opackets++;
2182 1.7.2.2 mjf
2183 1.7.2.2 mjf sc->tx_timer = 0;
2184 1.7.2.2 mjf ifp->if_flags &= ~IFF_OACTIVE;
2185 1.7.2.2 mjf zyd_start(ifp);
2186 1.7.2.2 mjf
2187 1.7.2.2 mjf splx(s);
2188 1.7.2.2 mjf }
2189 1.7.2.2 mjf
2190 1.7.2.2 mjf Static int
2191 1.7.2.2 mjf zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2192 1.7.2.2 mjf {
2193 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2194 1.7.2.2 mjf struct ifnet *ifp = &sc->sc_if;
2195 1.7.2.2 mjf struct zyd_tx_desc *desc;
2196 1.7.2.2 mjf struct zyd_tx_data *data;
2197 1.7.2.2 mjf struct ieee80211_frame *wh;
2198 1.7.2.2 mjf struct ieee80211_key *k;
2199 1.7.2.2 mjf int xferlen, totlen, rate;
2200 1.7.2.2 mjf uint16_t pktlen;
2201 1.7.2.2 mjf usbd_status error;
2202 1.7.2.2 mjf
2203 1.7.2.2 mjf wh = mtod(m0, struct ieee80211_frame *);
2204 1.7.2.2 mjf
2205 1.7.2.2 mjf if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2206 1.7.2.2 mjf rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
2207 1.7.2.2 mjf else
2208 1.7.2.2 mjf rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2209 1.7.2.2 mjf rate &= IEEE80211_RATE_VAL;
2210 1.7.2.2 mjf
2211 1.7.2.2 mjf if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2212 1.7.2.2 mjf k = ieee80211_crypto_encap(ic, ni, m0);
2213 1.7.2.2 mjf if (k == NULL) {
2214 1.7.2.2 mjf m_freem(m0);
2215 1.7.2.2 mjf return ENOBUFS;
2216 1.7.2.2 mjf }
2217 1.7.2.2 mjf
2218 1.7.2.2 mjf /* packet header may have moved, reset our local pointer */
2219 1.7.2.2 mjf wh = mtod(m0, struct ieee80211_frame *);
2220 1.7.2.2 mjf }
2221 1.7.2.2 mjf
2222 1.7.2.2 mjf data = &sc->tx_data[0];
2223 1.7.2.2 mjf desc = (struct zyd_tx_desc *)data->buf;
2224 1.7.2.2 mjf
2225 1.7.2.2 mjf data->ni = ni;
2226 1.7.2.2 mjf
2227 1.7.2.2 mjf xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
2228 1.7.2.2 mjf totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2229 1.7.2.2 mjf
2230 1.7.2.2 mjf /* fill Tx descriptor */
2231 1.7.2.2 mjf desc->len = htole16(totlen);
2232 1.7.2.2 mjf
2233 1.7.2.2 mjf desc->flags = ZYD_TX_FLAG_BACKOFF;
2234 1.7.2.2 mjf if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2235 1.7.2.2 mjf /* multicast frames are not sent at OFDM rates in 802.11b/g */
2236 1.7.2.2 mjf if (totlen > ic->ic_rtsthreshold) {
2237 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_RTS;
2238 1.7.2.2 mjf } else if (ZYD_RATE_IS_OFDM(rate) &&
2239 1.7.2.2 mjf (ic->ic_flags & IEEE80211_F_USEPROT)) {
2240 1.7.2.2 mjf if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2241 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2242 1.7.2.2 mjf else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2243 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_RTS;
2244 1.7.2.2 mjf }
2245 1.7.2.2 mjf } else
2246 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_MULTICAST;
2247 1.7.2.2 mjf
2248 1.7.2.2 mjf if ((wh->i_fc[0] &
2249 1.7.2.2 mjf (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2250 1.7.2.2 mjf (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2251 1.7.2.2 mjf desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2252 1.7.2.2 mjf
2253 1.7.2.2 mjf desc->phy = zyd_plcp_signal(rate);
2254 1.7.2.2 mjf if (ZYD_RATE_IS_OFDM(rate)) {
2255 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_OFDM;
2256 1.7.2.2 mjf if (ic->ic_curmode == IEEE80211_MODE_11A)
2257 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_5GHZ;
2258 1.7.2.2 mjf } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2259 1.7.2.2 mjf desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2260 1.7.2.2 mjf
2261 1.7.2.2 mjf /* actual transmit length (XXX why +10?) */
2262 1.7.2.2 mjf pktlen = sizeof (struct zyd_tx_desc) + 10;
2263 1.7.2.2 mjf if (sc->mac_rev == ZYD_ZD1211)
2264 1.7.2.2 mjf pktlen += totlen;
2265 1.7.2.2 mjf desc->pktlen = htole16(pktlen);
2266 1.7.2.2 mjf
2267 1.7.2.2 mjf desc->plcp_length = (16 * totlen + rate - 1) / rate;
2268 1.7.2.2 mjf desc->plcp_service = 0;
2269 1.7.2.2 mjf if (rate == 22) {
2270 1.7.2.2 mjf const int remainder = (16 * totlen) % 22;
2271 1.7.2.2 mjf if (remainder != 0 && remainder < 7)
2272 1.7.2.2 mjf desc->plcp_service |= ZYD_PLCP_LENGEXT;
2273 1.7.2.2 mjf }
2274 1.7.2.2 mjf
2275 1.7.2.2 mjf #if NBPFILTER > 0
2276 1.7.2.2 mjf if (sc->sc_drvbpf != NULL) {
2277 1.7.2.2 mjf struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2278 1.7.2.2 mjf
2279 1.7.2.2 mjf tap->wt_flags = 0;
2280 1.7.2.2 mjf tap->wt_rate = rate;
2281 1.7.2.2 mjf tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2282 1.7.2.2 mjf tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2283 1.7.2.2 mjf
2284 1.7.2.2 mjf bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2285 1.7.2.2 mjf }
2286 1.7.2.2 mjf #endif
2287 1.7.2.2 mjf
2288 1.7.2.2 mjf m_copydata(m0, 0, m0->m_pkthdr.len,
2289 1.7.2.2 mjf data->buf + sizeof (struct zyd_tx_desc));
2290 1.7.2.2 mjf
2291 1.7.2.2 mjf DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
2292 1.7.2.2 mjf USBDEVNAME(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2293 1.7.2.2 mjf
2294 1.7.2.2 mjf m_freem(m0); /* mbuf no longer needed */
2295 1.7.2.2 mjf
2296 1.7.2.2 mjf usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2297 1.7.2.2 mjf data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2298 1.7.2.2 mjf ZYD_TX_TIMEOUT, zyd_txeof);
2299 1.7.2.2 mjf error = usbd_transfer(data->xfer);
2300 1.7.2.2 mjf if (error != USBD_IN_PROGRESS && error != 0) {
2301 1.7.2.2 mjf ifp->if_oerrors++;
2302 1.7.2.2 mjf return EIO;
2303 1.7.2.2 mjf }
2304 1.7.2.2 mjf sc->tx_queued++;
2305 1.7.2.2 mjf
2306 1.7.2.2 mjf return 0;
2307 1.7.2.2 mjf }
2308 1.7.2.2 mjf
2309 1.7.2.2 mjf Static void
2310 1.7.2.2 mjf zyd_start(struct ifnet *ifp)
2311 1.7.2.2 mjf {
2312 1.7.2.2 mjf struct zyd_softc *sc = ifp->if_softc;
2313 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2314 1.7.2.2 mjf struct ether_header *eh;
2315 1.7.2.2 mjf struct ieee80211_node *ni;
2316 1.7.2.2 mjf struct mbuf *m0;
2317 1.7.2.2 mjf
2318 1.7.2.2 mjf for (;;) {
2319 1.7.2.2 mjf IF_POLL(&ic->ic_mgtq, m0);
2320 1.7.2.2 mjf if (m0 != NULL) {
2321 1.7.2.2 mjf if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2322 1.7.2.2 mjf ifp->if_flags |= IFF_OACTIVE;
2323 1.7.2.2 mjf break;
2324 1.7.2.2 mjf }
2325 1.7.2.2 mjf IF_DEQUEUE(&ic->ic_mgtq, m0);
2326 1.7.2.2 mjf
2327 1.7.2.2 mjf ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2328 1.7.2.2 mjf m0->m_pkthdr.rcvif = NULL;
2329 1.7.2.2 mjf #if NBPFILTER > 0
2330 1.7.2.2 mjf if (ic->ic_rawbpf != NULL)
2331 1.7.2.2 mjf bpf_mtap(ic->ic_rawbpf, m0);
2332 1.7.2.2 mjf #endif
2333 1.7.2.2 mjf if (zyd_tx_mgt(sc, m0, ni) != 0)
2334 1.7.2.2 mjf break;
2335 1.7.2.2 mjf } else {
2336 1.7.2.2 mjf if (ic->ic_state != IEEE80211_S_RUN)
2337 1.7.2.2 mjf break;
2338 1.7.2.2 mjf IFQ_POLL(&ifp->if_snd, m0);
2339 1.7.2.2 mjf if (m0 == NULL)
2340 1.7.2.2 mjf break;
2341 1.7.2.2 mjf if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2342 1.7.2.2 mjf ifp->if_flags |= IFF_OACTIVE;
2343 1.7.2.2 mjf break;
2344 1.7.2.2 mjf }
2345 1.7.2.2 mjf IFQ_DEQUEUE(&ifp->if_snd, m0);
2346 1.7.2.2 mjf
2347 1.7.2.2 mjf if (m0->m_len < sizeof(struct ether_header) &&
2348 1.7.2.2 mjf !(m0 = m_pullup(m0, sizeof(struct ether_header))))
2349 1.7.2.2 mjf continue;
2350 1.7.2.2 mjf
2351 1.7.2.2 mjf eh = mtod(m0, struct ether_header *);
2352 1.7.2.2 mjf ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2353 1.7.2.2 mjf if (ni == NULL) {
2354 1.7.2.2 mjf m_freem(m0);
2355 1.7.2.2 mjf continue;
2356 1.7.2.2 mjf }
2357 1.7.2.2 mjf #if NBPFILTER > 0
2358 1.7.2.2 mjf if (ifp->if_bpf != NULL)
2359 1.7.2.2 mjf bpf_mtap(ifp->if_bpf, m0);
2360 1.7.2.2 mjf #endif
2361 1.7.2.2 mjf if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
2362 1.7.2.2 mjf ieee80211_free_node(ni);
2363 1.7.2.2 mjf ifp->if_oerrors++;
2364 1.7.2.2 mjf continue;
2365 1.7.2.2 mjf }
2366 1.7.2.2 mjf #if NBPFILTER > 0
2367 1.7.2.2 mjf if (ic->ic_rawbpf != NULL)
2368 1.7.2.2 mjf bpf_mtap(ic->ic_rawbpf, m0);
2369 1.7.2.2 mjf #endif
2370 1.7.2.2 mjf if (zyd_tx_data(sc, m0, ni) != 0) {
2371 1.7.2.2 mjf ieee80211_free_node(ni);
2372 1.7.2.2 mjf ifp->if_oerrors++;
2373 1.7.2.2 mjf break;
2374 1.7.2.2 mjf }
2375 1.7.2.2 mjf }
2376 1.7.2.2 mjf
2377 1.7.2.2 mjf sc->tx_timer = 5;
2378 1.7.2.2 mjf ifp->if_timer = 1;
2379 1.7.2.2 mjf }
2380 1.7.2.2 mjf }
2381 1.7.2.2 mjf
2382 1.7.2.2 mjf Static void
2383 1.7.2.2 mjf zyd_watchdog(struct ifnet *ifp)
2384 1.7.2.2 mjf {
2385 1.7.2.2 mjf struct zyd_softc *sc = ifp->if_softc;
2386 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2387 1.7.2.2 mjf
2388 1.7.2.2 mjf ifp->if_timer = 0;
2389 1.7.2.2 mjf
2390 1.7.2.2 mjf if (sc->tx_timer > 0) {
2391 1.7.2.2 mjf if (--sc->tx_timer == 0) {
2392 1.7.2.2 mjf printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
2393 1.7.2.2 mjf /* zyd_init(ifp); XXX needs a process context ? */
2394 1.7.2.2 mjf ifp->if_oerrors++;
2395 1.7.2.2 mjf return;
2396 1.7.2.2 mjf }
2397 1.7.2.2 mjf ifp->if_timer = 1;
2398 1.7.2.2 mjf }
2399 1.7.2.2 mjf
2400 1.7.2.2 mjf ieee80211_watchdog(ic);
2401 1.7.2.2 mjf }
2402 1.7.2.2 mjf
2403 1.7.2.2 mjf Static int
2404 1.7.2.2 mjf zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2405 1.7.2.2 mjf {
2406 1.7.2.2 mjf struct zyd_softc *sc = ifp->if_softc;
2407 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2408 1.7.2.2 mjf int s, error = 0;
2409 1.7.2.2 mjf
2410 1.7.2.2 mjf s = splnet();
2411 1.7.2.2 mjf
2412 1.7.2.2 mjf switch (cmd) {
2413 1.7.2.2 mjf case SIOCSIFFLAGS:
2414 1.7.2.2 mjf if (ifp->if_flags & IFF_UP) {
2415 1.7.2.2 mjf if (!(ifp->if_flags & IFF_RUNNING))
2416 1.7.2.2 mjf zyd_init(ifp);
2417 1.7.2.2 mjf } else {
2418 1.7.2.2 mjf if (ifp->if_flags & IFF_RUNNING)
2419 1.7.2.2 mjf zyd_stop(ifp, 1);
2420 1.7.2.2 mjf }
2421 1.7.2.2 mjf break;
2422 1.7.2.2 mjf
2423 1.7.2.2 mjf default:
2424 1.7.2.2 mjf if (!sc->attached)
2425 1.7.2.2 mjf error = ENOTTY;
2426 1.7.2.2 mjf else
2427 1.7.2.2 mjf error = ieee80211_ioctl(ic, cmd, data);
2428 1.7.2.2 mjf }
2429 1.7.2.2 mjf
2430 1.7.2.2 mjf if (error == ENETRESET) {
2431 1.7.2.2 mjf if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
2432 1.7.2.2 mjf (IFF_RUNNING | IFF_UP))
2433 1.7.2.2 mjf zyd_init(ifp);
2434 1.7.2.2 mjf error = 0;
2435 1.7.2.2 mjf }
2436 1.7.2.2 mjf
2437 1.7.2.2 mjf splx(s);
2438 1.7.2.2 mjf
2439 1.7.2.2 mjf return error;
2440 1.7.2.2 mjf }
2441 1.7.2.2 mjf
2442 1.7.2.2 mjf Static int
2443 1.7.2.2 mjf zyd_init(struct ifnet *ifp)
2444 1.7.2.2 mjf {
2445 1.7.2.2 mjf struct zyd_softc *sc = ifp->if_softc;
2446 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2447 1.7.2.2 mjf int i, error;
2448 1.7.2.2 mjf
2449 1.7.2.2 mjf if ((sc->sc_flags & ZD1211_FWLOADED) == 0)
2450 1.7.2.2 mjf if ((error = zyd_attachhook(sc)) != 0)
2451 1.7.2.2 mjf return error;
2452 1.7.2.2 mjf
2453 1.7.2.2 mjf zyd_stop(ifp, 0);
2454 1.7.2.2 mjf
2455 1.7.2.2 mjf IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2456 1.7.2.2 mjf DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2457 1.7.2.2 mjf error = zyd_set_macaddr(sc, ic->ic_myaddr);
2458 1.7.2.2 mjf if (error != 0)
2459 1.7.2.2 mjf return error;
2460 1.7.2.2 mjf
2461 1.7.2.2 mjf /* we'll do software WEP decryption for now */
2462 1.7.2.2 mjf DPRINTF(("setting encryption type\n"));
2463 1.7.2.2 mjf error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2464 1.7.2.2 mjf if (error != 0)
2465 1.7.2.2 mjf return error;
2466 1.7.2.2 mjf
2467 1.7.2.2 mjf /* promiscuous mode */
2468 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_SNIFFER,
2469 1.7.2.2 mjf (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
2470 1.7.2.2 mjf
2471 1.7.2.2 mjf (void)zyd_set_rxfilter(sc);
2472 1.7.2.2 mjf
2473 1.7.2.2 mjf /* switch radio transmitter ON */
2474 1.7.2.2 mjf (void)zyd_switch_radio(sc, 1);
2475 1.7.2.2 mjf
2476 1.7.2.2 mjf /* set basic rates */
2477 1.7.2.2 mjf if (ic->ic_curmode == IEEE80211_MODE_11B)
2478 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
2479 1.7.2.2 mjf else if (ic->ic_curmode == IEEE80211_MODE_11A)
2480 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
2481 1.7.2.2 mjf else /* assumes 802.11b/g */
2482 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
2483 1.7.2.2 mjf
2484 1.7.2.2 mjf /* set mandatory rates */
2485 1.7.2.2 mjf if (ic->ic_curmode == IEEE80211_MODE_11B)
2486 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
2487 1.7.2.2 mjf else if (ic->ic_curmode == IEEE80211_MODE_11A)
2488 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
2489 1.7.2.2 mjf else /* assumes 802.11b/g */
2490 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
2491 1.7.2.2 mjf
2492 1.7.2.2 mjf /* set default BSS channel */
2493 1.7.2.2 mjf ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2494 1.7.2.2 mjf zyd_set_chan(sc, ic->ic_bss->ni_chan);
2495 1.7.2.2 mjf
2496 1.7.2.2 mjf /* enable interrupts */
2497 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2498 1.7.2.2 mjf
2499 1.7.2.2 mjf /*
2500 1.7.2.2 mjf * Allocate Tx and Rx xfer queues.
2501 1.7.2.2 mjf */
2502 1.7.2.2 mjf if ((error = zyd_alloc_tx_list(sc)) != 0) {
2503 1.7.2.2 mjf printf("%s: could not allocate Tx list\n",
2504 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
2505 1.7.2.2 mjf goto fail;
2506 1.7.2.2 mjf }
2507 1.7.2.2 mjf if ((error = zyd_alloc_rx_list(sc)) != 0) {
2508 1.7.2.2 mjf printf("%s: could not allocate Rx list\n",
2509 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
2510 1.7.2.2 mjf goto fail;
2511 1.7.2.2 mjf }
2512 1.7.2.2 mjf
2513 1.7.2.2 mjf /*
2514 1.7.2.2 mjf * Start up the receive pipe.
2515 1.7.2.2 mjf */
2516 1.7.2.2 mjf for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2517 1.7.2.2 mjf struct zyd_rx_data *data = &sc->rx_data[i];
2518 1.7.2.2 mjf
2519 1.7.2.2 mjf usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data,
2520 1.7.2.2 mjf NULL, ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2521 1.7.2.2 mjf USBD_NO_TIMEOUT, zyd_rxeof);
2522 1.7.2.2 mjf error = usbd_transfer(data->xfer);
2523 1.7.2.2 mjf if (error != USBD_IN_PROGRESS && error != 0) {
2524 1.7.2.2 mjf printf("%s: could not queue Rx transfer\n",
2525 1.7.2.2 mjf USBDEVNAME(sc->sc_dev));
2526 1.7.2.2 mjf goto fail;
2527 1.7.2.2 mjf }
2528 1.7.2.2 mjf }
2529 1.7.2.2 mjf
2530 1.7.2.2 mjf ifp->if_flags &= ~IFF_OACTIVE;
2531 1.7.2.2 mjf ifp->if_flags |= IFF_RUNNING;
2532 1.7.2.2 mjf
2533 1.7.2.2 mjf if (ic->ic_opmode == IEEE80211_M_MONITOR)
2534 1.7.2.2 mjf ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2535 1.7.2.2 mjf else
2536 1.7.2.2 mjf ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2537 1.7.2.2 mjf
2538 1.7.2.2 mjf return 0;
2539 1.7.2.2 mjf
2540 1.7.2.2 mjf fail: zyd_stop(ifp, 1);
2541 1.7.2.2 mjf return error;
2542 1.7.2.2 mjf }
2543 1.7.2.2 mjf
2544 1.7.2.2 mjf Static void
2545 1.7.2.2 mjf zyd_stop(struct ifnet *ifp, int disable)
2546 1.7.2.2 mjf {
2547 1.7.2.2 mjf struct zyd_softc *sc = ifp->if_softc;
2548 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2549 1.7.2.2 mjf
2550 1.7.2.2 mjf ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2551 1.7.2.2 mjf
2552 1.7.2.2 mjf sc->tx_timer = 0;
2553 1.7.2.2 mjf ifp->if_timer = 0;
2554 1.7.2.2 mjf ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2555 1.7.2.2 mjf
2556 1.7.2.2 mjf /* switch radio transmitter OFF */
2557 1.7.2.2 mjf (void)zyd_switch_radio(sc, 0);
2558 1.7.2.2 mjf
2559 1.7.2.2 mjf /* disable Rx */
2560 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2561 1.7.2.2 mjf
2562 1.7.2.2 mjf /* disable interrupts */
2563 1.7.2.2 mjf (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2564 1.7.2.2 mjf
2565 1.7.2.2 mjf usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2566 1.7.2.2 mjf usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2567 1.7.2.2 mjf
2568 1.7.2.2 mjf zyd_free_rx_list(sc);
2569 1.7.2.2 mjf zyd_free_tx_list(sc);
2570 1.7.2.2 mjf }
2571 1.7.2.2 mjf
2572 1.7.2.2 mjf Static int
2573 1.7.2.2 mjf zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2574 1.7.2.2 mjf {
2575 1.7.2.2 mjf usb_device_request_t req;
2576 1.7.2.2 mjf uint16_t addr;
2577 1.7.2.2 mjf uint8_t stat;
2578 1.7.2.2 mjf
2579 1.7.2.2 mjf DPRINTF(("firmware size=%zu\n", size));
2580 1.7.2.2 mjf
2581 1.7.2.2 mjf req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2582 1.7.2.2 mjf req.bRequest = ZYD_DOWNLOADREQ;
2583 1.7.2.2 mjf USETW(req.wIndex, 0);
2584 1.7.2.2 mjf
2585 1.7.2.2 mjf addr = ZYD_FIRMWARE_START_ADDR;
2586 1.7.2.2 mjf while (size > 0) {
2587 1.7.2.2 mjf #if 0
2588 1.7.2.2 mjf const int mlen = min(size, 4096);
2589 1.7.2.2 mjf #else
2590 1.7.2.2 mjf /*
2591 1.7.2.2 mjf * XXXX: When the transfer size is 4096 bytes, it is not
2592 1.7.2.2 mjf * likely to be able to transfer it.
2593 1.7.2.2 mjf * The cause is port or machine or chip?
2594 1.7.2.2 mjf */
2595 1.7.2.2 mjf const int mlen = min(size, 64);
2596 1.7.2.2 mjf #endif
2597 1.7.2.2 mjf
2598 1.7.2.2 mjf DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
2599 1.7.2.2 mjf addr));
2600 1.7.2.2 mjf
2601 1.7.2.2 mjf USETW(req.wValue, addr);
2602 1.7.2.2 mjf USETW(req.wLength, mlen);
2603 1.7.2.2 mjf if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
2604 1.7.2.2 mjf return EIO;
2605 1.7.2.2 mjf
2606 1.7.2.2 mjf addr += mlen / 2;
2607 1.7.2.2 mjf fw += mlen;
2608 1.7.2.2 mjf size -= mlen;
2609 1.7.2.2 mjf }
2610 1.7.2.2 mjf
2611 1.7.2.2 mjf /* check whether the upload succeeded */
2612 1.7.2.2 mjf req.bmRequestType = UT_READ_VENDOR_DEVICE;
2613 1.7.2.2 mjf req.bRequest = ZYD_DOWNLOADSTS;
2614 1.7.2.2 mjf USETW(req.wValue, 0);
2615 1.7.2.2 mjf USETW(req.wIndex, 0);
2616 1.7.2.2 mjf USETW(req.wLength, sizeof stat);
2617 1.7.2.2 mjf if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
2618 1.7.2.2 mjf return EIO;
2619 1.7.2.2 mjf
2620 1.7.2.2 mjf return (stat & 0x80) ? EIO : 0;
2621 1.7.2.2 mjf }
2622 1.7.2.2 mjf
2623 1.7.2.2 mjf Static void
2624 1.7.2.2 mjf zyd_iter_func(void *arg, struct ieee80211_node *ni)
2625 1.7.2.2 mjf {
2626 1.7.2.2 mjf struct zyd_softc *sc = arg;
2627 1.7.2.2 mjf struct zyd_node *zn = (struct zyd_node *)ni;
2628 1.7.2.2 mjf
2629 1.7.2.2 mjf ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2630 1.7.2.2 mjf }
2631 1.7.2.2 mjf
2632 1.7.2.2 mjf Static void
2633 1.7.2.2 mjf zyd_amrr_timeout(void *arg)
2634 1.7.2.2 mjf {
2635 1.7.2.2 mjf struct zyd_softc *sc = arg;
2636 1.7.2.2 mjf struct ieee80211com *ic = &sc->sc_ic;
2637 1.7.2.2 mjf int s;
2638 1.7.2.2 mjf
2639 1.7.2.2 mjf s = splnet();
2640 1.7.2.2 mjf if (ic->ic_opmode == IEEE80211_M_STA)
2641 1.7.2.2 mjf zyd_iter_func(sc, ic->ic_bss);
2642 1.7.2.2 mjf else
2643 1.7.2.2 mjf ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
2644 1.7.2.2 mjf splx(s);
2645 1.7.2.2 mjf
2646 1.7.2.2 mjf usb_callout(sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
2647 1.7.2.2 mjf }
2648 1.7.2.2 mjf
2649 1.7.2.2 mjf Static void
2650 1.7.2.2 mjf zyd_newassoc(struct ieee80211_node *ni, int isnew)
2651 1.7.2.2 mjf {
2652 1.7.2.2 mjf struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
2653 1.7.2.2 mjf int i;
2654 1.7.2.2 mjf
2655 1.7.2.2 mjf ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
2656 1.7.2.2 mjf
2657 1.7.2.2 mjf /* set rate to some reasonable initial value */
2658 1.7.2.2 mjf for (i = ni->ni_rates.rs_nrates - 1;
2659 1.7.2.2 mjf i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2660 1.7.2.2 mjf i--);
2661 1.7.2.2 mjf ni->ni_txrate = i;
2662 1.7.2.2 mjf }
2663 1.7.2.2 mjf
2664 1.7.2.2 mjf int
2665 1.7.2.2 mjf zyd_activate(device_ptr_t self, enum devact act)
2666 1.7.2.2 mjf {
2667 1.7.2.2 mjf struct zyd_softc *sc = (struct zyd_softc *)self;
2668 1.7.2.2 mjf
2669 1.7.2.2 mjf switch (act) {
2670 1.7.2.2 mjf case DVACT_ACTIVATE:
2671 1.7.2.2 mjf break;
2672 1.7.2.2 mjf
2673 1.7.2.2 mjf case DVACT_DEACTIVATE:
2674 1.7.2.2 mjf if_deactivate(&sc->sc_if);
2675 1.7.2.2 mjf break;
2676 1.7.2.2 mjf }
2677 1.7.2.2 mjf return 0;
2678 1.7.2.2 mjf }
2679