From 52972616516a18e69bb2849c2f2e718ee644b27f Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 8 Sep 2009 13:19:05 +0000 Subject: Revert previous commit and add myself to the list of people who should know better than to commit with a cat in the area. --- sys/cam/scsi/scsi_cd.c | 2 +- sys/contrib/dev/acpica/executer/exfldio.c | 1 - sys/dev/aac/aac.c | 2 +- sys/dev/ae/if_ae.c | 2 - sys/dev/amr/amr.c | 2 +- sys/dev/ata/ata-raid.c | 3 +- sys/dev/ata/chipsets/ata-acerlabs.c | 1 - sys/dev/ata/chipsets/ata-marvell.c | 2 +- sys/dev/ath/ah_osdep.c | 1 - sys/dev/ath/ath_hal/ah.c | 1 - sys/dev/ath/ath_hal/ah_eeprom_v3.c | 2 - sys/dev/bce/if_bce.c | 8 +- sys/dev/e1000/if_em.c | 2 +- sys/dev/ep/if_ep.c | 1 - sys/dev/ep/if_epreg.h | 6 +- sys/dev/firewire/fwcrom.c | 4 +- sys/dev/firewire/fwdev.c | 1 - sys/dev/fxp/if_fxp.c | 1 - sys/dev/hptiop/hptiop.c | 2 - sys/dev/iir/iir.c | 1 - sys/dev/mpt/mpt_raid.c | 1 - sys/dev/msk/if_msk.c | 5 + sys/dev/usb/wlan/if_rum.c | 5 +- sys/dev/usb/wlan/if_rumreg.h | 2 +- sys/dev/usb/wlan/if_urtw.c | 3065 ++++++++++++++--------------- sys/dev/wi/if_wi.c | 1 - sys/fs/msdosfs/msdosfs_conv.c | 1 - sys/kern/kern_jail.c | 1 - sys/kern/kern_linker.c | 1 - sys/kern/kern_lock.c | 1 - sys/kern/kern_mutex.c | 11 +- sys/net80211/ieee80211_action.c | 2 +- sys/net80211/ieee80211_proto.h | 2 +- sys/security/audit/audit_bsm_token.c | 1 - 34 files changed, 1557 insertions(+), 1587 deletions(-) (limited to 'sys') diff --git a/sys/cam/scsi/scsi_cd.c b/sys/cam/scsi/scsi_cd.c index 52f7311..287c6b6 100644 --- a/sys/cam/scsi/scsi_cd.c +++ b/sys/cam/scsi/scsi_cd.c @@ -2528,7 +2528,7 @@ cdioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) error = cdgetmode(periph, ¶ms, AUDIO_PAGE); if (error) { - free(¶ms.mode_buf, M_SCSICD); + free(¶ms, M_SCSICD); cam_periph_unlock(periph); break; } diff --git a/sys/contrib/dev/acpica/executer/exfldio.c b/sys/contrib/dev/acpica/executer/exfldio.c index 1b819c0..f59dd62 100644 --- a/sys/contrib/dev/acpica/executer/exfldio.c +++ b/sys/contrib/dev/acpica/executer/exfldio.c @@ -992,7 +992,6 @@ AcpiExInsertIntoField ( /* Get initial Datum from the input buffer */ - /* XXX: Flexelint sees arg 9 for bufferlen 8 */ ACPI_MEMCPY (&RawDatum, Buffer, ACPI_MIN(ObjDesc->CommonField.AccessByteWidth, BufferLength - BufferOffset)); diff --git a/sys/dev/aac/aac.c b/sys/dev/aac/aac.c index 5c3180b..f3d931a 100644 --- a/sys/dev/aac/aac.c +++ b/sys/dev/aac/aac.c @@ -3556,7 +3556,7 @@ aac_supported_features(struct aac_softc *sc, caddr_t uptr) * associated with the feature in the data field or perform whatever * action needed indicates in the data field. */ - if (f.feat.fValue == 0) { + if (f.feat.fValue == 0) { f.feat.fBits.largeLBA = (sc->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0; /* TODO: In the future, add other features state here as well */ diff --git a/sys/dev/ae/if_ae.c b/sys/dev/ae/if_ae.c index a5837c3..da26ee7 100644 --- a/sys/dev/ae/if_ae.c +++ b/sys/dev/ae/if_ae.c @@ -1662,7 +1662,6 @@ ae_stop_rxmac(ae_softc_t *sc) /* * Wait for IDLE state. */ - /* XXX: Flexelint "<" test with -- */ for (i = 0; i < AE_IDLE_TIMEOUT; i--) { val = AE_READ_4(sc, AE_IDLE_REG); if ((val & (AE_IDLE_RXMAC | AE_IDLE_DMAWRITE)) == 0) @@ -1699,7 +1698,6 @@ ae_stop_txmac(ae_softc_t *sc) /* * Wait for IDLE state. */ - /* XXX: Flexelint "<" test with -- */ for (i = 0; i < AE_IDLE_TIMEOUT; i--) { val = AE_READ_4(sc, AE_IDLE_REG); if ((val & (AE_IDLE_TXMAC | AE_IDLE_DMAREAD)) == 0) diff --git a/sys/dev/amr/amr.c b/sys/dev/amr/amr.c index 463ec53..2061fcc 100644 --- a/sys/dev/amr/amr.c +++ b/sys/dev/amr/amr.c @@ -527,7 +527,7 @@ amr_rescan_drives(struct cdev *dev) sc->amr_drive[i].al_disk)) != 0) goto shutdown_out; - sc->amr_drive[i].al_disk = 0; + sc->amr_drive[i].al_disk = 0; } } diff --git a/sys/dev/ata/ata-raid.c b/sys/dev/ata/ata-raid.c index d31bceb..fc6a23c 100644 --- a/sys/dev/ata/ata-raid.c +++ b/sys/dev/ata/ata-raid.c @@ -407,7 +407,7 @@ ata_raid_strategy(struct bio *bp) if (rdp->status & AR_S_REBUILDING) blk = ((lba / rdp->interleave) * rdp->width) * rdp->interleave + (rdp->interleave * (drv % rdp->width)) + - lba % rdp->interleave; + lba % rdp->interleave;; if (bp->bio_cmd == BIO_READ) { int src_online = @@ -1138,7 +1138,6 @@ ata_raid_create(struct ata_ioc_raid_config *config) rdp->type == AR_T_RAID5) { int bit = 0; - /* XXX: Flexelint not happy */ while (config->interleave >>= 1) bit++; rdp->interleave = 1 << bit; diff --git a/sys/dev/ata/chipsets/ata-acerlabs.c b/sys/dev/ata/chipsets/ata-acerlabs.c index 9d19fa0..cf503c5 100644 --- a/sys/dev/ata/chipsets/ata-acerlabs.c +++ b/sys/dev/ata/chipsets/ata-acerlabs.c @@ -127,7 +127,6 @@ ata_ali_chipinit(device_t dev) RF_ACTIVE); if (res->bars[i] == NULL) { device_printf(dev, "Failed to allocate BAR %d\n", i); - /* XXX: Flexelint: Reuse of for loop variable 'i' at 'line 124' could cause chaos */ for (i--; i >=0; i--) bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(i), res->bars[i]); diff --git a/sys/dev/ata/chipsets/ata-marvell.c b/sys/dev/ata/chipsets/ata-marvell.c index bab3267..0544e19 100644 --- a/sys/dev/ata/chipsets/ata-marvell.c +++ b/sys/dev/ata/chipsets/ata-marvell.c @@ -153,7 +153,7 @@ ata_marvell_pata_ch_attach(device_t dev) return ENXIO; /* dont use 32 bit PIO transfers */ - ch->flags |= ATA_USE_16BIT; + ch->flags |= ATA_USE_16BIT; return 0; } diff --git a/sys/dev/ath/ah_osdep.c b/sys/dev/ath/ah_osdep.c index c37ef14..6b11b21 100644 --- a/sys/dev/ath/ah_osdep.c +++ b/sys/dev/ath/ah_osdep.c @@ -107,7 +107,6 @@ ath_hal_malloc(size_t size) return malloc(size, M_ATH_HAL, M_NOWAIT | M_ZERO); } -/* XXX: FlexeLint return in void function ? */ void ath_hal_free(void* p) { diff --git a/sys/dev/ath/ath_hal/ah.c b/sys/dev/ath/ath_hal/ah.c index db47dd2..cff9613 100644 --- a/sys/dev/ath/ath_hal/ah.c +++ b/sys/dev/ath/ath_hal/ah.c @@ -566,7 +566,6 @@ ath_hal_getregdump(struct ath_hal *ah, const HAL_REGRANGE *regs, uint32_t *dp = dstbuf; int i; - /* XXX: FlexeLint: ">" test with ++ */ for (i = 0; space >= 2*sizeof(uint32_t); i++) { u_int r = regs[i].start; u_int e = regs[i].end; diff --git a/sys/dev/ath/ath_hal/ah_eeprom_v3.c b/sys/dev/ath/ath_hal/ah_eeprom_v3.c index fef9a45..89a23c9 100644 --- a/sys/dev/ath/ath_hal/ah_eeprom_v3.c +++ b/sys/dev/ath/ath_hal/ah_eeprom_v3.c @@ -1753,8 +1753,6 @@ legacyEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz) /* * Reclaim any EEPROM-related storage. */ - -/* XXX: FlexeLint: return in void function */ static void legacyEepromDetach(struct ath_hal *ah) { diff --git a/sys/dev/bce/if_bce.c b/sys/dev/bce/if_bce.c index 76e22d9..431ed9f 100644 --- a/sys/dev/bce/if_bce.c +++ b/sys/dev/bce/if_bce.c @@ -608,17 +608,17 @@ bce_print_adapter_info(struct bce_softc *sc) #endif if (sc->bce_flags & BCE_USING_MSI_FLAG) { if (i > 0) printf("|"); - printf("MSI"); i++; + printf("MSI"); i++; } if (sc->bce_flags & BCE_USING_MSIX_FLAG) { if (i > 0) printf("|"); - printf("MSI-X "); i++; + printf("MSI-X "); i++; } if (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG) { if (i > 0) printf("|"); - printf("2.5G"); i++; + printf("2.5G"); i++; } if (sc->bce_flags & BCE_MFW_ENABLE_FLAG) { @@ -628,7 +628,7 @@ bce_print_adapter_info(struct bce_softc *sc) printf(")\n"); } - DBEXIT(BCE_VERBOSE_LOAD); + DBEXIT(BCE_VERBOSE_LOAD); } diff --git a/sys/dev/e1000/if_em.c b/sys/dev/e1000/if_em.c index ee580db..1def876 100644 --- a/sys/dev/e1000/if_em.c +++ b/sys/dev/e1000/if_em.c @@ -2826,7 +2826,7 @@ em_allocate_pci_resources(struct adapter *adapter) * Setup the Legacy or MSI Interrupt handler * **********************************************************************/ -static int +int em_allocate_legacy(struct adapter *adapter) { device_t dev = adapter->dev; diff --git a/sys/dev/ep/if_ep.c b/sys/dev/ep/if_ep.c index 0a7e808..f5fd475 100644 --- a/sys/dev/ep/if_ep.c +++ b/sys/dev/ep/if_ep.c @@ -666,7 +666,6 @@ rescan: sc->tx_underrun++; #endif } else { - /* XXX: FlexeLint doesn't like ; */ if (status & TXS_JABBER); else ++ifp->if_collisions; diff --git a/sys/dev/ep/if_epreg.h b/sys/dev/ep/if_epreg.h index 8c46403..f3c269f 100644 --- a/sys/dev/ep/if_epreg.h +++ b/sys/dev/ep/if_epreg.h @@ -315,11 +315,7 @@ S_TX_AVAIL|S_RX_COMPLETE|S_RX_EARLY) #define S_COMMAND_IN_PROGRESS (u_short) (0x1000) -#define EP_BUSY_WAIT(sc) \ - do { \ - while (CSR_READ_2(sc, EP_STATUS) & S_COMMAND_IN_PROGRESS) \ - continue; \ - } while (0) +#define EP_BUSY_WAIT(sc) while (CSR_READ_2(sc, EP_STATUS) & S_COMMAND_IN_PROGRESS) /* Address Config. Register. * Window 0/Port 06 diff --git a/sys/dev/firewire/fwcrom.c b/sys/dev/firewire/fwcrom.c index 9bafd21..8a53bc7 100644 --- a/sys/dev/firewire/fwcrom.c +++ b/sys/dev/firewire/fwcrom.c @@ -454,9 +454,9 @@ crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent, len = strlen(buf); if (len > MAX_TEXT) { #if defined(__DragonFly__) || __FreeBSD_version < 500000 - printf("text(%d) truncated to %d.\n", len, MAX_TEXT); + printf("text(%d) trancated to %d.\n", len, MAX_TEXT); #else - printf("text(%d) truncated to %td.\n", len, MAX_TEXT); + printf("text(%d) trancated to %td.\n", len, MAX_TEXT); #endif len = MAX_TEXT; } diff --git a/sys/dev/firewire/fwdev.c b/sys/dev/firewire/fwdev.c index 57d68f8..0facb6e 100644 --- a/sys/dev/firewire/fwdev.c +++ b/sys/dev/firewire/fwdev.c @@ -844,7 +844,6 @@ out: err = copyout(ptr, crom_buf->ptr, len); if (fwdev == NULL) /* myself */ - /* XXX: Flexelint no sure about modified pointer */ free(ptr, M_FW); break; default: diff --git a/sys/dev/fxp/if_fxp.c b/sys/dev/fxp/if_fxp.c index 57ac1fb..4fb890f 100644 --- a/sys/dev/fxp/if_fxp.c +++ b/sys/dev/fxp/if_fxp.c @@ -1597,7 +1597,6 @@ fxp_encap(struct fxp_softc *sc, struct mbuf **m_head) cbp->tbd_number = nseg; /* Configure TSO. */ if (m->m_pkthdr.csum_flags & CSUM_TSO) { - /* XXX: FlexeLint: negative subscript */ cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16); cbp->tbd[1].tb_size |= htole32(tcp_payload << 16); cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE | diff --git a/sys/dev/hptiop/hptiop.c b/sys/dev/hptiop/hptiop.c index 5ad2106..7218bc5 100644 --- a/sys/dev/hptiop/hptiop.c +++ b/sys/dev/hptiop/hptiop.c @@ -432,7 +432,6 @@ srb_complete: sg_list), (u_int8_t *)&ccb->csio.sense_data, MIN(dxfer, sizeof(ccb->csio.sense_data))); } else { - /* XXX: Flexelint arg2 is 16 bytes, arg3 is 32 */ memcpy(&ccb->csio.sense_data, &req->sg_list, MIN(dxfer, sizeof(ccb->csio.sense_data))); } @@ -576,7 +575,6 @@ static void hptiop_request_callback_mv(struct hpt_iop_hba * hba, ccb->ccb_h.status = CAM_BUSY; break; case IOP_RESULT_CHECK_CONDITION: - /* XXX: FlexeLint: arg2=16b arg3=32 */ memcpy(&ccb->csio.sense_data, &req->sg_list, MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data))); ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; diff --git a/sys/dev/iir/iir.c b/sys/dev/iir/iir.c index 9bfa283..f5f6d7e 100644 --- a/sys/dev/iir/iir.c +++ b/sys/dev/iir/iir.c @@ -1203,7 +1203,6 @@ gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb) bzero( ccb->csio.data_ptr+copylen, ccb->csio.dxfer_len - copylen ); page=((struct scsi_mode_sense_6 *)ccb->csio.cdb_io.cdb_bytes)->page; - /* XXX: FlexeLint: why ?? */ switch (page) { default: GDT_DPRINTF(GDT_D_MISC, ("MODE_SENSE_6: page 0x%x\n", page)); diff --git a/sys/dev/mpt/mpt_raid.c b/sys/dev/mpt/mpt_raid.c index 0a3fdca..6b7eb7b 100644 --- a/sys/dev/mpt/mpt_raid.c +++ b/sys/dev/mpt/mpt_raid.c @@ -562,7 +562,6 @@ mpt_raid_reply_frame_handler(struct mpt_softc *mpt, request_t *req, break; } action_result = REQ_TO_RAID_ACTION_RESULT(req); - /* XXX: FlexeLint: ActionData is only 4 bytes */ memcpy(&action_result->action_data, &reply->ActionData, sizeof(action_result->action_data)); action_result->action_status = le16toh(reply->ActionStatus); diff --git a/sys/dev/msk/if_msk.c b/sys/dev/msk/if_msk.c index ca9ef27..f5f94ba 100644 --- a/sys/dev/msk/if_msk.c +++ b/sys/dev/msk/if_msk.c @@ -118,21 +118,26 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include #include #include +#include #include #include +#include #include #include #include #include +#include #include +#include #include #include diff --git a/sys/dev/usb/wlan/if_rum.c b/sys/dev/usb/wlan/if_rum.c index 6af6ec2..7abd104 100644 --- a/sys/dev/usb/wlan/if_rum.c +++ b/sys/dev/usb/wlan/if_rum.c @@ -42,9 +42,12 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include #include #include +#include #include #include #include @@ -1486,7 +1489,7 @@ rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) return; } - tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | ((val & 0xfffff) << 2) | + tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | (reg & 3); rum_write(sc, RT2573_PHY_CSR4, tmp); diff --git a/sys/dev/usb/wlan/if_rumreg.h b/sys/dev/usb/wlan/if_rumreg.h index 44e627a..75a51bc 100644 --- a/sys/dev/usb/wlan/if_rumreg.h +++ b/sys/dev/usb/wlan/if_rumreg.h @@ -139,7 +139,7 @@ #define RT2573_BBP_BUSY (1 << 16) /* possible flags for register PHY_CSR4 */ #define RT2573_RF_20BIT (20 << 24) -#define RT2573_RF_BUSY (1U << 31) +#define RT2573_RF_BUSY (1 << 31) /* LED values */ #define RT2573_LED_RADIO (1 << 8) diff --git a/sys/dev/usb/wlan/if_urtw.c b/sys/dev/usb/wlan/if_urtw.c index 69b7bb1..0cd8d35 100644 --- a/sys/dev/usb/wlan/if_urtw.c +++ b/sys/dev/usb/wlan/if_urtw.c @@ -61,8 +61,6 @@ __FBSDID("$FreeBSD$"); #include #include -#define nitems(a) (sizeof(a) / sizeof((a)[0])) - SYSCTL_NODE(_hw_usb, OID_AUTO, urtw, CTLFLAG_RW, 0, "USB Realtek 8187L"); #ifdef URTW_DEBUG int urtw_debug = 0; @@ -177,46 +175,6 @@ struct urtw_pair { uint32_t val; }; -static const uint8_t urtw_8187b_reg_table[][3] = { - { 0xf0, 0x32, 0 }, { 0xf1, 0x32, 0 }, { 0xf2, 0x00, 0 }, - { 0xf3, 0x00, 0 }, { 0xf4, 0x32, 0 }, { 0xf5, 0x43, 0 }, - { 0xf6, 0x00, 0 }, { 0xf7, 0x00, 0 }, { 0xf8, 0x46, 0 }, - { 0xf9, 0xa4, 0 }, { 0xfa, 0x00, 0 }, { 0xfb, 0x00, 0 }, - { 0xfc, 0x96, 0 }, { 0xfd, 0xa4, 0 }, { 0xfe, 0x00, 0 }, - { 0xff, 0x00, 0 }, - - { 0x58, 0x4b, 1 }, { 0x59, 0x00, 1 }, { 0x5a, 0x4b, 1 }, - { 0x5b, 0x00, 1 }, { 0x60, 0x4b, 1 }, { 0x61, 0x09, 1 }, - { 0x62, 0x4b, 1 }, { 0x63, 0x09, 1 }, { 0xce, 0x0f, 1 }, - { 0xcf, 0x00, 1 }, { 0xe0, 0xff, 1 }, { 0xe1, 0x0f, 1 }, - { 0xe2, 0x00, 1 }, { 0xf0, 0x4e, 1 }, { 0xf1, 0x01, 1 }, - { 0xf2, 0x02, 1 }, { 0xf3, 0x03, 1 }, { 0xf4, 0x04, 1 }, - { 0xf5, 0x05, 1 }, { 0xf6, 0x06, 1 }, { 0xf7, 0x07, 1 }, - { 0xf8, 0x08, 1 }, - - { 0x4e, 0x00, 2 }, { 0x0c, 0x04, 2 }, { 0x21, 0x61, 2 }, - { 0x22, 0x68, 2 }, { 0x23, 0x6f, 2 }, { 0x24, 0x76, 2 }, - { 0x25, 0x7d, 2 }, { 0x26, 0x84, 2 }, { 0x27, 0x8d, 2 }, - { 0x4d, 0x08, 2 }, { 0x50, 0x05, 2 }, { 0x51, 0xf5, 2 }, - { 0x52, 0x04, 2 }, { 0x53, 0xa0, 2 }, { 0x54, 0x1f, 2 }, - { 0x55, 0x23, 2 }, { 0x56, 0x45, 2 }, { 0x57, 0x67, 2 }, - { 0x58, 0x08, 2 }, { 0x59, 0x08, 2 }, { 0x5a, 0x08, 2 }, - { 0x5b, 0x08, 2 }, { 0x60, 0x08, 2 }, { 0x61, 0x08, 2 }, - { 0x62, 0x08, 2 }, { 0x63, 0x08, 2 }, { 0x64, 0xcf, 2 }, - { 0x72, 0x56, 2 }, { 0x73, 0x9a, 2 }, - - { 0x34, 0xf0, 0 }, { 0x35, 0x0f, 0 }, { 0x5b, 0x40, 0 }, - { 0x84, 0x88, 0 }, { 0x85, 0x24, 0 }, { 0x88, 0x54, 0 }, - { 0x8b, 0xb8, 0 }, { 0x8c, 0x07, 0 }, { 0x8d, 0x00, 0 }, - { 0x94, 0x1b, 0 }, { 0x95, 0x12, 0 }, { 0x96, 0x00, 0 }, - { 0x97, 0x06, 0 }, { 0x9d, 0x1a, 0 }, { 0x9f, 0x10, 0 }, - { 0xb4, 0x22, 0 }, { 0xbe, 0x80, 0 }, { 0xdb, 0x00, 0 }, - { 0xee, 0x00, 0 }, { 0x91, 0x03, 0 }, - - { 0x4c, 0x00, 2 }, { 0x9f, 0x00, 3 }, { 0x8c, 0x01, 0 }, - { 0x8d, 0x10, 0 }, { 0x8e, 0x08, 0 }, { 0x8f, 0x00, 0 } -}; - static uint8_t urtw_8225_agc[] = { 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90, @@ -232,6 +190,21 @@ static uint8_t urtw_8225_agc[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; +static uint8_t urtw_8225z2_agc[] = { + 0x5e, 0x5e, 0x5e, 0x5e, 0x5d, 0x5b, 0x59, 0x57, 0x55, 0x53, 0x51, + 0x4f, 0x4d, 0x4b, 0x49, 0x47, 0x45, 0x43, 0x41, 0x3f, 0x3d, 0x3b, + 0x39, 0x37, 0x35, 0x33, 0x31, 0x2f, 0x2d, 0x2b, 0x29, 0x27, 0x25, + 0x23, 0x21, 0x1f, 0x1d, 0x1b, 0x19, 0x17, 0x15, 0x13, 0x11, 0x0f, + 0x0d, 0x0b, 0x09, 0x07, 0x05, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x19, 0x19, + 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2a, + 0x2a, 0x2b, 0x2b, 0x2b, 0x2c, 0x2c, 0x2c, 0x2d, 0x2d, 0x2d, 0x2d, + 0x2e, 0x2e, 0x2e, 0x2e, 0x2f, 0x2f, 0x2f, 0x30, 0x30, 0x31, 0x31, + 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, + 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31 +}; + static uint32_t urtw_8225_channel[] = { 0x0000, /* dummy channel 0 */ 0x085c, /* 1 */ @@ -264,7 +237,7 @@ static struct urtw_pair urtw_8225_rf_part1[] = { { 0x00, 0x0067 }, { 0x01, 0x0fe0 }, { 0x02, 0x044d }, { 0x03, 0x0441 }, { 0x04, 0x0486 }, { 0x05, 0x0bc0 }, { 0x06, 0x0ae6 }, { 0x07, 0x082a }, { 0x08, 0x001f }, { 0x09, 0x0334 }, { 0x0a, 0x0fd4 }, { 0x0b, 0x0391 }, - { 0x0c, 0x0050 }, { 0x0d, 0x06db }, { 0x0e, 0x0029 }, { 0x0f, 0x0914 } + { 0x0c, 0x0050 }, { 0x0d, 0x06db }, { 0x0e, 0x0029 }, { 0x0f, 0x0914 }, }; static struct urtw_pair urtw_8225_rf_part2[] = { @@ -306,7 +279,7 @@ static uint16_t urtw_8225_rxgain[] = { }; static uint8_t urtw_8225_threshold[] = { - 0x8d, 0x8d, 0x8d, 0x8d, 0x9d, 0xad, 0xbd + 0x8d, 0x8d, 0x8d, 0x8d, 0x9d, 0xad, 0xbd, }; static uint8_t urtw_8225_tx_gain_cck_ofdm[] = { @@ -331,29 +304,10 @@ static uint8_t urtw_8225_txpwr_cck_ch14[] = { 0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00 }; -static uint8_t urtw_8225_txpwr_ofdm[] = { +static uint8_t urtw_8225_txpwr_ofdm[]={ 0x80, 0x90, 0xa2, 0xb5, 0xcb, 0xe4 }; -static uint8_t urtw_8225z2_agc[] = { - 0x5e, 0x5e, 0x5e, 0x5e, 0x5d, 0x5b, 0x59, 0x57, - 0x55, 0x53, 0x51, 0x4f, 0x4d, 0x4b, 0x49, 0x47, - 0x45, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x39, 0x37, - 0x35, 0x33, 0x31, 0x2f, 0x2d, 0x2b, 0x29, 0x27, - 0x25, 0x23, 0x21, 0x1f, 0x1d, 0x1b, 0x19, 0x17, - 0x15, 0x13, 0x11, 0x0f, 0x0d, 0x0b, 0x09, 0x07, - 0x05, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, - 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, - 0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2a, - 0x2a, 0x2b, 0x2b, 0x2b, 0x2c, 0x2c, 0x2c, 0x2d, - 0x2d, 0x2d, 0x2d, 0x2e, 0x2e, 0x2e, 0x2e, 0x2f, - 0x2f, 0x2f, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31, - 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, - 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31 -}; - static uint8_t urtw_8225v2_gain_bg[]={ 0x23, 0x15, 0xa5, /* -82-1dbm */ 0x23, 0x15, 0xb5, /* -82-2dbm */ @@ -456,7 +410,11 @@ static uint8_t urtw_8225v2_tx_gain_cck_ofdm[] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, - 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23 + 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, +}; + +static uint8_t urtw_8225v2_txpwr_cck[] = { + 0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04 }; static uint8_t urtw_8225v2_txpwr_cck_ch14[] = { @@ -470,10 +428,6 @@ static uint8_t urtw_8225v2b_txpwr_cck[] = { 0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03 }; -static uint8_t urtw_8225v2_txpwr_cck[] = { - 0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04 -}; - static uint8_t urtw_8225v2b_txpwr_cck_ch14[] = { 0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00, @@ -487,6 +441,40 @@ static struct urtw_pair urtw_ratetable[] = { { 96, 10 }, { 108, 11 } }; +static const uint8_t urtw_8187b_reg_table[][3] = { + { 0xf0, 0x32, 0 }, { 0xf1, 0x32, 0 }, { 0xf2, 0x00, 0 }, + { 0xf3, 0x00, 0 }, { 0xf4, 0x32, 0 }, { 0xf5, 0x43, 0 }, + { 0xf6, 0x00, 0 }, { 0xf7, 0x00, 0 }, { 0xf8, 0x46, 0 }, + { 0xf9, 0xa4, 0 }, { 0xfa, 0x00, 0 }, { 0xfb, 0x00, 0 }, + { 0xfc, 0x96, 0 }, { 0xfd, 0xa4, 0 }, { 0xfe, 0x00, 0 }, + { 0xff, 0x00, 0 }, { 0x58, 0x4b, 1 }, { 0x59, 0x00, 1 }, + { 0x5a, 0x4b, 1 }, { 0x5b, 0x00, 1 }, { 0x60, 0x4b, 1 }, + { 0x61, 0x09, 1 }, { 0x62, 0x4b, 1 }, { 0x63, 0x09, 1 }, + { 0xce, 0x0f, 1 }, { 0xcf, 0x00, 1 }, { 0xe0, 0xff, 1 }, + { 0xe1, 0x0f, 1 }, { 0xe2, 0x00, 1 }, { 0xf0, 0x4e, 1 }, + { 0xf1, 0x01, 1 }, { 0xf2, 0x02, 1 }, { 0xf3, 0x03, 1 }, + { 0xf4, 0x04, 1 }, { 0xf5, 0x05, 1 }, { 0xf6, 0x06, 1 }, + { 0xf7, 0x07, 1 }, { 0xf8, 0x08, 1 }, { 0x4e, 0x00, 2 }, + { 0x0c, 0x04, 2 }, { 0x21, 0x61, 2 }, { 0x22, 0x68, 2 }, + { 0x23, 0x6f, 2 }, { 0x24, 0x76, 2 }, { 0x25, 0x7d, 2 }, + { 0x26, 0x84, 2 }, { 0x27, 0x8d, 2 }, { 0x4d, 0x08, 2 }, + { 0x50, 0x05, 2 }, { 0x51, 0xf5, 2 }, { 0x52, 0x04, 2 }, + { 0x53, 0xa0, 2 }, { 0x54, 0x1f, 2 }, { 0x55, 0x23, 2 }, + { 0x56, 0x45, 2 }, { 0x57, 0x67, 2 }, { 0x58, 0x08, 2 }, + { 0x59, 0x08, 2 }, { 0x5a, 0x08, 2 }, { 0x5b, 0x08, 2 }, + { 0x60, 0x08, 2 }, { 0x61, 0x08, 2 }, { 0x62, 0x08, 2 }, + { 0x63, 0x08, 2 }, { 0x64, 0xcf, 2 }, { 0x72, 0x56, 2 }, + { 0x73, 0x9a, 2 }, { 0x34, 0xf0, 0 }, { 0x35, 0x0f, 0 }, + { 0x5b, 0x40, 0 }, { 0x84, 0x88, 0 }, { 0x85, 0x24, 0 }, + { 0x88, 0x54, 0 }, { 0x8b, 0xb8, 0 }, { 0x8c, 0x07, 0 }, + { 0x8d, 0x00, 0 }, { 0x94, 0x1b, 0 }, { 0x95, 0x12, 0 }, + { 0x96, 0x00, 0 }, { 0x97, 0x06, 0 }, { 0x9d, 0x1a, 0 }, + { 0x9f, 0x10, 0 }, { 0xb4, 0x22, 0 }, { 0xbe, 0x80, 0 }, + { 0xdb, 0x00, 0 }, { 0xee, 0x00, 0 }, { 0x91, 0x03, 0 }, + { 0x4c, 0x00, 2 }, { 0x9f, 0x00, 3 }, { 0x8c, 0x01, 0 }, + { 0x8d, 0x10, 0 }, { 0x8e, 0x08, 0 }, { 0x8f, 0x00, 0 } +}; + static usb_callback_t urtw_bulk_rx_callback; static usb_callback_t urtw_bulk_tx_callback; @@ -1046,6 +1034,7 @@ urtw_init(void *arg) static usb_error_t urtw_adapter_start_b(struct urtw_softc *sc) { +#define N(a) (sizeof(a) / sizeof((a)[0])) int i; uint8_t data8; usb_error_t error; @@ -1109,7 +1098,7 @@ urtw_adapter_start_b(struct urtw_softc *sc) goto fail; urtw_write8_m(sc, URTW_WPA_CONFIG, 0); - for (i = 0; i < nitems(urtw_8187b_reg_table); i++) { + for (i = 0; i < N(urtw_8187b_reg_table); i++) { error = urtw_write8_i(sc, urtw_8187b_reg_table[i][0], urtw_8187b_reg_table[i][1], urtw_8187b_reg_table[i][2]); if (error) @@ -1219,6 +1208,7 @@ urtw_adapter_start_b(struct urtw_softc *sc) fail: return (error); +#undef N } static usb_error_t @@ -1283,6 +1273,19 @@ fail: return (error); } +static usb_error_t +urtw_set_mode(struct urtw_softc *sc, uint32_t mode) +{ + uint8_t data; + usb_error_t error; + + urtw_read8_m(sc, URTW_EPROM_CMD, &data); + data = (data & ~URTW_EPROM_CMD_MASK) | (mode << URTW_EPROM_CMD_SHIFT); + data = data & ~(URTW_EPROM_CS | URTW_EPROM_CK); + urtw_write8_m(sc, URTW_EPROM_CMD, data); +fail: + return (error); +} static usb_error_t urtw_8187b_cmd_reset(struct urtw_softc *sc) @@ -1392,6 +1395,56 @@ urtw_write32_i(struct urtw_softc *sc, int val, uint32_t data, int idx) } static void +urtw_stop_locked(struct ifnet *ifp, int disable) +{ + struct urtw_softc *sc = ifp->if_softc; + uint8_t data8; + usb_error_t error; + + (void)disable; + ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); + + error = urtw_intr_disable(sc); + if (error) + goto fail; + urtw_read8_m(sc, URTW_CMD, &data8); + data8 &= ~(URTW_CMD_RX_ENABLE | URTW_CMD_TX_ENABLE); + urtw_write8_m(sc, URTW_CMD, data8); + + error = sc->sc_rf_stop(sc); + if (error != 0) + goto fail; + + error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); + if (error) + goto fail; + urtw_read8_m(sc, URTW_CONFIG4, &data8); + urtw_write8_m(sc, URTW_CONFIG4, data8 | URTW_CONFIG4_VCOOFF); + error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); + if (error) + goto fail; +fail: + if (error) + device_printf(sc->sc_dev, "failed to stop (%s)\n", + usbd_errstr(error)); + + usb_callout_stop(&sc->sc_led_ch); + callout_stop(&sc->sc_watchdog_ch); + + urtw_abort_xfers(sc); +} + +static void +urtw_stop(struct ifnet *ifp, int disable) +{ + struct urtw_softc *sc = ifp->if_softc; + + URTW_LOCK(sc); + urtw_stop_locked(ifp, disable); + URTW_UNLOCK(sc); +} + +static void urtw_abort_xfers(struct urtw_softc *sc) { int i, max; @@ -1896,198 +1949,105 @@ urtw_set_multi(void *arg) ifp->if_flags |= IFF_ALLMULTI; } - static usb_error_t -urtw_get_macaddr(struct urtw_softc *sc) +urtw_set_rate(struct urtw_softc *sc) { - uint32_t data; + int i, basic_rate, min_rr_rate, max_rr_rate; + uint16_t data; usb_error_t error; - error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR, &data); - if (error != 0) - goto fail; - sc->sc_bssid[0] = data & 0xff; - sc->sc_bssid[1] = (data & 0xff00) >> 8; - error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 1, &data); - if (error != 0) - goto fail; - sc->sc_bssid[2] = data & 0xff; - sc->sc_bssid[3] = (data & 0xff00) >> 8; - error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 2, &data); - if (error != 0) - goto fail; - sc->sc_bssid[4] = data & 0xff; - sc->sc_bssid[5] = (data & 0xff00) >> 8; + basic_rate = urtw_rate2rtl(48); + min_rr_rate = urtw_rate2rtl(12); + max_rr_rate = urtw_rate2rtl(48); + + urtw_write8_m(sc, URTW_RESP_RATE, + max_rr_rate << URTW_RESP_MAX_RATE_SHIFT | + min_rr_rate << URTW_RESP_MIN_RATE_SHIFT); + + urtw_read16_m(sc, URTW_BRSR, &data); + data &= ~URTW_BRSR_MBR_8185; + + for (i = 0; i <= basic_rate; i++) + data |= (1 << i); + + urtw_write16_m(sc, URTW_BRSR, data); fail: return (error); } -static usb_error_t -urtw_eprom_read32(struct urtw_softc *sc, uint32_t addr, uint32_t *data) +static uint16_t +urtw_rate2rtl(int rate) { -#define URTW_READCMD_LEN 3 - int addrlen, i; - int16_t addrstr[8], data16, readcmd[] = { 1, 1, 0 }; - usb_error_t error; - - /* NB: make sure the buffer is initialized */ - *data = 0; - - /* enable EPROM programming */ - urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_PROGRAM_MODE); - DELAY(URTW_EPROM_DELAY); +#define N(a) (sizeof(a) / sizeof((a)[0])) + int i; - error = urtw_eprom_cs(sc, URTW_EPROM_ENABLE); - if (error != 0) - goto fail; - error = urtw_eprom_ck(sc); - if (error != 0) - goto fail; - error = urtw_eprom_sendbits(sc, readcmd, URTW_READCMD_LEN); - if (error != 0) - goto fail; - if (sc->sc_epromtype == URTW_EEPROM_93C56) { - addrlen = 8; - addrstr[0] = addr & (1 << 7); - addrstr[1] = addr & (1 << 6); - addrstr[2] = addr & (1 << 5); - addrstr[3] = addr & (1 << 4); - addrstr[4] = addr & (1 << 3); - addrstr[5] = addr & (1 << 2); - addrstr[6] = addr & (1 << 1); - addrstr[7] = addr & (1 << 0); - } else { - addrlen=6; - addrstr[0] = addr & (1 << 5); - addrstr[1] = addr & (1 << 4); - addrstr[2] = addr & (1 << 3); - addrstr[3] = addr & (1 << 2); - addrstr[4] = addr & (1 << 1); - addrstr[5] = addr & (1 << 0); + for (i = 0; i < N(urtw_ratetable); i++) { + if (rate == urtw_ratetable[i].reg) + return urtw_ratetable[i].val; } - error = urtw_eprom_sendbits(sc, addrstr, addrlen); - if (error != 0) - goto fail; - error = urtw_eprom_writebit(sc, 0); - if (error != 0) - goto fail; + return (3); +#undef N +} - for (i = 0; i < 16; i++) { - error = urtw_eprom_ck(sc); - if (error != 0) - goto fail; - error = urtw_eprom_readbit(sc, &data16); - if (error != 0) - goto fail; +static uint16_t +urtw_rtl2rate(int rate) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + int i; - (*data) |= (data16 << (15 - i)); + for (i = 0; i < N(urtw_ratetable); i++) { + if (rate == urtw_ratetable[i].val) + return urtw_ratetable[i].reg; } - error = urtw_eprom_cs(sc, URTW_EPROM_DISABLE); - if (error != 0) - goto fail; - error = urtw_eprom_ck(sc); - if (error != 0) - goto fail; - - /* now disable EPROM programming */ - urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_NORMAL_MODE); -fail: - return (error); -#undef URTW_READCMD_LEN + return (0); +#undef N } static usb_error_t -urtw_eprom_readbit(struct urtw_softc *sc, int16_t *data) +urtw_update_msr(struct urtw_softc *sc) { - uint8_t data8; + struct ifnet *ifp = sc->sc_ifp; + struct ieee80211com *ic = ifp->if_l2com; + uint8_t data; usb_error_t error; - urtw_read8_m(sc, URTW_EPROM_CMD, &data8); - *data = (data8 & URTW_EPROM_READBIT) ? 1 : 0; - DELAY(URTW_EPROM_DELAY); + urtw_read8_m(sc, URTW_MSR, &data); + data &= ~URTW_MSR_LINK_MASK; + + if (sc->sc_state == IEEE80211_S_RUN) { + switch (ic->ic_opmode) { + case IEEE80211_M_STA: + case IEEE80211_M_MONITOR: + data |= URTW_MSR_LINK_STA; + if (sc->sc_flags & URTW_RTL8187B) + data |= URTW_MSR_LINK_ENEDCA; + break; + case IEEE80211_M_IBSS: + data |= URTW_MSR_LINK_ADHOC; + break; + case IEEE80211_M_HOSTAP: + data |= URTW_MSR_LINK_HOSTAP; + break; + default: + panic("unsupported operation mode 0x%x\n", + ic->ic_opmode); + /* never reach */ + } + } else + data |= URTW_MSR_LINK_NONE; + urtw_write8_m(sc, URTW_MSR, data); fail: return (error); } static usb_error_t -urtw_eprom_sendbits(struct urtw_softc *sc, int16_t *buf, int buflen) +urtw_read8_c(struct urtw_softc *sc, int val, uint8_t *data) { - int i = 0; - usb_error_t error = 0; - - for (i = 0; i < buflen; i++) { - error = urtw_eprom_writebit(sc, buf[i]); - if (error != 0) - goto fail; - error = urtw_eprom_ck(sc); - if (error != 0) - goto fail; - } -fail: - return (error); -} - -static usb_error_t -urtw_eprom_writebit(struct urtw_softc *sc, int16_t bit) -{ - uint8_t data; - usb_error_t error; - - urtw_read8_m(sc, URTW_EPROM_CMD, &data); - if (bit != 0) - urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_WRITEBIT); - else - urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_WRITEBIT); - DELAY(URTW_EPROM_DELAY); -fail: - return (error); -} - -static usb_error_t -urtw_eprom_ck(struct urtw_softc *sc) -{ - uint8_t data; - usb_error_t error; - - /* masking */ - urtw_read8_m(sc, URTW_EPROM_CMD, &data); - urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CK); - DELAY(URTW_EPROM_DELAY); - /* unmasking */ - urtw_read8_m(sc, URTW_EPROM_CMD, &data); - urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CK); - DELAY(URTW_EPROM_DELAY); -fail: - return (error); -} - - -static usb_error_t -urtw_eprom_cs(struct urtw_softc *sc, int able) -{ - uint8_t data; - usb_error_t error; - - urtw_read8_m(sc, URTW_EPROM_CMD, &data); - if (able == URTW_EPROM_ENABLE) - urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CS); - else - urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CS); - DELAY(URTW_EPROM_DELAY); -fail: - return (error); -} - - - -static usb_error_t -urtw_read8_c(struct urtw_softc *sc, int val, uint8_t *data) -{ - struct usb_device_request req; - usb_error_t error; + struct usb_device_request req; + usb_error_t error; URTW_ASSERT_LOCKED(sc); @@ -2102,22 +2062,6 @@ urtw_read8_c(struct urtw_softc *sc, int val, uint8_t *data) } static usb_error_t -urtw_read8e(struct urtw_softc *sc, int val, uint8_t *data) -{ - struct usb_device_request req; - usb_error_t error; - - req.bmRequestType = UT_READ_VENDOR_DEVICE; - req.bRequest = URTW_8187_GETREGS_REQ; - USETW(req.wValue, val | 0xfe00); - USETW(req.wIndex, 0); - USETW(req.wLength, sizeof(uint8_t)); - - error = urtw_do_request(sc, &req, data); - return (error); -} - -static usb_error_t urtw_read16_c(struct urtw_softc *sc, int val, uint16_t *data) { struct usb_device_request req; @@ -2170,20 +2114,6 @@ urtw_write8_c(struct urtw_softc *sc, int val, uint8_t data) } static usb_error_t -urtw_write8e(struct urtw_softc *sc, int val, uint8_t data) -{ - struct usb_device_request req; - - req.bmRequestType = UT_WRITE_VENDOR_DEVICE; - req.bRequest = URTW_8187_SETREGS_REQ; - USETW(req.wValue, val | 0xfe00); - USETW(req.wIndex, 0); - USETW(req.wLength, sizeof(uint8_t)); - - return (urtw_do_request(sc, &req, &data)); -} - -static usb_error_t urtw_write16_c(struct urtw_softc *sc, int val, uint16_t data) { struct usb_device_request req; @@ -2216,180 +2146,308 @@ urtw_write32_c(struct urtw_softc *sc, int val, uint32_t data) } static usb_error_t -urtw_set_mode(struct urtw_softc *sc, uint32_t mode) +urtw_get_macaddr(struct urtw_softc *sc) { - uint8_t data; + uint32_t data; usb_error_t error; - urtw_read8_m(sc, URTW_EPROM_CMD, &data); - data = (data & ~URTW_EPROM_CMD_MASK) | (mode << URTW_EPROM_CMD_SHIFT); - data = data & ~(URTW_EPROM_CS | URTW_EPROM_CK); - urtw_write8_m(sc, URTW_EPROM_CMD, data); + error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR, &data); + if (error != 0) + goto fail; + sc->sc_bssid[0] = data & 0xff; + sc->sc_bssid[1] = (data & 0xff00) >> 8; + error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 1, &data); + if (error != 0) + goto fail; + sc->sc_bssid[2] = data & 0xff; + sc->sc_bssid[3] = (data & 0xff00) >> 8; + error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 2, &data); + if (error != 0) + goto fail; + sc->sc_bssid[4] = data & 0xff; + sc->sc_bssid[5] = (data & 0xff00) >> 8; fail: return (error); } static usb_error_t -urtw_8180_set_anaparam(struct urtw_softc *sc, uint32_t val) +urtw_eprom_read32(struct urtw_softc *sc, uint32_t addr, uint32_t *data) { - uint8_t data; +#define URTW_READCMD_LEN 3 + int addrlen, i; + int16_t addrstr[8], data16, readcmd[] = { 1, 1, 0 }; usb_error_t error; - error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); - if (error) + /* NB: make sure the buffer is initialized */ + *data = 0; + + /* enable EPROM programming */ + urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_PROGRAM_MODE); + DELAY(URTW_EPROM_DELAY); + + error = urtw_eprom_cs(sc, URTW_EPROM_ENABLE); + if (error != 0) + goto fail; + error = urtw_eprom_ck(sc); + if (error != 0) + goto fail; + error = urtw_eprom_sendbits(sc, readcmd, URTW_READCMD_LEN); + if (error != 0) + goto fail; + if (sc->sc_epromtype == URTW_EEPROM_93C56) { + addrlen = 8; + addrstr[0] = addr & (1 << 7); + addrstr[1] = addr & (1 << 6); + addrstr[2] = addr & (1 << 5); + addrstr[3] = addr & (1 << 4); + addrstr[4] = addr & (1 << 3); + addrstr[5] = addr & (1 << 2); + addrstr[6] = addr & (1 << 1); + addrstr[7] = addr & (1 << 0); + } else { + addrlen=6; + addrstr[0] = addr & (1 << 5); + addrstr[1] = addr & (1 << 4); + addrstr[2] = addr & (1 << 3); + addrstr[3] = addr & (1 << 2); + addrstr[4] = addr & (1 << 1); + addrstr[5] = addr & (1 << 0); + } + error = urtw_eprom_sendbits(sc, addrstr, addrlen); + if (error != 0) goto fail; - urtw_read8_m(sc, URTW_CONFIG3, &data); - urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE); - urtw_write32_m(sc, URTW_ANAPARAM, val); - urtw_read8_m(sc, URTW_CONFIG3, &data); - urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE); + error = urtw_eprom_writebit(sc, 0); + if (error != 0) + goto fail; - error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); - if (error) + for (i = 0; i < 16; i++) { + error = urtw_eprom_ck(sc); + if (error != 0) + goto fail; + error = urtw_eprom_readbit(sc, &data16); + if (error != 0) + goto fail; + + (*data) |= (data16 << (15 - i)); + } + + error = urtw_eprom_cs(sc, URTW_EPROM_DISABLE); + if (error != 0) + goto fail; + error = urtw_eprom_ck(sc); + if (error != 0) goto fail; + + /* now disable EPROM programming */ + urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_NORMAL_MODE); fail: return (error); +#undef URTW_READCMD_LEN } static usb_error_t -urtw_8185_set_anaparam2(struct urtw_softc *sc, uint32_t val) +urtw_eprom_cs(struct urtw_softc *sc, int able) { uint8_t data; usb_error_t error; - error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); - if (error) - goto fail; + urtw_read8_m(sc, URTW_EPROM_CMD, &data); + if (able == URTW_EPROM_ENABLE) + urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CS); + else + urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CS); + DELAY(URTW_EPROM_DELAY); +fail: + return (error); +} - urtw_read8_m(sc, URTW_CONFIG3, &data); - urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE); - urtw_write32_m(sc, URTW_ANAPARAM2, val); - urtw_read8_m(sc, URTW_CONFIG3, &data); - urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE); +static usb_error_t +urtw_eprom_ck(struct urtw_softc *sc) +{ + uint8_t data; + usb_error_t error; - error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); - if (error) - goto fail; + /* masking */ + urtw_read8_m(sc, URTW_EPROM_CMD, &data); + urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CK); + DELAY(URTW_EPROM_DELAY); + /* unmasking */ + urtw_read8_m(sc, URTW_EPROM_CMD, &data); + urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CK); + DELAY(URTW_EPROM_DELAY); fail: return (error); } - static usb_error_t -urtw_intr_disable(struct urtw_softc *sc) +urtw_eprom_readbit(struct urtw_softc *sc, int16_t *data) { + uint8_t data8; usb_error_t error; - urtw_write16_m(sc, URTW_INTR_MASK, 0); + urtw_read8_m(sc, URTW_EPROM_CMD, &data8); + *data = (data8 & URTW_EPROM_READBIT) ? 1 : 0; + DELAY(URTW_EPROM_DELAY); + fail: return (error); } static usb_error_t -urtw_reset(struct urtw_softc *sc) +urtw_eprom_writebit(struct urtw_softc *sc, int16_t bit) { uint8_t data; usb_error_t error; - error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); - if (error) - goto fail; - error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); - if (error) - goto fail; - - error = urtw_intr_disable(sc); - if (error) - goto fail; - usb_pause_mtx(&sc->sc_mtx, 100); - - error = urtw_write8e(sc, 0x18, 0x10); - if (error != 0) - goto fail; - error = urtw_write8e(sc, 0x18, 0x11); - if (error != 0) - goto fail; - error = urtw_write8e(sc, 0x18, 0x00); - if (error != 0) - goto fail; - usb_pause_mtx(&sc->sc_mtx, 100); + urtw_read8_m(sc, URTW_EPROM_CMD, &data); + if (bit != 0) + urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_WRITEBIT); + else + urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_WRITEBIT); + DELAY(URTW_EPROM_DELAY); +fail: + return (error); +} - urtw_read8_m(sc, URTW_CMD, &data); - data = (data & 0x2) | URTW_CMD_RST; - urtw_write8_m(sc, URTW_CMD, data); - usb_pause_mtx(&sc->sc_mtx, 100); +static usb_error_t +urtw_eprom_sendbits(struct urtw_softc *sc, int16_t *buf, int buflen) +{ + int i = 0; + usb_error_t error = 0; - urtw_read8_m(sc, URTW_CMD, &data); - if (data & URTW_CMD_RST) { - device_printf(sc->sc_dev, "reset timeout\n"); - goto fail; + for (i = 0; i < buflen; i++) { + error = urtw_eprom_writebit(sc, buf[i]); + if (error != 0) + goto fail; + error = urtw_eprom_ck(sc); + if (error != 0) + goto fail; } - - error = urtw_set_mode(sc, URTW_EPROM_CMD_LOAD); - if (error) - goto fail; - usb_pause_mtx(&sc->sc_mtx, 100); - - error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); - if (error) - goto fail; - error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); - if (error) - goto fail; fail: return (error); } + static usb_error_t -urtw_led_on(struct urtw_softc *sc, int type) +urtw_get_txpwr(struct urtw_softc *sc) { + int i, j; + uint32_t data; usb_error_t error; - if (type == URTW_LED_GPIO) { - switch (sc->sc_gpio_ledpin) { - case URTW_LED_PIN_GPIO0: - urtw_write8_m(sc, URTW_GPIO, 0x01); - urtw_write8_m(sc, URTW_GP_ENABLE, 0x00); - break; - default: - panic("unsupported LED PIN type 0x%x", - sc->sc_gpio_ledpin); - /* never reach */ - } + error = urtw_eprom_read32(sc, URTW_EPROM_TXPW_BASE, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck_base = data & 0xf; + sc->sc_txpwr_ofdm_base = (data >> 4) & 0xf; + + for (i = 1, j = 0; i < 6; i += 2, j++) { + error = urtw_eprom_read32(sc, URTW_EPROM_TXPW0 + j, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[i] = data & 0xf; + sc->sc_txpwr_cck[i + 1] = (data & 0xf00) >> 8; + sc->sc_txpwr_ofdm[i] = (data & 0xf0) >> 4; + sc->sc_txpwr_ofdm[i + 1] = (data & 0xf000) >> 12; + } + for (i = 1, j = 0; i < 4; i += 2, j++) { + error = urtw_eprom_read32(sc, URTW_EPROM_TXPW1 + j, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[i + 6] = data & 0xf; + sc->sc_txpwr_cck[i + 6 + 1] = (data & 0xf00) >> 8; + sc->sc_txpwr_ofdm[i + 6] = (data & 0xf0) >> 4; + sc->sc_txpwr_ofdm[i + 6 + 1] = (data & 0xf000) >> 12; + } + if (sc->sc_flags & URTW_RTL8187B) { + error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[1 + 6 + 4] = data & 0xf; + sc->sc_txpwr_ofdm[1 + 6 + 4] = (data & 0xf0) >> 4; + error = urtw_eprom_read32(sc, 0x0a, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[2 + 6 + 4] = data & 0xf; + sc->sc_txpwr_ofdm[2 + 6 + 4] = (data & 0xf0) >> 4; + error = urtw_eprom_read32(sc, 0x1c, &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[3 + 6 + 4] = data & 0xf; + sc->sc_txpwr_cck[3 + 6 + 4 + 1] = (data & 0xf00) >> 8; + sc->sc_txpwr_ofdm[3 + 6 + 4] = (data & 0xf0) >> 4; + sc->sc_txpwr_ofdm[3 + 6 + 4 + 1] = (data & 0xf000) >> 12; } else { - panic("unsupported LED type 0x%x", type); - /* never reach */ + for (i = 1, j = 0; i < 4; i += 2, j++) { + error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2 + j, + &data); + if (error != 0) + goto fail; + sc->sc_txpwr_cck[i + 6 + 4] = data & 0xf; + sc->sc_txpwr_cck[i + 6 + 4 + 1] = (data & 0xf00) >> 8; + sc->sc_txpwr_ofdm[i + 6 + 4] = (data & 0xf0) >> 4; + sc->sc_txpwr_ofdm[i + 6 + 4 + 1] = (data & 0xf000) >> 12; + } } - - sc->sc_gpio_ledon = 1; fail: return (error); } + static usb_error_t -urtw_led_off(struct urtw_softc *sc, int type) +urtw_get_rfchip(struct urtw_softc *sc) { + int ret; + uint8_t data8; + uint32_t data; usb_error_t error; - if (type == URTW_LED_GPIO) { - switch (sc->sc_gpio_ledpin) { - case URTW_LED_PIN_GPIO0: - urtw_write8_m(sc, URTW_GPIO, URTW_GPIO_DATA_MAGIC1); - urtw_write8_m(sc, - URTW_GP_ENABLE, URTW_GP_ENABLE_DATA_MAGIC1); - break; - default: - panic("unsupported LED PIN type 0x%x", - sc->sc_gpio_ledpin); - /* never reach */ + error = urtw_eprom_read32(sc, URTW_EPROM_RFCHIPID, &data); + if (error != 0) + goto fail; + switch (data & 0xff) { + case URTW_EPROM_RFCHIPID_RTL8225U: + error = urtw_8225_isv2(sc, &ret); + if (error != 0) + goto fail; + if (ret == 0) { + sc->sc_rf_init = urtw_8225_rf_init; + sc->sc_rf_set_sens = urtw_8225_rf_set_sens; + sc->sc_rf_set_chan = urtw_8225_rf_set_chan; + sc->sc_rf_stop = urtw_8225_rf_stop; + } else { + sc->sc_rf_init = urtw_8225v2_rf_init; + sc->sc_rf_set_chan = urtw_8225v2_rf_set_chan; + sc->sc_rf_stop = urtw_8225_rf_stop; } - } else { - panic("unsupported LED type 0x%x", type); + sc->sc_max_sens = URTW_8225_RF_MAX_SENS; + sc->sc_sens = URTW_8225_RF_DEF_SENS; + break; + case URTW_EPROM_RFCHIPID_RTL8225Z2: + sc->sc_rf_init = urtw_8225v2b_rf_init; + sc->sc_rf_set_chan = urtw_8225v2b_rf_set_chan; + sc->sc_max_sens = URTW_8225_RF_MAX_SENS; + sc->sc_sens = URTW_8225_RF_DEF_SENS; + sc->sc_rf_stop = urtw_8225_rf_stop; + break; + default: + panic("unsupported RF chip %d\n", data & 0xff); /* never reach */ } - sc->sc_gpio_ledon = 0; + if (sc->sc_flags & URTW_RTL8187B) { + urtw_read8_m(sc, 0xe1, &data8); + sc->sc_flags |= (data8 == 0) ? URTW_RTL8187B_REV_B : + (data8 == 1) ? URTW_RTL8187B_REV_D : URTW_RTL8187B_REV_E; + } + + device_printf(sc->sc_dev, "%s rf %s hwrev %s\n", + (sc->sc_flags & URTW_RTL8187B) ? "rtl8187b" : "rtl8187l", + ((data & 0xff) == URTW_EPROM_RFCHIPID_RTL8225U) ? "rtl8225u" : + "rtl8225z2", + (sc->sc_flags & URTW_RTL8187B) ? ((data8 == 0) ? "b" : + (data8 == 1) ? "d" : "e") : "none"); fail: return (error); @@ -2397,705 +2455,372 @@ fail: static usb_error_t -urtw_led_mode0(struct urtw_softc *sc, int mode) +urtw_led_init(struct urtw_softc *sc) { + uint32_t rev; + usb_error_t error; - switch (mode) { - case URTW_LED_CTL_POWER_ON: - sc->sc_gpio_ledstate = URTW_LED_POWER_ON_BLINK; - break; - case URTW_LED_CTL_TX: - if (sc->sc_gpio_ledinprogress == 1) - return (0); - - sc->sc_gpio_ledstate = URTW_LED_BLINK_NORMAL; - sc->sc_gpio_blinktime = 2; - break; - case URTW_LED_CTL_LINK: - sc->sc_gpio_ledstate = URTW_LED_ON; - break; - default: - panic("unsupported LED mode 0x%x", mode); - /* never reach */ - } + urtw_read8_m(sc, URTW_PSR, &sc->sc_psr); + error = urtw_eprom_read32(sc, URTW_EPROM_SWREV, &rev); + if (error != 0) + goto fail; - switch (sc->sc_gpio_ledstate) { - case URTW_LED_ON: - if (sc->sc_gpio_ledinprogress != 0) - break; - urtw_led_on(sc, URTW_LED_GPIO); + switch (rev & URTW_EPROM_CID_MASK) { + case URTW_EPROM_CID_ALPHA0: + sc->sc_strategy = URTW_SW_LED_MODE1; break; - case URTW_LED_BLINK_NORMAL: - if (sc->sc_gpio_ledinprogress != 0) - break; - sc->sc_gpio_ledinprogress = 1; - sc->sc_gpio_blinkstate = (sc->sc_gpio_ledon != 0) ? - URTW_LED_OFF : URTW_LED_ON; - usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc); + case URTW_EPROM_CID_SERCOMM_PS: + sc->sc_strategy = URTW_SW_LED_MODE3; break; - case URTW_LED_POWER_ON_BLINK: - urtw_led_on(sc, URTW_LED_GPIO); - usb_pause_mtx(&sc->sc_mtx, 100); - urtw_led_off(sc, URTW_LED_GPIO); + case URTW_EPROM_CID_HW_LED: + sc->sc_strategy = URTW_HW_LED; break; + case URTW_EPROM_CID_RSVD0: + case URTW_EPROM_CID_RSVD1: default: - panic("unknown LED status 0x%x", sc->sc_gpio_ledstate); - /* never reach */ + sc->sc_strategy = URTW_SW_LED_MODE0; + break; } - return (0); -} -static usb_error_t -urtw_led_mode1(struct urtw_softc *sc, int mode) -{ + sc->sc_gpio_ledpin = URTW_LED_PIN_GPIO0; - return (USB_ERR_INVAL); +fail: + return (error); } -static usb_error_t -urtw_led_mode2(struct urtw_softc *sc, int mode) -{ - - return (USB_ERR_INVAL); -} static usb_error_t -urtw_led_mode3(struct urtw_softc *sc, int mode) +urtw_8225_rf_init(struct urtw_softc *sc) { +#define N(a) (sizeof(a) / sizeof((a)[0])) + int i; + uint16_t data; + usb_error_t error; - return (USB_ERR_INVAL); -} + error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); + if (error) + goto fail; -static void -urtw_ledtask(void *arg, int pending) -{ - struct urtw_softc *sc = arg; + error = urtw_8225_usb_init(sc); + if (error) + goto fail; - if (sc->sc_strategy != URTW_SW_LED_MODE0) - panic("could not process a LED strategy 0x%x", sc->sc_strategy); + urtw_write32_m(sc, URTW_RF_TIMING, 0x000a8008); + urtw_read16_m(sc, URTW_BRSR, &data); /* XXX ??? */ + urtw_write16_m(sc, URTW_BRSR, 0xffff); + urtw_write32_m(sc, URTW_RF_PARA, 0x100044); - URTW_LOCK(sc); - urtw_led_blink(sc); - URTW_UNLOCK(sc); -} + error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); + if (error) + goto fail; + urtw_write8_m(sc, URTW_CONFIG3, 0x44); + error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); + if (error) + goto fail; -static usb_error_t -urtw_led_ctl(struct urtw_softc *sc, int mode) -{ - usb_error_t error = 0; + error = urtw_8185_rf_pins_enable(sc); + if (error) + goto fail; + usb_pause_mtx(&sc->sc_mtx, 1000); - switch (sc->sc_strategy) { - case URTW_SW_LED_MODE0: - error = urtw_led_mode0(sc, mode); - break; - case URTW_SW_LED_MODE1: - error = urtw_led_mode1(sc, mode); - break; - case URTW_SW_LED_MODE2: - error = urtw_led_mode2(sc, mode); - break; - case URTW_SW_LED_MODE3: - error = urtw_led_mode3(sc, mode); - break; - default: - panic("unsupported LED mode %d\n", sc->sc_strategy); - /* never reach */ + for (i = 0; i < N(urtw_8225_rf_part1); i++) { + urtw_8225_write(sc, urtw_8225_rf_part1[i].reg, + urtw_8225_rf_part1[i].val); + usb_pause_mtx(&sc->sc_mtx, 1); } + usb_pause_mtx(&sc->sc_mtx, 100); + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); + usb_pause_mtx(&sc->sc_mtx, 200); + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); + usb_pause_mtx(&sc->sc_mtx, 200); + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC3); - return (error); -} + for (i = 0; i < 95; i++) { + urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1)); + urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, urtw_8225_rxgain[i]); + } -static usb_error_t -urtw_led_blink(struct urtw_softc *sc) -{ - uint8_t ing = 0; - usb_error_t error; + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC4); + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC5); - if (sc->sc_gpio_blinkstate == URTW_LED_ON) - error = urtw_led_on(sc, URTW_LED_GPIO); - else - error = urtw_led_off(sc, URTW_LED_GPIO); - sc->sc_gpio_blinktime--; - if (sc->sc_gpio_blinktime == 0) - ing = 1; - else { - if (sc->sc_gpio_ledstate != URTW_LED_BLINK_NORMAL && - sc->sc_gpio_ledstate != URTW_LED_BLINK_SLOWLY && - sc->sc_gpio_ledstate != URTW_LED_BLINK_CM3) - ing = 1; + for (i = 0; i < 128; i++) { + urtw_8187_write_phy_ofdm(sc, 0xb, urtw_8225_agc[i]); + usb_pause_mtx(&sc->sc_mtx, 1); + urtw_8187_write_phy_ofdm(sc, 0xa, (uint8_t)i + 0x80); + usb_pause_mtx(&sc->sc_mtx, 1); } - if (ing == 1) { - if (sc->sc_gpio_ledstate == URTW_LED_ON && - sc->sc_gpio_ledon == 0) - error = urtw_led_on(sc, URTW_LED_GPIO); - else if (sc->sc_gpio_ledstate == URTW_LED_OFF && - sc->sc_gpio_ledon == 1) - error = urtw_led_off(sc, URTW_LED_GPIO); - sc->sc_gpio_blinktime = 0; - sc->sc_gpio_ledinprogress = 0; - return (0); + for (i = 0; i < N(urtw_8225_rf_part2); i++) { + urtw_8187_write_phy_ofdm(sc, urtw_8225_rf_part2[i].reg, + urtw_8225_rf_part2[i].val); + usb_pause_mtx(&sc->sc_mtx, 1); } - sc->sc_gpio_blinkstate = (sc->sc_gpio_blinkstate != URTW_LED_ON) ? - URTW_LED_ON : URTW_LED_OFF; + error = urtw_8225_setgain(sc, 4); + if (error) + goto fail; - switch (sc->sc_gpio_ledstate) { - case URTW_LED_BLINK_NORMAL: - usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc); - break; - default: - panic("unknown LED status 0x%x", sc->sc_gpio_ledstate); - /* never reach */ + for (i = 0; i < N(urtw_8225_rf_part3); i++) { + urtw_8187_write_phy_cck(sc, urtw_8225_rf_part3[i].reg, + urtw_8225_rf_part3[i].val); + usb_pause_mtx(&sc->sc_mtx, 1); } - return (0); -} -static usb_error_t -urtw_update_msr(struct urtw_softc *sc) -{ - struct ifnet *ifp = sc->sc_ifp; - struct ieee80211com *ic = ifp->if_l2com; - uint8_t data; - usb_error_t error; + urtw_write8_m(sc, URTW_TESTR, 0x0d); - urtw_read8_m(sc, URTW_MSR, &data); - data &= ~URTW_MSR_LINK_MASK; + error = urtw_8225_set_txpwrlvl(sc, 1); + if (error) + goto fail; - if (sc->sc_state == IEEE80211_S_RUN) { - switch (ic->ic_opmode) { - case IEEE80211_M_STA: - case IEEE80211_M_MONITOR: - data |= URTW_MSR_LINK_STA; - if (sc->sc_flags & URTW_RTL8187B) - data |= URTW_MSR_LINK_ENEDCA; - break; - case IEEE80211_M_IBSS: - data |= URTW_MSR_LINK_ADHOC; - break; - case IEEE80211_M_HOSTAP: - data |= URTW_MSR_LINK_HOSTAP; - break; - default: - panic("unsupported operation mode 0x%x\n", - ic->ic_opmode); - /* never reach */ - } - } else - data |= URTW_MSR_LINK_NONE; + urtw_8187_write_phy_cck(sc, 0x10, 0x9b); + usb_pause_mtx(&sc->sc_mtx, 1); + urtw_8187_write_phy_ofdm(sc, 0x26, 0x90); + usb_pause_mtx(&sc->sc_mtx, 1); - urtw_write8_m(sc, URTW_MSR, data); + /* TX ant A, 0x0 for B */ + error = urtw_8185_tx_antenna(sc, 0x3); + if (error) + goto fail; + urtw_write32_m(sc, URTW_HSSI_PARA, 0x3dc00002); + + error = urtw_8225_rf_set_chan(sc, 1); fail: return (error); +#undef N } -static uint16_t -urtw_rate2rtl(int rate) +static usb_error_t +urtw_8185_rf_pins_enable(struct urtw_softc *sc) { - int i; - - for (i = 0; i < nitems(urtw_ratetable); i++) { - if (rate == urtw_ratetable[i].reg) - return urtw_ratetable[i].val; - } + usb_error_t error = 0; - return (3); + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x1ff7); +fail: + return (error); } -static uint16_t -urtw_rtl2rate(int rate) +static usb_error_t +urtw_8185_tx_antenna(struct urtw_softc *sc, uint8_t ant) { - int i; - - for (i = 0; i < nitems(urtw_ratetable); i++) { - if (rate == urtw_ratetable[i].val) - return urtw_ratetable[i].reg; - } + usb_error_t error; - return (0); + urtw_write8_m(sc, URTW_TX_ANTENNA, ant); + usb_pause_mtx(&sc->sc_mtx, 1); +fail: + return (error); } static usb_error_t -urtw_set_rate(struct urtw_softc *sc) +urtw_8187_write_phy_ofdm_c(struct urtw_softc *sc, uint8_t addr, uint32_t data) { - int i, basic_rate, min_rr_rate, max_rr_rate; - uint16_t data; - usb_error_t error; - basic_rate = urtw_rate2rtl(48); - min_rr_rate = urtw_rate2rtl(12); - max_rr_rate = urtw_rate2rtl(48); + data = data & 0xff; + return urtw_8187_write_phy(sc, addr, data); +} - urtw_write8_m(sc, URTW_RESP_RATE, - max_rr_rate << URTW_RESP_MAX_RATE_SHIFT | - min_rr_rate << URTW_RESP_MIN_RATE_SHIFT); +static usb_error_t +urtw_8187_write_phy_cck_c(struct urtw_softc *sc, uint8_t addr, uint32_t data) +{ - urtw_read16_m(sc, URTW_BRSR, &data); - data &= ~URTW_BRSR_MBR_8185; + data = data & 0xff; + return urtw_8187_write_phy(sc, addr, data | 0x10000); +} - for (i = 0; i <= basic_rate; i++) - data |= (1 << i); +static usb_error_t +urtw_8187_write_phy(struct urtw_softc *sc, uint8_t addr, uint32_t data) +{ + uint32_t phyw; + usb_error_t error; - urtw_write16_m(sc, URTW_BRSR, data); + phyw = ((data << 8) | (addr | 0x80)); + urtw_write8_m(sc, URTW_PHY_MAGIC4, ((phyw & 0xff000000) >> 24)); + urtw_write8_m(sc, URTW_PHY_MAGIC3, ((phyw & 0x00ff0000) >> 16)); + urtw_write8_m(sc, URTW_PHY_MAGIC2, ((phyw & 0x0000ff00) >> 8)); + urtw_write8_m(sc, URTW_PHY_MAGIC1, ((phyw & 0x000000ff))); + usb_pause_mtx(&sc->sc_mtx, 1); fail: return (error); } static usb_error_t -urtw_intr_enable(struct urtw_softc *sc) +urtw_8225_setgain(struct urtw_softc *sc, int16_t gain) { usb_error_t error; - urtw_write16_m(sc, URTW_INTR_MASK, 0xffff); + urtw_8187_write_phy_ofdm(sc, 0x0d, urtw_8225_gain[gain * 4]); + urtw_8187_write_phy_ofdm(sc, 0x1b, urtw_8225_gain[gain * 4 + 2]); + urtw_8187_write_phy_ofdm(sc, 0x1d, urtw_8225_gain[gain * 4 + 3]); + urtw_8187_write_phy_ofdm(sc, 0x23, urtw_8225_gain[gain * 4 + 1]); fail: return (error); } static usb_error_t -urtw_rx_setconf(struct urtw_softc *sc) +urtw_8225_usb_init(struct urtw_softc *sc) { - struct ifnet *ifp = sc->sc_ifp; - struct ieee80211com *ic = ifp->if_l2com; - uint32_t data; + uint8_t data; usb_error_t error; - urtw_read32_m(sc, URTW_RX, &data); - data = data &~ URTW_RX_FILTER_MASK; - if (sc->sc_flags & URTW_RTL8187B) { - data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA | - URTW_RX_FILTER_MCAST | URTW_RX_FILTER_BCAST | - URTW_RX_FILTER_NICMAC | URTW_RX_CHECK_BSSID | - URTW_RX_FIFO_THRESHOLD_NONE | - URTW_MAX_RX_DMA_2048 | - URTW_RX_AUTORESETPHY | URTW_RCR_ONLYERLPKT; - } else { - data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA; - data = data | URTW_RX_FILTER_BCAST | URTW_RX_FILTER_MCAST; - - if (ic->ic_opmode == IEEE80211_M_MONITOR) { - data = data | URTW_RX_FILTER_ICVERR; - data = data | URTW_RX_FILTER_PWR; - } - if (sc->sc_crcmon == 1 && ic->ic_opmode == IEEE80211_M_MONITOR) - data = data | URTW_RX_FILTER_CRCERR; - - if (ic->ic_opmode == IEEE80211_M_MONITOR || - (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) { - data = data | URTW_RX_FILTER_ALLMAC; - } else { - data = data | URTW_RX_FILTER_NICMAC; - data = data | URTW_RX_CHECK_BSSID; - } + urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 0); + urtw_write8_m(sc, URTW_GPIO, 0); + error = urtw_read8e(sc, 0x53, &data); + if (error) + goto fail; + error = urtw_write8e(sc, 0x53, data | (1 << 7)); + if (error) + goto fail; + urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 4); + urtw_write8_m(sc, URTW_GPIO, 0x20); + urtw_write8_m(sc, URTW_GP_ENABLE, 0); - data = data &~ URTW_RX_FIFO_THRESHOLD_MASK; - data = data | URTW_RX_FIFO_THRESHOLD_NONE | - URTW_RX_AUTORESETPHY; - data = data &~ URTW_MAX_RX_DMA_MASK; - data = data | URTW_MAX_RX_DMA_2048 | URTW_RCR_ONLYERLPKT; - } + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, 0x80); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, 0x80); + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x80); - urtw_write32_m(sc, URTW_RX, data); + usb_pause_mtx(&sc->sc_mtx, 500); fail: return (error); } - static usb_error_t -urtw_rx_enable(struct urtw_softc *sc) +urtw_8225_write_c(struct urtw_softc *sc, uint8_t addr, uint16_t data) { - uint8_t data; + uint16_t d80, d82, d84; usb_error_t error; - usbd_transfer_start((sc->sc_flags & URTW_RTL8187B) ? - sc->sc_xfer[URTW_8187B_BULK_RX] : sc->sc_xfer[URTW_8187L_BULK_RX]); + urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &d80); + d80 &= URTW_RF_PINS_MAGIC1; + urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &d82); + urtw_read16_m(sc, URTW_RF_PINS_SELECT, &d84); + d84 &= URTW_RF_PINS_MAGIC2; + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, d82 | URTW_RF_PINS_MAGIC3); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84 | URTW_RF_PINS_MAGIC3); + DELAY(10); - error = urtw_rx_setconf(sc); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80); + DELAY(10); + + error = urtw_8225_write_s16(sc, addr, 0x8225, &data); if (error != 0) goto fail; - urtw_read8_m(sc, URTW_CMD, &data); - urtw_write8_m(sc, URTW_CMD, data | URTW_CMD_RX_ENABLE); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); + DELAY(10); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84); + usb_pause_mtx(&sc->sc_mtx, 2); fail: return (error); } +/* XXX why we should allocalte memory buffer instead of using memory stack? */ static usb_error_t -urtw_tx_enable(struct urtw_softc *sc) +urtw_8225_write_s16(struct urtw_softc *sc, uint8_t addr, int index, + uint16_t *data) { - uint8_t data8; - uint32_t data; - usb_error_t error; - - if (sc->sc_flags & URTW_RTL8187B) { - urtw_read32_m(sc, URTW_TX_CONF, &data); - data &= ~URTW_TX_LOOPBACK_MASK; - data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK); - data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK); - data &= ~URTW_TX_SWPLCPLEN; - data |= URTW_TX_HW_SEQNUM | URTW_TX_DISREQQSIZE | - (7 << 8) | /* short retry limit */ - (7 << 0) | /* long retry limit */ - (7 << 21); /* MAX TX DMA */ - urtw_write32_m(sc, URTW_TX_CONF, data); + uint8_t *buf; + uint16_t data16; + struct usb_device_request *req; + usb_error_t error = 0; - urtw_read8_m(sc, URTW_CMD, &data8); - urtw_write8_m(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE); - return (error); + data16 = *data; + req = (usb_device_request_t *)malloc(sizeof(usb_device_request_t), + M_80211_VAP, M_NOWAIT | M_ZERO); + if (req == NULL) { + device_printf(sc->sc_dev, "could not allocate a memory\n"); + goto fail0; + } + buf = (uint8_t *)malloc(2, M_80211_VAP, M_NOWAIT | M_ZERO); + if (req == NULL) { + device_printf(sc->sc_dev, "could not allocate a memory\n"); + goto fail1; } - urtw_read8_m(sc, URTW_CW_CONF, &data8); - data8 &= ~(URTW_CW_CONF_PERPACKET_CW | URTW_CW_CONF_PERPACKET_RETRY); - urtw_write8_m(sc, URTW_CW_CONF, data8); - - urtw_read8_m(sc, URTW_TX_AGC_CTL, &data8); - data8 &= ~URTW_TX_AGC_CTL_PERPACKET_GAIN; - data8 &= ~URTW_TX_AGC_CTL_PERPACKET_ANTSEL; - data8 &= ~URTW_TX_AGC_CTL_FEEDBACK_ANT; - urtw_write8_m(sc, URTW_TX_AGC_CTL, data8); + req->bmRequestType = UT_WRITE_VENDOR_DEVICE; + req->bRequest = URTW_8187_SETREGS_REQ; + USETW(req->wValue, addr); + USETW(req->wIndex, index); + USETW(req->wLength, sizeof(uint16_t)); + buf[0] = (data16 & 0x00ff); + buf[1] = (data16 & 0xff00) >> 8; - urtw_read32_m(sc, URTW_TX_CONF, &data); - data &= ~URTW_TX_LOOPBACK_MASK; - data |= URTW_TX_LOOPBACK_NONE; - data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK); - data |= sc->sc_tx_retry << URTW_TX_DPRETRY_SHIFT; - data |= sc->sc_rts_retry << URTW_TX_RTSRETRY_SHIFT; - data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK); - data |= URTW_TX_MXDMA_2048 | URTW_TX_CWMIN | URTW_TX_DISCW; - data &= ~URTW_TX_SWPLCPLEN; - data |= URTW_TX_NOICV; - urtw_write32_m(sc, URTW_TX_CONF, data); + error = urtw_do_request(sc, req, buf); - urtw_read8_m(sc, URTW_CMD, &data8); - urtw_write8_m(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE); -fail: - return (error); + free(buf, M_80211_VAP); +fail1: free(req, M_80211_VAP); +fail0: return (error); } - static usb_error_t -urtw_get_txpwr(struct urtw_softc *sc) +urtw_8225_rf_set_chan(struct urtw_softc *sc, int chan) { - int i, j; - uint32_t data; + struct ieee80211com *ic = sc->sc_ifp->if_l2com; + struct ieee80211_channel *c = ic->ic_curchan; usb_error_t error; - error = urtw_eprom_read32(sc, URTW_EPROM_TXPW_BASE, &data); - if (error != 0) + error = urtw_8225_set_txpwrlvl(sc, chan); + if (error) goto fail; - sc->sc_txpwr_cck_base = data & 0xf; - sc->sc_txpwr_ofdm_base = (data >> 4) & 0xf; + urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]); + usb_pause_mtx(&sc->sc_mtx, 10); - for (i = 1, j = 0; i < 6; i += 2, j++) { - error = urtw_eprom_read32(sc, URTW_EPROM_TXPW0 + j, &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[i] = data & 0xf; - sc->sc_txpwr_cck[i + 1] = (data & 0xf00) >> 8; - sc->sc_txpwr_ofdm[i] = (data & 0xf0) >> 4; - sc->sc_txpwr_ofdm[i + 1] = (data & 0xf000) >> 12; - } - for (i = 1, j = 0; i < 4; i += 2, j++) { - error = urtw_eprom_read32(sc, URTW_EPROM_TXPW1 + j, &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[i + 6] = data & 0xf; - sc->sc_txpwr_cck[i + 6 + 1] = (data & 0xf00) >> 8; - sc->sc_txpwr_ofdm[i + 6] = (data & 0xf0) >> 4; - sc->sc_txpwr_ofdm[i + 6 + 1] = (data & 0xf000) >> 12; - } - if (sc->sc_flags & URTW_RTL8187B) { - error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2, &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[1 + 6 + 4] = data & 0xf; - sc->sc_txpwr_ofdm[1 + 6 + 4] = (data & 0xf0) >> 4; - error = urtw_eprom_read32(sc, 0x0a, &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[2 + 6 + 4] = data & 0xf; - sc->sc_txpwr_ofdm[2 + 6 + 4] = (data & 0xf0) >> 4; - error = urtw_eprom_read32(sc, 0x1c, &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[3 + 6 + 4] = data & 0xf; - sc->sc_txpwr_cck[3 + 6 + 4 + 1] = (data & 0xf00) >> 8; - sc->sc_txpwr_ofdm[3 + 6 + 4] = (data & 0xf0) >> 4; - sc->sc_txpwr_ofdm[3 + 6 + 4 + 1] = (data & 0xf000) >> 12; + urtw_write8_m(sc, URTW_SIFS, 0x22); + + if (sc->sc_state == IEEE80211_S_ASSOC && + ic->ic_flags & IEEE80211_F_SHSLOT) + urtw_write8_m(sc, URTW_SLOT, 0x9); + else + urtw_write8_m(sc, URTW_SLOT, 0x14); + + if (IEEE80211_IS_CHAN_G(c)) { + /* for G */ + urtw_write8_m(sc, URTW_DIFS, 0x14); + urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x14); + urtw_write8_m(sc, URTW_CW_VAL, 0x73); } else { - for (i = 1, j = 0; i < 4; i += 2, j++) { - error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2 + j, - &data); - if (error != 0) - goto fail; - sc->sc_txpwr_cck[i + 6 + 4] = data & 0xf; - sc->sc_txpwr_cck[i + 6 + 4 + 1] = (data & 0xf00) >> 8; - sc->sc_txpwr_ofdm[i + 6 + 4] = (data & 0xf0) >> 4; - sc->sc_txpwr_ofdm[i + 6 + 4 + 1] = (data & 0xf000) >> 12; - } + /* for B */ + urtw_write8_m(sc, URTW_DIFS, 0x24); + urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x24); + urtw_write8_m(sc, URTW_CW_VAL, 0xa5); } + fail: return (error); } - static usb_error_t -urtw_get_rfchip(struct urtw_softc *sc) +urtw_8225_rf_set_sens(struct urtw_softc *sc, int sens) { - int ret; - uint8_t data8; - uint32_t data; usb_error_t error; - error = urtw_eprom_read32(sc, URTW_EPROM_RFCHIPID, &data); - if (error != 0) + if (sens < 0 || sens > 6) + return -1; + + if (sens > 4) + urtw_8225_write(sc, + URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC1); + else + urtw_8225_write(sc, + URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC2); + + sens = 6 - sens; + error = urtw_8225_setgain(sc, sens); + if (error) goto fail; - switch (data & 0xff) { - case URTW_EPROM_RFCHIPID_RTL8225U: - error = urtw_8225_isv2(sc, &ret); - if (error != 0) - goto fail; - if (ret == 0) { - sc->sc_rf_init = urtw_8225_rf_init; - sc->sc_rf_set_sens = urtw_8225_rf_set_sens; - sc->sc_rf_set_chan = urtw_8225_rf_set_chan; - sc->sc_rf_stop = urtw_8225_rf_stop; - } else { - sc->sc_rf_init = urtw_8225v2_rf_init; - sc->sc_rf_set_chan = urtw_8225v2_rf_set_chan; - sc->sc_rf_stop = urtw_8225_rf_stop; - } - sc->sc_max_sens = URTW_8225_RF_MAX_SENS; - sc->sc_sens = URTW_8225_RF_DEF_SENS; - break; - case URTW_EPROM_RFCHIPID_RTL8225Z2: - sc->sc_rf_init = urtw_8225v2b_rf_init; - sc->sc_rf_set_chan = urtw_8225v2b_rf_set_chan; - sc->sc_max_sens = URTW_8225_RF_MAX_SENS; - sc->sc_sens = URTW_8225_RF_DEF_SENS; - sc->sc_rf_stop = urtw_8225_rf_stop; - break; - default: - panic("unsupported RF chip %d\n", data & 0xff); - /* never reach */ - } - - if (sc->sc_flags & URTW_RTL8187B) { - urtw_read8_m(sc, 0xe1, &data8); - sc->sc_flags |= (data8 == 0) ? URTW_RTL8187B_REV_B : - (data8 == 1) ? URTW_RTL8187B_REV_D : URTW_RTL8187B_REV_E; - } - - device_printf(sc->sc_dev, "%s rf %s hwrev %s\n", - (sc->sc_flags & URTW_RTL8187B) ? "rtl8187b" : "rtl8187l", - ((data & 0xff) == URTW_EPROM_RFCHIPID_RTL8225U) ? "rtl8225u" : - "rtl8225z2", - (sc->sc_flags & URTW_RTL8187B) ? ((data8 == 0) ? "b" : - (data8 == 1) ? "d" : "e") : "none"); - -fail: - return (error); -} - - -static usb_error_t -urtw_led_init(struct urtw_softc *sc) -{ - uint32_t rev; - usb_error_t error; - - urtw_read8_m(sc, URTW_PSR, &sc->sc_psr); - error = urtw_eprom_read32(sc, URTW_EPROM_SWREV, &rev); - if (error != 0) - goto fail; - - switch (rev & URTW_EPROM_CID_MASK) { - case URTW_EPROM_CID_ALPHA0: - sc->sc_strategy = URTW_SW_LED_MODE1; - break; - case URTW_EPROM_CID_SERCOMM_PS: - sc->sc_strategy = URTW_SW_LED_MODE3; - break; - case URTW_EPROM_CID_HW_LED: - sc->sc_strategy = URTW_HW_LED; - break; - case URTW_EPROM_CID_RSVD0: - case URTW_EPROM_CID_RSVD1: - default: - sc->sc_strategy = URTW_SW_LED_MODE0; - break; - } - - sc->sc_gpio_ledpin = URTW_LED_PIN_GPIO0; - -fail: - return (error); -} - -static usb_error_t -urtw_8185_rf_pins_enable(struct urtw_softc *sc) -{ - usb_error_t error = 0; - - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x1ff7); -fail: - return (error); -} - -static usb_error_t -urtw_8185_tx_antenna(struct urtw_softc *sc, uint8_t ant) -{ - usb_error_t error; - - urtw_write8_m(sc, URTW_TX_ANTENNA, ant); - usb_pause_mtx(&sc->sc_mtx, 1); -fail: - return (error); -} - -static usb_error_t -urtw_8187_write_phy_ofdm_c(struct urtw_softc *sc, uint8_t addr, uint32_t data) -{ - - data = data & 0xff; - return urtw_8187_write_phy(sc, addr, data); -} - -static usb_error_t -urtw_8187_write_phy_cck_c(struct urtw_softc *sc, uint8_t addr, uint32_t data) -{ - - data = data & 0xff; - return urtw_8187_write_phy(sc, addr, data | 0x10000); -} - -static usb_error_t -urtw_8187_write_phy(struct urtw_softc *sc, uint8_t addr, uint32_t data) -{ - uint32_t phyw; - usb_error_t error; - - phyw = ((data << 8) | (addr | 0x80)); - urtw_write8_m(sc, URTW_PHY_MAGIC4, ((phyw & 0xff000000) >> 24)); - urtw_write8_m(sc, URTW_PHY_MAGIC3, ((phyw & 0x00ff0000) >> 16)); - urtw_write8_m(sc, URTW_PHY_MAGIC2, ((phyw & 0x0000ff00) >> 8)); - urtw_write8_m(sc, URTW_PHY_MAGIC1, ((phyw & 0x000000ff))); - usb_pause_mtx(&sc->sc_mtx, 1); -fail: - return (error); -} - -static usb_error_t -urtw_8225_setgain(struct urtw_softc *sc, int16_t gain) -{ - usb_error_t error; - - urtw_8187_write_phy_ofdm(sc, 0x0d, urtw_8225_gain[gain * 4]); - urtw_8187_write_phy_ofdm(sc, 0x1b, urtw_8225_gain[gain * 4 + 2]); - urtw_8187_write_phy_ofdm(sc, 0x1d, urtw_8225_gain[gain * 4 + 3]); - urtw_8187_write_phy_ofdm(sc, 0x23, urtw_8225_gain[gain * 4 + 1]); -fail: - return (error); -} - -static usb_error_t -urtw_8225_usb_init(struct urtw_softc *sc) -{ - uint8_t data; - usb_error_t error; - - urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 0); - urtw_write8_m(sc, URTW_GPIO, 0); - error = urtw_read8e(sc, 0x53, &data); - if (error) - goto fail; - error = urtw_write8e(sc, 0x53, data | (1 << 7)); - if (error) - goto fail; - urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 4); - urtw_write8_m(sc, URTW_GPIO, 0x20); - urtw_write8_m(sc, URTW_GP_ENABLE, 0); - - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, 0x80); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, 0x80); - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x80); - - usb_pause_mtx(&sc->sc_mtx, 500); -fail: - return (error); -} - -static usb_error_t -urtw_8225_write_c(struct urtw_softc *sc, uint8_t addr, uint16_t data) -{ - uint16_t d80, d82, d84; - usb_error_t error; - - urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &d80); - d80 &= URTW_RF_PINS_MAGIC1; - urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &d82); - urtw_read16_m(sc, URTW_RF_PINS_SELECT, &d84); - d84 &= URTW_RF_PINS_MAGIC2; - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, d82 | URTW_RF_PINS_MAGIC3); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84 | URTW_RF_PINS_MAGIC3); - DELAY(10); - - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80); - DELAY(10); - error = urtw_8225_write_s16(sc, addr, 0x8225, &data); - if (error != 0) - goto fail; + urtw_8187_write_phy_cck(sc, 0x41, urtw_8225_threshold[sens]); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); - DELAY(10); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84); - usb_pause_mtx(&sc->sc_mtx, 2); fail: return (error); } -/* XXX why we should allocalte memory buffer instead of using memory stack? */ -static usb_error_t -urtw_8225_write_s16(struct urtw_softc *sc, uint8_t addr, int index, - uint16_t *data) -{ - uint8_t *buf; - uint16_t data16; - struct usb_device_request *req; - usb_error_t error = 0; - - data16 = *data; - req = (usb_device_request_t *)malloc(sizeof(usb_device_request_t), - M_80211_VAP, M_NOWAIT | M_ZERO); - if (req == NULL) { - device_printf(sc->sc_dev, "could not allocate a memory\n"); - goto fail0; - } - buf = (uint8_t *)malloc(2, M_80211_VAP, M_NOWAIT | M_ZERO); - if (req == NULL) { - device_printf(sc->sc_dev, "could not allocate a memory\n"); - goto fail1; - } - - req->bmRequestType = UT_WRITE_VENDOR_DEVICE; - req->bRequest = URTW_8187_SETREGS_REQ; - USETW(req->wValue, addr); - USETW(req->wIndex, index); - USETW(req->wLength, sizeof(uint16_t)); - buf[0] = (data16 & 0x00ff); - buf[1] = (data16 & 0xff00) >> 8; - - error = urtw_do_request(sc, req, buf); - - free(buf, M_80211_VAP); -fail1: free(req, M_80211_VAP); -fail0: return (error); -} - - static usb_error_t urtw_8225_set_txpwrlvl(struct urtw_softc *sc, int chan) { @@ -3184,12 +2909,13 @@ fail: return (error); } - static usb_error_t -urtw_8225_rf_init(struct urtw_softc *sc) +urtw_8225v2_rf_init(struct urtw_softc *sc) { +#define N(a) (sizeof(a) / sizeof((a)[0])) int i; uint16_t data; + uint32_t data32; usb_error_t error; error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); @@ -3216,66 +2942,86 @@ urtw_8225_rf_init(struct urtw_softc *sc) error = urtw_8185_rf_pins_enable(sc); if (error) goto fail; - usb_pause_mtx(&sc->sc_mtx, 1000); - for (i = 0; i < nitems(urtw_8225_rf_part1); i++) { - urtw_8225_write(sc, urtw_8225_rf_part1[i].reg, - urtw_8225_rf_part1[i].val); - usb_pause_mtx(&sc->sc_mtx, 1); + usb_pause_mtx(&sc->sc_mtx, 500); + + for (i = 0; i < N(urtw_8225v2_rf_part1); i++) { + urtw_8225_write(sc, urtw_8225v2_rf_part1[i].reg, + urtw_8225v2_rf_part1[i].val); } - usb_pause_mtx(&sc->sc_mtx, 100); - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); - usb_pause_mtx(&sc->sc_mtx, 200); - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); - usb_pause_mtx(&sc->sc_mtx, 200); - urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC3); + usb_pause_mtx(&sc->sc_mtx, 50); - for (i = 0; i < nitems(urtw_8225_rxgain); i++) { + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC1); + + for (i = 0; i < 95; i++) { urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1)); - urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, urtw_8225_rxgain[i]); + urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, + urtw_8225v2_rxgain[i]); } urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC4); + URTW_8225_ADDR_3_MAGIC, URTW_8225_ADDR_3_DATA_MAGIC1); urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC5); + URTW_8225_ADDR_5_MAGIC, URTW_8225_ADDR_5_DATA_MAGIC1); + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC2); + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); + usb_pause_mtx(&sc->sc_mtx, 100); + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); + usb_pause_mtx(&sc->sc_mtx, 100); + + error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32); + if (error != 0) + goto fail; + if (data32 != URTW_8225_ADDR_6_DATA_MAGIC1) + device_printf(sc->sc_dev, "expect 0xe6!! (0x%x)\n", data32); + if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2)) { + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); + usb_pause_mtx(&sc->sc_mtx, 100); + urtw_8225_write(sc, + URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); + usb_pause_mtx(&sc->sc_mtx, 50); + error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32); + if (error != 0) + goto fail; + if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2)) + device_printf(sc->sc_dev, "RF calibration failed\n"); + } + usb_pause_mtx(&sc->sc_mtx, 100); - for (i = 0; i < nitems(urtw_8225_agc); i++) { + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC6); + for (i = 0; i < 128; i++) { urtw_8187_write_phy_ofdm(sc, 0xb, urtw_8225_agc[i]); - usb_pause_mtx(&sc->sc_mtx, 1); urtw_8187_write_phy_ofdm(sc, 0xa, (uint8_t)i + 0x80); - usb_pause_mtx(&sc->sc_mtx, 1); } - for (i = 0; i < nitems(urtw_8225_rf_part2); i++) { - urtw_8187_write_phy_ofdm(sc, urtw_8225_rf_part2[i].reg, - urtw_8225_rf_part2[i].val); - usb_pause_mtx(&sc->sc_mtx, 1); + for (i = 0; i < N(urtw_8225v2_rf_part2); i++) { + urtw_8187_write_phy_ofdm(sc, urtw_8225v2_rf_part2[i].reg, + urtw_8225v2_rf_part2[i].val); } - error = urtw_8225_setgain(sc, 4); + error = urtw_8225v2_setgain(sc, 4); if (error) goto fail; - for (i = 0; i < nitems(urtw_8225_rf_part3); i++) { - urtw_8187_write_phy_cck(sc, urtw_8225_rf_part3[i].reg, - urtw_8225_rf_part3[i].val); - usb_pause_mtx(&sc->sc_mtx, 1); + for (i = 0; i < N(urtw_8225v2_rf_part3); i++) { + urtw_8187_write_phy_cck(sc, urtw_8225v2_rf_part3[i].reg, + urtw_8225v2_rf_part3[i].val); } urtw_write8_m(sc, URTW_TESTR, 0x0d); - error = urtw_8225_set_txpwrlvl(sc, 1); + error = urtw_8225v2_set_txpwrlvl(sc, 1); if (error) goto fail; urtw_8187_write_phy_cck(sc, 0x10, 0x9b); - usb_pause_mtx(&sc->sc_mtx, 1); urtw_8187_write_phy_ofdm(sc, 0x26, 0x90); - usb_pause_mtx(&sc->sc_mtx, 1); /* TX ant A, 0x0 for B */ error = urtw_8185_tx_antenna(sc, 0x3); @@ -3286,24 +3032,26 @@ urtw_8225_rf_init(struct urtw_softc *sc) error = urtw_8225_rf_set_chan(sc, 1); fail: return (error); +#undef N } static usb_error_t -urtw_8225_rf_set_chan(struct urtw_softc *sc, int chan) +urtw_8225v2_rf_set_chan(struct urtw_softc *sc, int chan) { struct ieee80211com *ic = sc->sc_ifp->if_l2com; struct ieee80211_channel *c = ic->ic_curchan; usb_error_t error; - error = urtw_8225_set_txpwrlvl(sc, chan); + error = urtw_8225v2_set_txpwrlvl(sc, chan); if (error) goto fail; + urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]); usb_pause_mtx(&sc->sc_mtx, 10); urtw_write8_m(sc, URTW_SIFS, 0x22); - if (sc->sc_state == IEEE80211_S_ASSOC && + if(sc->sc_state == IEEE80211_S_ASSOC && ic->ic_flags & IEEE80211_F_SHSLOT) urtw_write8_m(sc, URTW_SLOT, 0x9); else @@ -3326,548 +3074,948 @@ fail: } static usb_error_t -urtw_8225_rf_set_sens(struct urtw_softc *sc, int sens) +urtw_8225_read(struct urtw_softc *sc, uint8_t addr, uint32_t *data) { + int i; + int16_t bit; + uint8_t rlen = 12, wlen = 6; + uint16_t o1, o2, o3, tmp; + uint32_t d2w = ((uint32_t)(addr & 0x1f)) << 27; + uint32_t mask = 0x80000000, value = 0; usb_error_t error; - if (sens < 0 || sens > 6) - return -1; + urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &o1); + urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &o2); + urtw_read16_m(sc, URTW_RF_PINS_SELECT, &o3); + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2 | URTW_RF_PINS_MAGIC4); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3 | URTW_RF_PINS_MAGIC4); + o1 &= ~URTW_RF_PINS_MAGIC4; + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN); + DELAY(5); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1); + DELAY(5); - if (sens > 4) - urtw_8225_write(sc, - URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC1); - else - urtw_8225_write(sc, - URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC2); + for (i = 0; i < (wlen / 2); i++, mask = mask >> 1) { + bit = ((d2w & mask) != 0) ? 1 : 0; - sens = 6 - sens; - error = urtw_8225_setgain(sc, sens); - if (error) - goto fail; + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | + URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | + URTW_BB_HOST_BANG_CLK); + DELAY(2); + mask = mask >> 1; + if (i == 2) + break; + bit = ((d2w & mask) != 0) ? 1 : 0; + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | + URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | + URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1); + DELAY(1); + } + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW | + URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_RW); + DELAY(2); - urtw_8187_write_phy_cck(sc, 0x41, urtw_8225_threshold[sens]); + mask = 0x800; + for (i = 0; i < rlen; i++, mask = mask >> 1) { + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, + o1 | URTW_BB_HOST_BANG_RW); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, + o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, + o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); + DELAY(2); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, + o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); + DELAY(2); + + urtw_read16_m(sc, URTW_RF_PINS_INPUT, &tmp); + value |= ((tmp & URTW_BB_HOST_BANG_CLK) ? mask : 0); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, + o1 | URTW_BB_HOST_BANG_RW); + DELAY(2); + } + + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN | + URTW_BB_HOST_BANG_RW); + DELAY(2); + + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3); + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_OUTPUT_MAGIC1); + if (data != NULL) + *data = value; fail: return (error); } -static void -urtw_stop_locked(struct ifnet *ifp, int disable) + +static usb_error_t +urtw_8225v2_set_txpwrlvl(struct urtw_softc *sc, int chan) { - struct urtw_softc *sc = ifp->if_softc; - uint8_t data8; + int i; + uint8_t *cck_pwrtable; + uint8_t cck_pwrlvl_max = 15, ofdm_pwrlvl_max = 25, ofdm_pwrlvl_min = 10; + uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff; + uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff; usb_error_t error; - (void)disable; - ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); + /* CCK power setting */ + cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? cck_pwrlvl_max : cck_pwrlvl; + cck_pwrlvl += sc->sc_txpwr_cck_base; + cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl; + cck_pwrtable = (chan == 14) ? urtw_8225v2_txpwr_cck_ch14 : + urtw_8225v2_txpwr_cck; - error = urtw_intr_disable(sc); - if (error) - goto fail; - urtw_read8_m(sc, URTW_CMD, &data8); - data8 &= ~(URTW_CMD_RX_ENABLE | URTW_CMD_TX_ENABLE); - urtw_write8_m(sc, URTW_CMD, data8); + for (i = 0; i < 8; i++) + urtw_8187_write_phy_cck(sc, 0x44 + i, cck_pwrtable[i]); - error = sc->sc_rf_stop(sc); - if (error != 0) - goto fail; + urtw_write8_m(sc, URTW_TX_GAIN_CCK, + urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl]); + usb_pause_mtx(&sc->sc_mtx, 1); - error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); - if (error) - goto fail; - urtw_read8_m(sc, URTW_CONFIG4, &data8); - urtw_write8_m(sc, URTW_CONFIG4, data8 | URTW_CONFIG4_VCOOFF); - error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); + /* OFDM power setting */ + ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ? + ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min; + ofdm_pwrlvl += sc->sc_txpwr_ofdm_base; + ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl; + + error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); if (error) goto fail; -fail: - if (error) - device_printf(sc->sc_dev, "failed to stop (%s)\n", - usbd_errstr(error)); - usb_callout_stop(&sc->sc_led_ch); - callout_stop(&sc->sc_watchdog_ch); + urtw_8187_write_phy_ofdm(sc, 2, 0x42); + urtw_8187_write_phy_ofdm(sc, 5, 0x0); + urtw_8187_write_phy_ofdm(sc, 6, 0x40); + urtw_8187_write_phy_ofdm(sc, 7, 0x0); + urtw_8187_write_phy_ofdm(sc, 8, 0x40); - urtw_abort_xfers(sc); + urtw_write8_m(sc, URTW_TX_GAIN_OFDM, + urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl]); + usb_pause_mtx(&sc->sc_mtx, 1); +fail: + return (error); } -static void -urtw_stop(struct ifnet *ifp, int disable) +static usb_error_t +urtw_8225v2_setgain(struct urtw_softc *sc, int16_t gain) { - struct urtw_softc *sc = ifp->if_softc; + uint8_t *gainp; + usb_error_t error; - URTW_LOCK(sc); - urtw_stop_locked(ifp, disable); - URTW_UNLOCK(sc); + /* XXX for A? */ + gainp = urtw_8225v2_gain_bg; + urtw_8187_write_phy_ofdm(sc, 0x0d, gainp[gain * 3]); + usb_pause_mtx(&sc->sc_mtx, 1); + urtw_8187_write_phy_ofdm(sc, 0x1b, gainp[gain * 3 + 1]); + usb_pause_mtx(&sc->sc_mtx, 1); + urtw_8187_write_phy_ofdm(sc, 0x1d, gainp[gain * 3 + 2]); + usb_pause_mtx(&sc->sc_mtx, 1); + urtw_8187_write_phy_ofdm(sc, 0x21, 0x17); + usb_pause_mtx(&sc->sc_mtx, 1); +fail: + return (error); } - -static int -urtw_isbmode(uint16_t rate) +static usb_error_t +urtw_8225_isv2(struct urtw_softc *sc, int *ret) { + uint32_t data; + usb_error_t error; - rate = urtw_rtl2rate(rate); + *ret = 1; - return ((rate <= 22 && rate != 12 && rate != 18) || - rate == 44) ? (1) : (0); -} - -static struct mbuf * -urtw_rxeof(struct usb_xfer *xfer, struct urtw_data *data, int *rssi_p, - int8_t *nf_p) -{ - int actlen, flen, len, nf = -95, rssi; - struct ieee80211_frame *wh; - struct mbuf *m, *mnew; - struct urtw_8187b_rxhdr *bhdr; - struct urtw_softc *sc = data->sc; - struct ifnet *ifp = sc->sc_ifp; - struct ieee80211com *ic = ifp->if_l2com; - uint8_t *desc, quality = 0, rate; + urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_MAGIC5); + urtw_write16_m(sc, URTW_RF_PINS_SELECT, URTW_RF_PINS_MAGIC5); + urtw_write16_m(sc, URTW_RF_PINS_ENABLE, URTW_RF_PINS_MAGIC5); + usb_pause_mtx(&sc->sc_mtx, 500); - usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); + urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, + URTW_8225_ADDR_0_DATA_MAGIC1); - if (actlen < URTW_MIN_RXBUFSZ) { - ifp->if_ierrors++; - return (NULL); + error = urtw_8225_read(sc, URTW_8225_ADDR_8_MAGIC, &data); + if (error != 0) + goto fail; + if (data != URTW_8225_ADDR_8_DATA_MAGIC1) + *ret = 0; + else { + error = urtw_8225_read(sc, URTW_8225_ADDR_9_MAGIC, &data); + if (error != 0) + goto fail; + if (data != URTW_8225_ADDR_9_DATA_MAGIC1) + *ret = 0; } - if (sc->sc_flags & URTW_RTL8187B) { - len = actlen - (sizeof(struct urtw_8187b_rxhdr)); - bhdr = (struct urtw_8187b_rxhdr *)(data->buf + len); - desc = data->buf + len; - flen = ((desc[1] & 0x0f) << 8) + (desc[0] & 0xff); - if (flen > actlen) { - ifp->if_ierrors++; - return (NULL); - } - rate = (le32toh(bhdr->flags) >> 20) & 0xf; - rssi = 14 + (bhdr->rssi / 2); - if (rssi > 95) - rssi = 95; - } else { - /* 4 dword and 4 byte CRC */ - len = actlen - (4 * 4); - desc = data->buf + len; - flen = ((desc[1] & 0x0f) << 8) + (desc[0] & 0xff); - if (flen > actlen) { - ifp->if_ierrors++; - return (NULL); - } + urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, + URTW_8225_ADDR_0_DATA_MAGIC2); +fail: + return (error); +} - rate = (desc[2] & 0xf0) >> 4; - quality = desc[4] & 0xff; - /* XXX correct? */ - rssi = (desc[6] & 0xfe) >> 1; - if (!urtw_isbmode(rate)) { - rssi = (rssi > 90) ? 90 : ((rssi < 25) ? 25 : rssi); - rssi = ((90 - rssi) * 100) / 65; - } else { - rssi = (rssi > 90) ? 95 : ((rssi < 30) ? 30 : rssi); - rssi = ((95 - rssi) * 100) / 65; - } - } +static usb_error_t +urtw_8225v2b_rf_init(struct urtw_softc *sc) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + int i; + usb_error_t error; - mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); - if (mnew == NULL) { - ifp->if_ierrors++; - return (NULL); - } + for (i = 0; i < N(urtw_8225v2b_rf_part1); i++) + urtw_8225_write(sc, urtw_8225v2b_rf_part1[i].reg, + urtw_8225v2b_rf_part1[i].val); - m = data->m; - data->m = mnew; - data->buf = mtod(mnew, uint8_t *); + urtw_8225_write(sc, + URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC1); - /* finalize mbuf */ - m->m_pkthdr.rcvif = ifp; - m->m_pkthdr.len = m->m_len = flen - 4; + for (i = 0; i < N(urtw_8225v2b_rxgain); i++) { + urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1)); + urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, + urtw_8225v2b_rxgain[i]); + } - if (ieee80211_radiotap_active(ic)) { - struct urtw_rx_radiotap_header *tap = &sc->sc_rxtap; + urtw_8225_write(sc, URTW_8225_ADDR_3_MAGIC, 0x080); + urtw_8225_write(sc, URTW_8225_ADDR_5_MAGIC, 0x004); + urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x0b7); + urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0xc4d); + urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0x44d); + urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x2bf); - /* XXX Are variables correct? */ - tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); - tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); - tap->wr_dbm_antsignal = (int8_t)rssi; + urtw_write8_m(sc, URTW_TX_GAIN_CCK, 0x03); + urtw_write8_m(sc, URTW_TX_GAIN_OFDM, 0x07); + urtw_write8_m(sc, URTW_TX_ANTENNA, 0x03); + + urtw_8187_write_phy_ofdm(sc, 0x80, 0x12); + for (i = 0; i < N(urtw_8225z2_agc); i++) { + urtw_8187_write_phy_ofdm(sc, 0xf, urtw_8225z2_agc[i]); + urtw_8187_write_phy_ofdm(sc, 0xe, 0x80 + i); + urtw_8187_write_phy_ofdm(sc, 0xe, 0); } + urtw_8187_write_phy_ofdm(sc, 0x80, 0x10); - wh = mtod(m, struct ieee80211_frame *); - if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) - sc->sc_currate = (rate > 0) ? rate : sc->sc_currate; - /* XXX correct? */ - if ((sc->sc_flags & URTW_RTL8187B) == 0) - nf = (quality > 64) ? 0 : ((64 - quality) * 100) / 64; + for (i = 0; i < N(urtw_8225v2b_rf_part2); i++) + urtw_8187_write_phy_ofdm(sc, i, urtw_8225v2b_rf_part2[i].val); - *rssi_p = rssi; - *nf_p = nf; + urtw_write32_m(sc, 0xf0, (7 << 12) | (3 << 8) | 0x1c); + urtw_write32_m(sc, 0xf4, (7 << 12) | (3 << 8) | 0x1c); + urtw_write32_m(sc, 0xf8, (7 << 12) | (3 << 8) | 0x1c); + urtw_write32_m(sc, 0xfc, (7 << 12) | (3 << 8) | 0x1c); + urtw_write8_m(sc, URTW_ACM_CONTROL, 0); - return (m); + urtw_8187_write_phy_ofdm(sc, 0x97, 0x46); + urtw_8187_write_phy_ofdm(sc, 0xa4, 0xb6); + urtw_8187_write_phy_ofdm(sc, 0x85, 0xfc); + urtw_8187_write_phy_cck(sc, 0xc1, 0x88); +fail: + return (error); +#undef N } static usb_error_t -urtw_8225v2_setgain(struct urtw_softc *sc, int16_t gain) +urtw_8225v2b_rf_set_chan(struct urtw_softc *sc, int chan) { - uint8_t *gainp; + int ack; + struct ieee80211com *ic = sc->sc_ifp->if_l2com; usb_error_t error; - /* XXX for A? */ - gainp = urtw_8225v2_gain_bg; - urtw_8187_write_phy_ofdm(sc, 0x0d, gainp[gain * 3]); - usb_pause_mtx(&sc->sc_mtx, 1); - urtw_8187_write_phy_ofdm(sc, 0x1b, gainp[gain * 3 + 1]); - usb_pause_mtx(&sc->sc_mtx, 1); - urtw_8187_write_phy_ofdm(sc, 0x1d, gainp[gain * 3 + 2]); - usb_pause_mtx(&sc->sc_mtx, 1); - urtw_8187_write_phy_ofdm(sc, 0x21, 0x17); - usb_pause_mtx(&sc->sc_mtx, 1); + error = urtw_8225v2b_set_txpwrlvl(sc, chan); + if (error) + goto fail; + + urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]); + usb_pause_mtx(&sc->sc_mtx, 10); + + urtw_write8_m(sc, URTW_SIFS, 0xa); + if (ic->ic_flags & IEEE80211_F_SHSLOT) { + urtw_write8_m(sc, URTW_SLOT, 0x9); + urtw_write8_m(sc, URTW_DIFS, 0x1c); + /* In 8187B, BRSR + 1 ==> EIFS register */ + urtw_write8_m(sc, URTW_BRSR + 1, 0x53); + + ack = 112 + 48 + 0x1c; + ack += (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? + 72 : 144; + urtw_write8_m(sc, URTW_CARRIER_SCOUNT, + roundup2(ack, 4)); + } else { + urtw_write8_m(sc, URTW_SLOT, 0x14); + urtw_write8_m(sc, URTW_DIFS, 0x32); + /* In 8187B, BRSR + 1 ==> EIFS register */ + urtw_write8_m(sc, URTW_BRSR + 1, 0x5b); + + ack = 112 + 48 + 0x32; + ack += (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? + 72 : 144; + urtw_write8_m(sc, URTW_CARRIER_SCOUNT, + roundup2(ack, 4)); + + } + fail: return (error); } static usb_error_t -urtw_8225v2_set_txpwrlvl(struct urtw_softc *sc, int chan) +urtw_8225v2b_set_txpwrlvl(struct urtw_softc *sc, int chan) { int i; uint8_t *cck_pwrtable; - uint8_t cck_pwrlvl_max = 15, ofdm_pwrlvl_max = 25, ofdm_pwrlvl_min = 10; + uint8_t cck_pwrlvl_max = 15; uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff; uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff; usb_error_t error; /* CCK power setting */ - cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? cck_pwrlvl_max : cck_pwrlvl; + cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? cck_pwrlvl_max : 22) : + (cck_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 0 : 7)); cck_pwrlvl += sc->sc_txpwr_cck_base; cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl; - cck_pwrtable = (chan == 14) ? urtw_8225v2_txpwr_cck_ch14 : - urtw_8225v2_txpwr_cck; + cck_pwrtable = (chan == 14) ? urtw_8225v2b_txpwr_cck_ch14 : + urtw_8225v2b_txpwr_cck; + + if (sc->sc_flags & URTW_RTL8187B_REV_B) + cck_pwrtable += (cck_pwrlvl <= 6) ? 0 : + ((cck_pwrlvl <= 11) ? 8 : 16); + else + cck_pwrtable += (cck_pwrlvl <= 5) ? 0 : + ((cck_pwrlvl <= 11) ? 8 : ((cck_pwrlvl <= 17) ? 16 : 24)); for (i = 0; i < 8; i++) urtw_8187_write_phy_cck(sc, 0x44 + i, cck_pwrtable[i]); urtw_write8_m(sc, URTW_TX_GAIN_CCK, - urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl]); + urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl] << 1); usb_pause_mtx(&sc->sc_mtx, 1); /* OFDM power setting */ - ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ? - ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min; + ofdm_pwrlvl = (ofdm_pwrlvl > 15) ? + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 17 : 25) : + (ofdm_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 2 : 10)); ofdm_pwrlvl += sc->sc_txpwr_ofdm_base; ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl; - error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); - if (error) - goto fail; - - urtw_8187_write_phy_ofdm(sc, 2, 0x42); - urtw_8187_write_phy_ofdm(sc, 5, 0x0); - urtw_8187_write_phy_ofdm(sc, 6, 0x40); - urtw_8187_write_phy_ofdm(sc, 7, 0x0); - urtw_8187_write_phy_ofdm(sc, 8, 0x40); - urtw_write8_m(sc, URTW_TX_GAIN_OFDM, - urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl]); + urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl] << 1); + + if (sc->sc_flags & URTW_RTL8187B_REV_B) { + if (ofdm_pwrlvl <= 11) { + urtw_8187_write_phy_ofdm(sc, 0x87, 0x60); + urtw_8187_write_phy_ofdm(sc, 0x89, 0x60); + } else { + urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c); + urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c); + } + } else { + if (ofdm_pwrlvl <= 11) { + urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c); + urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c); + } else if (ofdm_pwrlvl <= 17) { + urtw_8187_write_phy_ofdm(sc, 0x87, 0x54); + urtw_8187_write_phy_ofdm(sc, 0x89, 0x54); + } else { + urtw_8187_write_phy_ofdm(sc, 0x87, 0x50); + urtw_8187_write_phy_ofdm(sc, 0x89, 0x50); + } + } usb_pause_mtx(&sc->sc_mtx, 1); fail: return (error); } - static usb_error_t -urtw_8225v2_rf_init(struct urtw_softc *sc) +urtw_read8e(struct urtw_softc *sc, int val, uint8_t *data) { - int i; - uint16_t data; - uint32_t data32; + struct usb_device_request req; usb_error_t error; - error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); - if (error) + req.bmRequestType = UT_READ_VENDOR_DEVICE; + req.bRequest = URTW_8187_GETREGS_REQ; + USETW(req.wValue, val | 0xfe00); + USETW(req.wIndex, 0); + USETW(req.wLength, sizeof(uint8_t)); + + error = urtw_do_request(sc, &req, data); + return (error); +} + +static usb_error_t +urtw_write8e(struct urtw_softc *sc, int val, uint8_t data) +{ + struct usb_device_request req; + + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + req.bRequest = URTW_8187_SETREGS_REQ; + USETW(req.wValue, val | 0xfe00); + USETW(req.wIndex, 0); + USETW(req.wLength, sizeof(uint8_t)); + + return (urtw_do_request(sc, &req, &data)); +} + +static usb_error_t +urtw_8180_set_anaparam(struct urtw_softc *sc, uint32_t val) +{ + uint8_t data; + usb_error_t error; + + error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); + if (error) goto fail; - error = urtw_8225_usb_init(sc); + urtw_read8_m(sc, URTW_CONFIG3, &data); + urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE); + urtw_write32_m(sc, URTW_ANAPARAM, val); + urtw_read8_m(sc, URTW_CONFIG3, &data); + urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE); + + error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); if (error) goto fail; +fail: + return (error); +} - urtw_write32_m(sc, URTW_RF_TIMING, 0x000a8008); - urtw_read16_m(sc, URTW_BRSR, &data); /* XXX ??? */ - urtw_write16_m(sc, URTW_BRSR, 0xffff); - urtw_write32_m(sc, URTW_RF_PARA, 0x100044); +static usb_error_t +urtw_8185_set_anaparam2(struct urtw_softc *sc, uint32_t val) +{ + uint8_t data; + usb_error_t error; error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG); if (error) goto fail; - urtw_write8_m(sc, URTW_CONFIG3, 0x44); + + urtw_read8_m(sc, URTW_CONFIG3, &data); + urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE); + urtw_write32_m(sc, URTW_ANAPARAM2, val); + urtw_read8_m(sc, URTW_CONFIG3, &data); + urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE); + error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL); if (error) goto fail; +fail: + return (error); +} - error = urtw_8185_rf_pins_enable(sc); - if (error) - goto fail; +static usb_error_t +urtw_intr_enable(struct urtw_softc *sc) +{ + usb_error_t error; - usb_pause_mtx(&sc->sc_mtx, 500); + urtw_write16_m(sc, URTW_INTR_MASK, 0xffff); +fail: + return (error); +} - for (i = 0; i < nitems(urtw_8225v2_rf_part1); i++) { - urtw_8225_write(sc, urtw_8225v2_rf_part1[i].reg, - urtw_8225v2_rf_part1[i].val); - } - usb_pause_mtx(&sc->sc_mtx, 50); +static usb_error_t +urtw_intr_disable(struct urtw_softc *sc) +{ + usb_error_t error; - urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC1); + urtw_write16_m(sc, URTW_INTR_MASK, 0); +fail: + return (error); +} - for (i = 0; i < 95; i++) { - urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1)); - urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, - urtw_8225v2_rxgain[i]); - } +static usb_error_t +urtw_reset(struct urtw_softc *sc) +{ + uint8_t data; + usb_error_t error; - urtw_8225_write(sc, - URTW_8225_ADDR_3_MAGIC, URTW_8225_ADDR_3_DATA_MAGIC1); - urtw_8225_write(sc, - URTW_8225_ADDR_5_MAGIC, URTW_8225_ADDR_5_DATA_MAGIC1); - urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC2); - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); - usb_pause_mtx(&sc->sc_mtx, 100); - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); + error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); + if (error) + goto fail; + error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); + if (error) + goto fail; + + error = urtw_intr_disable(sc); + if (error) + goto fail; usb_pause_mtx(&sc->sc_mtx, 100); - error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32); + error = urtw_write8e(sc, 0x18, 0x10); + if (error != 0) + goto fail; + error = urtw_write8e(sc, 0x18, 0x11); + if (error != 0) + goto fail; + error = urtw_write8e(sc, 0x18, 0x00); if (error != 0) goto fail; - if (data32 != URTW_8225_ADDR_6_DATA_MAGIC1) - device_printf(sc->sc_dev, "expect 0xe6!! (0x%x)\n", data32); - if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2)) { - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1); - usb_pause_mtx(&sc->sc_mtx, 100); - urtw_8225_write(sc, - URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2); - usb_pause_mtx(&sc->sc_mtx, 50); - error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32); - if (error != 0) - goto fail; - if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2)) - device_printf(sc->sc_dev, "RF calibration failed\n"); - } usb_pause_mtx(&sc->sc_mtx, 100); - urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC6); - for (i = 0; i < 128; i++) { - urtw_8187_write_phy_ofdm(sc, 0xb, urtw_8225_agc[i]); - urtw_8187_write_phy_ofdm(sc, 0xa, (uint8_t)i + 0x80); - } + urtw_read8_m(sc, URTW_CMD, &data); + data = (data & 0x2) | URTW_CMD_RST; + urtw_write8_m(sc, URTW_CMD, data); + usb_pause_mtx(&sc->sc_mtx, 100); - for (i = 0; i < nitems(urtw_8225v2_rf_part2); i++) { - urtw_8187_write_phy_ofdm(sc, urtw_8225v2_rf_part2[i].reg, - urtw_8225v2_rf_part2[i].val); + urtw_read8_m(sc, URTW_CMD, &data); + if (data & URTW_CMD_RST) { + device_printf(sc->sc_dev, "reset timeout\n"); + goto fail; } - error = urtw_8225v2_setgain(sc, 4); + error = urtw_set_mode(sc, URTW_EPROM_CMD_LOAD); if (error) goto fail; + usb_pause_mtx(&sc->sc_mtx, 100); - for (i = 0; i < nitems(urtw_8225v2_rf_part3); i++) { - urtw_8187_write_phy_cck(sc, urtw_8225v2_rf_part3[i].reg, - urtw_8225v2_rf_part3[i].val); - } - - urtw_write8_m(sc, URTW_TESTR, 0x0d); - - error = urtw_8225v2_set_txpwrlvl(sc, 1); + error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON); if (error) goto fail; - - urtw_8187_write_phy_cck(sc, 0x10, 0x9b); - urtw_8187_write_phy_ofdm(sc, 0x26, 0x90); - - /* TX ant A, 0x0 for B */ - error = urtw_8185_tx_antenna(sc, 0x3); + error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON); if (error) goto fail; - urtw_write32_m(sc, URTW_HSSI_PARA, 0x3dc00002); - - error = urtw_8225_rf_set_chan(sc, 1); fail: return (error); } static usb_error_t -urtw_8225v2_rf_set_chan(struct urtw_softc *sc, int chan) +urtw_led_ctl(struct urtw_softc *sc, int mode) { - struct ieee80211com *ic = sc->sc_ifp->if_l2com; - struct ieee80211_channel *c = ic->ic_curchan; - usb_error_t error; - - error = urtw_8225v2_set_txpwrlvl(sc, chan); - if (error) - goto fail; - - urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]); - usb_pause_mtx(&sc->sc_mtx, 10); - - urtw_write8_m(sc, URTW_SIFS, 0x22); - - if(sc->sc_state == IEEE80211_S_ASSOC && - ic->ic_flags & IEEE80211_F_SHSLOT) - urtw_write8_m(sc, URTW_SLOT, 0x9); - else - urtw_write8_m(sc, URTW_SLOT, 0x14); + usb_error_t error = 0; - if (IEEE80211_IS_CHAN_G(c)) { - /* for G */ - urtw_write8_m(sc, URTW_DIFS, 0x14); - urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x14); - urtw_write8_m(sc, URTW_CW_VAL, 0x73); - } else { - /* for B */ - urtw_write8_m(sc, URTW_DIFS, 0x24); - urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x24); - urtw_write8_m(sc, URTW_CW_VAL, 0xa5); + switch (sc->sc_strategy) { + case URTW_SW_LED_MODE0: + error = urtw_led_mode0(sc, mode); + break; + case URTW_SW_LED_MODE1: + error = urtw_led_mode1(sc, mode); + break; + case URTW_SW_LED_MODE2: + error = urtw_led_mode2(sc, mode); + break; + case URTW_SW_LED_MODE3: + error = urtw_led_mode3(sc, mode); + break; + default: + panic("unsupported LED mode %d\n", sc->sc_strategy); + /* never reach */ } -fail: return (error); } static usb_error_t -urtw_8225_read(struct urtw_softc *sc, uint8_t addr, uint32_t *data) +urtw_led_mode0(struct urtw_softc *sc, int mode) { - int i; - int16_t bit; - uint8_t rlen = 12, wlen = 6; - uint16_t o1, o2, o3, tmp; - uint32_t d2w = ((uint32_t)(addr & 0x1f)) << 27; - uint32_t mask = 0x80000000, value = 0; - usb_error_t error; - urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &o1); - urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &o2); - urtw_read16_m(sc, URTW_RF_PINS_SELECT, &o3); - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2 | URTW_RF_PINS_MAGIC4); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3 | URTW_RF_PINS_MAGIC4); - o1 &= ~URTW_RF_PINS_MAGIC4; - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN); - DELAY(5); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1); - DELAY(5); + switch (mode) { + case URTW_LED_CTL_POWER_ON: + sc->sc_gpio_ledstate = URTW_LED_POWER_ON_BLINK; + break; + case URTW_LED_CTL_TX: + if (sc->sc_gpio_ledinprogress == 1) + return (0); - for (i = 0; i < (wlen / 2); i++, mask = mask >> 1) { - bit = ((d2w & mask) != 0) ? 1 : 0; + sc->sc_gpio_ledstate = URTW_LED_BLINK_NORMAL; + sc->sc_gpio_blinktime = 2; + break; + case URTW_LED_CTL_LINK: + sc->sc_gpio_ledstate = URTW_LED_ON; + break; + default: + panic("unsupported LED mode 0x%x", mode); + /* never reach */ + } - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | - URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | - URTW_BB_HOST_BANG_CLK); - DELAY(2); - mask = mask >> 1; - if (i == 2) + switch (sc->sc_gpio_ledstate) { + case URTW_LED_ON: + if (sc->sc_gpio_ledinprogress != 0) break; - bit = ((d2w & mask) != 0) ? 1 : 0; - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | - URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | - URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1); - DELAY(1); + urtw_led_on(sc, URTW_LED_GPIO); + break; + case URTW_LED_BLINK_NORMAL: + if (sc->sc_gpio_ledinprogress != 0) + break; + sc->sc_gpio_ledinprogress = 1; + sc->sc_gpio_blinkstate = (sc->sc_gpio_ledon != 0) ? + URTW_LED_OFF : URTW_LED_ON; + usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc); + break; + case URTW_LED_POWER_ON_BLINK: + urtw_led_on(sc, URTW_LED_GPIO); + usb_pause_mtx(&sc->sc_mtx, 100); + urtw_led_off(sc, URTW_LED_GPIO); + break; + default: + panic("unknown LED status 0x%x", sc->sc_gpio_ledstate); + /* never reach */ } - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW | - URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_RW); - DELAY(2); + return (0); +} - mask = 0x800; - for (i = 0; i < rlen; i++, mask = mask >> 1) { - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, - o1 | URTW_BB_HOST_BANG_RW); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, - o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, - o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); - DELAY(2); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, - o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK); - DELAY(2); +static usb_error_t +urtw_led_mode1(struct urtw_softc *sc, int mode) +{ - urtw_read16_m(sc, URTW_RF_PINS_INPUT, &tmp); - value |= ((tmp & URTW_BB_HOST_BANG_CLK) ? mask : 0); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, - o1 | URTW_BB_HOST_BANG_RW); - DELAY(2); + return (USB_ERR_INVAL); +} + +static usb_error_t +urtw_led_mode2(struct urtw_softc *sc, int mode) +{ + + return (USB_ERR_INVAL); +} + +static usb_error_t +urtw_led_mode3(struct urtw_softc *sc, int mode) +{ + + return (USB_ERR_INVAL); +} + +static usb_error_t +urtw_led_on(struct urtw_softc *sc, int type) +{ + usb_error_t error; + + if (type == URTW_LED_GPIO) { + switch (sc->sc_gpio_ledpin) { + case URTW_LED_PIN_GPIO0: + urtw_write8_m(sc, URTW_GPIO, 0x01); + urtw_write8_m(sc, URTW_GP_ENABLE, 0x00); + break; + default: + panic("unsupported LED PIN type 0x%x", + sc->sc_gpio_ledpin); + /* never reach */ + } + } else { + panic("unsupported LED type 0x%x", type); + /* never reach */ } - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN | - URTW_BB_HOST_BANG_RW); - DELAY(2); + sc->sc_gpio_ledon = 1; +fail: + return (error); +} + +static usb_error_t +urtw_led_off(struct urtw_softc *sc, int type) +{ + usb_error_t error; + + if (type == URTW_LED_GPIO) { + switch (sc->sc_gpio_ledpin) { + case URTW_LED_PIN_GPIO0: + urtw_write8_m(sc, URTW_GPIO, URTW_GPIO_DATA_MAGIC1); + urtw_write8_m(sc, + URTW_GP_ENABLE, URTW_GP_ENABLE_DATA_MAGIC1); + break; + default: + panic("unsupported LED PIN type 0x%x", + sc->sc_gpio_ledpin); + /* never reach */ + } + } else { + panic("unsupported LED type 0x%x", type); + /* never reach */ + } + + sc->sc_gpio_ledon = 0; + +fail: + return (error); +} + +static void +urtw_led_ch(void *arg) +{ + struct urtw_softc *sc = arg; + struct ifnet *ifp = sc->sc_ifp; + struct ieee80211com *ic = ifp->if_l2com; + + ieee80211_runtask(ic, &sc->sc_led_task); +} + +static void +urtw_ledtask(void *arg, int pending) +{ + struct urtw_softc *sc = arg; + + if (sc->sc_strategy != URTW_SW_LED_MODE0) + panic("could not process a LED strategy 0x%x", sc->sc_strategy); + + URTW_LOCK(sc); + urtw_led_blink(sc); + URTW_UNLOCK(sc); +} + +static usb_error_t +urtw_led_blink(struct urtw_softc *sc) +{ + uint8_t ing = 0; + usb_error_t error; + + if (sc->sc_gpio_blinkstate == URTW_LED_ON) + error = urtw_led_on(sc, URTW_LED_GPIO); + else + error = urtw_led_off(sc, URTW_LED_GPIO); + sc->sc_gpio_blinktime--; + if (sc->sc_gpio_blinktime == 0) + ing = 1; + else { + if (sc->sc_gpio_ledstate != URTW_LED_BLINK_NORMAL && + sc->sc_gpio_ledstate != URTW_LED_BLINK_SLOWLY && + sc->sc_gpio_ledstate != URTW_LED_BLINK_CM3) + ing = 1; + } + if (ing == 1) { + if (sc->sc_gpio_ledstate == URTW_LED_ON && + sc->sc_gpio_ledon == 0) + error = urtw_led_on(sc, URTW_LED_GPIO); + else if (sc->sc_gpio_ledstate == URTW_LED_OFF && + sc->sc_gpio_ledon == 1) + error = urtw_led_off(sc, URTW_LED_GPIO); + + sc->sc_gpio_blinktime = 0; + sc->sc_gpio_ledinprogress = 0; + return (0); + } + + sc->sc_gpio_blinkstate = (sc->sc_gpio_blinkstate != URTW_LED_ON) ? + URTW_LED_ON : URTW_LED_OFF; + + switch (sc->sc_gpio_ledstate) { + case URTW_LED_BLINK_NORMAL: + usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc); + break; + default: + panic("unknown LED status 0x%x", sc->sc_gpio_ledstate); + /* never reach */ + } + return (0); +} + +static usb_error_t +urtw_rx_enable(struct urtw_softc *sc) +{ + uint8_t data; + usb_error_t error; + + usbd_transfer_start((sc->sc_flags & URTW_RTL8187B) ? + sc->sc_xfer[URTW_8187B_BULK_RX] : sc->sc_xfer[URTW_8187L_BULK_RX]); + + error = urtw_rx_setconf(sc); + if (error != 0) + goto fail; + + urtw_read8_m(sc, URTW_CMD, &data); + urtw_write8_m(sc, URTW_CMD, data | URTW_CMD_RX_ENABLE); +fail: + return (error); +} + +static usb_error_t +urtw_tx_enable(struct urtw_softc *sc) +{ + uint8_t data8; + uint32_t data; + usb_error_t error; + + if (sc->sc_flags & URTW_RTL8187B) { + urtw_read32_m(sc, URTW_TX_CONF, &data); + data &= ~URTW_TX_LOOPBACK_MASK; + data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK); + data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK); + data &= ~URTW_TX_SWPLCPLEN; + data |= URTW_TX_HW_SEQNUM | URTW_TX_DISREQQSIZE | + (7 << 8) | /* short retry limit */ + (7 << 0) | /* long retry limit */ + (7 << 21); /* MAX TX DMA */ + urtw_write32_m(sc, URTW_TX_CONF, data); + + urtw_read8_m(sc, URTW_CMD, &data8); + urtw_write8_m(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE); + return (error); + } + + urtw_read8_m(sc, URTW_CW_CONF, &data8); + data8 &= ~(URTW_CW_CONF_PERPACKET_CW | URTW_CW_CONF_PERPACKET_RETRY); + urtw_write8_m(sc, URTW_CW_CONF, data8); + + urtw_read8_m(sc, URTW_TX_AGC_CTL, &data8); + data8 &= ~URTW_TX_AGC_CTL_PERPACKET_GAIN; + data8 &= ~URTW_TX_AGC_CTL_PERPACKET_ANTSEL; + data8 &= ~URTW_TX_AGC_CTL_FEEDBACK_ANT; + urtw_write8_m(sc, URTW_TX_AGC_CTL, data8); + + urtw_read32_m(sc, URTW_TX_CONF, &data); + data &= ~URTW_TX_LOOPBACK_MASK; + data |= URTW_TX_LOOPBACK_NONE; + data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK); + data |= sc->sc_tx_retry << URTW_TX_DPRETRY_SHIFT; + data |= sc->sc_rts_retry << URTW_TX_RTSRETRY_SHIFT; + data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK); + data |= URTW_TX_MXDMA_2048 | URTW_TX_CWMIN | URTW_TX_DISCW; + data &= ~URTW_TX_SWPLCPLEN; + data |= URTW_TX_NOICV; + urtw_write32_m(sc, URTW_TX_CONF, data); + + urtw_read8_m(sc, URTW_CMD, &data8); + urtw_write8_m(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE); +fail: + return (error); +} + +static usb_error_t +urtw_rx_setconf(struct urtw_softc *sc) +{ + struct ifnet *ifp = sc->sc_ifp; + struct ieee80211com *ic = ifp->if_l2com; + uint32_t data; + usb_error_t error; + + urtw_read32_m(sc, URTW_RX, &data); + data = data &~ URTW_RX_FILTER_MASK; + if (sc->sc_flags & URTW_RTL8187B) { + data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA | + URTW_RX_FILTER_MCAST | URTW_RX_FILTER_BCAST | + URTW_RX_FILTER_NICMAC | URTW_RX_CHECK_BSSID | + URTW_RX_FIFO_THRESHOLD_NONE | + URTW_MAX_RX_DMA_2048 | + URTW_RX_AUTORESETPHY | URTW_RCR_ONLYERLPKT; + } else { + data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA; + data = data | URTW_RX_FILTER_BCAST | URTW_RX_FILTER_MCAST; + + if (ic->ic_opmode == IEEE80211_M_MONITOR) { + data = data | URTW_RX_FILTER_ICVERR; + data = data | URTW_RX_FILTER_PWR; + } + if (sc->sc_crcmon == 1 && ic->ic_opmode == IEEE80211_M_MONITOR) + data = data | URTW_RX_FILTER_CRCERR; + + if (ic->ic_opmode == IEEE80211_M_MONITOR || + (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) { + data = data | URTW_RX_FILTER_ALLMAC; + } else { + data = data | URTW_RX_FILTER_NICMAC; + data = data | URTW_RX_CHECK_BSSID; + } + + data = data &~ URTW_RX_FIFO_THRESHOLD_MASK; + data = data | URTW_RX_FIFO_THRESHOLD_NONE | + URTW_RX_AUTORESETPHY; + data = data &~ URTW_MAX_RX_DMA_MASK; + data = data | URTW_MAX_RX_DMA_2048 | URTW_RCR_ONLYERLPKT; + } + + urtw_write32_m(sc, URTW_RX, data); +fail: + return (error); +} + +static struct mbuf * +urtw_rxeof(struct usb_xfer *xfer, struct urtw_data *data, int *rssi_p, + int8_t *nf_p) +{ + int actlen, flen, len, nf = -95, rssi; + struct ieee80211_frame *wh; + struct mbuf *m, *mnew; + struct urtw_8187b_rxhdr *bhdr; + struct urtw_softc *sc = data->sc; + struct ifnet *ifp = sc->sc_ifp; + struct ieee80211com *ic = ifp->if_l2com; + uint8_t *desc, quality = 0, rate; + + usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); + + if (actlen < URTW_MIN_RXBUFSZ) { + ifp->if_ierrors++; + return (NULL); + } + + if (sc->sc_flags & URTW_RTL8187B) { + len = actlen - (sizeof(struct urtw_8187b_rxhdr)); + bhdr = (struct urtw_8187b_rxhdr *)(data->buf + len); + desc = data->buf + len; + flen = ((desc[1] & 0x0f) << 8) + (desc[0] & 0xff); + if (flen > actlen) { + ifp->if_ierrors++; + return (NULL); + } + rate = (le32toh(bhdr->flags) >> 20) & 0xf; + rssi = 14 + (bhdr->rssi / 2); + if (rssi > 95) + rssi = 95; + } else { + /* 4 dword and 4 byte CRC */ + len = actlen - (4 * 4); + desc = data->buf + len; + flen = ((desc[1] & 0x0f) << 8) + (desc[0] & 0xff); + if (flen > actlen) { + ifp->if_ierrors++; + return (NULL); + } - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3); - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_OUTPUT_MAGIC1); + rate = (desc[2] & 0xf0) >> 4; + quality = desc[4] & 0xff; + /* XXX correct? */ + rssi = (desc[6] & 0xfe) >> 1; + if (!urtw_isbmode(rate)) { + rssi = (rssi > 90) ? 90 : ((rssi < 25) ? 25 : rssi); + rssi = ((90 - rssi) * 100) / 65; + } else { + rssi = (rssi > 90) ? 95 : ((rssi < 30) ? 30 : rssi); + rssi = ((95 - rssi) * 100) / 65; + } + } - if (data != NULL) - *data = value; -fail: - return (error); -} + mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); + if (mnew == NULL) { + ifp->if_ierrors++; + return (NULL); + } + m = data->m; + data->m = mnew; + data->buf = mtod(mnew, uint8_t *); -static usb_error_t -urtw_8225_isv2(struct urtw_softc *sc, int *ret) -{ - uint32_t data; - usb_error_t error; + /* finalize mbuf */ + m->m_pkthdr.rcvif = ifp; + m->m_pkthdr.len = m->m_len = flen - 4; - *ret = 1; + if (ieee80211_radiotap_active(ic)) { + struct urtw_rx_radiotap_header *tap = &sc->sc_rxtap; - urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_MAGIC5); - urtw_write16_m(sc, URTW_RF_PINS_SELECT, URTW_RF_PINS_MAGIC5); - urtw_write16_m(sc, URTW_RF_PINS_ENABLE, URTW_RF_PINS_MAGIC5); - usb_pause_mtx(&sc->sc_mtx, 500); + /* XXX Are variables correct? */ + tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); + tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); + tap->wr_dbm_antsignal = (int8_t)rssi; + } - urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, - URTW_8225_ADDR_0_DATA_MAGIC1); + wh = mtod(m, struct ieee80211_frame *); + if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) + sc->sc_currate = (rate > 0) ? rate : sc->sc_currate; + /* XXX correct? */ + if ((sc->sc_flags & URTW_RTL8187B) == 0) + nf = (quality > 64) ? 0 : ((64 - quality) * 100) / 64; - error = urtw_8225_read(sc, URTW_8225_ADDR_8_MAGIC, &data); - if (error != 0) - goto fail; - if (data != URTW_8225_ADDR_8_DATA_MAGIC1) - *ret = 0; - else { - error = urtw_8225_read(sc, URTW_8225_ADDR_9_MAGIC, &data); - if (error != 0) - goto fail; - if (data != URTW_8225_ADDR_9_DATA_MAGIC1) - *ret = 0; - } + *rssi_p = rssi; + *nf_p = nf; - urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, - URTW_8225_ADDR_0_DATA_MAGIC2); -fail: - return (error); + return (m); } -static void -urtw_led_ch(void *arg) -{ - struct urtw_softc *sc = arg; - struct ifnet *ifp = sc->sc_ifp; - struct ieee80211com *ic = ifp->if_l2com; - ieee80211_runtask(ic, &sc->sc_led_task); -} static void urtw_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) { @@ -4058,175 +4206,16 @@ urtw_getbuf(struct urtw_softc *sc) return (bf); } -static usb_error_t -urtw_8225v2b_rf_init(struct urtw_softc *sc) -{ - int i; - usb_error_t error; - - for (i = 0; i < nitems(urtw_8225v2b_rf_part1); i++) - urtw_8225_write(sc, urtw_8225v2b_rf_part1[i].reg, - urtw_8225v2b_rf_part1[i].val); - - urtw_8225_write(sc, - URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC1); - - for (i = 0; i < nitems(urtw_8225v2b_rxgain); i++) { - urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1)); - urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, - urtw_8225v2b_rxgain[i]); - } - - urtw_8225_write(sc, URTW_8225_ADDR_3_MAGIC, 0x080); - urtw_8225_write(sc, URTW_8225_ADDR_5_MAGIC, 0x004); - urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x0b7); - urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0xc4d); - urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0x44d); - urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x2bf); - - urtw_write8_m(sc, URTW_TX_GAIN_CCK, 0x03); - urtw_write8_m(sc, URTW_TX_GAIN_OFDM, 0x07); - urtw_write8_m(sc, URTW_TX_ANTENNA, 0x03); - - urtw_8187_write_phy_ofdm(sc, 0x80, 0x12); - for (i = 0; i < nitems(urtw_8225z2_agc); i++) { - urtw_8187_write_phy_ofdm(sc, 0xf, urtw_8225z2_agc[i]); - urtw_8187_write_phy_ofdm(sc, 0xe, 0x80 + i); - urtw_8187_write_phy_ofdm(sc, 0xe, 0); - } - urtw_8187_write_phy_ofdm(sc, 0x80, 0x10); - - for (i = 0; i < nitems(urtw_8225v2b_rf_part2); i++) - urtw_8187_write_phy_ofdm(sc, i, urtw_8225v2b_rf_part2[i].val); - - urtw_write32_m(sc, 0xf0, (7 << 12) | (3 << 8) | 0x1c); - urtw_write32_m(sc, 0xf4, (7 << 12) | (3 << 8) | 0x1c); - urtw_write32_m(sc, 0xf8, (7 << 12) | (3 << 8) | 0x1c); - urtw_write32_m(sc, 0xfc, (7 << 12) | (3 << 8) | 0x1c); - urtw_write8_m(sc, URTW_ACM_CONTROL, 0); - - urtw_8187_write_phy_ofdm(sc, 0x97, 0x46); - urtw_8187_write_phy_ofdm(sc, 0xa4, 0xb6); - urtw_8187_write_phy_ofdm(sc, 0x85, 0xfc); - urtw_8187_write_phy_cck(sc, 0xc1, 0x88); -fail: - return (error); -} - - -static usb_error_t -urtw_8225v2b_rf_set_chan(struct urtw_softc *sc, int chan) -{ - int ack; - struct ieee80211com *ic = sc->sc_ifp->if_l2com; - usb_error_t error; - - error = urtw_8225v2b_set_txpwrlvl(sc, chan); - if (error) - goto fail; - - urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]); - usb_pause_mtx(&sc->sc_mtx, 10); - - urtw_write8_m(sc, URTW_SIFS, 0xa); - if (ic->ic_flags & IEEE80211_F_SHSLOT) { - urtw_write8_m(sc, URTW_SLOT, 0x9); - urtw_write8_m(sc, URTW_DIFS, 0x1c); - /* In 8187B, BRSR + 1 ==> EIFS register */ - urtw_write8_m(sc, URTW_BRSR + 1, 0x53); - - ack = 112 + 48 + 0x1c; - ack += (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? - 72 : 144; - urtw_write8_m(sc, URTW_CARRIER_SCOUNT, - roundup2(ack, 4)); - } else { - urtw_write8_m(sc, URTW_SLOT, 0x14); - urtw_write8_m(sc, URTW_DIFS, 0x32); - /* In 8187B, BRSR + 1 ==> EIFS register */ - urtw_write8_m(sc, URTW_BRSR + 1, 0x5b); - - ack = 112 + 48 + 0x32; - ack += (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? - 72 : 144; - urtw_write8_m(sc, URTW_CARRIER_SCOUNT, - roundup2(ack, 4)); - - } - -fail: - return (error); -} - -static usb_error_t -urtw_8225v2b_set_txpwrlvl(struct urtw_softc *sc, int chan) +static int +urtw_isbmode(uint16_t rate) { - int i; - uint8_t *cck_pwrtable; - uint8_t cck_pwrlvl_max = 15; - uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff; - uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff; - usb_error_t error; - - /* CCK power setting */ - cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? - ((sc->sc_flags & URTW_RTL8187B_REV_B) ? cck_pwrlvl_max : 22) : - (cck_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 0 : 7)); - cck_pwrlvl += sc->sc_txpwr_cck_base; - cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl; - cck_pwrtable = (chan == 14) ? urtw_8225v2b_txpwr_cck_ch14 : - urtw_8225v2b_txpwr_cck; - - if (sc->sc_flags & URTW_RTL8187B_REV_B) - cck_pwrtable += (cck_pwrlvl <= 6) ? 0 : - ((cck_pwrlvl <= 11) ? 8 : 16); - else - cck_pwrtable += (cck_pwrlvl <= 5) ? 0 : - ((cck_pwrlvl <= 11) ? 8 : ((cck_pwrlvl <= 17) ? 16 : 24)); - - for (i = 0; i < 8; i++) - urtw_8187_write_phy_cck(sc, 0x44 + i, cck_pwrtable[i]); - - urtw_write8_m(sc, URTW_TX_GAIN_CCK, - urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl] << 1); - usb_pause_mtx(&sc->sc_mtx, 1); - - /* OFDM power setting */ - ofdm_pwrlvl = (ofdm_pwrlvl > 15) ? - ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 17 : 25) : - (ofdm_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 2 : 10)); - ofdm_pwrlvl += sc->sc_txpwr_ofdm_base; - ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl; - urtw_write8_m(sc, URTW_TX_GAIN_OFDM, - urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl] << 1); + rate = urtw_rtl2rate(rate); - if (sc->sc_flags & URTW_RTL8187B_REV_B) { - if (ofdm_pwrlvl <= 11) { - urtw_8187_write_phy_ofdm(sc, 0x87, 0x60); - urtw_8187_write_phy_ofdm(sc, 0x89, 0x60); - } else { - urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c); - urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c); - } - } else { - if (ofdm_pwrlvl <= 11) { - urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c); - urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c); - } else if (ofdm_pwrlvl <= 17) { - urtw_8187_write_phy_ofdm(sc, 0x87, 0x54); - urtw_8187_write_phy_ofdm(sc, 0x89, 0x54); - } else { - urtw_8187_write_phy_ofdm(sc, 0x87, 0x50); - urtw_8187_write_phy_ofdm(sc, 0x89, 0x50); - } - } - usb_pause_mtx(&sc->sc_mtx, 1); -fail: - return (error); + return ((rate <= 22 && rate != 12 && rate != 18) || + rate == 44) ? (1) : (0); } - static device_method_t urtw_methods[] = { DEVMETHOD(device_probe, urtw_match), DEVMETHOD(device_attach, urtw_attach), diff --git a/sys/dev/wi/if_wi.c b/sys/dev/wi/if_wi.c index 74653d1e..616c5d7 100644 --- a/sys/dev/wi/if_wi.c +++ b/sys/dev/wi/if_wi.c @@ -1673,7 +1673,6 @@ wi_read_nicid(struct wi_softc *sc) memset(ident, 0, sizeof(ident)); len = sizeof(ident); /* value should be the format like "V2.00-11" */ - /* XXX: Flexelint: ident is local, p is arg */ if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && *(p = (char *)ident) >= 'A' && p[2] == '.' && p[5] == '-' && p[8] == '\0') { diff --git a/sys/fs/msdosfs/msdosfs_conv.c b/sys/fs/msdosfs/msdosfs_conv.c index b9b6f4a..50dc1a0 100644 --- a/sys/fs/msdosfs/msdosfs_conv.c +++ b/sys/fs/msdosfs/msdosfs_conv.c @@ -266,7 +266,6 @@ dos2unixfn(dn, un, lower, pmp) *un++ = c; thislong++; } - /* XXX: FlexeLint: Not kosher, dn is an array, not a pointer */ dn += i; /* diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 47baeea..0cc330c 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -1641,7 +1641,6 @@ kern_jail_set(struct thread *td, struct uio *optuio, int flags) #ifdef INET6 while (redo_ip6) { ip6s = pr->pr_ip6s; - /* XXX: FlexeLint claims mem leak */ ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); mtx_lock(&pr->pr_mtx); redo_ip6 = 0; diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c index cfe316f..9387ba8 100644 --- a/sys/kern/kern_linker.c +++ b/sys/kern/kern_linker.c @@ -1875,7 +1875,6 @@ linker_search_kld(const char *name) return (linker_strdup(name)); /* traverse the linker path */ - /* XXX: FlexeLint: this is not safe, ep will count past NUL */ len = strlen(name); for (ep = linker_path; *ep; ep++) { cp = ep; diff --git a/sys/kern/kern_lock.c b/sys/kern/kern_lock.c index 92009fa..e6f2f53 100644 --- a/sys/kern/kern_lock.c +++ b/sys/kern/kern_lock.c @@ -312,7 +312,6 @@ lock_lockmgr(struct lock_object *lock, int how) panic("lockmgr locks do not support sleep interlocking"); } -/* XXX: flexelint retval */ static int unlock_lockmgr(struct lock_object *lock) { diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 4a425bb..85ca646 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -133,28 +133,28 @@ struct lock_class lock_class_mtx_spin = { struct mtx blocked_lock; struct mtx Giant; -static void +void assert_mtx(struct lock_object *lock, int what) { mtx_assert((struct mtx *)lock, what); } -static void +void lock_mtx(struct lock_object *lock, int how) { mtx_lock((struct mtx *)lock); } -static void +void lock_spin(struct lock_object *lock, int how) { panic("spin locks can only use msleep_spin"); } -static int +int unlock_mtx(struct lock_object *lock) { struct mtx *m; @@ -165,8 +165,7 @@ unlock_mtx(struct lock_object *lock) return (0); } -/* XXX: FlexeLint retval */ -static int +int unlock_spin(struct lock_object *lock) { diff --git a/sys/net80211/ieee80211_action.c b/sys/net80211/ieee80211_action.c index 8c11471..5371f6e 100644 --- a/sys/net80211/ieee80211_action.c +++ b/sys/net80211/ieee80211_action.c @@ -105,7 +105,7 @@ ieee80211_send_action_register(int cat, int act, ieee80211_send_action_func *f) meshlm_send_action[act] = f; return 0; case IEEE80211_ACTION_CAT_MESHPATH: - if (act >= N(hwmp_send_action)) + if (act > N(hwmp_send_action)) break; hwmp_send_action[act] = f; return 0; diff --git a/sys/net80211/ieee80211_proto.h b/sys/net80211/ieee80211_proto.h index b7c2c43..9bfbc61 100644 --- a/sys/net80211/ieee80211_proto.h +++ b/sys/net80211/ieee80211_proto.h @@ -315,7 +315,7 @@ struct ieee80211_beacon_offsets { uint8_t *bo_ath; /* start of ATH parameters */ uint8_t *bo_appie; /* start of AppIE element */ uint16_t bo_appie_len; /* AppIE length in bytes */ - uint16_t bo_csa_trailer_len; + uint16_t bo_csa_trailer_len;; uint8_t *bo_csa; /* start of CSA element */ uint8_t *bo_spare[4]; }; diff --git a/sys/security/audit/audit_bsm_token.c b/sys/security/audit/audit_bsm_token.c index a050df1..5523ea4 100644 --- a/sys/security/audit/audit_bsm_token.c +++ b/sys/security/audit/audit_bsm_token.c @@ -36,7 +36,6 @@ #include __FBSDID("$FreeBSD$"); -#include #include #include #include -- cgit v1.1