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