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