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