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