if_zyd.c revision 184659
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: head/sys/dev/usb2/wlan/if_zyd2.c 184659 2008-11-04 20:57:39Z rink $ */ 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#include <sys/cdefs.h> 23__FBSDID("$FreeBSD: head/sys/dev/usb2/wlan/if_zyd2.c 184659 2008-11-04 20:57:39Z rink $"); 24 25/* 26 * ZyDAS ZD1211/ZD1211B USB WLAN driver 27 * 28 * NOTE: all function names beginning like "zyd_cfg_" can only 29 * be called from within the config thread function ! 30 */ 31 32#include <dev/usb2/include/usb2_devid.h> 33#include <dev/usb2/include/usb2_standard.h> 34#include <dev/usb2/include/usb2_mfunc.h> 35#include <dev/usb2/include/usb2_error.h> 36 37#define usb2_config_td_cc zyd_config_copy 38#define usb2_config_td_softc zyd_softc 39 40#define USB_DEBUG_VAR zyd_debug 41 42#include <dev/usb2/core/usb2_core.h> 43#include <dev/usb2/core/usb2_lookup.h> 44#include <dev/usb2/core/usb2_process.h> 45#include <dev/usb2/core/usb2_config_td.h> 46#include <dev/usb2/core/usb2_debug.h> 47#include <dev/usb2/core/usb2_request.h> 48#include <dev/usb2/core/usb2_busdma.h> 49#include <dev/usb2/core/usb2_util.h> 50 51#include <dev/usb2/wlan/usb2_wlan.h> 52#include <dev/usb2/wlan/if_zyd2_reg.h> 53#include <dev/usb2/wlan/if_zyd2_fw.h> 54 55#if USB_DEBUG 56static int zyd_debug = 0; 57 58SYSCTL_NODE(_hw_usb2, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd"); 59SYSCTL_INT(_hw_usb2_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0, 60 "zyd debug level"); 61#endif 62 63#undef INDEXES 64#define INDEXES(a) (sizeof(a) / sizeof((a)[0])) 65 66static device_probe_t zyd_probe; 67static device_attach_t zyd_attach; 68static device_detach_t zyd_detach; 69 70static usb2_callback_t zyd_intr_read_clear_stall_callback; 71static usb2_callback_t zyd_intr_read_callback; 72static usb2_callback_t zyd_intr_write_clear_stall_callback; 73static usb2_callback_t zyd_intr_write_callback; 74static usb2_callback_t zyd_bulk_read_clear_stall_callback; 75static usb2_callback_t zyd_bulk_read_callback; 76static usb2_callback_t zyd_bulk_write_clear_stall_callback; 77static usb2_callback_t zyd_bulk_write_callback; 78 79static usb2_config_td_command_t zyd_cfg_first_time_setup; 80static usb2_config_td_command_t zyd_cfg_update_promisc; 81static usb2_config_td_command_t zyd_cfg_set_chan; 82static usb2_config_td_command_t zyd_cfg_pre_init; 83static usb2_config_td_command_t zyd_cfg_init; 84static usb2_config_td_command_t zyd_cfg_pre_stop; 85static usb2_config_td_command_t zyd_cfg_stop; 86static usb2_config_td_command_t zyd_config_copy; 87static usb2_config_td_command_t zyd_cfg_scan_start; 88static usb2_config_td_command_t zyd_cfg_scan_end; 89static usb2_config_td_command_t zyd_cfg_set_rxfilter; 90static usb2_config_td_command_t zyd_cfg_amrr_timeout; 91 92static uint8_t zyd_plcp2ieee(uint8_t signal, uint8_t isofdm); 93static void zyd_cfg_usbrequest(struct zyd_softc *sc, struct usb2_device_request *req, uint8_t *data); 94static void zyd_cfg_usb2_intr_read(struct zyd_softc *sc, void *data, uint32_t size); 95static void zyd_cfg_usb2_intr_write(struct zyd_softc *sc, const void *data, uint16_t code, uint32_t size); 96static void zyd_cfg_read16(struct zyd_softc *sc, uint16_t addr, uint16_t *value); 97static void zyd_cfg_read32(struct zyd_softc *sc, uint16_t addr, uint32_t *value); 98static void zyd_cfg_write16(struct zyd_softc *sc, uint16_t addr, uint16_t value); 99static void zyd_cfg_write32(struct zyd_softc *sc, uint16_t addr, uint32_t value); 100static void zyd_cfg_rfwrite(struct zyd_softc *sc, uint32_t value); 101static uint8_t zyd_cfg_uploadfirmware(struct zyd_softc *sc, const uint8_t *fw_ptr, uint32_t fw_len); 102static void zyd_cfg_lock_phy(struct zyd_softc *sc); 103static void zyd_cfg_unlock_phy(struct zyd_softc *sc); 104static void zyd_cfg_set_beacon_interval(struct zyd_softc *sc, uint32_t interval); 105static const char *zyd_rf_name(uint8_t type); 106static void zyd_cfg_rf_rfmd_init(struct zyd_softc *sc, struct zyd_rf *rf); 107static void zyd_cfg_rf_rfmd_switch_radio(struct zyd_softc *sc, uint8_t onoff); 108static void zyd_cfg_rf_rfmd_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, uint8_t channel); 109static void zyd_cfg_rf_al2230_switch_radio(struct zyd_softc *sc, uint8_t onoff); 110static void zyd_cfg_rf_al2230_init(struct zyd_softc *sc, struct zyd_rf *rf); 111static void zyd_cfg_rf_al2230_init_b(struct zyd_softc *sc, struct zyd_rf *rf); 112static void zyd_cfg_rf_al2230_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, uint8_t channel); 113static uint8_t zyd_cfg_rf_init_hw(struct zyd_softc *sc, struct zyd_rf *rf); 114static uint8_t zyd_cfg_hw_init(struct zyd_softc *sc); 115static void zyd_cfg_set_mac_addr(struct zyd_softc *sc, const uint8_t *addr); 116static void zyd_cfg_switch_radio(struct zyd_softc *sc, uint8_t onoff); 117static void zyd_cfg_set_bssid(struct zyd_softc *sc, uint8_t *addr); 118static void zyd_start_cb(struct ifnet *ifp); 119static void zyd_init_cb(void *arg); 120static int zyd_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data); 121static void zyd_watchdog(void *arg); 122static void zyd_end_of_commands(struct zyd_softc *sc); 123static void zyd_newassoc_cb(struct ieee80211_node *ni, int isnew); 124static void zyd_scan_start_cb(struct ieee80211com *ic); 125static void zyd_scan_end_cb(struct ieee80211com *ic); 126static void zyd_set_channel_cb(struct ieee80211com *ic); 127static void zyd_cfg_set_led(struct zyd_softc *sc, uint32_t which, uint8_t on); 128static struct ieee80211vap *zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, int opmode, int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t mac[IEEE80211_ADDR_LEN]); 129static void zyd_vap_delete(struct ieee80211vap *); 130static struct ieee80211_node *zyd_node_alloc_cb(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]); 131static void zyd_cfg_set_run(struct zyd_softc *sc, struct usb2_config_td_cc *cc); 132static void zyd_fill_write_queue(struct zyd_softc *sc); 133static void zyd_tx_clean_queue(struct zyd_softc *sc); 134static void zyd_tx_freem(struct mbuf *m); 135static void zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m, struct ieee80211_node *ni); 136static struct ieee80211vap *zyd_get_vap(struct zyd_softc *sc); 137static void zyd_tx_data(struct zyd_softc *sc, struct mbuf *m, struct ieee80211_node *ni); 138static int zyd_raw_xmit_cb(struct ieee80211_node *ni, struct mbuf *m, const struct ieee80211_bpf_params *params); 139static void zyd_setup_desc_and_tx(struct zyd_softc *sc, struct mbuf *m, uint16_t rate); 140static int zyd_newstate_cb(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg); 141static void zyd_cfg_amrr_start(struct zyd_softc *sc); 142static void zyd_update_mcast_cb(struct ifnet *ifp); 143static void zyd_update_promisc_cb(struct ifnet *ifp); 144 145static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY; 146static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB; 147 148/* various supported device vendors/products */ 149#define ZYD_ZD1211 0 150#define ZYD_ZD1211B 1 151 152static const struct usb2_device_id zyd_devs[] = { 153 /* ZYD_ZD1211 */ 154 {USB_VPI(USB_VENDOR_3COM2, USB_PRODUCT_3COM2_3CRUSB10075, ZYD_ZD1211)}, 155 {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WL54, ZYD_ZD1211)}, 156 {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL159G, ZYD_ZD1211)}, 157 {USB_VPI(USB_VENDOR_CYBERTAN, USB_PRODUCT_CYBERTAN_TG54USB, ZYD_ZD1211)}, 158 {USB_VPI(USB_VENDOR_DRAYTEK, USB_PRODUCT_DRAYTEK_VIGOR550, ZYD_ZD1211)}, 159 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GD, ZYD_ZD1211)}, 160 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54GZL, ZYD_ZD1211)}, 161 {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54GZ, ZYD_ZD1211)}, 162 {USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GWUS54MINI, ZYD_ZD1211)}, 163 {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG760A, ZYD_ZD1211)}, 164 {USB_VPI(USB_VENDOR_SENAO, USB_PRODUCT_SENAO_NUB8301, ZYD_ZD1211)}, 165 {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113, ZYD_ZD1211)}, 166 {USB_VPI(USB_VENDOR_SWEEX, USB_PRODUCT_SWEEX_ZD1211, ZYD_ZD1211)}, 167 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_QUICKWLAN, ZYD_ZD1211)}, 168 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_1, ZYD_ZD1211)}, 169 {USB_VPI(USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZD1211_2, ZYD_ZD1211)}, 170 {USB_VPI(USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_G240, ZYD_ZD1211)}, 171 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_ALL0298V2, ZYD_ZD1211)}, 172 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB_A, ZYD_ZD1211)}, 173 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UB, ZYD_ZD1211)}, 174 {USB_VPI(USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR055G, ZYD_ZD1211)}, 175 {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211, ZYD_ZD1211)}, 176 {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211, ZYD_ZD1211)}, 177 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_AG225H, ZYD_ZD1211)}, 178 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_ZYAIRG220, ZYD_ZD1211)}, 179 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G200V2, ZYD_ZD1211)}, 180 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G202, ZYD_ZD1211)}, 181 /* ZYD_ZD1211B */ 182 {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SMCWUSBG, ZYD_ZD1211B)}, 183 {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_ZD1211B, ZYD_ZD1211B)}, 184 {USB_VPI(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_A9T_WIFI, ZYD_ZD1211B)}, 185 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050_V4000, ZYD_ZD1211B)}, 186 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_ZD1211B, ZYD_ZD1211B)}, 187 {USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSBF54G, ZYD_ZD1211B)}, 188 {USB_VPI(USB_VENDOR_FIBERLINE, USB_PRODUCT_FIBERLINE_WL430U, ZYD_ZD1211B)}, 189 {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54L, ZYD_ZD1211B)}, 190 {USB_VPI(USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_SNU5600, ZYD_ZD1211B)}, 191 {USB_VPI(USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GW_US54GXS, ZYD_ZD1211B)}, 192 {USB_VPI(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_XG76NA, ZYD_ZD1211B)}, 193 {USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_ZD1211B, ZYD_ZD1211B)}, 194 {USB_VPI(USB_VENDOR_UMEDIA, USB_PRODUCT_UMEDIA_TEW429UBC1, ZYD_ZD1211B)}, 195 {USB_VPI(USB_VENDOR_USR, USB_PRODUCT_USR_USR5423, ZYD_ZD1211B)}, 196 {USB_VPI(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_ZD1211B, ZYD_ZD1211B)}, 197 {USB_VPI(USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_ZD1211B, ZYD_ZD1211B)}, 198 {USB_VPI(USB_VENDOR_ZYDAS, USB_PRODUCT_ZYDAS_ZD1211B, ZYD_ZD1211B)}, 199 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_M202, ZYD_ZD1211B)}, 200 {USB_VPI(USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_G220V2, ZYD_ZD1211B)}, 201}; 202 203static const struct usb2_config zyd_config[ZYD_N_TRANSFER] = { 204 [ZYD_TR_BULK_DT_WR] = { 205 .type = UE_BULK, 206 .endpoint = UE_ADDR_ANY, 207 .direction = UE_DIR_OUT, 208 .mh.bufsize = ZYD_MAX_TXBUFSZ, 209 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 210 .mh.callback = &zyd_bulk_write_callback, 211 .ep_index = 0, 212 .mh.timeout = 10000, /* 10 seconds */ 213 }, 214 215 [ZYD_TR_BULK_DT_RD] = { 216 .type = UE_BULK, 217 .endpoint = UE_ADDR_ANY, 218 .direction = UE_DIR_IN, 219 .mh.bufsize = ZYX_MAX_RXBUFSZ, 220 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 221 .mh.callback = &zyd_bulk_read_callback, 222 .ep_index = 0, 223 }, 224 225 [ZYD_TR_BULK_CS_WR] = { 226 .type = UE_CONTROL, 227 .endpoint = 0x00, /* Control pipe */ 228 .direction = UE_DIR_ANY, 229 .mh.bufsize = sizeof(struct usb2_device_request), 230 .mh.flags = {}, 231 .mh.callback = &zyd_bulk_write_clear_stall_callback, 232 .mh.timeout = 1000, /* 1 second */ 233 .mh.interval = 50, /* 50ms */ 234 }, 235 236 [ZYD_TR_BULK_CS_RD] = { 237 .type = UE_CONTROL, 238 .endpoint = 0x00, /* Control pipe */ 239 .direction = UE_DIR_ANY, 240 .mh.bufsize = sizeof(struct usb2_device_request), 241 .mh.flags = {}, 242 .mh.callback = &zyd_bulk_read_clear_stall_callback, 243 .mh.timeout = 1000, /* 1 second */ 244 .mh.interval = 50, /* 50ms */ 245 }, 246 247 [ZYD_TR_INTR_DT_WR] = { 248 .type = UE_BULK_INTR, 249 .endpoint = UE_ADDR_ANY, 250 .direction = UE_DIR_OUT, 251 .mh.bufsize = sizeof(struct zyd_cmd), 252 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 253 .mh.callback = &zyd_intr_write_callback, 254 .mh.timeout = 1000, /* 1 second */ 255 .ep_index = 1, 256 }, 257 258 [ZYD_TR_INTR_DT_RD] = { 259 .type = UE_BULK_INTR, 260 .endpoint = UE_ADDR_ANY, 261 .direction = UE_DIR_IN, 262 .mh.bufsize = sizeof(struct zyd_cmd), 263 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 264 .mh.callback = &zyd_intr_read_callback, 265 .ep_index = 1, 266 }, 267 268 [ZYD_TR_INTR_CS_WR] = { 269 .type = UE_CONTROL, 270 .endpoint = 0x00, /* Control pipe */ 271 .direction = UE_DIR_ANY, 272 .mh.bufsize = sizeof(struct usb2_device_request), 273 .mh.flags = {}, 274 .mh.callback = &zyd_intr_write_clear_stall_callback, 275 .mh.timeout = 1000, /* 1 second */ 276 .mh.interval = 50, /* 50ms */ 277 }, 278 279 [ZYD_TR_INTR_CS_RD] = { 280 .type = UE_CONTROL, 281 .endpoint = 0x00, /* Control pipe */ 282 .direction = UE_DIR_ANY, 283 .mh.bufsize = sizeof(struct usb2_device_request), 284 .mh.flags = {}, 285 .mh.callback = &zyd_intr_read_clear_stall_callback, 286 .mh.timeout = 1000, /* 1 second */ 287 .mh.interval = 50, /* 50ms */ 288 }, 289}; 290 291static devclass_t zyd_devclass; 292 293static device_method_t zyd_methods[] = { 294 DEVMETHOD(device_probe, zyd_probe), 295 DEVMETHOD(device_attach, zyd_attach), 296 DEVMETHOD(device_detach, zyd_detach), 297 {0, 0} 298}; 299 300static driver_t zyd_driver = { 301 .name = "zyd", 302 .methods = zyd_methods, 303 .size = sizeof(struct zyd_softc), 304}; 305 306DRIVER_MODULE(zyd, ushub, zyd_driver, zyd_devclass, NULL, 0); 307MODULE_DEPEND(zyd, usb2_wlan, 1, 1, 1); 308MODULE_DEPEND(zyd, usb2_core, 1, 1, 1); 309MODULE_DEPEND(zyd, wlan, 1, 1, 1); 310MODULE_DEPEND(zyd, wlan_amrr, 1, 1, 1); 311 312static uint8_t 313zyd_plcp2ieee(uint8_t signal, uint8_t isofdm) 314{ 315 if (isofdm) { 316 static const uint8_t ofdmrates[16] = 317 {0, 0, 0, 0, 0, 0, 0, 96, 48, 24, 12, 108, 72, 36, 18}; 318 319 return ofdmrates[signal & 0xf]; 320 } else { 321 static const uint8_t cckrates[16] = 322 {0, 0, 0, 0, 4, 0, 0, 11, 0, 0, 2, 0, 0, 0, 22, 0}; 323 324 return cckrates[signal & 0xf]; 325 } 326} 327 328/* 329 * USB request basic wrapper 330 */ 331static void 332zyd_cfg_usbrequest(struct zyd_softc *sc, struct usb2_device_request *req, uint8_t *data) 333{ 334 usb2_error_t err; 335 uint16_t length; 336 337 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 338 goto error; 339 } 340 err = usb2_do_request_flags 341 (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000); 342 343 if (err) { 344 345 DPRINTFN(0, "%s: device request failed, err=%s " 346 "(ignored)\n", sc->sc_name, usb2_errstr(err)); 347 348error: 349 length = UGETW(req->wLength); 350 351 if ((req->bmRequestType & UT_READ) && length) { 352 bzero(data, length); 353 } 354 } 355 return; 356} 357 358static void 359zyd_intr_read_clear_stall_callback(struct usb2_xfer *xfer) 360{ 361 struct zyd_softc *sc = xfer->priv_sc; 362 struct usb2_xfer *xfer_other = sc->sc_xfer[ZYD_TR_INTR_DT_RD]; 363 364 if (usb2_clear_stall_callback(xfer, xfer_other)) { 365 DPRINTF("stall cleared\n"); 366 sc->sc_flags &= ~ZYD_FLAG_INTR_READ_STALL; 367 usb2_transfer_start(xfer_other); 368 } 369 return; 370} 371 372/* 373 * Callback handler for interrupt transfer 374 */ 375static void 376zyd_intr_read_callback(struct usb2_xfer *xfer) 377{ 378 struct zyd_softc *sc = xfer->priv_sc; 379 struct zyd_cmd *cmd = &sc->sc_intr_ibuf; 380 uint32_t actlen; 381 382 switch (USB_GET_STATE(xfer)) { 383 case USB_ST_TRANSFERRED: 384 385 actlen = xfer->actlen; 386 387 DPRINTFN(3, "length=%d\n", actlen); 388 389 if (actlen > sizeof(sc->sc_intr_ibuf)) { 390 actlen = sizeof(sc->sc_intr_ibuf); 391 } 392 usb2_copy_out(xfer->frbuffers, 0, 393 &sc->sc_intr_ibuf, actlen); 394 395 switch (le16toh(cmd->code)) { 396 case ZYD_NOTIF_RETRYSTATUS: 397 goto handle_notif_retrystatus; 398 case ZYD_NOTIF_IORD: 399 goto handle_notif_iord; 400 default: 401 DPRINTFN(2, "unknown indication: 0x%04x\n", 402 le16toh(cmd->code)); 403 } 404 405 /* fallthrough */ 406 407 case USB_ST_SETUP: 408tr_setup: 409 if (sc->sc_flags & ZYD_FLAG_INTR_READ_STALL) { 410 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_CS_RD]); 411 break; 412 } 413 xfer->frlengths[0] = xfer->max_data_length; 414 usb2_start_hardware(xfer); 415 break; 416 417 default: /* Error */ 418 DPRINTFN(3, "error = %s\n", 419 usb2_errstr(xfer->error)); 420 421 if (xfer->error != USB_ERR_CANCELLED) { 422 /* try to clear stall first */ 423 sc->sc_flags |= ZYD_FLAG_INTR_READ_STALL; 424 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_CS_RD]); 425 } 426 break; 427 } 428 return; 429 430handle_notif_retrystatus:{ 431 432 struct zyd_notif_retry *retry = (void *)(cmd->data); 433 struct ifnet *ifp = sc->sc_ifp; 434 struct ieee80211vap *vap; 435 struct ieee80211_node *ni; 436 437 DPRINTF("retry intr: rate=0x%x " 438 "addr=%02x:%02x:%02x:%02x:%02x:%02x count=%d (0x%x)\n", 439 le16toh(retry->rate), retry->macaddr[0], retry->macaddr[1], 440 retry->macaddr[2], retry->macaddr[3], retry->macaddr[4], 441 retry->macaddr[5], le16toh(retry->count) & 0xff, 442 le16toh(retry->count)); 443 444 vap = zyd_get_vap(sc); 445 if ((vap != NULL) && (sc->sc_amrr_timer)) { 446 /* 447 * Find the node to which the packet was sent 448 * and update its retry statistics. In BSS 449 * mode, this node is the AP we're associated 450 * to so no lookup is actually needed. 451 */ 452 ni = ieee80211_find_txnode(vap, retry->macaddr); 453 if (ni != NULL) { 454 ieee80211_amrr_tx_complete(&ZYD_NODE(ni)->amn, 455 IEEE80211_AMRR_FAILURE, 1); 456 ieee80211_free_node(ni); 457 } 458 } 459 if (retry->count & htole16(0x100)) { 460 ifp->if_oerrors++; /* too many retries */ 461 } 462 goto tr_setup; 463 } 464 465handle_notif_iord: 466 467 if (*(uint16_t *)cmd->data == htole16(ZYD_CR_INTERRUPT)) { 468 goto tr_setup; /* HMAC interrupt */ 469 } 470 if (actlen < 4) { 471 DPRINTFN(0, "too short, %u bytes\n", actlen); 472 goto tr_setup; /* too short */ 473 } 474 actlen -= 4; 475 476 sc->sc_intr_ilen = actlen; 477 478 if (sc->sc_intr_iwakeup) { 479 sc->sc_intr_iwakeup = 0; 480 usb2_cv_signal(&sc->sc_intr_cv); 481 } else { 482 sc->sc_intr_iwakeup = 1; 483 } 484 /* 485 * We pause reading data from the interrupt endpoint until the 486 * data has been picked up! 487 */ 488 return; 489} 490 491/* 492 * Interrupt call reply transfer, read 493 */ 494static void 495zyd_cfg_usb2_intr_read(struct zyd_softc *sc, void *data, uint32_t size) 496{ 497 uint16_t actlen; 498 uint16_t x; 499 500 if (size > sizeof(sc->sc_intr_ibuf.data)) { 501 DPRINTFN(0, "truncating transfer size!\n"); 502 size = sizeof(sc->sc_intr_ibuf.data); 503 } 504 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 505 bzero(data, size); 506 goto done; 507 } 508 if (sc->sc_intr_iwakeup) { 509 DPRINTF("got data already!\n"); 510 sc->sc_intr_iwakeup = 0; 511 goto skip0; 512 } 513repeat: 514 sc->sc_intr_iwakeup = 1; 515 516 while (sc->sc_intr_iwakeup) { 517 518 /* wait for data */ 519 520 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_DT_RD]); 521 522 if (usb2_cv_timedwait(&sc->sc_intr_cv, 523 &sc->sc_mtx, hz / 2)) { 524 /* should not happen */ 525 } 526 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 527 bzero(data, size); 528 goto done; 529 } 530 } 531skip0: 532 if (size != sc->sc_intr_ilen) { 533 DPRINTFN(0, "unexpected length %u != %u\n", 534 size, sc->sc_intr_ilen); 535 goto repeat; 536 } 537 actlen = sc->sc_intr_ilen; 538 actlen /= 4; 539 540 /* verify register values */ 541 for (x = 0; x != actlen; x++) { 542 if (sc->sc_intr_obuf.data[(2 * x)] != 543 sc->sc_intr_ibuf.data[(4 * x)]) { 544 /* invalid register */ 545 DPRINTFN(0, "Invalid register (1) at %u!\n", x); 546 goto repeat; 547 } 548 if (sc->sc_intr_obuf.data[(2 * x) + 1] != 549 sc->sc_intr_ibuf.data[(4 * x) + 1]) { 550 /* invalid register */ 551 DPRINTFN(0, "Invalid register (2) at %u!\n", x); 552 goto repeat; 553 } 554 } 555 556 bcopy(sc->sc_intr_ibuf.data, data, size); 557 558 /* 559 * We have fetched the data from the shared buffer and it is 560 * safe to restart the interrupt transfer! 561 */ 562 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_DT_RD]); 563done: 564 return; 565} 566 567static void 568zyd_intr_write_clear_stall_callback(struct usb2_xfer *xfer) 569{ 570 struct zyd_softc *sc = xfer->priv_sc; 571 struct usb2_xfer *xfer_other = sc->sc_xfer[ZYD_TR_INTR_DT_WR]; 572 573 if (usb2_clear_stall_callback(xfer, xfer_other)) { 574 DPRINTF("stall cleared\n"); 575 sc->sc_flags &= ~ZYD_FLAG_INTR_WRITE_STALL; 576 usb2_transfer_start(xfer_other); 577 } 578 return; 579} 580 581static void 582zyd_intr_write_callback(struct usb2_xfer *xfer) 583{ 584 struct zyd_softc *sc = xfer->priv_sc; 585 586 switch (USB_GET_STATE(xfer)) { 587 case USB_ST_TRANSFERRED: 588 DPRINTFN(3, "length=%d\n", xfer->actlen); 589 goto wakeup; 590 591 case USB_ST_SETUP: 592 593 if (sc->sc_flags & ZYD_FLAG_INTR_WRITE_STALL) { 594 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_CS_WR]); 595 goto wakeup; 596 } 597 if (sc->sc_intr_owakeup) { 598 usb2_copy_in(xfer->frbuffers, 0, &sc->sc_intr_obuf, 599 sc->sc_intr_olen); 600 601 xfer->frlengths[0] = sc->sc_intr_olen; 602 usb2_start_hardware(xfer); 603 } 604 break; 605 606 default: /* Error */ 607 DPRINTFN(3, "error = %s\n", 608 usb2_errstr(xfer->error)); 609 610 if (xfer->error != USB_ERR_CANCELLED) { 611 /* try to clear stall first */ 612 sc->sc_flags |= ZYD_FLAG_INTR_WRITE_STALL; 613 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_CS_WR]); 614 } 615 goto wakeup; 616 } 617 return; 618 619wakeup: 620 if (sc->sc_intr_owakeup) { 621 sc->sc_intr_owakeup = 0; 622 usb2_cv_signal(&sc->sc_intr_cv); 623 } 624 return; 625} 626 627/* 628 * Interrupt transfer, write. 629 * 630 * Not always an "interrupt transfer". If operating in 631 * full speed mode, EP4 is bulk out, not interrupt out. 632 */ 633static void 634zyd_cfg_usb2_intr_write(struct zyd_softc *sc, const void *data, 635 uint16_t code, uint32_t size) 636{ 637 if (size > sizeof(sc->sc_intr_obuf.data)) { 638 DPRINTFN(0, "truncating transfer size!\n"); 639 size = sizeof(sc->sc_intr_obuf.data); 640 } 641 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 642 goto done; 643 } 644 sc->sc_intr_olen = size + 2; 645 sc->sc_intr_owakeup = 1; 646 647 sc->sc_intr_obuf.code = htole16(code); 648 bcopy(data, sc->sc_intr_obuf.data, size); 649 650 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_DT_WR]); 651 652 while (sc->sc_intr_owakeup) { 653 if (usb2_cv_timedwait(&sc->sc_intr_cv, 654 &sc->sc_mtx, hz / 2)) { 655 /* should not happen */ 656 } 657 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 658 sc->sc_intr_owakeup = 0; 659 goto done; 660 } 661 } 662done: 663 return; 664} 665 666static void 667zyd_cfg_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, uint16_t ilen, 668 void *odata, uint16_t olen, uint16_t flags) 669{ 670 zyd_cfg_usb2_intr_write(sc, idata, code, ilen); 671 672 if (flags & ZYD_CMD_FLAG_READ) { 673 zyd_cfg_usb2_intr_read(sc, odata, olen); 674 } 675 return; 676} 677 678static void 679zyd_cfg_read16(struct zyd_softc *sc, uint16_t addr, uint16_t *value) 680{ 681 struct zyd_pair tmp[1]; 682 683 addr = htole16(addr); 684 zyd_cfg_cmd(sc, ZYD_CMD_IORD, &addr, sizeof(addr), 685 tmp, sizeof(tmp), ZYD_CMD_FLAG_READ); 686 *value = le16toh(tmp[0].val); 687 return; 688} 689 690static void 691zyd_cfg_read32(struct zyd_softc *sc, uint16_t addr, uint32_t *value) 692{ 693 struct zyd_pair tmp[2]; 694 uint16_t regs[2]; 695 696 regs[0] = ZYD_REG32_HI(addr); 697 regs[1] = ZYD_REG32_LO(addr); 698 regs[0] = htole16(regs[0]); 699 regs[1] = htole16(regs[1]); 700 701 zyd_cfg_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), 702 tmp, sizeof(tmp), ZYD_CMD_FLAG_READ); 703 *value = (le16toh(tmp[0].val) << 16) | le16toh(tmp[1].val); 704 return; 705} 706 707static void 708zyd_cfg_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 709{ 710 struct zyd_pair pair[1]; 711 712 pair[0].reg = htole16(reg); 713 pair[0].val = htole16(val); 714 715 zyd_cfg_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 716 return; 717} 718 719static void 720zyd_cfg_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 721{ 722 struct zyd_pair pair[2]; 723 724 pair[0].reg = htole16(ZYD_REG32_HI(reg)); 725 pair[0].val = htole16(val >> 16); 726 pair[1].reg = htole16(ZYD_REG32_LO(reg)); 727 pair[1].val = htole16(val & 0xffff); 728 729 zyd_cfg_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0); 730 return; 731} 732 733/*------------------------------------------------------------------------* 734 * zyd_cfg_rfwrite - write RF registers 735 *------------------------------------------------------------------------*/ 736static void 737zyd_cfg_rfwrite(struct zyd_softc *sc, uint32_t value) 738{ 739 struct zyd_rf *rf = &sc->sc_rf; 740 struct zyd_rfwrite req; 741 uint16_t cr203; 742 uint16_t i; 743 744 zyd_cfg_read16(sc, ZYD_CR203, &cr203); 745 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 746 747 req.code = htole16(2); 748 req.width = htole16(rf->width); 749 for (i = 0; i != rf->width; i++) { 750 req.bit[i] = htole16(cr203); 751 if (value & (1 << (rf->width - 1 - i))) 752 req.bit[i] |= htole16(ZYD_RF_DATA); 753 } 754 zyd_cfg_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + (2 * rf->width), NULL, 0, 0); 755 return; 756} 757 758static void 759zyd_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 760{ 761 struct zyd_softc *sc = xfer->priv_sc; 762 struct usb2_xfer *xfer_other = sc->sc_xfer[ZYD_TR_BULK_DT_RD]; 763 764 if (usb2_clear_stall_callback(xfer, xfer_other)) { 765 DPRINTF("stall cleared\n"); 766 sc->sc_flags &= ~ZYD_FLAG_BULK_READ_STALL; 767 usb2_transfer_start(xfer_other); 768 } 769 return; 770} 771 772static void 773zyd_bulk_read_callback_sub(struct usb2_xfer *xfer, struct zyd_ifq *mq, 774 uint32_t offset, uint16_t len) 775{ 776 enum { 777 ZYD_OVERHEAD = (ZYD_HW_PADDING + IEEE80211_CRC_LEN), 778 }; 779 struct zyd_softc *sc = xfer->priv_sc; 780 struct ifnet *ifp = sc->sc_ifp; 781 struct zyd_plcphdr plcp; 782 struct zyd_rx_stat stat; 783 struct mbuf *m; 784 785 if (len < ZYD_OVERHEAD) { 786 DPRINTF("frame too " 787 "short (length=%d)\n", len); 788 ifp->if_ierrors++; 789 return; 790 } 791 usb2_copy_out(xfer->frbuffers, offset, &plcp, sizeof(plcp)); 792 usb2_copy_out(xfer->frbuffers, offset + len - sizeof(stat), 793 &stat, sizeof(stat)); 794 795 if (stat.flags & ZYD_RX_ERROR) { 796 DPRINTF("RX status indicated " 797 "error (0x%02x)\n", stat.flags); 798 ifp->if_ierrors++; 799 return; 800 } 801 /* compute actual frame length */ 802 len -= ZYD_OVERHEAD; 803 804 /* allocate a mbuf to store the frame */ 805 if (len > MCLBYTES) { 806 DPRINTF("too large frame, " 807 "%u bytes\n", len); 808 return; 809 } else if (len > MHLEN) 810 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 811 else 812 m = m_gethdr(M_DONTWAIT, MT_DATA); 813 814 if (m == NULL) { 815 DPRINTF("could not allocate rx mbuf\n"); 816 ifp->if_ierrors++; 817 return; 818 } 819 m->m_pkthdr.rcvif = ifp; 820 m->m_pkthdr.len = len; 821 m->m_len = len; 822 823 usb2_copy_out(xfer->frbuffers, offset + 824 sizeof(plcp), m->m_data, len); 825 826 if (bpf_peers_present(ifp->if_bpf)) { 827 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap; 828 829 tap->wr_flags = 0; 830 if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32)) 831 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 832 /* XXX toss, no way to express errors */ 833 if (stat.flags & ZYD_RX_DECRYPTERR) 834 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 835 tap->wr_rate = 836 zyd_plcp2ieee(plcp.signal, stat.flags & ZYD_RX_OFDM); 837 tap->wr_antsignal = stat.rssi + -95; 838 tap->wr_antnoise = -95; /* XXX */ 839 840 bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m); 841 } 842 if (sizeof(m->m_hdr.pad) > 0) { 843 m->m_hdr.pad[0] = stat.rssi; /* XXX hack */ 844 } 845 _IF_ENQUEUE(mq, m); 846 847 return; 848} 849 850static void 851zyd_bulk_read_callback(struct usb2_xfer *xfer) 852{ 853 struct zyd_softc *sc = xfer->priv_sc; 854 struct ifnet *ifp = sc->sc_ifp; 855 struct ieee80211com *ic = ifp->if_l2com; 856 struct ieee80211_node *ni; 857 struct zyd_rx_desc rx_desc; 858 struct zyd_ifq mq = {NULL, NULL, 0}; 859 struct mbuf *m; 860 uint32_t offset; 861 uint16_t len16; 862 uint8_t x; 863 uint8_t rssi; 864 int8_t nf; 865 866 switch (USB_GET_STATE(xfer)) { 867 case USB_ST_TRANSFERRED: 868 869 if (xfer->actlen < MAX(sizeof(rx_desc), ZYD_MIN_FRAGSZ)) { 870 DPRINTFN(0, "xfer too short, %d bytes\n", xfer->actlen); 871 ifp->if_ierrors++; 872 goto tr_setup; 873 } 874 usb2_copy_out(xfer->frbuffers, xfer->actlen - sizeof(rx_desc), 875 &rx_desc, sizeof(rx_desc)); 876 877 if (UGETW(rx_desc.tag) == ZYD_TAG_MULTIFRAME) { 878 879 offset = 0; 880 881 DPRINTFN(4, "received multi-frame transfer, " 882 "%u bytes\n", xfer->actlen); 883 884 for (x = 0; x < ZYD_MAX_RXFRAMECNT; x++) { 885 len16 = UGETW(rx_desc.len[x]); 886 887 if ((len16 == 0) || (len16 > xfer->actlen)) { 888 break; 889 } 890 zyd_bulk_read_callback_sub(xfer, &mq, offset, len16); 891 892 /* 893 * next frame is aligned on a 32-bit 894 * boundary 895 */ 896 len16 = (len16 + 3) & ~3; 897 offset += len16; 898 if (len16 > xfer->actlen) { 899 break; 900 } 901 xfer->actlen -= len16; 902 } 903 } else { 904 DPRINTFN(4, "received single-frame transfer, " 905 "%u bytes\n", xfer->actlen); 906 zyd_bulk_read_callback_sub(xfer, &mq, 0, xfer->actlen); 907 } 908 909 case USB_ST_SETUP: 910tr_setup: 911 DPRINTF("setup\n"); 912 913 if (sc->sc_flags & ZYD_FLAG_BULK_READ_STALL) { 914 usb2_transfer_start(sc->sc_xfer[ZYD_TR_BULK_CS_RD]); 915 } else { 916 xfer->frlengths[0] = xfer->max_data_length; 917 usb2_start_hardware(xfer); 918 } 919 920 /* 921 * At the end of a USB callback it is always safe to unlock 922 * the private mutex of a device! That is why we do the 923 * "ieee80211_input" here, and not some lines up! 924 */ 925 if (mq.ifq_head) { 926 927 mtx_unlock(&sc->sc_mtx); 928 929 while (1) { 930 931 _IF_DEQUEUE(&mq, m); 932 933 if (m == NULL) 934 break; 935 936 rssi = m->m_hdr.pad[0]; /* XXX hack */ 937 938 rssi = (rssi > 63) ? 127 : 2 * rssi; 939 nf = -95; /* XXX */ 940 941 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 942 if (ni != NULL) { 943 if (ieee80211_input(ni, m, rssi, nf, 0)) { 944 /* ignore */ 945 } 946 ieee80211_free_node(ni); 947 } else { 948 if (ieee80211_input_all(ic, m, rssi, nf, 0)) { 949 /* ignore */ 950 } 951 } 952 } 953 954 mtx_lock(&sc->sc_mtx); 955 } 956 break; 957 958 default: /* Error */ 959 DPRINTF("frame error: %s\n", usb2_errstr(xfer->error)); 960 961 if (xfer->error != USB_ERR_CANCELLED) { 962 /* try to clear stall first */ 963 sc->sc_flags |= ZYD_FLAG_BULK_READ_STALL; 964 usb2_transfer_start(sc->sc_xfer[ZYD_TR_BULK_CS_RD]); 965 } 966 break; 967 } 968 return; 969} 970 971/*------------------------------------------------------------------------* 972 * zyd_cfg_uploadfirmware 973 * Returns: 974 * 0: Success 975 * Else: Failure 976 *------------------------------------------------------------------------*/ 977static uint8_t 978zyd_cfg_uploadfirmware(struct zyd_softc *sc, const uint8_t *fw_ptr, 979 uint32_t fw_len) 980{ 981 struct usb2_device_request req; 982 uint16_t temp; 983 uint16_t addr; 984 uint8_t stat; 985 986 DPRINTF("firmware %p size=%u\n", fw_ptr, fw_len); 987 988 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 989 req.bRequest = ZYD_DOWNLOADREQ; 990 USETW(req.wIndex, 0); 991 992 temp = 64; 993 994 addr = ZYD_FIRMWARE_START_ADDR; 995 while (fw_len > 0) { 996 997 if (fw_len < 64) { 998 temp = fw_len; 999 } 1000 DPRINTF("firmware block: fw_len=%u\n", fw_len); 1001 1002 USETW(req.wValue, addr); 1003 USETW(req.wLength, temp); 1004 1005 zyd_cfg_usbrequest(sc, &req, 1006 USB_ADD_BYTES(fw_ptr, 0)); 1007 1008 addr += (temp / 2); 1009 fw_len -= temp; 1010 fw_ptr += temp; 1011 } 1012 1013 /* check whether the upload succeeded */ 1014 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1015 req.bRequest = ZYD_DOWNLOADSTS; 1016 USETW(req.wValue, 0); 1017 USETW(req.wIndex, 0); 1018 USETW(req.wLength, sizeof(stat)); 1019 1020 zyd_cfg_usbrequest(sc, &req, &stat); 1021 1022 return ((stat & 0x80) ? 1 : 0); 1023} 1024 1025/* 1026 * Driver OS interface 1027 */ 1028 1029/* 1030 * Probe for a ZD1211-containing product 1031 */ 1032static int 1033zyd_probe(device_t dev) 1034{ 1035 struct usb2_attach_arg *uaa = device_get_ivars(dev); 1036 1037 if (uaa->usb2_mode != USB_MODE_HOST) { 1038 return (ENXIO); 1039 } 1040 if (uaa->info.bConfigIndex != 0) { 1041 return (ENXIO); 1042 } 1043 if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX) { 1044 return (ENXIO); 1045 } 1046 return (usb2_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa)); 1047} 1048 1049/* 1050 * Attach the interface. Allocate softc structures, do 1051 * setup and ethernet/BPF attach. 1052 */ 1053static int 1054zyd_attach(device_t dev) 1055{ 1056 struct usb2_attach_arg *uaa = device_get_ivars(dev); 1057 struct zyd_softc *sc = device_get_softc(dev); 1058 int error; 1059 uint8_t iface_index; 1060 1061 if (sc == NULL) { 1062 return (ENOMEM); 1063 } 1064 if (uaa->info.bcdDevice < 0x4330) { 1065 device_printf(dev, "device version mismatch: 0x%X " 1066 "(only >= 43.30 supported)\n", 1067 uaa->info.bcdDevice); 1068 return (EINVAL); 1069 } 1070 device_set_usb2_desc(dev); 1071 1072 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 1073 device_get_nameunit(dev)); 1074 1075 sc->sc_unit = device_get_unit(dev); 1076 sc->sc_udev = uaa->device; 1077 sc->sc_mac_rev = USB_GET_DRIVER_INFO(uaa); 1078 1079 mtx_init(&sc->sc_mtx, "zyd lock", MTX_NETWORK_LOCK, 1080 MTX_DEF | MTX_RECURSE); 1081 1082 usb2_cv_init(&sc->sc_intr_cv, "IWAIT"); 1083 1084 usb2_callout_init_mtx(&sc->sc_watchdog, 1085 &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 1086 1087 /* 1088 * Endpoint 1 = Bulk out (512b @ high speed / 64b @ full speed) 1089 * Endpoint 2 = Bulk in (512b @ high speed / 64b @ full speed) 1090 * Endpoint 3 = Intr in (64b) 1091 * Endpoint 4 = Intr out @ high speed / bulk out @ full speed (64b) 1092 */ 1093 iface_index = ZYD_IFACE_INDEX; 1094 error = usb2_transfer_setup(uaa->device, &iface_index, 1095 sc->sc_xfer, zyd_config, ZYD_N_TRANSFER, sc, &sc->sc_mtx); 1096 if (error) { 1097 device_printf(dev, "could not allocate USB " 1098 "transfers: %s\n", usb2_errstr(error)); 1099 goto detach; 1100 } 1101 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 1102 &zyd_end_of_commands, sizeof(struct usb2_config_td_cc), 16); 1103 if (error) { 1104 device_printf(dev, "could not setup config " 1105 "thread!\n"); 1106 goto detach; 1107 } 1108 mtx_lock(&sc->sc_mtx); 1109 1110 /* start setup */ 1111 1112 usb2_config_td_queue_command 1113 (&sc->sc_config_td, NULL, &zyd_cfg_first_time_setup, 0, 0); 1114 1115 /* start watchdog (will exit mutex) */ 1116 1117 zyd_watchdog(sc); 1118 1119 return (0); 1120 1121detach: 1122 zyd_detach(dev); 1123 return (ENXIO); 1124} 1125 1126/* 1127 * Lock PHY registers 1128 */ 1129static void 1130zyd_cfg_lock_phy(struct zyd_softc *sc) 1131{ 1132 uint32_t temp; 1133 1134 zyd_cfg_read32(sc, ZYD_MAC_MISC, &temp); 1135 temp &= ~ZYD_UNLOCK_PHY_REGS; 1136 zyd_cfg_write32(sc, ZYD_MAC_MISC, temp); 1137} 1138 1139/* 1140 * Unlock PHY registers 1141 */ 1142static void 1143zyd_cfg_unlock_phy(struct zyd_softc *sc) 1144{ 1145 uint32_t temp; 1146 1147 zyd_cfg_read32(sc, ZYD_MAC_MISC, &temp); 1148 temp |= ZYD_UNLOCK_PHY_REGS; 1149 zyd_cfg_write32(sc, ZYD_MAC_MISC, temp); 1150} 1151 1152static void 1153zyd_cfg_set_beacon_interval(struct zyd_softc *sc, uint32_t bintval) 1154{ 1155 /* XXX this is probably broken.. */ 1156 zyd_cfg_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2); 1157 zyd_cfg_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1); 1158 zyd_cfg_write32(sc, ZYD_CR_BCN_INTERVAL, bintval); 1159 return; 1160} 1161 1162/* 1163 * Get RF name 1164 */ 1165static const char * 1166zyd_rf_name(uint8_t type) 1167{ 1168 static const char *const zyd_rfs[] = { 1169 "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1170 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1171 "PV2000", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1172 "PHILIPS" 1173 }; 1174 1175 return (zyd_rfs[(type > 15) ? 0 : type]); 1176} 1177 1178/* 1179 * RF driver: Init for RFMD chip 1180 */ 1181static void 1182zyd_cfg_rf_rfmd_init(struct zyd_softc *sc, struct zyd_rf *rf) 1183{ 1184 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 1185 static const uint32_t rfini[] = ZYD_RFMD_RF; 1186 uint32_t i; 1187 1188 /* init RF-dependent PHY registers */ 1189 for (i = 0; i != INDEXES(phyini); i++) { 1190 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1191 } 1192 1193 /* init RFMD radio */ 1194 for (i = 0; i != INDEXES(rfini); i++) { 1195 zyd_cfg_rfwrite(sc, rfini[i]); 1196 } 1197 return; 1198} 1199 1200/* 1201 * RF driver: Switch radio on/off for RFMD chip 1202 */ 1203static void 1204zyd_cfg_rf_rfmd_switch_radio(struct zyd_softc *sc, uint8_t on) 1205{ 1206 zyd_cfg_write16(sc, ZYD_CR10, on ? 0x89 : 0x15); 1207 zyd_cfg_write16(sc, ZYD_CR11, on ? 0x00 : 0x81); 1208 return; 1209} 1210 1211/* 1212 * RF driver: Channel setting for RFMD chip 1213 */ 1214static void 1215zyd_cfg_rf_rfmd_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1216 uint8_t channel) 1217{ 1218 static const struct { 1219 uint32_t r1, r2; 1220 } rfprog[] = ZYD_RFMD_CHANTABLE; 1221 1222 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r1); 1223 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r2); 1224 return; 1225} 1226 1227/* 1228 * RF driver: Switch radio on/off for AL2230 chip 1229 */ 1230static void 1231zyd_cfg_rf_al2230_switch_radio(struct zyd_softc *sc, uint8_t on) 1232{ 1233 uint8_t on251 = (sc->sc_mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f; 1234 1235 zyd_cfg_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1236 zyd_cfg_write16(sc, ZYD_CR251, on ? on251 : 0x2f); 1237 return; 1238} 1239 1240/* 1241 * RF driver: Init for AL2230 chip 1242 */ 1243static void 1244zyd_cfg_rf_al2230_init(struct zyd_softc *sc, struct zyd_rf *rf) 1245{ 1246 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 1247 static const uint32_t rfini[] = ZYD_AL2230_RF; 1248 uint32_t i; 1249 1250 /* init RF-dependent PHY registers */ 1251 for (i = 0; i != INDEXES(phyini); i++) { 1252 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1253 } 1254 1255 /* init AL2230 radio */ 1256 for (i = 0; i != INDEXES(rfini); i++) { 1257 zyd_cfg_rfwrite(sc, rfini[i]); 1258 } 1259 return; 1260} 1261 1262static void 1263zyd_cfg_rf_al2230_init_b(struct zyd_softc *sc, struct zyd_rf *rf) 1264{ 1265 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1266 static const uint32_t rfini[] = ZYD_AL2230_RF_B; 1267 uint32_t i; 1268 1269 /* init RF-dependent PHY registers */ 1270 for (i = 0; i != INDEXES(phyini); i++) { 1271 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1272 } 1273 1274 /* init AL2230 radio */ 1275 for (i = 0; i != INDEXES(rfini); i++) { 1276 zyd_cfg_rfwrite(sc, rfini[i]); 1277 } 1278 return; 1279} 1280 1281/* 1282 * RF driver: Channel setting for AL2230 chip 1283 */ 1284static void 1285zyd_cfg_rf_al2230_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1286 uint8_t channel) 1287{ 1288 static const struct { 1289 uint32_t r1, r2, r3; 1290 } rfprog[] = ZYD_AL2230_CHANTABLE; 1291 1292 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r1); 1293 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r2); 1294 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r3); 1295 1296 zyd_cfg_write16(sc, ZYD_CR138, 0x28); 1297 zyd_cfg_write16(sc, ZYD_CR203, 0x06); 1298 return; 1299} 1300 1301/* 1302 * AL7230B RF methods. 1303 */ 1304static void 1305zyd_cfg_rf_al7230b_switch_radio(struct zyd_softc *sc, uint8_t on) 1306{ 1307 zyd_cfg_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1308 zyd_cfg_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1309 return; 1310} 1311 1312static void 1313zyd_cfg_rf_al7230b_init(struct zyd_softc *sc, struct zyd_rf *rf) 1314{ 1315 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1316 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1317 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1318 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1319 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1320 uint32_t i; 1321 1322 /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1323 1324 /* init RF-dependent PHY registers, part one */ 1325 for (i = 0; i != INDEXES(phyini_1); i++) { 1326 zyd_cfg_write16(sc, phyini_1[i].reg, phyini_1[i].val); 1327 } 1328 /* init AL7230B radio, part one */ 1329 for (i = 0; i != INDEXES(rfini_1); i++) { 1330 zyd_cfg_rfwrite(sc, rfini_1[i]); 1331 } 1332 /* init RF-dependent PHY registers, part two */ 1333 for (i = 0; i != INDEXES(phyini_2); i++) { 1334 zyd_cfg_write16(sc, phyini_2[i].reg, phyini_2[i].val); 1335 } 1336 /* init AL7230B radio, part two */ 1337 for (i = 0; i != INDEXES(rfini_2); i++) { 1338 zyd_cfg_rfwrite(sc, rfini_2[i]); 1339 } 1340 /* init RF-dependent PHY registers, part three */ 1341 for (i = 0; i != INDEXES(phyini_3); i++) { 1342 zyd_cfg_write16(sc, phyini_3[i].reg, phyini_3[i].val); 1343 } 1344 return; 1345} 1346 1347static void 1348zyd_cfg_rf_al7230b_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1349 uint8_t channel) 1350{ 1351 static const struct { 1352 uint32_t r1, r2; 1353 } rfprog[] = ZYD_AL7230B_CHANTABLE; 1354 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1355 uint32_t i; 1356 1357 zyd_cfg_write16(sc, ZYD_CR240, 0x57); 1358 zyd_cfg_write16(sc, ZYD_CR251, 0x2f); 1359 1360 for (i = 0; i != INDEXES(rfsc); i++) { 1361 zyd_cfg_rfwrite(sc, rfsc[i]); 1362 } 1363 1364 zyd_cfg_write16(sc, ZYD_CR128, 0x14); 1365 zyd_cfg_write16(sc, ZYD_CR129, 0x12); 1366 zyd_cfg_write16(sc, ZYD_CR130, 0x10); 1367 zyd_cfg_write16(sc, ZYD_CR38, 0x38); 1368 zyd_cfg_write16(sc, ZYD_CR136, 0xdf); 1369 1370 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r1); 1371 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r2); 1372 zyd_cfg_rfwrite(sc, 0x3c9000); 1373 1374 zyd_cfg_write16(sc, ZYD_CR251, 0x3f); 1375 zyd_cfg_write16(sc, ZYD_CR203, 0x06); 1376 zyd_cfg_write16(sc, ZYD_CR240, 0x08); 1377 1378 return; 1379} 1380 1381/* 1382 * AL2210 RF methods. 1383 */ 1384static void 1385zyd_cfg_rf_al2210_switch_radio(struct zyd_softc *sc, uint8_t on) 1386{ 1387 1388} 1389 1390static void 1391zyd_cfg_rf_al2210_init(struct zyd_softc *sc, struct zyd_rf *rf) 1392{ 1393 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1394 static const uint32_t rfini[] = ZYD_AL2210_RF; 1395 uint32_t tmp; 1396 uint32_t i; 1397 1398 zyd_cfg_write32(sc, ZYD_CR18, 2); 1399 1400 /* init RF-dependent PHY registers */ 1401 for (i = 0; i != INDEXES(phyini); i++) { 1402 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1403 } 1404 /* init AL2210 radio */ 1405 for (i = 0; i != INDEXES(rfini); i++) { 1406 zyd_cfg_rfwrite(sc, rfini[i]); 1407 } 1408 zyd_cfg_write16(sc, ZYD_CR47, 0x1e); 1409 zyd_cfg_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1410 zyd_cfg_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1411 zyd_cfg_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1412 zyd_cfg_write32(sc, ZYD_CR_RFCFG, 0x05); 1413 zyd_cfg_write32(sc, ZYD_CR_RFCFG, 0x00); 1414 zyd_cfg_write16(sc, ZYD_CR47, 0x1e); 1415 zyd_cfg_write32(sc, ZYD_CR18, 3); 1416 1417 return; 1418} 1419 1420static void 1421zyd_cfg_rf_al2210_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1422 uint8_t channel) 1423{ 1424 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1425 uint32_t tmp; 1426 1427 zyd_cfg_write32(sc, ZYD_CR18, 2); 1428 zyd_cfg_write16(sc, ZYD_CR47, 0x1e); 1429 zyd_cfg_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1430 zyd_cfg_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1431 zyd_cfg_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1432 zyd_cfg_write32(sc, ZYD_CR_RFCFG, 0x05); 1433 1434 zyd_cfg_write32(sc, ZYD_CR_RFCFG, 0x00); 1435 zyd_cfg_write16(sc, ZYD_CR47, 0x1e); 1436 1437 /* actually set the channel */ 1438 zyd_cfg_rfwrite(sc, rfprog[channel - 1]); 1439 1440 zyd_cfg_write32(sc, ZYD_CR18, 3); 1441 return; 1442} 1443 1444/* 1445 * GCT RF methods. 1446 */ 1447static void 1448zyd_cfg_rf_gct_switch_radio(struct zyd_softc *sc, uint8_t on) 1449{ 1450 /* vendor driver does nothing for this RF chip */ 1451 1452 return; 1453} 1454 1455static void 1456zyd_cfg_rf_gct_init(struct zyd_softc *sc, struct zyd_rf *rf) 1457{ 1458 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1459 static const uint32_t rfini[] = ZYD_GCT_RF; 1460 uint32_t i; 1461 1462 /* init RF-dependent PHY registers */ 1463 for (i = 0; i != INDEXES(phyini); i++) { 1464 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1465 } 1466 /* init cgt radio */ 1467 for (i = 0; i != INDEXES(rfini); i++) { 1468 zyd_cfg_rfwrite(sc, rfini[i]); 1469 } 1470 return; 1471} 1472 1473static void 1474zyd_cfg_rf_gct_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1475 uint8_t channel) 1476{ 1477 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE; 1478 1479 zyd_cfg_rfwrite(sc, 0x1c0000); 1480 zyd_cfg_rfwrite(sc, rfprog[channel - 1]); 1481 zyd_cfg_rfwrite(sc, 0x1c0008); 1482 1483 return; 1484} 1485 1486/* 1487 * Maxim RF methods. 1488 */ 1489static void 1490zyd_cfg_rf_maxim_switch_radio(struct zyd_softc *sc, uint8_t on) 1491{ 1492 /* vendor driver does nothing for this RF chip */ 1493 1494 return; 1495} 1496 1497static void 1498zyd_cfg_rf_maxim_init(struct zyd_softc *sc, struct zyd_rf *rf) 1499{ 1500 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1501 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1502 uint16_t tmp; 1503 uint32_t i; 1504 1505 /* init RF-dependent PHY registers */ 1506 for (i = 0; i != INDEXES(phyini); i++) { 1507 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1508 } 1509 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1510 zyd_cfg_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1511 1512 /* init maxim radio */ 1513 for (i = 0; i != INDEXES(rfini); i++) { 1514 zyd_cfg_rfwrite(sc, rfini[i]); 1515 } 1516 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1517 zyd_cfg_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1518 1519 return; 1520} 1521 1522static void 1523zyd_cfg_rf_maxim_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1524 uint8_t channel) 1525{ 1526 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1527 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1528 static const struct { 1529 uint32_t r1, r2; 1530 } rfprog[] = ZYD_MAXIM_CHANTABLE; 1531 uint16_t tmp; 1532 uint32_t i; 1533 1534 /* 1535 * Do the same as we do when initializing it, except for the channel 1536 * values coming from the two channel tables. 1537 */ 1538 1539 /* init RF-dependent PHY registers */ 1540 for (i = 0; i != INDEXES(phyini); i++) { 1541 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1542 } 1543 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1544 zyd_cfg_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1545 1546 /* first two values taken from the chantables */ 1547 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r1); 1548 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r2); 1549 1550 /* init maxim radio - skipping the two first values */ 1551 if (INDEXES(rfini) > 2) { 1552 for (i = 2; i != INDEXES(rfini); i++) { 1553 zyd_cfg_rfwrite(sc, rfini[i]); 1554 } 1555 } 1556 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1557 zyd_cfg_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1558 1559 return; 1560} 1561 1562/* 1563 * Maxim2 RF methods. 1564 */ 1565static void 1566zyd_cfg_rf_maxim2_switch_radio(struct zyd_softc *sc, uint8_t on) 1567{ 1568 /* vendor driver does nothing for this RF chip */ 1569 return; 1570} 1571 1572static void 1573zyd_cfg_rf_maxim2_init(struct zyd_softc *sc, struct zyd_rf *rf) 1574{ 1575 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1576 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1577 uint16_t tmp; 1578 uint32_t i; 1579 1580 /* init RF-dependent PHY registers */ 1581 for (i = 0; i != INDEXES(phyini); i++) { 1582 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1583 } 1584 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1585 zyd_cfg_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1586 1587 /* init maxim2 radio */ 1588 for (i = 0; i != INDEXES(rfini); i++) { 1589 zyd_cfg_rfwrite(sc, rfini[i]); 1590 } 1591 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1592 zyd_cfg_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1593 return; 1594} 1595 1596static void 1597zyd_cfg_rf_maxim2_set_channel(struct zyd_softc *sc, struct zyd_rf *rf, 1598 uint8_t channel) 1599{ 1600 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1601 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1602 static const struct { 1603 uint32_t r1, r2; 1604 } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1605 uint16_t tmp; 1606 uint32_t i; 1607 1608 /* 1609 * Do the same as we do when initializing it, except for the channel 1610 * values coming from the two channel tables. 1611 */ 1612 1613 /* init RF-dependent PHY registers */ 1614 for (i = 0; i != INDEXES(phyini); i++) { 1615 zyd_cfg_write16(sc, phyini[i].reg, phyini[i].val); 1616 } 1617 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1618 zyd_cfg_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1619 1620 /* first two values taken from the chantables */ 1621 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r1); 1622 zyd_cfg_rfwrite(sc, rfprog[channel - 1].r2); 1623 1624 /* init maxim2 radio - skipping the two first values */ 1625 if (INDEXES(rfini) > 2) { 1626 for (i = 2; i != INDEXES(rfini); i++) { 1627 zyd_cfg_rfwrite(sc, rfini[i]); 1628 } 1629 } 1630 zyd_cfg_read16(sc, ZYD_CR203, &tmp); 1631 zyd_cfg_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1632 return; 1633} 1634 1635/* 1636 * Assign drivers and init the RF 1637 */ 1638static uint8_t 1639zyd_cfg_rf_init_hw(struct zyd_softc *sc, struct zyd_rf *rf) 1640{ 1641 ; /* fix for indent */ 1642 1643 switch (sc->sc_rf_rev) { 1644 case ZYD_RF_RFMD: 1645 rf->cfg_init_hw = zyd_cfg_rf_rfmd_init; 1646 rf->cfg_switch_radio = zyd_cfg_rf_rfmd_switch_radio; 1647 rf->cfg_set_channel = zyd_cfg_rf_rfmd_set_channel; 1648 rf->width = 24; /* 24-bit RF values */ 1649 break; 1650 case ZYD_RF_AL2230: 1651 if (sc->sc_mac_rev == ZYD_ZD1211B) 1652 rf->cfg_init_hw = zyd_cfg_rf_al2230_init_b; 1653 else 1654 rf->cfg_init_hw = zyd_cfg_rf_al2230_init; 1655 rf->cfg_switch_radio = zyd_cfg_rf_al2230_switch_radio; 1656 rf->cfg_set_channel = zyd_cfg_rf_al2230_set_channel; 1657 rf->width = 24; /* 24-bit RF values */ 1658 break; 1659 case ZYD_RF_AL7230B: 1660 rf->cfg_init_hw = zyd_cfg_rf_al7230b_init; 1661 rf->cfg_switch_radio = zyd_cfg_rf_al7230b_switch_radio; 1662 rf->cfg_set_channel = zyd_cfg_rf_al7230b_set_channel; 1663 rf->width = 24; /* 24-bit RF values */ 1664 break; 1665 case ZYD_RF_AL2210: 1666 rf->cfg_init_hw = zyd_cfg_rf_al2210_init; 1667 rf->cfg_switch_radio = zyd_cfg_rf_al2210_switch_radio; 1668 rf->cfg_set_channel = zyd_cfg_rf_al2210_set_channel; 1669 rf->width = 24; /* 24-bit RF values */ 1670 break; 1671 case ZYD_RF_GCT: 1672 rf->cfg_init_hw = zyd_cfg_rf_gct_init; 1673 rf->cfg_switch_radio = zyd_cfg_rf_gct_switch_radio; 1674 rf->cfg_set_channel = zyd_cfg_rf_gct_set_channel; 1675 rf->width = 21; /* 21-bit RF values */ 1676 break; 1677 case ZYD_RF_MAXIM_NEW: 1678 rf->cfg_init_hw = zyd_cfg_rf_maxim_init; 1679 rf->cfg_switch_radio = zyd_cfg_rf_maxim_switch_radio; 1680 rf->cfg_set_channel = zyd_cfg_rf_maxim_set_channel; 1681 rf->width = 18; /* 18-bit RF values */ 1682 break; 1683 case ZYD_RF_MAXIM_NEW2: 1684 rf->cfg_init_hw = zyd_cfg_rf_maxim2_init; 1685 rf->cfg_switch_radio = zyd_cfg_rf_maxim2_switch_radio; 1686 rf->cfg_set_channel = zyd_cfg_rf_maxim2_set_channel; 1687 rf->width = 18; /* 18-bit RF values */ 1688 break; 1689 default: 1690 DPRINTFN(0, "%s: Sorry, radio %s is not supported yet\n", 1691 sc->sc_name, zyd_rf_name(sc->sc_rf_rev)); 1692 return (1); 1693 } 1694 1695 zyd_cfg_lock_phy(sc); 1696 (rf->cfg_init_hw) (sc, rf); 1697 zyd_cfg_unlock_phy(sc); 1698 1699 return (0); /* success */ 1700} 1701 1702/* 1703 * Init the hardware 1704 */ 1705static uint8_t 1706zyd_cfg_hw_init(struct zyd_softc *sc) 1707{ 1708 const struct zyd_phy_pair *phyp; 1709 uint32_t tmp; 1710 1711 /* specify that the plug and play is finished */ 1712 zyd_cfg_write32(sc, ZYD_MAC_AFTER_PNP, 1); 1713 1714 zyd_cfg_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_firmware_base); 1715 DPRINTF("firmware base address=0x%04x\n", sc->sc_firmware_base); 1716 1717 /* retrieve firmware revision number */ 1718 zyd_cfg_read16(sc, sc->sc_firmware_base + ZYD_FW_FIRMWARE_REV, &sc->sc_fw_rev); 1719 1720 zyd_cfg_write32(sc, ZYD_CR_GPI_EN, 0); 1721 zyd_cfg_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1722 1723 /* disable interrupts */ 1724 zyd_cfg_write32(sc, ZYD_CR_INTERRUPT, 0); 1725 1726 /* PHY init */ 1727 zyd_cfg_lock_phy(sc); 1728 phyp = (sc->sc_mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1729 for (; phyp->reg != 0; phyp++) { 1730 zyd_cfg_write16(sc, phyp->reg, phyp->val); 1731 } 1732 if (sc->sc_fix_cr157) { 1733 zyd_cfg_read32(sc, ZYD_EEPROM_PHY_REG, &tmp); 1734 zyd_cfg_write32(sc, ZYD_CR157, tmp >> 8); 1735 } 1736 zyd_cfg_unlock_phy(sc); 1737 1738 /* HMAC init */ 1739 zyd_cfg_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1740 zyd_cfg_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1741 1742 if (sc->sc_mac_rev == ZYD_ZD1211) { 1743 zyd_cfg_write32(sc, ZYD_MAC_RETRY, 0x00000002); 1744 } else { 1745 zyd_cfg_write32(sc, ZYD_MACB_MAX_RETRY, 0x02020202); 1746 zyd_cfg_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1747 zyd_cfg_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1748 zyd_cfg_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1749 zyd_cfg_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1750 zyd_cfg_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1751 zyd_cfg_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1752 zyd_cfg_write32(sc, ZYD_MACB_TXOP, 0x01800824); 1753 } 1754 1755 zyd_cfg_write32(sc, ZYD_MAC_SNIFFER, 0x00000000); 1756 zyd_cfg_write32(sc, ZYD_MAC_RXFILTER, 0x00000000); 1757 zyd_cfg_write32(sc, ZYD_MAC_GHTBL, 0x00000000); 1758 zyd_cfg_write32(sc, ZYD_MAC_GHTBH, 0x80000000); 1759 zyd_cfg_write32(sc, ZYD_MAC_MISC, 0x000000a4); 1760 zyd_cfg_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1761 zyd_cfg_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1762 zyd_cfg_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1763 zyd_cfg_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1764 zyd_cfg_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1765 zyd_cfg_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1766 zyd_cfg_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032); 1767 zyd_cfg_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1768 zyd_cfg_write32(sc, ZYD_CR_PS_CTRL, 0x10000000); 1769 zyd_cfg_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1770 zyd_cfg_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1771 zyd_cfg_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1772 1773 /* init beacon interval to 100ms */ 1774 zyd_cfg_set_beacon_interval(sc, 100); 1775 1776 return (0); /* success */ 1777} 1778 1779/* 1780 * Read information from EEPROM 1781 */ 1782static void 1783zyd_cfg_read_eeprom(struct zyd_softc *sc) 1784{ 1785 uint32_t tmp; 1786 uint16_t i; 1787 uint16_t val; 1788 1789 /* read MAC address */ 1790 zyd_cfg_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp); 1791 sc->sc_myaddr[0] = tmp & 0xff; 1792 sc->sc_myaddr[1] = tmp >> 8; 1793 sc->sc_myaddr[2] = tmp >> 16; 1794 sc->sc_myaddr[3] = tmp >> 24; 1795 zyd_cfg_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp); 1796 sc->sc_myaddr[4] = tmp & 0xff; 1797 sc->sc_myaddr[5] = tmp >> 8; 1798 1799 zyd_cfg_read32(sc, ZYD_EEPROM_POD, &tmp); 1800 sc->sc_rf_rev = tmp & 0x0f; 1801 sc->sc_fix_cr47 = (tmp >> 8) & 0x01; 1802 sc->sc_fix_cr157 = (tmp >> 13) & 0x01; 1803 sc->sc_pa_rev = (tmp >> 16) & 0x0f; 1804 1805 /* read regulatory domain (currently unused) */ 1806 zyd_cfg_read32(sc, ZYD_EEPROM_SUBID, &tmp); 1807 sc->sc_regdomain = tmp >> 16; 1808 DPRINTF("regulatory domain %x\n", sc->sc_regdomain); 1809 1810 /* read Tx power calibration tables */ 1811 for (i = 0; i < 7; i++) { 1812 zyd_cfg_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1813 sc->sc_pwr_cal[(i * 2)] = val >> 8; 1814 sc->sc_pwr_cal[(i * 2) + 1] = val & 0xff; 1815 1816 zyd_cfg_read16(sc, ZYD_EEPROM_PWR_INT + i, &val); 1817 sc->sc_pwr_int[(i * 2)] = val >> 8; 1818 sc->sc_pwr_int[(i * 2) + 1] = val & 0xff; 1819 1820 zyd_cfg_read16(sc, ZYD_EEPROM_36M_CAL + i, &val); 1821 sc->sc_ofdm36_cal[(i * 2)] = val >> 8; 1822 sc->sc_ofdm36_cal[(i * 2) + 1] = val & 0xff; 1823 1824 zyd_cfg_read16(sc, ZYD_EEPROM_48M_CAL + i, &val); 1825 sc->sc_ofdm48_cal[(i * 2)] = val >> 8; 1826 sc->sc_ofdm48_cal[(i * 2) + 1] = val & 0xff; 1827 1828 zyd_cfg_read16(sc, ZYD_EEPROM_54M_CAL + i, &val); 1829 sc->sc_ofdm54_cal[(i * 2)] = val >> 8; 1830 sc->sc_ofdm54_cal[(i * 2) + 1] = val & 0xff; 1831 } 1832 return; 1833} 1834 1835static void 1836zyd_cfg_set_mac_addr(struct zyd_softc *sc, const uint8_t *addr) 1837{ 1838 uint32_t tmp; 1839 1840 tmp = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 1841 zyd_cfg_write32(sc, ZYD_MAC_MACADRL, tmp); 1842 1843 tmp = (addr[5] << 8) | addr[4]; 1844 zyd_cfg_write32(sc, ZYD_MAC_MACADRH, tmp); 1845 return; 1846} 1847 1848/* 1849 * Switch radio on/off 1850 */ 1851static void 1852zyd_cfg_switch_radio(struct zyd_softc *sc, uint8_t onoff) 1853{ 1854 zyd_cfg_lock_phy(sc); 1855 (sc->sc_rf.cfg_switch_radio) (sc, onoff); 1856 zyd_cfg_unlock_phy(sc); 1857 1858 return; 1859} 1860 1861/* 1862 * Set BSSID 1863 */ 1864static void 1865zyd_cfg_set_bssid(struct zyd_softc *sc, uint8_t *addr) 1866{ 1867 uint32_t tmp; 1868 1869 tmp = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 1870 zyd_cfg_write32(sc, ZYD_MAC_BSSADRL, tmp); 1871 1872 tmp = (addr[5] << 8) | addr[4]; 1873 zyd_cfg_write32(sc, ZYD_MAC_BSSADRH, tmp); 1874 return; 1875} 1876 1877/* 1878 * Complete the attach process 1879 */ 1880static void 1881zyd_cfg_first_time_setup(struct zyd_softc *sc, 1882 struct usb2_config_td_cc *cc, uint16_t refcount) 1883{ 1884 struct usb2_config_descriptor *cd; 1885 struct ieee80211com *ic; 1886 struct ifnet *ifp; 1887 const uint8_t *fw_ptr; 1888 uint32_t fw_len; 1889 uint8_t bands; 1890 usb2_error_t err; 1891 1892 /* setup RX tap header */ 1893 sc->sc_rxtap_len = sizeof(sc->sc_rxtap); 1894 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 1895 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT); 1896 1897 /* setup TX tap header */ 1898 sc->sc_txtap_len = sizeof(sc->sc_txtap); 1899 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 1900 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT); 1901 1902 if (sc->sc_mac_rev == ZYD_ZD1211) { 1903 fw_ptr = zd1211_firmware; 1904 fw_len = sizeof(zd1211_firmware); 1905 } else { 1906 fw_ptr = zd1211b_firmware; 1907 fw_len = sizeof(zd1211b_firmware); 1908 } 1909 1910 if (zyd_cfg_uploadfirmware(sc, fw_ptr, fw_len)) { 1911 DPRINTFN(0, "%s: could not " 1912 "upload firmware!\n", sc->sc_name); 1913 return; 1914 } 1915 cd = usb2_get_config_descriptor(sc->sc_udev); 1916 1917 /* reset device */ 1918 err = usb2_req_set_config(sc->sc_udev, &sc->sc_mtx, 1919 cd->bConfigurationValue); 1920 if (err) { 1921 DPRINTF("reset failed (ignored)\n"); 1922 } 1923 /* Read MAC and other stuff rom EEPROM */ 1924 zyd_cfg_read_eeprom(sc); 1925 1926 /* Init hardware */ 1927 if (zyd_cfg_hw_init(sc)) { 1928 DPRINTFN(0, "%s: HW init failed!\n", sc->sc_name); 1929 return; 1930 } 1931 /* Now init the RF chip */ 1932 if (zyd_cfg_rf_init_hw(sc, &sc->sc_rf)) { 1933 DPRINTFN(0, "%s: RF init failed!\n", sc->sc_name); 1934 return; 1935 } 1936 printf("%s: HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %02x:%02x:%02x:%02x:%02x:%02x\n", 1937 sc->sc_name, (sc->sc_mac_rev == ZYD_ZD1211) ? "" : "B", 1938 sc->sc_fw_rev >> 8, sc->sc_fw_rev & 0xff, zyd_rf_name(sc->sc_rf_rev), 1939 sc->sc_pa_rev, sc->sc_myaddr[0], 1940 sc->sc_myaddr[1], sc->sc_myaddr[2], 1941 sc->sc_myaddr[3], sc->sc_myaddr[4], 1942 sc->sc_myaddr[5]); 1943 1944 mtx_unlock(&sc->sc_mtx); 1945 1946 ifp = if_alloc(IFT_IEEE80211); 1947 1948 mtx_lock(&sc->sc_mtx); 1949 1950 if (ifp == NULL) { 1951 DPRINTFN(0, "%s: could not if_alloc()!\n", 1952 sc->sc_name); 1953 goto done; 1954 } 1955 sc->sc_evilhack = ifp; 1956 sc->sc_ifp = ifp; 1957 ic = ifp->if_l2com; 1958 1959 ifp->if_softc = sc; 1960 if_initname(ifp, "zyd", sc->sc_unit); 1961 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1962 ifp->if_init = &zyd_init_cb; 1963 ifp->if_ioctl = &zyd_ioctl_cb; 1964 ifp->if_start = &zyd_start_cb; 1965 ifp->if_watchdog = NULL; 1966 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 1967 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 1968 IFQ_SET_READY(&ifp->if_snd); 1969 1970 bcopy(sc->sc_myaddr, ic->ic_myaddr, sizeof(ic->ic_myaddr)); 1971 1972 ic->ic_ifp = ifp; 1973 ic->ic_phytype = IEEE80211_T_OFDM; 1974 ic->ic_opmode = IEEE80211_M_STA; 1975 1976 /* Set device capabilities */ 1977 ic->ic_caps = 1978 IEEE80211_C_STA /* station mode supported */ 1979 | IEEE80211_C_MONITOR /* monitor mode */ 1980 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 1981 | IEEE80211_C_SHSLOT /* short slot time supported */ 1982 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 1983 | IEEE80211_C_WPA /* 802.11i */ 1984 ; 1985 1986 bands = 0; 1987 setbit(&bands, IEEE80211_MODE_11B); 1988 setbit(&bands, IEEE80211_MODE_11G); 1989 ieee80211_init_channels(ic, NULL, &bands); 1990 1991 mtx_unlock(&sc->sc_mtx); 1992 1993 ieee80211_ifattach(ic); 1994 1995 mtx_lock(&sc->sc_mtx); 1996 1997 ic->ic_node_alloc = &zyd_node_alloc_cb; 1998 ic->ic_raw_xmit = &zyd_raw_xmit_cb; 1999 ic->ic_newassoc = &zyd_newassoc_cb; 2000 2001 ic->ic_scan_start = &zyd_scan_start_cb; 2002 ic->ic_scan_end = &zyd_scan_end_cb; 2003 ic->ic_set_channel = &zyd_set_channel_cb; 2004 ic->ic_vap_create = &zyd_vap_create; 2005 ic->ic_vap_delete = &zyd_vap_delete; 2006 ic->ic_update_mcast = &zyd_update_mcast_cb; 2007 ic->ic_update_promisc = &zyd_update_promisc_cb; 2008 2009 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan); 2010 2011 mtx_unlock(&sc->sc_mtx); 2012 2013 bpfattach(ifp, DLT_IEEE802_11_RADIO, 2014 sizeof(struct ieee80211_frame) + 2015 sizeof(sc->sc_txtap)); 2016 2017 mtx_lock(&sc->sc_mtx); 2018 2019 if (bootverbose) { 2020 ieee80211_announce(ic); 2021 } 2022 usb2_transfer_start(sc->sc_xfer[ZYD_TR_INTR_DT_RD]); 2023done: 2024 return; 2025} 2026 2027/* 2028 * Detach device 2029 */ 2030static int 2031zyd_detach(device_t dev) 2032{ 2033 struct zyd_softc *sc = device_get_softc(dev); 2034 struct ieee80211com *ic; 2035 struct ifnet *ifp; 2036 2037 usb2_config_td_drain(&sc->sc_config_td); 2038 2039 mtx_lock(&sc->sc_mtx); 2040 2041 usb2_callout_stop(&sc->sc_watchdog); 2042 2043 zyd_cfg_pre_stop(sc, NULL, 0); 2044 2045 ifp = sc->sc_ifp; 2046 ic = ifp->if_l2com; 2047 2048 mtx_unlock(&sc->sc_mtx); 2049 2050 /* stop all USB transfers first */ 2051 usb2_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER); 2052 2053 /* get rid of any late children */ 2054 bus_generic_detach(dev); 2055 2056 if (ifp) { 2057 bpfdetach(ifp); 2058 ieee80211_ifdetach(ic); 2059 if_free(ifp); 2060 } 2061 usb2_config_td_unsetup(&sc->sc_config_td); 2062 2063 usb2_callout_drain(&sc->sc_watchdog); 2064 2065 usb2_cv_destroy(&sc->sc_intr_cv); 2066 2067 mtx_destroy(&sc->sc_mtx); 2068 2069 return (0); 2070} 2071 2072static void 2073zyd_cfg_newstate(struct zyd_softc *sc, 2074 struct usb2_config_td_cc *cc, uint16_t refcount) 2075{ 2076 struct ifnet *ifp = sc->sc_ifp; 2077 struct ieee80211com *ic = ifp->if_l2com; 2078 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2079 struct zyd_vap *uvp = ZYD_VAP(vap); 2080 enum ieee80211_state ostate; 2081 enum ieee80211_state nstate; 2082 int arg; 2083 2084 ostate = vap->iv_state; 2085 nstate = sc->sc_ns_state; 2086 arg = sc->sc_ns_arg; 2087 2088 switch (nstate) { 2089 case IEEE80211_S_INIT: 2090 break; 2091 2092 case IEEE80211_S_RUN: 2093 zyd_cfg_set_run(sc, cc); 2094 break; 2095 2096 default: 2097 break; 2098 } 2099 2100 mtx_unlock(&sc->sc_mtx); 2101 IEEE80211_LOCK(ic); 2102 uvp->newstate(vap, nstate, arg); 2103 if (vap->iv_newstate_cb != NULL) 2104 vap->iv_newstate_cb(vap, nstate, arg); 2105 IEEE80211_UNLOCK(ic); 2106 mtx_lock(&sc->sc_mtx); 2107 return; 2108} 2109 2110static void 2111zyd_cfg_set_run(struct zyd_softc *sc, 2112 struct usb2_config_td_cc *cc) 2113{ 2114 zyd_cfg_set_chan(sc, cc, 0); 2115 2116 if (cc->ic_opmode != IEEE80211_M_MONITOR) { 2117 /* turn link LED on */ 2118 zyd_cfg_set_led(sc, ZYD_LED1, 1); 2119 2120 /* make data LED blink upon Tx */ 2121 zyd_cfg_write32(sc, sc->sc_firmware_base + ZYD_FW_LINK_STATUS, 1); 2122 2123 zyd_cfg_set_bssid(sc, cc->iv_bss.ni_bssid); 2124 } 2125 if (cc->iv_bss.fixed_rate_none) { 2126 /* enable automatic rate adaptation */ 2127 zyd_cfg_amrr_start(sc); 2128 } 2129 return; 2130} 2131 2132static int 2133zyd_newstate_cb(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2134{ 2135 struct zyd_vap *uvp = ZYD_VAP(vap); 2136 struct ieee80211com *ic = vap->iv_ic; 2137 struct zyd_softc *sc = ic->ic_ifp->if_softc; 2138 2139 DPRINTF("setting new state: %d\n", nstate); 2140 2141 mtx_lock(&sc->sc_mtx); 2142 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 2143 mtx_unlock(&sc->sc_mtx); 2144 /* Special case which happens at detach. */ 2145 if (nstate == IEEE80211_S_INIT) { 2146 (uvp->newstate) (vap, nstate, arg); 2147 } 2148 return (0); /* nothing to do */ 2149 } 2150 /* store next state */ 2151 sc->sc_ns_state = nstate; 2152 sc->sc_ns_arg = arg; 2153 2154 /* stop timers */ 2155 sc->sc_amrr_timer = 0; 2156 2157 /* 2158 * USB configuration can only be done from the USB configuration 2159 * thread: 2160 */ 2161 usb2_config_td_queue_command 2162 (&sc->sc_config_td, &zyd_config_copy, 2163 &zyd_cfg_newstate, 0, 0); 2164 2165 mtx_unlock(&sc->sc_mtx); 2166 2167 return EINPROGRESS; 2168} 2169 2170static void 2171zyd_cfg_update_promisc(struct zyd_softc *sc, 2172 struct usb2_config_td_cc *cc, uint16_t refcount) 2173{ 2174 uint32_t low; 2175 uint32_t high; 2176 2177 if ((cc->ic_opmode == IEEE80211_M_MONITOR) || 2178 (cc->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) { 2179 low = 0xffffffff; 2180 high = 0xffffffff; 2181 } else { 2182 low = cc->zyd_multi_low; 2183 high = cc->zyd_multi_high; 2184 } 2185 2186 /* reprogram multicast global hash table */ 2187 zyd_cfg_write32(sc, ZYD_MAC_GHTBL, low); 2188 zyd_cfg_write32(sc, ZYD_MAC_GHTBH, high); 2189 return; 2190} 2191 2192/* 2193 * Rate-to-bit-converter (Field "rate" in zyd_controlsetformat) 2194 */ 2195static uint8_t 2196zyd_plcp_signal(uint8_t rate) 2197{ 2198 ; /* fix for indent */ 2199 2200 switch (rate) { 2201 /* CCK rates (NB: not IEEE std, device-specific) */ 2202 case 2: 2203 return (0x0); 2204 case 4: 2205 return (0x1); 2206 case 11: 2207 return (0x2); 2208 case 22: 2209 return (0x3); 2210 2211 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 2212 case 12: 2213 return (0xb); 2214 case 18: 2215 return (0xf); 2216 case 24: 2217 return (0xa); 2218 case 36: 2219 return (0xe); 2220 case 48: 2221 return (0x9); 2222 case 72: 2223 return (0xd); 2224 case 96: 2225 return (0x8); 2226 case 108: 2227 return (0xc); 2228 2229 /* XXX unsupported/unknown rate */ 2230 default: 2231 return (0xff); 2232 } 2233} 2234 2235static void 2236zyd_std_command(struct ieee80211com *ic, usb2_config_td_command_t *func) 2237{ 2238 struct zyd_softc *sc = ic->ic_ifp->if_softc; 2239 2240 mtx_lock(&sc->sc_mtx); 2241 2242 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan); 2243 2244 usb2_config_td_queue_command 2245 (&sc->sc_config_td, &zyd_config_copy, func, 0, 0); 2246 2247 mtx_unlock(&sc->sc_mtx); 2248 2249 return; 2250} 2251 2252static void 2253zyd_scan_start_cb(struct ieee80211com *ic) 2254{ 2255 zyd_std_command(ic, &zyd_cfg_scan_start); 2256 return; 2257} 2258 2259static void 2260zyd_scan_end_cb(struct ieee80211com *ic) 2261{ 2262 zyd_std_command(ic, &zyd_cfg_scan_end); 2263 return; 2264} 2265 2266static void 2267zyd_set_channel_cb(struct ieee80211com *ic) 2268{ 2269 zyd_std_command(ic, &zyd_cfg_set_chan); 2270 return; 2271} 2272 2273/*========================================================================* 2274 * configure sub-routines, zyd_cfg_xxx 2275 *========================================================================*/ 2276 2277static void 2278zyd_cfg_scan_start(struct zyd_softc *sc, 2279 struct usb2_config_td_cc *cc, uint16_t refcount) 2280{ 2281 zyd_cfg_set_bssid(sc, cc->if_broadcastaddr); 2282 return; 2283} 2284 2285static void 2286zyd_cfg_scan_end(struct zyd_softc *sc, 2287 struct usb2_config_td_cc *cc, uint16_t refcount) 2288{ 2289 zyd_cfg_set_bssid(sc, cc->iv_bss.ni_bssid); 2290 return; 2291} 2292 2293static void 2294zyd_cfg_set_chan(struct zyd_softc *sc, 2295 struct usb2_config_td_cc *cc, uint16_t refcount) 2296{ 2297 uint32_t chan; 2298 uint32_t tmp; 2299 2300 chan = cc->ic_curchan.chan_to_ieee; 2301 2302 DPRINTF("Will try %d\n", chan); 2303 2304 if ((chan == 0) || (chan == IEEE80211_CHAN_ANY)) { 2305 DPRINTF("0 or ANY, exiting\n"); 2306 return; 2307 } 2308 zyd_cfg_lock_phy(sc); 2309 2310 (sc->sc_rf.cfg_set_channel) (sc, &sc->sc_rf, chan); 2311 2312 /* update Tx power */ 2313 zyd_cfg_write16(sc, ZYD_CR31, sc->sc_pwr_int[chan - 1]); 2314 2315 if (sc->sc_mac_rev == ZYD_ZD1211B) { 2316 zyd_cfg_write16(sc, ZYD_CR67, sc->sc_ofdm36_cal[chan - 1]); 2317 zyd_cfg_write16(sc, ZYD_CR66, sc->sc_ofdm48_cal[chan - 1]); 2318 zyd_cfg_write16(sc, ZYD_CR65, sc->sc_ofdm54_cal[chan - 1]); 2319 2320 zyd_cfg_write16(sc, ZYD_CR68, sc->sc_pwr_cal[chan - 1]); 2321 2322 zyd_cfg_write16(sc, ZYD_CR69, 0x28); 2323 zyd_cfg_write16(sc, ZYD_CR69, 0x2a); 2324 } 2325 if (sc->sc_fix_cr47) { 2326 /* set CCK baseband gain from EEPROM */ 2327 zyd_cfg_read32(sc, ZYD_EEPROM_PHY_REG, &tmp); 2328 zyd_cfg_write16(sc, ZYD_CR47, tmp & 0xff); 2329 } 2330 zyd_cfg_write32(sc, ZYD_CR_CONFIG_PHILIPS, 0); 2331 2332 zyd_cfg_unlock_phy(sc); 2333 2334 sc->sc_rxtap.wr_chan_freq = 2335 sc->sc_txtap.wt_chan_freq = 2336 htole16(cc->ic_curchan.ic_freq); 2337 2338 sc->sc_rxtap.wr_chan_flags = 2339 sc->sc_txtap.wt_chan_flags = 2340 htole16(cc->ic_flags); 2341 2342 return; 2343} 2344 2345/* 2346 * Interface: init 2347 */ 2348 2349/* immediate configuration */ 2350 2351static void 2352zyd_cfg_pre_init(struct zyd_softc *sc, 2353 struct usb2_config_td_cc *cc, uint16_t refcount) 2354{ 2355 struct ifnet *ifp = sc->sc_ifp; 2356 struct ieee80211com *ic = ifp->if_l2com; 2357 2358 zyd_cfg_pre_stop(sc, cc, 0); 2359 2360 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2361 2362 sc->sc_flags |= ZYD_FLAG_HL_READY; 2363 2364 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2365 2366 return; 2367} 2368 2369/* delayed configuration */ 2370 2371static void 2372zyd_cfg_init(struct zyd_softc *sc, 2373 struct usb2_config_td_cc *cc, uint16_t refcount) 2374{ 2375 zyd_cfg_stop(sc, cc, 0); 2376 2377 /* Do initial setup */ 2378 2379 zyd_cfg_set_mac_addr(sc, cc->ic_myaddr); 2380 2381 zyd_cfg_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER); 2382 2383 /* promiscuous mode */ 2384 zyd_cfg_write32(sc, ZYD_MAC_SNIFFER, 2385 (cc->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0); 2386 2387 /* multicast setup */ 2388 zyd_cfg_update_promisc(sc, cc, refcount); 2389 2390 zyd_cfg_set_rxfilter(sc, cc, refcount); 2391 2392 /* switch radio transmitter ON */ 2393 zyd_cfg_switch_radio(sc, 1); 2394 2395 /* XXX wrong, can't set here */ 2396 /* set basic rates */ 2397 if (cc->ic_curmode == IEEE80211_MODE_11B) 2398 zyd_cfg_write32(sc, ZYD_MAC_BAS_RATE, 0x0003); 2399 else if (cc->ic_curmode == IEEE80211_MODE_11A) 2400 zyd_cfg_write32(sc, ZYD_MAC_BAS_RATE, 0x1500); 2401 else /* assumes 802.11b/g */ 2402 zyd_cfg_write32(sc, ZYD_MAC_BAS_RATE, 0x000f); 2403 2404 /* set mandatory rates */ 2405 if (cc->ic_curmode == IEEE80211_MODE_11B) 2406 zyd_cfg_write32(sc, ZYD_MAC_MAN_RATE, 0x000f); 2407 else if (cc->ic_curmode == IEEE80211_MODE_11A) 2408 zyd_cfg_write32(sc, ZYD_MAC_MAN_RATE, 0x1500); 2409 else /* assumes 802.11b/g */ 2410 zyd_cfg_write32(sc, ZYD_MAC_MAN_RATE, 0x150f); 2411 2412 /* set default BSS channel */ 2413 zyd_cfg_set_chan(sc, cc, 0); 2414 2415 /* enable interrupts */ 2416 zyd_cfg_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK); 2417 2418 /* make sure that the transfers get started */ 2419 sc->sc_flags |= ( 2420 ZYD_FLAG_BULK_READ_STALL | 2421 ZYD_FLAG_BULK_WRITE_STALL | 2422 ZYD_FLAG_LL_READY); 2423 2424 if ((sc->sc_flags & ZYD_FLAG_LL_READY) && 2425 (sc->sc_flags & ZYD_FLAG_HL_READY)) { 2426 struct ifnet *ifp = sc->sc_ifp; 2427 struct ieee80211com *ic = ifp->if_l2com; 2428 2429 /* 2430 * start the USB transfers, if not already started: 2431 */ 2432 usb2_transfer_start(sc->sc_xfer[1]); 2433 usb2_transfer_start(sc->sc_xfer[0]); 2434 2435 /* 2436 * start IEEE802.11 layer 2437 */ 2438 mtx_unlock(&sc->sc_mtx); 2439 ieee80211_start_all(ic); 2440 mtx_lock(&sc->sc_mtx); 2441 } 2442 return; 2443} 2444 2445/* immediate configuration */ 2446 2447static void 2448zyd_cfg_pre_stop(struct zyd_softc *sc, 2449 struct usb2_config_td_cc *cc, uint16_t refcount) 2450{ 2451 struct ifnet *ifp = sc->sc_ifp; 2452 2453 if (cc) { 2454 /* copy the needed configuration */ 2455 zyd_config_copy(sc, cc, refcount); 2456 } 2457 if (ifp) { 2458 /* clear flags */ 2459 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2460 } 2461 sc->sc_flags &= ~(ZYD_FLAG_HL_READY | 2462 ZYD_FLAG_LL_READY); 2463 2464 /* 2465 * stop all the transfers, if not already stopped: 2466 */ 2467 usb2_transfer_stop(sc->sc_xfer[ZYD_TR_BULK_DT_WR]); 2468 usb2_transfer_stop(sc->sc_xfer[ZYD_TR_BULK_DT_RD]); 2469 usb2_transfer_stop(sc->sc_xfer[ZYD_TR_BULK_CS_WR]); 2470 usb2_transfer_stop(sc->sc_xfer[ZYD_TR_BULK_CS_RD]); 2471 2472 /* clean up transmission */ 2473 zyd_tx_clean_queue(sc); 2474 return; 2475} 2476 2477/* delayed configuration */ 2478 2479static void 2480zyd_cfg_stop(struct zyd_softc *sc, 2481 struct usb2_config_td_cc *cc, uint16_t refcount) 2482{ 2483 /* switch radio transmitter OFF */ 2484 zyd_cfg_switch_radio(sc, 0); 2485 2486 /* disable Rx */ 2487 zyd_cfg_write32(sc, ZYD_MAC_RXFILTER, 0); 2488 2489 /* disable interrupts */ 2490 zyd_cfg_write32(sc, ZYD_CR_INTERRUPT, 0); 2491 2492 return; 2493} 2494 2495static void 2496zyd_update_mcast_cb(struct ifnet *ifp) 2497{ 2498 struct zyd_softc *sc = ifp->if_softc; 2499 2500 mtx_lock(&sc->sc_mtx); 2501 usb2_config_td_queue_command 2502 (&sc->sc_config_td, &zyd_config_copy, 2503 &zyd_cfg_update_promisc, 0, 0); 2504 mtx_unlock(&sc->sc_mtx); 2505 return; 2506} 2507 2508static void 2509zyd_update_promisc_cb(struct ifnet *ifp) 2510{ 2511 struct zyd_softc *sc = ifp->if_softc; 2512 2513 mtx_lock(&sc->sc_mtx); 2514 usb2_config_td_queue_command 2515 (&sc->sc_config_td, &zyd_config_copy, 2516 &zyd_cfg_update_promisc, 0, 0); 2517 mtx_unlock(&sc->sc_mtx); 2518 return; 2519} 2520 2521static void 2522zyd_cfg_set_rxfilter(struct zyd_softc *sc, 2523 struct usb2_config_td_cc *cc, uint16_t refcount) 2524{ 2525 uint32_t rxfilter; 2526 2527 switch (cc->ic_opmode) { 2528 case IEEE80211_M_STA: 2529 rxfilter = ZYD_FILTER_BSS; 2530 break; 2531 case IEEE80211_M_IBSS: 2532 case IEEE80211_M_HOSTAP: 2533 rxfilter = ZYD_FILTER_HOSTAP; 2534 break; 2535 case IEEE80211_M_MONITOR: 2536 rxfilter = ZYD_FILTER_MONITOR; 2537 break; 2538 default: 2539 /* should not get there */ 2540 return; 2541 } 2542 zyd_cfg_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 2543 return; 2544} 2545 2546static void 2547zyd_cfg_set_led(struct zyd_softc *sc, uint32_t which, uint8_t on) 2548{ 2549 uint32_t tmp; 2550 2551 zyd_cfg_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 2552 if (on) 2553 tmp |= which; 2554 else 2555 tmp &= ~which; 2556 2557 zyd_cfg_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 2558 return; 2559} 2560 2561static void 2562zyd_start_cb(struct ifnet *ifp) 2563{ 2564 struct zyd_softc *sc = ifp->if_softc; 2565 2566 mtx_lock(&sc->sc_mtx); 2567 usb2_transfer_start(sc->sc_xfer[ZYD_TR_BULK_DT_WR]); 2568 mtx_unlock(&sc->sc_mtx); 2569 return; 2570} 2571 2572static void 2573zyd_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 2574{ 2575 struct zyd_softc *sc = xfer->priv_sc; 2576 struct usb2_xfer *xfer_other = sc->sc_xfer[ZYD_TR_BULK_DT_WR]; 2577 2578 if (usb2_clear_stall_callback(xfer, xfer_other)) { 2579 DPRINTF("stall cleared\n"); 2580 sc->sc_flags &= ~ZYD_FLAG_BULK_WRITE_STALL; 2581 usb2_transfer_start(xfer_other); 2582 } 2583 return; 2584} 2585 2586/* 2587 * We assume that "m->m_pkthdr.rcvif" is pointing to the "ni" that 2588 * should be freed, when "zyd_setup_desc_and_tx" is called. 2589 */ 2590static void 2591zyd_setup_desc_and_tx(struct zyd_softc *sc, struct mbuf *m, 2592 uint16_t rate) 2593{ 2594 struct ifnet *ifp = sc->sc_ifp; 2595 struct ieee80211com *ic = ifp->if_l2com; 2596 struct mbuf *mm; 2597 enum ieee80211_phytype phytype; 2598 uint16_t len; 2599 uint16_t totlen; 2600 uint16_t pktlen; 2601 uint8_t remainder; 2602 2603 if (sc->sc_tx_queue.ifq_len >= IFQ_MAXLEN) { 2604 /* free packet */ 2605 zyd_tx_freem(m); 2606 ifp->if_oerrors++; 2607 return; 2608 } 2609 if (!((sc->sc_flags & ZYD_FLAG_LL_READY) && 2610 (sc->sc_flags & ZYD_FLAG_HL_READY))) { 2611 /* free packet */ 2612 zyd_tx_freem(m); 2613 ifp->if_oerrors++; 2614 return; 2615 } 2616 if (rate < 2) { 2617 DPRINTF("rate < 2!\n"); 2618 2619 /* avoid division by zero */ 2620 rate = 2; 2621 } 2622 ic->ic_lastdata = ticks; 2623 2624 if (bpf_peers_present(ifp->if_bpf)) { 2625 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap; 2626 2627 tap->wt_flags = 0; 2628 tap->wt_rate = rate; 2629 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2630 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2631 2632 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m); 2633 } 2634 len = m->m_pkthdr.len; 2635 totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 2636 phytype = ieee80211_rate2phytype(sc->sc_rates, rate); 2637 2638 sc->sc_tx_desc.len = htole16(totlen); 2639 sc->sc_tx_desc.phy = zyd_plcp_signal(rate); 2640 if (phytype == IEEE80211_T_OFDM) { 2641 sc->sc_tx_desc.phy |= ZYD_TX_PHY_OFDM; 2642 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 2643 sc->sc_tx_desc.phy |= ZYD_TX_PHY_5GHZ; 2644 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2645 sc->sc_tx_desc.phy |= ZYD_TX_PHY_SHPREAMBLE; 2646 2647 /* actual transmit length (XXX why +10?) */ 2648 pktlen = sizeof(struct zyd_tx_desc) + 10; 2649 if (sc->sc_mac_rev == ZYD_ZD1211) 2650 pktlen += totlen; 2651 sc->sc_tx_desc.pktlen = htole16(pktlen); 2652 2653 sc->sc_tx_desc.plcp_length = ((16 * totlen) + rate - 1) / rate; 2654 sc->sc_tx_desc.plcp_service = 0; 2655 if (rate == 22) { 2656 remainder = (16 * totlen) % 22; 2657 if ((remainder != 0) && (remainder < 7)) 2658 sc->sc_tx_desc.plcp_service |= ZYD_PLCP_LENGEXT; 2659 } 2660 if (sizeof(sc->sc_tx_desc) > MHLEN) { 2661 DPRINTF("No room for header structure!\n"); 2662 zyd_tx_freem(m); 2663 return; 2664 } 2665 mm = m_gethdr(M_NOWAIT, MT_DATA); 2666 if (mm == NULL) { 2667 DPRINTF("Could not allocate header mbuf!\n"); 2668 zyd_tx_freem(m); 2669 return; 2670 } 2671 bcopy(&sc->sc_tx_desc, mm->m_data, sizeof(sc->sc_tx_desc)); 2672 mm->m_len = sizeof(sc->sc_tx_desc); 2673 2674 mm->m_next = m; 2675 mm->m_pkthdr.len = mm->m_len + m->m_pkthdr.len; 2676 mm->m_pkthdr.rcvif = NULL; 2677 2678 /* start write transfer, if not started */ 2679 _IF_ENQUEUE(&sc->sc_tx_queue, mm); 2680 2681 usb2_transfer_start(sc->sc_xfer[0]); 2682 return; 2683} 2684 2685static void 2686zyd_bulk_write_callback(struct usb2_xfer *xfer) 2687{ 2688 struct zyd_softc *sc = xfer->priv_sc; 2689 struct ifnet *ifp = sc->sc_ifp; 2690 struct mbuf *m; 2691 uint16_t temp_len; 2692 2693 DPRINTF("\n"); 2694 2695 switch (USB_GET_STATE(xfer)) { 2696 case USB_ST_TRANSFERRED: 2697 DPRINTFN(11, "transfer complete\n"); 2698 2699 ifp->if_opackets++; 2700 2701 case USB_ST_SETUP: 2702 if (sc->sc_flags & ZYD_FLAG_BULK_WRITE_STALL) { 2703 usb2_transfer_start(sc->sc_xfer[ZYD_TR_BULK_CS_WR]); 2704 DPRINTFN(11, "write stalled\n"); 2705 break; 2706 } 2707 if (sc->sc_flags & ZYD_FLAG_WAIT_COMMAND) { 2708 /* 2709 * don't send anything while a command is pending ! 2710 */ 2711 DPRINTFN(11, "wait command\n"); 2712 break; 2713 } 2714 zyd_fill_write_queue(sc); 2715 2716 _IF_DEQUEUE(&sc->sc_tx_queue, m); 2717 2718 if (m) { 2719 if (m->m_pkthdr.len > ZYD_MAX_TXBUFSZ) { 2720 DPRINTFN(0, "data overflow, %u bytes\n", 2721 m->m_pkthdr.len); 2722 m->m_pkthdr.len = ZYD_MAX_TXBUFSZ; 2723 } 2724 usb2_m_copy_in(xfer->frbuffers, 0, 2725 m, 0, m->m_pkthdr.len); 2726 2727 /* get transfer length */ 2728 temp_len = m->m_pkthdr.len; 2729 2730 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 2731 m->m_pkthdr.len, temp_len); 2732 2733 xfer->frlengths[0] = temp_len; 2734 2735 usb2_start_hardware(xfer); 2736 2737 /* free mbuf and node */ 2738 zyd_tx_freem(m); 2739 } 2740 break; 2741 2742 default: /* Error */ 2743 DPRINTFN(11, "transfer error, %s\n", 2744 usb2_errstr(xfer->error)); 2745 2746 if (xfer->error != USB_ERR_CANCELLED) { 2747 /* try to clear stall first */ 2748 sc->sc_flags |= ZYD_FLAG_BULK_WRITE_STALL; 2749 usb2_transfer_start(sc->sc_xfer[ZYD_TR_BULK_CS_WR]); 2750 } 2751 ifp->if_oerrors++; 2752 break; 2753 } 2754 return; 2755} 2756 2757static void 2758zyd_init_cb(void *arg) 2759{ 2760 struct zyd_softc *sc = arg; 2761 2762 mtx_lock(&sc->sc_mtx); 2763 usb2_config_td_queue_command 2764 (&sc->sc_config_td, &zyd_cfg_pre_init, 2765 &zyd_cfg_init, 0, 0); 2766 mtx_unlock(&sc->sc_mtx); 2767 2768 return; 2769} 2770 2771static int 2772zyd_ioctl_cb(struct ifnet *ifp, u_long cmd, caddr_t data) 2773{ 2774 struct zyd_softc *sc = ifp->if_softc; 2775 struct ieee80211com *ic = ifp->if_l2com; 2776 int error; 2777 2778 switch (cmd) { 2779 case SIOCSIFFLAGS: 2780 mtx_lock(&sc->sc_mtx); 2781 if (ifp->if_flags & IFF_UP) { 2782 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2783 usb2_config_td_queue_command 2784 (&sc->sc_config_td, &zyd_cfg_pre_init, 2785 &zyd_cfg_init, 0, 0); 2786 } 2787 } else { 2788 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2789 usb2_config_td_queue_command 2790 (&sc->sc_config_td, &zyd_cfg_pre_stop, 2791 &zyd_cfg_stop, 0, 0); 2792 } 2793 } 2794 mtx_unlock(&sc->sc_mtx); 2795 error = 0; 2796 break; 2797 2798 case SIOCGIFMEDIA: 2799 case SIOCADDMULTI: 2800 case SIOCDELMULTI: 2801 error = ifmedia_ioctl(ifp, (void *)data, &ic->ic_media, cmd); 2802 break; 2803 2804 default: 2805 error = ether_ioctl(ifp, cmd, data); 2806 break; 2807 } 2808 return (error); 2809} 2810 2811static void 2812zyd_watchdog(void *arg) 2813{ 2814 struct zyd_softc *sc = arg; 2815 2816 mtx_assert(&sc->sc_mtx, MA_OWNED); 2817 2818 if (sc->sc_amrr_timer) { 2819 usb2_config_td_queue_command 2820 (&sc->sc_config_td, NULL, 2821 &zyd_cfg_amrr_timeout, 0, 0); 2822 } 2823 usb2_callout_reset(&sc->sc_watchdog, 2824 hz, &zyd_watchdog, sc); 2825 2826 mtx_unlock(&sc->sc_mtx); 2827 2828 return; 2829} 2830 2831static void 2832zyd_config_copy_chan(struct zyd_config_copy_chan *cc, 2833 struct ieee80211com *ic, struct ieee80211_channel *c) 2834{ 2835 if (!c) 2836 return; 2837 cc->chan_to_ieee = 2838 ieee80211_chan2ieee(ic, c); 2839 if (c != IEEE80211_CHAN_ANYC) { 2840 cc->chan_to_mode = 2841 ieee80211_chan2mode(c); 2842 cc->ic_freq = c->ic_freq; 2843 if (IEEE80211_IS_CHAN_B(c)) 2844 cc->chan_is_b = 1; 2845 if (IEEE80211_IS_CHAN_A(c)) 2846 cc->chan_is_a = 1; 2847 if (IEEE80211_IS_CHAN_2GHZ(c)) 2848 cc->chan_is_2ghz = 1; 2849 if (IEEE80211_IS_CHAN_5GHZ(c)) 2850 cc->chan_is_5ghz = 1; 2851 if (IEEE80211_IS_CHAN_ANYG(c)) 2852 cc->chan_is_g = 1; 2853 } 2854 return; 2855} 2856 2857static void 2858zyd_config_copy(struct zyd_softc *sc, 2859 struct usb2_config_td_cc *cc, uint16_t refcount) 2860{ 2861 const struct ieee80211_txparam *tp; 2862 struct ieee80211vap *vap; 2863 struct ifmultiaddr *ifma; 2864 struct ieee80211_node *ni; 2865 struct ieee80211com *ic; 2866 struct ifnet *ifp; 2867 2868 bzero(cc, sizeof(*cc)); 2869 2870 ifp = sc->sc_ifp; 2871 if (ifp) { 2872 cc->if_flags = ifp->if_flags; 2873 bcopy(ifp->if_broadcastaddr, cc->if_broadcastaddr, 2874 sizeof(cc->if_broadcastaddr)); 2875 2876 cc->zyd_multi_low = 0x00000000; 2877 cc->zyd_multi_high = 0x80000000; 2878 2879 IF_ADDR_LOCK(ifp); 2880 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2881 uint8_t v; 2882 2883 if (ifma->ifma_addr->sa_family != AF_LINK) 2884 continue; 2885 v = ((uint8_t *)LLADDR((struct sockaddr_dl *) 2886 ifma->ifma_addr))[5] >> 2; 2887 if (v < 32) 2888 cc->zyd_multi_low |= 1 << v; 2889 else 2890 cc->zyd_multi_high |= 1 << (v - 32); 2891 } 2892 IF_ADDR_UNLOCK(ifp); 2893 2894 ic = ifp->if_l2com; 2895 if (ic) { 2896 zyd_config_copy_chan(&cc->ic_curchan, ic, ic->ic_curchan); 2897 zyd_config_copy_chan(&cc->ic_bsschan, ic, ic->ic_bsschan); 2898 vap = TAILQ_FIRST(&ic->ic_vaps); 2899 if (vap) { 2900 ni = vap->iv_bss; 2901 if (ni) { 2902 cc->iv_bss.ni_intval = ni->ni_intval; 2903 bcopy(ni->ni_bssid, cc->iv_bss.ni_bssid, 2904 sizeof(cc->iv_bss.ni_bssid)); 2905 } 2906 tp = vap->iv_txparms + cc->ic_bsschan.chan_to_mode; 2907 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 2908 cc->iv_bss.fixed_rate_none = 1; 2909 } 2910 } 2911 cc->ic_opmode = ic->ic_opmode; 2912 cc->ic_flags = ic->ic_flags; 2913 cc->ic_txpowlimit = ic->ic_txpowlimit; 2914 cc->ic_curmode = ic->ic_curmode; 2915 2916 bcopy(ic->ic_myaddr, cc->ic_myaddr, 2917 sizeof(cc->ic_myaddr)); 2918 } 2919 } 2920 sc->sc_flags |= ZYD_FLAG_WAIT_COMMAND; 2921 return; 2922} 2923 2924static void 2925zyd_end_of_commands(struct zyd_softc *sc) 2926{ 2927 sc->sc_flags &= ~ZYD_FLAG_WAIT_COMMAND; 2928 2929 /* start write transfer, if not started */ 2930 usb2_transfer_start(sc->sc_xfer[0]); 2931 return; 2932} 2933 2934static void 2935zyd_newassoc_cb(struct ieee80211_node *ni, int isnew) 2936{ 2937 struct ieee80211vap *vap = ni->ni_vap; 2938 2939 ieee80211_amrr_node_init(&ZYD_VAP(vap)->amrr, &ZYD_NODE(ni)->amn, ni); 2940 return; 2941} 2942 2943static void 2944zyd_cfg_amrr_timeout(struct zyd_softc *sc, 2945 struct usb2_config_td_cc *cc, uint16_t refcount) 2946{ 2947 struct ieee80211vap *vap; 2948 struct ieee80211_node *ni; 2949 2950 vap = zyd_get_vap(sc); 2951 if (vap == NULL) { 2952 return; 2953 } 2954 ni = vap->iv_bss; 2955 if (ni == NULL) { 2956 return; 2957 } 2958 if ((sc->sc_flags & ZYD_FLAG_LL_READY) && 2959 (sc->sc_flags & ZYD_FLAG_HL_READY)) { 2960 2961 if (sc->sc_amrr_timer) { 2962 2963 if (ieee80211_amrr_choose(ni, &ZYD_NODE(ni)->amn)) { 2964 /* ignore */ 2965 } 2966 } 2967 } 2968 return; 2969} 2970 2971static void 2972zyd_cfg_amrr_start(struct zyd_softc *sc) 2973{ 2974 struct ieee80211vap *vap; 2975 struct ieee80211_node *ni; 2976 2977 vap = zyd_get_vap(sc); 2978 2979 if (vap == NULL) { 2980 return; 2981 } 2982 ni = vap->iv_bss; 2983 if (ni == NULL) { 2984 return; 2985 } 2986 /* init AMRR */ 2987 2988 ieee80211_amrr_node_init(&ZYD_VAP(vap)->amrr, &ZYD_NODE(ni)->amn, ni); 2989 2990 /* enable AMRR timer */ 2991 2992 sc->sc_amrr_timer = 1; 2993 return; 2994} 2995 2996static struct ieee80211vap * 2997zyd_vap_create(struct ieee80211com *ic, 2998 const char name[IFNAMSIZ], int unit, int opmode, int flags, 2999 const uint8_t bssid[IEEE80211_ADDR_LEN], 3000 const uint8_t mac[IEEE80211_ADDR_LEN]) 3001{ 3002 struct zyd_vap *zvp; 3003 struct ieee80211vap *vap; 3004 struct zyd_softc *sc = ic->ic_ifp->if_softc; 3005 3006 /* Need to sync with config thread: */ 3007 mtx_lock(&sc->sc_mtx); 3008 if (usb2_config_td_sync(&sc->sc_config_td)) { 3009 mtx_unlock(&sc->sc_mtx); 3010 /* config thread is gone */ 3011 return (NULL); 3012 } 3013 mtx_unlock(&sc->sc_mtx); 3014 3015 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 3016 return NULL; 3017 zvp = (struct zyd_vap *)malloc(sizeof(struct zyd_vap), 3018 M_80211_VAP, M_NOWAIT | M_ZERO); 3019 if (zvp == NULL) 3020 return NULL; 3021 vap = &zvp->vap; 3022 /* enable s/w bmiss handling for sta mode */ 3023 ieee80211_vap_setup(ic, vap, name, unit, opmode, 3024 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 3025 3026 /* override state transition machine */ 3027 zvp->newstate = vap->iv_newstate; 3028 vap->iv_newstate = &zyd_newstate_cb; 3029 3030 ieee80211_amrr_init(&zvp->amrr, vap, 3031 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 3032 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 3033 1000 /* 1 sec */ ); 3034 3035 /* complete setup */ 3036 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 3037 ic->ic_opmode = opmode; 3038 3039 return (vap); 3040} 3041 3042static void 3043zyd_vap_delete(struct ieee80211vap *vap) 3044{ 3045 struct zyd_vap *zvp = ZYD_VAP(vap); 3046 struct zyd_softc *sc = vap->iv_ic->ic_ifp->if_softc; 3047 3048 /* Need to sync with config thread: */ 3049 mtx_lock(&sc->sc_mtx); 3050 if (usb2_config_td_sync(&sc->sc_config_td)) { 3051 /* ignore */ 3052 } 3053 mtx_unlock(&sc->sc_mtx); 3054 3055 ieee80211_amrr_cleanup(&zvp->amrr); 3056 ieee80211_vap_detach(vap); 3057 free(zvp, M_80211_VAP); 3058 return; 3059} 3060 3061/* ARGUSED */ 3062static struct ieee80211_node * 3063zyd_node_alloc_cb(struct ieee80211vap *vap __unused, 3064 const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 3065{ 3066 struct zyd_node *zn; 3067 3068 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO); 3069 return ((zn != NULL) ? &zn->ni : NULL); 3070} 3071 3072static void 3073zyd_fill_write_queue(struct zyd_softc *sc) 3074{ 3075 struct ifnet *ifp = sc->sc_ifp; 3076 struct ieee80211_node *ni; 3077 struct mbuf *m; 3078 3079 /* 3080 * We only fill up half of the queue with data frames. The rest is 3081 * reserved for other kinds of frames. 3082 */ 3083 3084 while (sc->sc_tx_queue.ifq_len < (IFQ_MAXLEN / 2)) { 3085 3086 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 3087 if (m == NULL) 3088 break; 3089 3090 ni = (void *)(m->m_pkthdr.rcvif); 3091 m = ieee80211_encap(ni, m); 3092 if (m == NULL) { 3093 ieee80211_free_node(ni); 3094 continue; 3095 } 3096 zyd_tx_data(sc, m, ni); 3097 } 3098 return; 3099} 3100 3101static void 3102zyd_tx_clean_queue(struct zyd_softc *sc) 3103{ 3104 struct mbuf *m; 3105 3106 for (;;) { 3107 _IF_DEQUEUE(&sc->sc_tx_queue, m); 3108 3109 if (!m) { 3110 break; 3111 } 3112 zyd_tx_freem(m); 3113 } 3114 3115 return; 3116} 3117 3118static void 3119zyd_tx_freem(struct mbuf *m) 3120{ 3121 struct ieee80211_node *ni; 3122 3123 while (m) { 3124 ni = (void *)(m->m_pkthdr.rcvif); 3125 if (!ni) { 3126 m = m_free(m); 3127 continue; 3128 } 3129 if (m->m_flags & M_TXCB) { 3130 ieee80211_process_callback(ni, m, 0); 3131 } 3132 m_freem(m); 3133 ieee80211_free_node(ni); 3134 3135 break; 3136 } 3137 return; 3138} 3139 3140static void 3141zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 3142{ 3143 struct ieee80211vap *vap = ni->ni_vap; 3144 struct ieee80211com *ic = ni->ni_ic; 3145 const struct ieee80211_txparam *tp; 3146 struct ieee80211_frame *wh; 3147 struct ieee80211_key *k; 3148 uint16_t totlen; 3149 uint16_t rate; 3150 3151 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 3152 rate = tp->mgmtrate; 3153 3154 wh = mtod(m, struct ieee80211_frame *); 3155 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 3156 k = ieee80211_crypto_encap(ni, m); 3157 if (k == NULL) { 3158 m_freem(m); 3159 ieee80211_free_node(ni); 3160 return; 3161 } 3162 wh = mtod(m, struct ieee80211_frame *); 3163 } 3164 /* fill Tx descriptor */ 3165 3166 sc->sc_tx_desc.flags = ZYD_TX_FLAG_BACKOFF; 3167 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3168 /* get total length */ 3169 totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3170 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 3171 if (totlen > vap->iv_rtsthreshold) { 3172 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_RTS; 3173 } else if (ZYD_RATE_IS_OFDM(rate) && 3174 (ic->ic_flags & IEEE80211_F_USEPROT)) { 3175 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 3176 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_CTS_TO_SELF; 3177 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 3178 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_RTS; 3179 } 3180 } else 3181 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_MULTICAST; 3182 3183 if ((wh->i_fc[0] & 3184 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 3185 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 3186 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 3187 3188 m->m_pkthdr.rcvif = (void *)ni; 3189 zyd_setup_desc_and_tx(sc, m, rate); 3190 return; 3191} 3192 3193static void 3194zyd_tx_data(struct zyd_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 3195{ 3196 struct ieee80211vap *vap = ni->ni_vap; 3197 struct ieee80211com *ic = ni->ni_ic; 3198 const struct ieee80211_txparam *tp; 3199 struct ieee80211_frame *wh; 3200 struct ieee80211_key *k; 3201 uint16_t rate; 3202 3203 wh = mtod(m, struct ieee80211_frame *); 3204 3205 sc->sc_tx_desc.flags = ZYD_TX_FLAG_BACKOFF; 3206 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 3207 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3208 rate = tp->mcastrate; 3209 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_MULTICAST; 3210 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 3211 rate = tp->ucastrate; 3212 } else 3213 rate = ni->ni_txrate; 3214 3215 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 3216 k = ieee80211_crypto_encap(ni, m); 3217 if (k == NULL) { 3218 m_freem(m); 3219 ieee80211_free_node(ni); 3220 return; 3221 } 3222 /* packet header may have moved, reset our local pointer */ 3223 wh = mtod(m, struct ieee80211_frame *); 3224 } 3225 /* fill Tx descriptor */ 3226 3227 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3228 uint16_t totlen; 3229 3230 totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3231 3232 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 3233 if (totlen > vap->iv_rtsthreshold) { 3234 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_RTS; 3235 } else if (ZYD_RATE_IS_OFDM(rate) && 3236 (ic->ic_flags & IEEE80211_F_USEPROT)) { 3237 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 3238 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_CTS_TO_SELF; 3239 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 3240 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_RTS; 3241 } 3242 } 3243 if ((wh->i_fc[0] & 3244 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 3245 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) 3246 sc->sc_tx_desc.flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL); 3247 3248 m->m_pkthdr.rcvif = (void *)ni; 3249 zyd_setup_desc_and_tx(sc, m, rate); 3250 return; 3251} 3252 3253static int 3254zyd_raw_xmit_cb(struct ieee80211_node *ni, struct mbuf *m, 3255 const struct ieee80211_bpf_params *params) 3256{ 3257 struct ieee80211com *ic = ni->ni_ic; 3258 struct ifnet *ifp = ic->ic_ifp; 3259 struct zyd_softc *sc = ifp->if_softc; 3260 3261 mtx_lock(&sc->sc_mtx); 3262 if (params == NULL) { 3263 /* 3264 * Legacy path; interpret frame contents to decide 3265 * precisely how to send the frame. 3266 */ 3267 zyd_tx_mgt(sc, m, ni); 3268 } else { 3269 /* 3270 * Caller supplied explicit parameters to use in 3271 * sending the frame. 3272 */ 3273 zyd_tx_mgt(sc, m, ni); /* XXX zyd_tx_raw() */ 3274 } 3275 mtx_unlock(&sc->sc_mtx); 3276 return (0); 3277} 3278 3279static struct ieee80211vap * 3280zyd_get_vap(struct zyd_softc *sc) 3281{ 3282 struct ifnet *ifp; 3283 struct ieee80211com *ic; 3284 3285 if (sc == NULL) { 3286 return NULL; 3287 } 3288 ifp = sc->sc_ifp; 3289 if (ifp == NULL) { 3290 return NULL; 3291 } 3292 ic = ifp->if_l2com; 3293 if (ic == NULL) { 3294 return NULL; 3295 } 3296 return TAILQ_FIRST(&ic->ic_vaps); 3297} 3298