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