📄 an.c
字号:
/* $OpenBSD: an.c,v 1.23 2002/03/14 01:26:54 millert Exp $ */
/*
* Copyright (c) 1997, 1998, 1999
* Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Bill Paul.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD: src/sys/dev/an/if_an.c,v 1.21 2001/09/10 02:05:09 brooks Exp $
*/
/*
* Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
*
* Written by Bill Paul <wpaul@ctr.columbia.edu>
* Electrical Engineering Department
* Columbia University, New York City
*/
/*
* The Aironet 4500/4800 series cards some in PCMCIA, ISA and PCI form.
* This driver supports all three device types (PCI devices are supported
* through an extra PCI shim: /sys/pci/if_an_p.c). ISA devices can be
* supported either using hard-coded IO port/IRQ settings or via Plug
* and Play. The 4500 series devices support 1Mbps and 2Mbps data rates.
* The 4800 devices support 1, 2, 5.5 and 11Mbps rates.
*
* Like the WaveLAN/IEEE cards, the Aironet NICs are all essentially
* PCMCIA devices. The ISA and PCI cards are a combination of a PCMCIA
* device and a PCMCIA to ISA or PCMCIA to PCI adapter card. There are
* a couple of important differences though:
*
* - Lucent doesn't currently offer a PCI card, however Aironet does
* - Lucent ISA card looks to the host like a PCMCIA controller with
* a PCMCIA WaveLAN card inserted. This means that even desktop
* machines need to be configured with PCMCIA support in order to
* use WaveLAN/IEEE ISA cards. The Aironet cards on the other hand
* actually look like normal ISA and PCI devices to the host, so
* no PCMCIA controller support is needed
*
* The latter point results in a small gotcha. The Aironet PCMCIA
* cards can be configured for one of two operating modes depending
* on how the Vpp1 and Vpp2 programming voltages are set when the
* card is activated. In order to put the card in proper PCMCIA
* operation (where the CIS table is visible and the interface is
* programmed for PCMCIA operation), both Vpp1 and Vpp2 have to be
* set to 5 volts. FreeBSD by default doesn't set the Vpp voltages,
* which leaves the card in ISA/PCI mode, which prevents it from
* being activated as an PCMCIA device. Consequently, /sys/pccard/pccard.c
* has to be patched slightly in order to enable the Vpp voltages in
* order to make the Aironet PCMCIA cards work.
*
* Note that some PCMCIA controller software packages for Windows NT
* fail to set the voltages as well.
*
* The Aironet devices can operate in both station mode and access point
* mode. Typically, when programmed for station mode, the card can be set
* to automatically perform encapsulation/decapsulation of Ethernet II
* and 802.3 frames within 802.11 frames so that the host doesn't have
* to do it itself. This driver doesn't program the card that way: the
* driver handles all of the encapsulation/decapsulation itself.
*/
#ifdef INET
#define ANCACHE /* enable signal strength cache */
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/timeout.h>
#ifdef ANCACHE
#include <sys/syslog.h>
#include <sys/sysctl.h>
#endif
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#endif
#include "bpfilter.h"
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <machine/bus.h>
#include <machine/intr.h>
#include <dev/ic/anvar.h>
#include <dev/ic/anreg.h>
#define TIMEOUT(handle,func,sc,time) timeout_add(&(handle), (time))
#define UNTIMEOUT(func,sc,handle) timeout_del(&(handle))
#define BPF_MTAP(if,mbuf) bpf_mtap((if)->if_bpf, (mbuf))
#define BPFATTACH(if_bpf,if,dlt,sz)
struct cfdriver an_cd = {
NULL, "an", DV_IFNET
};
void an_reset(struct an_softc *);
int an_ioctl(struct ifnet *, u_long, caddr_t);
int an_init_tx_ring(struct an_softc *);
void an_start(struct ifnet *);
void an_watchdog(struct ifnet *);
void an_rxeof(struct an_softc *);
void an_txeof(struct an_softc *, int);
void an_promisc(struct an_softc *, int);
int an_cmd(struct an_softc *, int, int);
int an_read_record(struct an_softc *, struct an_ltv_gen *);
int an_write_record(struct an_softc *, struct an_ltv_gen *);
int an_read_data(struct an_softc *, int,
int, caddr_t, int);
int an_write_data(struct an_softc *, int,
int, caddr_t, int);
int an_seek(struct an_softc *, int, int, int);
int an_alloc_nicmem(struct an_softc *, int, int *);
void an_stats_update(void *);
void an_setdef(struct an_softc *, struct an_req *);
#ifdef ANCACHE
void an_cache_store(struct an_softc *, struct ether_header *,
struct mbuf *, unsigned short);
#endif
int an_media_change(struct ifnet *);
void an_media_status(struct ifnet *, struct ifmediareq *);
static __inline void
an_swap16(u_int16_t *p, int cnt)
{
for (; cnt--; p++)
*p = swap16(*p);
}
int
an_attach(sc)
struct an_softc *sc;
{
struct ifnet *ifp = &sc->arpcom.ac_if;
sc->an_gone = 0;
sc->an_associated = 0;
/* disable interrupts */
CSR_WRITE_2(sc, AN_INT_EN, 0);
CSR_WRITE_2(sc, AN_EVENT_ACK, 0xffff);
/* Reset the NIC. */
an_reset(sc);
/* Load factory config */
if (an_cmd(sc, AN_CMD_READCFG, 0)) {
printf("%s: failed to load config data\n", ifp->if_xname);
return(EIO);
}
/* Read the current configuration */
sc->an_config.an_type = AN_RID_GENCONFIG;
sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
printf("%s: read record failed\n", ifp->if_xname);
return(EIO);
}
/* Read the card capabilities */
sc->an_caps.an_type = AN_RID_CAPABILITIES;
sc->an_caps.an_len = sizeof(struct an_ltv_caps);
if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_caps)) {
printf("%s: read record failed\n", ifp->if_xname);
return(EIO);
}
/* Read ssid list */
sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist);
if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
printf("%s: read record failed\n", ifp->if_xname);
return(EIO);
}
/* Read AP list */
sc->an_aplist.an_type = AN_RID_APLIST;
sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
printf("%s: read record failed\n", ifp->if_xname);
return(EIO);
}
bcopy((char *)&sc->an_caps.an_oemaddr,
(char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
printf(": address %6s\n", ether_sprintf(sc->arpcom.ac_enaddr));
bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
ifp->if_softc = sc;
ifp->if_mtu = ETHERMTU;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = an_ioctl;
ifp->if_start = an_start;
ifp->if_watchdog = an_watchdog;
ifp->if_baudrate = 10000000;
IFQ_SET_READY(&ifp->if_snd);
bzero(sc->an_config.an_nodename, sizeof(sc->an_config.an_nodename));
bcopy(AN_DEFAULT_NODENAME, sc->an_config.an_nodename,
sizeof(AN_DEFAULT_NODENAME) - 1);
bzero(sc->an_ssidlist.an_ssid1, sizeof(sc->an_ssidlist.an_ssid1));
bcopy(AN_DEFAULT_NETNAME, sc->an_ssidlist.an_ssid1,
sizeof(AN_DEFAULT_NETNAME) - 1);
sc->an_ssidlist.an_ssid1_len = strlen(AN_DEFAULT_NETNAME);
sc->an_config.an_opmode = AN_OPMODE_INFRASTRUCTURE_STATION;
sc->an_tx_rate = 0;
bzero((char *)&sc->an_stats, sizeof(sc->an_stats));
#ifdef ANCACHE
sc->an_sigitems = sc->an_nextitem = 0;
#endif
ifmedia_init(&sc->an_ifmedia, 0, an_media_change, an_media_status);
#define ADD(m, c) ifmedia_add(&sc->an_ifmedia, (m), (c), NULL)
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
IFM_IEEE80211_ADHOC, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
IFM_IEEE80211_ADHOC, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0);
if (sc->an_caps.an_rates[2] == AN_RATE_5_5MBPS) {
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
IFM_IEEE80211_ADHOC, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 0, 0), 0);
}
if (sc->an_caps.an_rates[3] == AN_RATE_11MBPS) {
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
IFM_IEEE80211_ADHOC, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0);
}
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
IFM_IEEE80211_ADHOC, 0), 0);
ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0);
#undef ADD
ifmedia_set(&sc->an_ifmedia, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
0, 0));
/*
* Call MI attach routines.
*/
if_attach(ifp);
ether_ifattach(ifp);
timeout_set(&sc->an_stat_ch, an_stats_update, sc);
#if NBPFILTER > 0
BPFATTACH(&sc->arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
sizeof(struct ether_header));
#endif
shutdownhook_establish(an_shutdown, sc);
an_reset(sc);
an_init(sc);
return(0);
}
void
an_rxeof(sc)
struct an_softc *sc;
{
struct ifnet *ifp;
struct ether_header *eh;
#ifdef ANCACHE
struct an_rxframe rx_frame;
#endif
struct an_rxframe_802_3 rx_frame_802_3;
struct mbuf *m;
int id, error = 0;
ifp = &sc->arpcom.ac_if;
id = CSR_READ_2(sc, AN_RX_FID);
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
ifp->if_ierrors++;
return;
}
MCLGET(m, M_DONTWAIT);
if (!(m->m_flags & M_EXT)) {
m_freem(m);
ifp->if_ierrors++;
return;
}
m->m_pkthdr.rcvif = ifp;
eh = mtod(m, struct ether_header *);
#ifdef ANCACHE
/* Read NIC frame header */
if (an_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) {
ifp->if_ierrors++;
return;
}
#endif
/* Read in the 802_3 frame header */
if (an_read_data(sc, id, 0x34, (caddr_t)&rx_frame_802_3,
sizeof(rx_frame_802_3))) {
ifp->if_ierrors++;
return;
}
if (rx_frame_802_3.an_rx_802_3_status != 0) {
ifp->if_ierrors++;
return;
}
/* Check for insane frame length */
if (letoh16(rx_frame_802_3.an_rx_802_3_payload_len) > MCLBYTES) {
ifp->if_ierrors++;
return;
}
m->m_pkthdr.len = m->m_len =
letoh16(rx_frame_802_3.an_rx_802_3_payload_len) + 12;
bcopy((char *)&rx_frame_802_3.an_rx_dst_addr,
(char *)&eh->ether_dhost, ETHER_ADDR_LEN);
bcopy((char *)&rx_frame_802_3.an_rx_src_addr,
(char *)&eh->ether_shost, ETHER_ADDR_LEN);
/* in mbuf header type is just before payload */
error = an_read_data(sc, id, 0x44, (caddr_t)&(eh->ether_type),
letoh16(rx_frame_802_3.an_rx_802_3_payload_len));
if (error) {
m_freem(m);
ifp->if_ierrors++;
return;
}
ifp->if_ipackets++;
#if NBPFILTER > 0
/* Handle BPF listeners. */
if (ifp->if_bpf)
BPF_MTAP(ifp, m);
#endif
/* Receive packet. */
#ifdef ANCACHE
an_cache_store(sc, eh, m, rx_frame.an_rx_signal_strength);
#endif
ether_input_mbuf(ifp, m);
}
void
an_txeof(sc, status)
struct an_softc *sc;
int status;
{
struct ifnet *ifp;
int id;
ifp = &sc->arpcom.ac_if;
ifp->if_timer = 0;
ifp->if_flags &= ~IFF_OACTIVE;
id = CSR_READ_2(sc, AN_TX_CMP_FID);
if (status & AN_EV_TX_EXC)
ifp->if_oerrors++;
else
ifp->if_opackets++;
if (id != sc->an_rdata.an_tx_ring[sc->an_rdata.an_tx_cons])
printf("%s: id mismatch: expected %x, got %x\n", ifp->if_xname,
sc->an_rdata.an_tx_ring[sc->an_rdata.an_tx_cons], id);
sc->an_rdata.an_tx_ring[sc->an_rdata.an_tx_cons] = 0;
AN_INC(sc->an_rdata.an_tx_cons, AN_TX_RING_CNT);
}
/*
* We abuse the stats updater to check the current NIC status. This
* is important because we don't want to allow transmissions until
* the NIC has synchronized to the current cell (either as the master
* in an ad-hoc group, or as a station connected to an access point).
*/
void
an_stats_update(xsc)
void *xsc;
{
struct an_softc *sc;
struct ifnet *ifp;
int s;
s = splimp();
sc = xsc;
ifp = &sc->arpcom.ac_if;
sc->an_status.an_type = AN_RID_STATUS;
sc->an_status.an_len = sizeof(struct an_ltv_status);
an_read_record(sc, (struct an_ltv_gen *)&sc->an_status);
if (sc->an_status.an_opmode & AN_STATUS_OPMODE_IN_SYNC)
sc->an_associated = 1;
else
sc->an_associated = 0;
/* Don't do this while we're transmitting */
if (!(ifp->if_flags & IFF_OACTIVE)) {
sc->an_stats.an_len = sizeof(struct an_ltv_stats);
sc->an_stats.an_type = AN_RID_32BITS_CUM;
an_read_record(sc, (struct an_ltv_gen *)&sc->an_stats.an_len);
}
splx(s);
TIMEOUT(sc->an_stat_ch, an_stats_update, sc, hz);
}
int
an_intr(xsc)
void *xsc;
{
struct an_softc *sc;
struct ifnet *ifp;
u_int16_t status;
sc = (struct an_softc*)xsc;
if (sc->an_gone)
return 0;
ifp = &sc->arpcom.ac_if;
if (!(ifp->if_flags & IFF_UP)) {
CSR_WRITE_2(sc, AN_EVENT_ACK, 0xFFFF);
CSR_WRITE_2(sc, AN_INT_EN, 0);
return 0;
}
/* Disable interrupts. */
CSR_WRITE_2(sc, AN_INT_EN, 0);
status = CSR_READ_2(sc, AN_EVENT_STAT);
CSR_WRITE_2(sc, AN_EVENT_ACK, ~AN_INTRS);
if (status & AN_EV_AWAKE) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_AWAKE);
}
if (status & AN_EV_LINKSTAT) {
if (CSR_READ_2(sc, AN_LINKSTAT) == AN_LINKSTAT_ASSOCIATED)
sc->an_associated = 1;
else
sc->an_associated = 0;
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
}
if (status & AN_EV_RX) {
an_rxeof(sc);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
}
if (status & AN_EV_TX) {
an_txeof(sc, status);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_TX);
}
if (status & AN_EV_TX_EXC) {
an_txeof(sc, status);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_TX_EXC);
}
if (status & AN_EV_ALLOC)
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
/* Re-enable interrupts. */
CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS);
if (!IFQ_IS_EMPTY(&ifp->if_snd))
an_start(ifp);
return 1;
}
int
an_cmd(sc, cmd, val)
struct an_softc *sc;
int cmd;
int val;
{
int i, stat;
/* make sure previous command completed */
if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
printf("%s: command busy\n", sc->sc_dev.dv_xname);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -