#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/timeout.h>
#include <net/if.h>
#include <net/if_media.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_ioctl.h>
#include <machine/bus.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/ic/if_wireg.h>
#include <dev/ic/if_wi_ieee.h>
#include <dev/ic/if_wivar.h>
#define CIS_MFG_NAME_OFFSET 0x16
#define CIS_INFO_SIZE 256
const struct wi_pci_product *wi_pci_lookup(struct pci_attach_args *pa);
int wi_pci_match(struct device *, void *, void *);
void wi_pci_attach(struct device *, struct device *, void *);
int wi_pci_activate(struct device *, int);
void wi_pci_wakeup(struct wi_softc *);
int wi_pci_acex_attach(struct pci_attach_args *pa, struct wi_softc *sc);
int wi_pci_plx_attach(struct pci_attach_args *pa, struct wi_softc *sc);
int wi_pci_tmd_attach(struct pci_attach_args *pa, struct wi_softc *sc);
int wi_pci_native_attach(struct pci_attach_args *pa, struct wi_softc *sc);
int wi_pci_common_attach(struct pci_attach_args *pa, struct wi_softc *sc);
void wi_pci_plx_print_cis(struct wi_softc *);
struct wi_pci_softc {
struct wi_softc sc_wi;
};
const struct cfattach wi_pci_ca = {
sizeof (struct wi_pci_softc), wi_pci_match, wi_pci_attach, NULL,
wi_pci_activate
};
static const struct wi_pci_product {
pci_vendor_id_t pp_vendor;
pci_product_id_t pp_product;
int (*pp_attach)(struct pci_attach_args *pa, struct wi_softc *sc);
} wi_pci_products[] = {
{ PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P, wi_pci_plx_attach },
{ PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P02, wi_pci_plx_attach },
{ PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P03, wi_pci_plx_attach },
{ PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_8031, wi_pci_plx_attach },
{ PCI_VENDOR_EUMITCOM, PCI_PRODUCT_EUMITCOM_WL11000P, wi_pci_plx_attach },
{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_WL11000P, wi_pci_plx_attach },
{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE777A, wi_pci_plx_attach },
{ PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_MA301, wi_pci_plx_attach },
{ PCI_VENDOR_EFFICIENTNETS, PCI_PRODUCT_EFFICIENTNETS_SS1023, wi_pci_plx_attach },
{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_AWA100, wi_pci_plx_attach },
{ PCI_VENDOR_BELKIN, PCI_PRODUCT_BELKIN_F5D6000, wi_pci_plx_attach },
{ PCI_VENDOR_NDC, PCI_PRODUCT_NDC_NCP130, wi_pci_plx_attach },
{ PCI_VENDOR_NDC, PCI_PRODUCT_NDC_NCP130A2, wi_pci_tmd_attach },
{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN, wi_pci_native_attach },
{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_ISL3872, wi_pci_native_attach },
{ PCI_VENDOR_SAMSUNG, PCI_PRODUCT_SAMSUNG_SWL2210P, wi_pci_native_attach },
{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_211818A, wi_pci_acex_attach },
{ PCI_VENDOR_SYMBOL, PCI_PRODUCT_SYMBOL_LA41X3, wi_pci_acex_attach },
{ 0, 0, 0 }
};
const struct wi_pci_product *
wi_pci_lookup(struct pci_attach_args *pa)
{
const struct wi_pci_product *pp;
for (pp = wi_pci_products; pp->pp_product != 0; pp++) {
if (PCI_VENDOR(pa->pa_id) == pp->pp_vendor &&
PCI_PRODUCT(pa->pa_id) == pp->pp_product)
return (pp);
}
return (NULL);
}
int
wi_pci_match(struct device *parent, void *match, void *aux)
{
return (wi_pci_lookup(aux) != NULL);
}
void
wi_pci_attach(struct device *parent, struct device *self, void *aux)
{
struct wi_softc *sc = (struct wi_softc *)self;
struct pci_attach_args *pa = aux;
const struct wi_pci_product *pp;
pp = wi_pci_lookup(pa);
if (pp->pp_attach(pa, sc) != 0)
return;
printf("\n");
wi_attach(sc, &wi_func_io);
}
int
wi_pci_activate(struct device *self, int act)
{
struct wi_softc *sc = (struct wi_softc *)self;
struct ifnet *ifp = &sc->sc_ic.ic_if;
switch (act) {
case DVACT_SUSPEND:
if (ifp->if_flags & IFF_RUNNING)
wi_stop(sc);
break;
case DVACT_WAKEUP:
if (ifp->if_flags & IFF_UP)
wi_pci_wakeup(sc);
break;
}
return (0);
}
void
wi_pci_wakeup(struct wi_softc *sc)
{
int s;
s = splnet();
while (sc->wi_flags & WI_FLAGS_BUSY)
tsleep_nsec(&sc->wi_flags, 0, "wipwr", INFSLP);
sc->wi_flags |= WI_FLAGS_BUSY;
wi_init(sc);
sc->wi_flags &= ~WI_FLAGS_BUSY;
wakeup(&sc->wi_flags);
splx(s);
}
int
wi_pci_acex_attach(struct pci_attach_args *pa, struct wi_softc *sc)
{
bus_space_handle_t commandh, localh, ioh;
bus_space_tag_t commandt, localt;
bus_space_tag_t iot = pa->pa_iot;
bus_size_t commandsize, localsize, iosize;
int i;
if (pci_mapreg_map(pa, WI_ACEX_CMDRES, PCI_MAPREG_TYPE_IO,
0, &commandt, &commandh, NULL, &commandsize, 0) != 0) {
printf(": can't map command i/o space\n");
return (ENXIO);
}
if (pci_mapreg_map(pa, WI_ACEX_LOCALRES, PCI_MAPREG_TYPE_IO,
0, &localt, &localh, NULL, &localsize, 0) != 0) {
printf(": can't map local i/o space\n");
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
sc->wi_ltag = localt;
sc->wi_lhandle = localh;
if (pci_mapreg_map(pa, WI_TMD_IORES, PCI_MAPREG_TYPE_IO,
0, &iot, &ioh, NULL, &iosize, 0) != 0) {
printf(": can't map i/o space\n");
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
sc->wi_btag = iot;
sc->wi_bhandle = ioh;
if (bus_space_read_4(commandt, commandh, 0) & 1) {
printf(": bridge not ready\n");
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
bus_space_write_4(commandt, commandh, 2, 0x118);
bus_space_write_4(commandt, commandh, 2, 0x108);
DELAY(30 * 1000);
bus_space_write_4(commandt, commandh, 2, 0x8);
for (i = 0; i < 30; i++) {
DELAY(30 * 1000);
if (bus_space_read_4(commandt, commandh, 0) & 0x10)
break;
}
if (i == 30) {
printf(": bridge timeout\n");
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
if ((bus_space_read_4(localt, localh, 0xe0) & 1) ||
(bus_space_read_4(localt, localh, 0xe2) & 1) ||
(bus_space_read_4(localt, localh, 0xe4) & 1)) {
printf(": failed bridge setup\n");
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
if (wi_pci_common_attach(pa, sc) != 0) {
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(commandt, commandh, commandsize);
return (ENXIO);
}
bus_space_write_1(localt, localh, WI_ACEX_COR_OFFSET, WI_COR_IOMODE);
sc->wi_cor_offset = WI_ACEX_COR_OFFSET;
bus_space_unmap(commandt, commandh, commandsize);
return (0);
}
int
wi_pci_plx_attach(struct pci_attach_args *pa, struct wi_softc *sc)
{
bus_space_handle_t localh, ioh, memh;
bus_space_tag_t localt;
bus_space_tag_t iot = pa->pa_iot;
bus_space_tag_t memt = pa->pa_memt;
bus_size_t localsize, memsize, iosize;
u_int32_t intcsr;
if (pci_mapreg_map(pa, WI_PLX_MEMRES, PCI_MAPREG_TYPE_MEM, 0,
&memt, &memh, NULL, &memsize, 0) != 0) {
printf(": can't map mem space\n");
return (ENXIO);
}
sc->wi_ltag = memt;
sc->wi_lhandle = memh;
if (pci_mapreg_map(pa, WI_PLX_IORES,
PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, NULL, &iosize, 0) != 0) {
printf(": can't map i/o space\n");
bus_space_unmap(memt, memh, memsize);
return (ENXIO);
}
sc->wi_btag = iot;
sc->wi_bhandle = ioh;
localsize = 0;
if (pci_mapreg_type(pa->pa_pc, pa->pa_tag, WI_PLX_LOCALRES)
== PCI_MAPREG_TYPE_IO) {
if (pci_mapreg_map(pa, WI_PLX_LOCALRES, PCI_MAPREG_TYPE_IO,
0, &localt, &localh, NULL, &localsize, 0) != 0)
printf(": can't map PLX I/O space\n");
}
if (wi_pci_common_attach(pa, sc) != 0) {
if (localsize)
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(memt, memh, memsize);
return (ENXIO);
}
if (localsize != 0) {
intcsr = bus_space_read_4(localt, localh,
WI_PLX_INTCSR);
if (intcsr & WI_PLX_LINT1STAT) {
printf("\n%s: no PCMCIA card detected in bridge card\n",
WI_PRT_ARG(sc));
pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
if (localsize)
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(memt, memh, memsize);
return (ENXIO);
}
if (!(intcsr & WI_PLX_INTEN)) {
intcsr |= WI_PLX_INTEN;
bus_space_write_4(localt, localh, WI_PLX_INTCSR,
intcsr);
}
}
bus_space_write_1(memt, memh, WI_PLX_COR_OFFSET, WI_COR_IOMODE);
sc->wi_cor_offset = WI_PLX_COR_OFFSET;
if (localsize != 0) {
CSR_WRITE_2(sc, WI_SW0, WI_DRVR_MAGIC);
DELAY(1000);
if (CSR_READ_2(sc, WI_SW0) != WI_DRVR_MAGIC) {
printf("\n%s: no PCMCIA card detected in bridge card\n",
WI_PRT_ARG(sc));
pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
if (localsize)
bus_space_unmap(localt, localh, localsize);
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(memt, memh, memsize);
return (ENXIO);
}
bus_space_unmap(localt, localh, localsize);
wi_pci_plx_print_cis(sc);
}
return (0);
}
int
wi_pci_tmd_attach(struct pci_attach_args *pa, struct wi_softc *sc)
{
bus_space_handle_t localh, ioh;
bus_space_tag_t localt;
bus_space_tag_t iot = pa->pa_iot;
bus_size_t localsize, iosize;
if (pci_mapreg_map(pa, WI_TMD_LOCALRES, PCI_MAPREG_TYPE_IO,
0, &localt, &localh, NULL, &localsize, 0) != 0) {
printf(": can't map TMD I/O space\n");
return (ENXIO);
}
sc->wi_ltag = localt;
sc->wi_lhandle = localh;
if (pci_mapreg_map(pa, WI_TMD_IORES, PCI_MAPREG_TYPE_IO,
0, &iot, &ioh, NULL, &iosize, 0) != 0) {
printf(": can't map i/o space\n");
bus_space_unmap(localt, localh, localsize);
return (ENXIO);
}
sc->wi_btag = iot;
sc->wi_bhandle = ioh;
if (wi_pci_common_attach(pa, sc) != 0) {
bus_space_unmap(iot, ioh, iosize);
bus_space_unmap(localt, localh, localsize);
return (ENXIO);
}
bus_space_write_1(localt, localh, 0, WI_COR_IOMODE);
sc->wi_cor_offset = 0;
return (0);
}
int
wi_pci_native_attach(struct pci_attach_args *pa, struct wi_softc *sc)
{
bus_space_handle_t ioh;
bus_space_tag_t iot = pa->pa_iot;
bus_size_t iosize;
if (pci_mapreg_map(pa, WI_PCI_CBMA, PCI_MAPREG_TYPE_MEM,
0, &iot, &ioh, NULL, &iosize, 0) != 0) {
printf(": can't map mem space\n");
return (ENXIO);
}
sc->wi_ltag = iot;
sc->wi_lhandle = ioh;
sc->wi_btag = iot;
sc->wi_bhandle = ioh;
sc->sc_pci = 1;
if (wi_pci_common_attach(pa, sc) != 0) {
bus_space_unmap(iot, ioh, iosize);
return (ENXIO);
}
bus_space_write_2(iot, ioh, WI_PCI_COR_OFFSET, WI_COR_SOFT_RESET);
DELAY(100*1000);
bus_space_write_2(iot, ioh, WI_PCI_COR_OFFSET, WI_COR_CLEAR);
DELAY(100*1000);
sc->wi_cor_offset = WI_PCI_COR_OFFSET;
return (0);
}
int
wi_pci_common_attach(struct pci_attach_args *pa, struct wi_softc *sc)
{
pci_intr_handle_t ih;
pci_chipset_tag_t pc = pa->pa_pc;
const char *intrstr;
CSR_WRITE_2(sc, WI_INT_EN, 0);
CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
if (pci_intr_map(pa, &ih)) {
printf(": couldn't map interrupt\n");
return (ENXIO);
}
intrstr = pci_intr_string(pc, ih);
sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc,
sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": couldn't establish interrupt");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
return (ENXIO);
}
printf(": %s", intrstr);
return (0);
}
void
wi_pci_plx_print_cis(struct wi_softc *sc)
{
int i, stringno;
char cisbuf[CIS_INFO_SIZE];
char *cis_strings[3];
u_int8_t value;
const u_int8_t cis_magic[] = {
0x01, 0x03, 0x00, 0x00, 0xff, 0x17, 0x04, 0x67
};
for (i = 0; i < 8; i++) {
value = bus_space_read_1(sc->wi_ltag, sc->wi_lhandle, i * 2);
if (value != cis_magic[i])
return;
}
cis_strings[0] = cisbuf;
stringno = 0;
for (i = 0; i < CIS_INFO_SIZE && stringno < 3; i++) {
cisbuf[i] = bus_space_read_1(sc->wi_ltag,
sc->wi_lhandle, (CIS_MFG_NAME_OFFSET + i) * 2);
if (cisbuf[i] == '\0' && ++stringno < 3)
cis_strings[stringno] = &cisbuf[i + 1];
}
cisbuf[CIS_INFO_SIZE - 1] = '\0';
printf("\n%s: \"%s, %s, %s\"", WI_PRT_ARG(sc),
cis_strings[0], cis_strings[1], cis_strings[2]);
}