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