mhzc.c revision 1.34.12.1       1  1.34.12.1      tron /*	$NetBSD: mhzc.c,v 1.34.12.1 2006/03/28 09:42:14 tron Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*-
      4       1.24   mycroft  * Copyright (c) 1999, 2000, 2004 The NetBSD Foundation, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.24   mycroft  * NASA Ames Research Center, and by Charles M. Hannum.
     10        1.1   thorpej  *
     11        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.1   thorpej  * modification, are permitted provided that the following conditions
     13        1.1   thorpej  * are met:
     14        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20        1.1   thorpej  *    must display the following acknowledgement:
     21        1.1   thorpej  *	This product includes software developed by the NetBSD
     22        1.1   thorpej  *	Foundation, Inc. and its contributors.
     23        1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.1   thorpej  *    contributors may be used to endorse or promote products derived
     25        1.1   thorpej  *    from this software without specific prior written permission.
     26        1.1   thorpej  *
     27        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38        1.1   thorpej  */
     39        1.1   thorpej 
     40        1.1   thorpej /*
     41        1.1   thorpej  * Device driver for the Megaherz X-JACK Ethernet/Modem combo cards.
     42        1.1   thorpej  *
     43        1.1   thorpej  * Many thanks to Chuck Cranor for having the patience to sift through
     44        1.1   thorpej  * the Linux smc91c92_cs.c driver to find the magic details to get this
     45        1.1   thorpej  * working!
     46        1.1   thorpej  */
     47        1.8     lukem 
     48        1.8     lukem #include <sys/cdefs.h>
     49  1.34.12.1      tron __KERNEL_RCSID(0, "$NetBSD: mhzc.c,v 1.34.12.1 2006/03/28 09:42:14 tron Exp $");
     50        1.1   thorpej 
     51       1.31     perry #include "opt_inet.h"
     52        1.1   thorpej #include "opt_ns.h"
     53        1.1   thorpej #include "bpfilter.h"
     54        1.1   thorpej 
     55        1.1   thorpej #include <sys/param.h>
     56        1.1   thorpej #include <sys/systm.h>
     57        1.1   thorpej #include <sys/mbuf.h>
     58        1.1   thorpej #include <sys/socket.h>
     59        1.1   thorpej #include <sys/ioctl.h>
     60        1.1   thorpej #include <sys/errno.h>
     61        1.1   thorpej #include <sys/syslog.h>
     62        1.1   thorpej #include <sys/select.h>
     63        1.1   thorpej #include <sys/tty.h>
     64        1.1   thorpej #include <sys/device.h>
     65       1.29   mycroft #include <sys/kernel.h>
     66       1.29   mycroft #include <sys/proc.h>
     67        1.1   thorpej 
     68       1.31     perry #include <net/if.h>
     69       1.31     perry #include <net/if_dl.h>
     70        1.1   thorpej #include <net/if_ether.h>
     71       1.31     perry #include <net/if_media.h>
     72        1.1   thorpej 
     73        1.1   thorpej #ifdef INET
     74        1.1   thorpej #include <netinet/in.h>
     75        1.1   thorpej #include <netinet/in_systm.h>
     76        1.1   thorpej #include <netinet/in_var.h>
     77        1.1   thorpej #include <netinet/ip.h>
     78        1.1   thorpej #include <netinet/if_inarp.h>
     79        1.1   thorpej #endif
     80       1.31     perry 
     81       1.31     perry #ifdef NS
     82        1.1   thorpej #include <netns/ns.h>
     83        1.1   thorpej #include <netns/ns_if.h>
     84       1.31     perry #endif
     85        1.1   thorpej 
     86        1.1   thorpej #if NBPFILTER > 0
     87        1.1   thorpej #include <net/bpf.h>
     88        1.1   thorpej #include <net/bpfdesc.h>
     89        1.1   thorpej #endif
     90        1.1   thorpej 
     91        1.1   thorpej #include <machine/intr.h>
     92        1.1   thorpej #include <machine/bus.h>
     93        1.1   thorpej 
     94        1.1   thorpej #include <dev/ic/comreg.h>
     95        1.1   thorpej #include <dev/ic/comvar.h>
     96        1.6    briggs 
     97        1.6    briggs #include <dev/mii/mii.h>
     98        1.6    briggs #include <dev/mii/miivar.h>
     99        1.1   thorpej 
    100        1.1   thorpej #include <dev/ic/smc91cxxreg.h>
    101        1.1   thorpej #include <dev/ic/smc91cxxvar.h>
    102        1.1   thorpej 
    103        1.1   thorpej #include <dev/pcmcia/pcmciareg.h>
    104        1.1   thorpej #include <dev/pcmcia/pcmciavar.h>
    105        1.1   thorpej #include <dev/pcmcia/pcmciadevs.h>
    106        1.1   thorpej 
    107        1.1   thorpej #include "mhzc.h"
    108        1.1   thorpej 
    109        1.1   thorpej struct mhzc_softc {
    110        1.1   thorpej 	struct device sc_dev;		/* generic device glue */
    111        1.1   thorpej 
    112        1.1   thorpej 	struct pcmcia_function *sc_pf;	/* our PCMCIA function */
    113        1.1   thorpej 	void *sc_ih;			/* interrupt handle */
    114        1.1   thorpej 
    115        1.1   thorpej 	const struct mhzc_product *sc_product;
    116        1.1   thorpej 
    117        1.1   thorpej 	/*
    118        1.1   thorpej 	 * Data for the Modem portion.
    119        1.1   thorpej 	 */
    120        1.1   thorpej 	struct device *sc_modem;
    121        1.1   thorpej 	struct pcmcia_io_handle sc_modem_pcioh;
    122        1.1   thorpej 	int sc_modem_io_window;
    123        1.1   thorpej 
    124        1.1   thorpej 	/*
    125        1.1   thorpej 	 * Data for the Ethernet portion.
    126        1.1   thorpej 	 */
    127        1.1   thorpej 	struct device *sc_ethernet;
    128        1.1   thorpej 	struct pcmcia_io_handle sc_ethernet_pcioh;
    129        1.1   thorpej 	int sc_ethernet_io_window;
    130        1.1   thorpej 
    131        1.1   thorpej 	int sc_flags;
    132        1.1   thorpej };
    133        1.1   thorpej 
    134        1.1   thorpej /* sc_flags */
    135        1.1   thorpej #define	MHZC_MODEM_MAPPED	0x01
    136        1.1   thorpej #define	MHZC_ETHERNET_MAPPED	0x02
    137        1.1   thorpej #define	MHZC_MODEM_ENABLED	0x04
    138        1.1   thorpej #define	MHZC_ETHERNET_ENABLED	0x08
    139       1.21   mycroft #define	MHZC_MODEM_ALLOCED	0x10
    140       1.21   mycroft #define	MHZC_ETHERNET_ALLOCED	0x20
    141        1.1   thorpej 
    142       1.30     perry int	mhzc_match(struct device *, struct cfdata *, void *);
    143       1.30     perry void	mhzc_attach(struct device *, struct device *, void *);
    144       1.30     perry int	mhzc_detach(struct device *, int);
    145       1.30     perry int	mhzc_activate(struct device *, enum devact);
    146        1.1   thorpej 
    147       1.12   thorpej CFATTACH_DECL(mhzc, sizeof(struct mhzc_softc),
    148       1.13   thorpej     mhzc_match, mhzc_attach, mhzc_detach, mhzc_activate);
    149        1.1   thorpej 
    150       1.30     perry int	mhzc_em3336_enaddr(struct mhzc_softc *, u_int8_t *);
    151       1.30     perry int	mhzc_em3336_enable(struct mhzc_softc *);
    152        1.1   thorpej 
    153        1.4       cgd const struct mhzc_product {
    154        1.4       cgd 	struct pcmcia_product mp_product;
    155        1.1   thorpej 
    156        1.1   thorpej 	/* Get the Ethernet address for this card. */
    157       1.30     perry 	int		(*mp_enaddr)(struct mhzc_softc *, u_int8_t *);
    158        1.1   thorpej 
    159        1.1   thorpej 	/* Perform any special `enable' magic. */
    160       1.30     perry 	int		(*mp_enable)(struct mhzc_softc *);
    161        1.1   thorpej } mhzc_products[] = {
    162       1.27   mycroft 	{ { PCMCIA_VENDOR_MEGAHERTZ, PCMCIA_PRODUCT_MEGAHERTZ_EM3336,
    163       1.27   mycroft 	    PCMCIA_CIS_INVALID },
    164        1.1   thorpej 	  mhzc_em3336_enaddr,		mhzc_em3336_enable },
    165        1.1   thorpej };
    166       1.27   mycroft static const size_t mhzc_nproducts =
    167       1.27   mycroft     sizeof(mhzc_products) / sizeof(mhzc_products[0]);
    168        1.1   thorpej 
    169       1.30     perry int	mhzc_print(void *, const char *);
    170        1.1   thorpej 
    171       1.30     perry int	mhzc_check_cfe(struct mhzc_softc *, struct pcmcia_config_entry *);
    172       1.30     perry int	mhzc_alloc_ethernet(struct mhzc_softc *, struct pcmcia_config_entry *);
    173        1.1   thorpej 
    174       1.30     perry int	mhzc_enable(struct mhzc_softc *, int);
    175       1.30     perry void	mhzc_disable(struct mhzc_softc *, int);
    176        1.1   thorpej 
    177       1.30     perry int	mhzc_intr(void *);
    178        1.1   thorpej 
    179        1.1   thorpej int
    180        1.1   thorpej mhzc_match(parent, match, aux)
    181        1.1   thorpej 	struct device *parent;
    182        1.1   thorpej 	struct cfdata *match;
    183        1.1   thorpej 	void *aux;
    184        1.1   thorpej {
    185        1.1   thorpej 	struct pcmcia_attach_args *pa = aux;
    186        1.1   thorpej 
    187       1.27   mycroft 	if (pcmcia_product_lookup(pa, mhzc_products, mhzc_nproducts,
    188       1.27   mycroft 	    sizeof(mhzc_products[0]), NULL))
    189       1.27   mycroft 		return (2);		/* beat `com' */
    190        1.1   thorpej 	return (0);
    191        1.1   thorpej }
    192        1.1   thorpej 
    193        1.1   thorpej void
    194        1.1   thorpej mhzc_attach(parent, self, aux)
    195        1.1   thorpej 	struct device *parent, *self;
    196        1.1   thorpej 	void *aux;
    197        1.1   thorpej {
    198        1.1   thorpej 	struct mhzc_softc *sc = (void *)self;
    199        1.1   thorpej 	struct pcmcia_attach_args *pa = aux;
    200        1.1   thorpej 	struct pcmcia_config_entry *cfe;
    201       1.25   mycroft 	int error;
    202        1.1   thorpej 
    203        1.1   thorpej 	sc->sc_pf = pa->pf;
    204        1.1   thorpej 
    205       1.27   mycroft 	sc->sc_product = pcmcia_product_lookup(pa, mhzc_products,
    206       1.27   mycroft 	    mhzc_nproducts, sizeof(mhzc_products[0]), NULL);
    207       1.27   mycroft 	if (!sc->sc_product)
    208        1.1   thorpej 		panic("mhzc_attach: impossible");
    209        1.1   thorpej 
    210        1.1   thorpej 	/*
    211        1.1   thorpej 	 * The address decoders on these cards are wacky.  The configuration
    212        1.1   thorpej 	 * entries are set up to look like serial ports, and have no
    213        1.1   thorpej 	 * information about the Ethernet portion.  In order to talk to
    214        1.1   thorpej 	 * the Modem portion, the I/O address must have bit 0x80 set.
    215        1.1   thorpej 	 * In order to talk to the Ethernet portion, the I/O address must
    216        1.1   thorpej 	 * have the 0x80 bit clear.
    217        1.1   thorpej 	 *
    218        1.1   thorpej 	 * The standard configuration entries conveniently have 0x80 set
    219        1.1   thorpej 	 * in them, and have a length of 8 (a 16550's size, convenient!),
    220        1.1   thorpej 	 * so we use those to set up the Modem portion.
    221        1.1   thorpej 	 *
    222        1.1   thorpej 	 * Once we have the Modem's address established, we search for
    223        1.1   thorpej 	 * an address suitable for the Ethernet portion.  We do this by
    224        1.1   thorpej 	 * rounding up to the next 16-byte aligned address where 0x80
    225        1.1   thorpej 	 * isn't set (the SMC Ethernet chip has a 16-byte address size)
    226        1.1   thorpej 	 * and attemping to allocate a 16-byte region until we succeed.
    227        1.1   thorpej 	 *
    228        1.1   thorpej 	 * Sure would have been nice if Megahertz had made the card a
    229        1.1   thorpej 	 * proper multi-function device.
    230        1.1   thorpej 	 */
    231        1.9     lukem 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    232        1.1   thorpej 		if (mhzc_check_cfe(sc, cfe)) {
    233        1.1   thorpej 			/* Found one! */
    234        1.1   thorpej 			break;
    235        1.1   thorpej 		}
    236        1.1   thorpej 	}
    237        1.1   thorpej 	if (cfe == NULL) {
    238       1.16   mycroft 		aprint_error("%s: unable to find suitable config table entry\n",
    239       1.16   mycroft 		    self->dv_xname);
    240       1.21   mycroft 		goto fail;
    241        1.1   thorpej 	}
    242        1.1   thorpej 
    243        1.7       uwe 	if (mhzc_alloc_ethernet(sc, cfe) == 0) {
    244       1.16   mycroft 		aprint_error("%s: unable to allocate space for Ethernet portion\n",
    245       1.16   mycroft 		    self->dv_xname);
    246       1.21   mycroft 		goto fail;
    247        1.1   thorpej 	}
    248        1.1   thorpej 
    249        1.1   thorpej 	/* Enable the card. */
    250        1.1   thorpej 	pcmcia_function_init(pa->pf, cfe);
    251       1.21   mycroft 
    252       1.23   mycroft 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO8, &sc->sc_modem_pcioh,
    253       1.23   mycroft 	    &sc->sc_modem_io_window)) {
    254       1.23   mycroft 		aprint_error("%s: unable to map I/O space\n",
    255       1.23   mycroft 		    sc->sc_dev.dv_xname);
    256       1.23   mycroft 		goto fail;
    257       1.23   mycroft 	}
    258       1.23   mycroft 	sc->sc_flags |= MHZC_MODEM_MAPPED;
    259       1.23   mycroft 
    260       1.23   mycroft 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_AUTO, &sc->sc_ethernet_pcioh,
    261       1.23   mycroft 	    &sc->sc_ethernet_io_window)) {
    262       1.23   mycroft 		aprint_error("%s: unable to map I/O space\n",
    263       1.23   mycroft 		    sc->sc_dev.dv_xname);
    264       1.23   mycroft 		goto fail;
    265       1.23   mycroft 	}
    266       1.23   mycroft 	sc->sc_flags |= MHZC_ETHERNET_MAPPED;
    267       1.23   mycroft 
    268       1.25   mycroft 	error = mhzc_enable(sc, MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED);
    269       1.25   mycroft 	if (error)
    270       1.21   mycroft 		goto fail;
    271        1.1   thorpej 
    272       1.32  christos 	/*XXXUNCONST*/
    273       1.32  christos 	sc->sc_modem = config_found(self, __UNCONST("com"), mhzc_print);
    274       1.32  christos 	/*XXXUNCONST*/
    275       1.32  christos 	sc->sc_ethernet = config_found(self, __UNCONST("sm"), mhzc_print);
    276        1.1   thorpej 
    277       1.21   mycroft 	mhzc_disable(sc, MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED);
    278        1.5     enami 	return;
    279        1.5     enami 
    280       1.21   mycroft fail:
    281       1.23   mycroft 	/* I/O spaces will be freed by detach. */
    282       1.23   mycroft 	;
    283        1.1   thorpej }
    284        1.1   thorpej 
    285        1.1   thorpej int
    286        1.1   thorpej mhzc_check_cfe(sc, cfe)
    287        1.1   thorpej 	struct mhzc_softc *sc;
    288        1.1   thorpej 	struct pcmcia_config_entry *cfe;
    289        1.1   thorpej {
    290        1.1   thorpej 
    291        1.1   thorpej 	if (cfe->num_memspace != 0)
    292        1.1   thorpej 		return (0);
    293        1.1   thorpej 
    294        1.1   thorpej 	if (cfe->num_iospace != 1)
    295        1.1   thorpej 		return (0);
    296        1.1   thorpej 
    297        1.1   thorpej 	if (pcmcia_io_alloc(sc->sc_pf,
    298        1.1   thorpej 	    cfe->iospace[0].start,
    299        1.1   thorpej 	    cfe->iospace[0].length,
    300        1.1   thorpej 	    cfe->iospace[0].length,
    301        1.1   thorpej 	    &sc->sc_modem_pcioh) == 0) {
    302        1.1   thorpej 		/* Found one for the modem! */
    303       1.21   mycroft 		sc->sc_flags |= MHZC_MODEM_ALLOCED;
    304        1.1   thorpej 		return (1);
    305        1.1   thorpej 	}
    306        1.1   thorpej 
    307        1.1   thorpej 	return (0);
    308        1.1   thorpej }
    309        1.1   thorpej 
    310        1.1   thorpej int
    311        1.7       uwe mhzc_alloc_ethernet(sc, cfe)
    312        1.1   thorpej 	struct mhzc_softc *sc;
    313        1.7       uwe 	struct pcmcia_config_entry *cfe;
    314        1.1   thorpej {
    315        1.1   thorpej 	bus_addr_t addr, maxaddr;
    316        1.1   thorpej 
    317        1.7       uwe 	addr = cfe->iospace[0].start + cfe->iospace[0].length;
    318        1.7       uwe 	maxaddr = 0x1000;
    319        1.1   thorpej 
    320        1.1   thorpej 	/*
    321        1.1   thorpej 	 * Now round it up so that it starts on a 16-byte boundary.
    322        1.1   thorpej 	 */
    323        1.1   thorpej 	addr = roundup(addr, 0x10);
    324        1.1   thorpej 
    325        1.1   thorpej 	for (; (addr + 0x10) < maxaddr; addr += 0x10) {
    326        1.1   thorpej 		if (addr & 0x80)
    327        1.1   thorpej 			continue;
    328        1.1   thorpej 		if (pcmcia_io_alloc(sc->sc_pf, addr, 0x10, 0x10,
    329        1.1   thorpej 		    &sc->sc_ethernet_pcioh) == 0) {
    330        1.1   thorpej 			/* Found one for the ethernet! */
    331       1.21   mycroft 			sc->sc_flags |= MHZC_ETHERNET_ALLOCED;
    332        1.1   thorpej 			return (1);
    333        1.1   thorpej 		}
    334        1.1   thorpej 	}
    335        1.1   thorpej 
    336        1.1   thorpej 	return (0);
    337        1.1   thorpej }
    338        1.1   thorpej 
    339        1.1   thorpej int
    340        1.1   thorpej mhzc_print(aux, pnp)
    341        1.1   thorpej 	void *aux;
    342        1.1   thorpej 	const char *pnp;
    343        1.1   thorpej {
    344        1.1   thorpej 	const char *name = aux;
    345        1.1   thorpej 
    346        1.1   thorpej 	if (pnp)
    347       1.14   thorpej 		aprint_normal("%s at %s(*)",  name, pnp);
    348        1.1   thorpej 
    349        1.1   thorpej 	return (UNCONF);
    350        1.1   thorpej }
    351        1.1   thorpej 
    352        1.1   thorpej int
    353        1.1   thorpej mhzc_detach(self, flags)
    354        1.1   thorpej 	struct device *self;
    355        1.1   thorpej 	int flags;
    356        1.1   thorpej {
    357        1.1   thorpej 	struct mhzc_softc *sc = (void *)self;
    358        1.1   thorpej 	int rv;
    359        1.1   thorpej 
    360        1.1   thorpej 	if (sc->sc_ethernet != NULL) {
    361        1.1   thorpej 		rv = config_detach(sc->sc_ethernet, flags);
    362        1.1   thorpej 		if (rv != 0)
    363        1.1   thorpej 			return (rv);
    364        1.5     enami 		sc->sc_ethernet = NULL;
    365        1.1   thorpej 	}
    366        1.1   thorpej 
    367        1.1   thorpej 	if (sc->sc_modem != NULL) {
    368        1.1   thorpej 		rv = config_detach(sc->sc_modem, flags);
    369        1.1   thorpej 		if (rv != 0)
    370        1.1   thorpej 			return (rv);
    371        1.5     enami 		sc->sc_modem = NULL;
    372        1.1   thorpej 	}
    373        1.1   thorpej 
    374        1.1   thorpej 	/* Unmap our i/o windows. */
    375        1.1   thorpej 	if (sc->sc_flags & MHZC_MODEM_MAPPED)
    376        1.1   thorpej 		pcmcia_io_unmap(sc->sc_pf, sc->sc_modem_io_window);
    377        1.1   thorpej 	if (sc->sc_flags & MHZC_ETHERNET_MAPPED)
    378        1.1   thorpej 		pcmcia_io_unmap(sc->sc_pf, sc->sc_ethernet_io_window);
    379        1.1   thorpej 
    380        1.1   thorpej 	/* Free our i/o spaces. */
    381       1.21   mycroft 	if (sc->sc_flags & MHZC_ETHERNET_ALLOCED)
    382        1.5     enami 		pcmcia_io_free(sc->sc_pf, &sc->sc_modem_pcioh);
    383       1.21   mycroft 	if (sc->sc_flags & MHZC_MODEM_ALLOCED)
    384        1.5     enami 		pcmcia_io_free(sc->sc_pf, &sc->sc_ethernet_pcioh);
    385       1.23   mycroft 
    386       1.21   mycroft 	sc->sc_flags = 0;
    387        1.1   thorpej 
    388        1.1   thorpej 	return (0);
    389        1.1   thorpej }
    390        1.1   thorpej 
    391        1.1   thorpej int
    392        1.1   thorpej mhzc_activate(self, act)
    393        1.1   thorpej 	struct device *self;
    394        1.1   thorpej 	enum devact act;
    395        1.1   thorpej {
    396        1.1   thorpej 	struct mhzc_softc *sc = (void *)self;
    397        1.1   thorpej 	int s, rv = 0;
    398        1.1   thorpej 
    399        1.1   thorpej 	s = splhigh();
    400        1.1   thorpej 	switch (act) {
    401        1.1   thorpej 	case DVACT_ACTIVATE:
    402        1.1   thorpej 		rv = EOPNOTSUPP;
    403        1.1   thorpej 		break;
    404        1.1   thorpej 
    405        1.1   thorpej 	case DVACT_DEACTIVATE:
    406        1.1   thorpej 		if (sc->sc_ethernet != NULL) {
    407        1.1   thorpej 			rv = config_deactivate(sc->sc_ethernet);
    408        1.1   thorpej 			if (rv != 0)
    409        1.1   thorpej 				goto out;
    410        1.1   thorpej 		}
    411        1.1   thorpej 
    412        1.1   thorpej 		if (sc->sc_modem != NULL) {
    413        1.1   thorpej 			rv = config_deactivate(sc->sc_modem);
    414        1.1   thorpej 			if (rv != 0)
    415        1.1   thorpej 				goto out;
    416        1.1   thorpej 		}
    417        1.1   thorpej 		break;
    418        1.1   thorpej 	}
    419        1.1   thorpej  out:
    420        1.1   thorpej 	splx(s);
    421        1.1   thorpej 	return (rv);
    422        1.1   thorpej }
    423        1.1   thorpej 
    424        1.1   thorpej int
    425        1.1   thorpej mhzc_intr(arg)
    426        1.1   thorpej 	void *arg;
    427        1.1   thorpej {
    428        1.1   thorpej 	struct mhzc_softc *sc = arg;
    429        1.1   thorpej 	int rval = 0;
    430        1.1   thorpej 
    431        1.1   thorpej #if NCOM_MHZC > 0
    432        1.1   thorpej 	if (sc->sc_modem != NULL &&
    433        1.1   thorpej 	    (sc->sc_flags & MHZC_MODEM_ENABLED) != 0)
    434        1.1   thorpej 		rval |= comintr(sc->sc_modem);
    435        1.1   thorpej #endif
    436        1.1   thorpej 
    437        1.1   thorpej #if NSM_MHZC > 0
    438        1.1   thorpej 	if (sc->sc_ethernet != NULL &&
    439        1.1   thorpej 	    (sc->sc_flags & MHZC_ETHERNET_ENABLED) != 0)
    440        1.1   thorpej 		rval |= smc91cxx_intr(sc->sc_ethernet);
    441        1.1   thorpej #endif
    442        1.1   thorpej 
    443        1.1   thorpej 	return (rval);
    444        1.1   thorpej }
    445        1.1   thorpej 
    446        1.1   thorpej int
    447        1.1   thorpej mhzc_enable(sc, flag)
    448        1.1   thorpej 	struct mhzc_softc *sc;
    449        1.1   thorpej 	int flag;
    450        1.1   thorpej {
    451       1.25   mycroft 	int error;
    452        1.1   thorpej 
    453       1.21   mycroft 	if ((sc->sc_flags & flag) == flag) {
    454       1.21   mycroft 		printf("%s: already enabled\n", sc->sc_dev.dv_xname);
    455       1.21   mycroft 		return (0);
    456        1.1   thorpej 	}
    457        1.1   thorpej 
    458        1.1   thorpej 	if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0) {
    459        1.1   thorpej 		sc->sc_flags |= flag;
    460        1.1   thorpej 		return (0);
    461        1.1   thorpej 	}
    462        1.1   thorpej 
    463        1.1   thorpej 	/*
    464        1.1   thorpej 	 * Establish our interrupt handler.
    465        1.1   thorpej 	 *
    466        1.1   thorpej 	 * XXX Note, we establish this at IPL_NET.  This is suboptimal
    467        1.1   thorpej 	 * XXX the Modem portion, but is necessary to make the Ethernet
    468        1.1   thorpej 	 * XXX portion have the correct interrupt level semantics.
    469        1.1   thorpej 	 *
    470        1.1   thorpej 	 * XXX Eventually we should use the `enabled' bits in the
    471        1.1   thorpej 	 * XXX flags word to determine which level we should be at.
    472        1.1   thorpej 	 */
    473        1.1   thorpej 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET,
    474        1.1   thorpej 	    mhzc_intr, sc);
    475       1.25   mycroft 	if (!sc->sc_ih)
    476       1.25   mycroft 		return (EIO);
    477        1.1   thorpej 
    478       1.25   mycroft 	error = pcmcia_function_enable(sc->sc_pf);
    479       1.25   mycroft 	if (error) {
    480        1.2   thorpej 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    481       1.26   mycroft 		sc->sc_ih = 0;
    482       1.25   mycroft 		return (error);
    483        1.2   thorpej 	}
    484        1.2   thorpej 
    485        1.2   thorpej 	/*
    486        1.2   thorpej 	 * Perform any special enable magic necessary.
    487        1.2   thorpej 	 */
    488        1.2   thorpej 	if (sc->sc_product->mp_enable != NULL &&
    489        1.2   thorpej 	    (*sc->sc_product->mp_enable)(sc) != 0) {
    490        1.2   thorpej 		pcmcia_function_disable(sc->sc_pf);
    491        1.1   thorpej 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    492        1.1   thorpej 		return (1);
    493        1.1   thorpej 	}
    494        1.1   thorpej 
    495        1.1   thorpej 	sc->sc_flags |= flag;
    496        1.1   thorpej 	return (0);
    497        1.1   thorpej }
    498        1.1   thorpej 
    499        1.1   thorpej void
    500        1.1   thorpej mhzc_disable(sc, flag)
    501        1.1   thorpej 	struct mhzc_softc *sc;
    502        1.1   thorpej 	int flag;
    503        1.1   thorpej {
    504        1.1   thorpej 
    505        1.1   thorpej 	if ((sc->sc_flags & flag) == 0) {
    506       1.21   mycroft 		printf("%s: already disabled\n", sc->sc_dev.dv_xname);
    507       1.21   mycroft 		return;
    508        1.1   thorpej 	}
    509        1.1   thorpej 
    510        1.1   thorpej 	sc->sc_flags &= ~flag;
    511        1.1   thorpej 	if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0)
    512        1.1   thorpej 		return;
    513        1.1   thorpej 
    514       1.22   mycroft 	pcmcia_function_disable(sc->sc_pf);
    515        1.1   thorpej 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    516       1.25   mycroft 	sc->sc_ih = 0;
    517        1.1   thorpej }
    518        1.1   thorpej 
    519        1.1   thorpej /*****************************************************************************
    520        1.1   thorpej  * Megahertz EM3336 (and compatibles) support
    521        1.1   thorpej  *****************************************************************************/
    522        1.1   thorpej 
    523       1.30     perry int	mhzc_em3336_lannid_ciscallback(struct pcmcia_tuple *, void *);
    524       1.30     perry int	mhzc_em3336_ascii_enaddr(const char *cisstr, u_int8_t *);
    525        1.1   thorpej 
    526        1.1   thorpej int
    527        1.1   thorpej mhzc_em3336_enaddr(sc, myla)
    528        1.1   thorpej 	struct mhzc_softc *sc;
    529        1.1   thorpej 	u_int8_t *myla;
    530        1.1   thorpej {
    531        1.1   thorpej 
    532        1.1   thorpej 	/* Get the station address from CIS tuple 0x81. */
    533  1.34.12.1      tron 	if (pcmcia_scan_cis(device_parent(&sc->sc_dev),
    534        1.1   thorpej 	    mhzc_em3336_lannid_ciscallback, myla) != 1) {
    535        1.1   thorpej 		printf("%s: unable to get Ethernet address from CIS\n",
    536        1.1   thorpej 		    sc->sc_dev.dv_xname);
    537        1.1   thorpej 		return (0);
    538        1.1   thorpej 	}
    539        1.1   thorpej 
    540        1.1   thorpej 	return (1);
    541        1.1   thorpej }
    542        1.1   thorpej 
    543        1.1   thorpej int
    544        1.1   thorpej mhzc_em3336_enable(sc)
    545        1.1   thorpej 	struct mhzc_softc *sc;
    546        1.1   thorpej {
    547        1.1   thorpej 	struct pcmcia_mem_handle memh;
    548       1.33       jdc 	bus_size_t memoff;
    549        1.1   thorpej 	int memwin, reg;
    550        1.1   thorpej 
    551        1.1   thorpej 	/*
    552        1.1   thorpej 	 * Bring the chip to live by touching its registers in the correct
    553        1.1   thorpej 	 * way (as per my reference... the Linux smc91c92_cs.c driver by
    554        1.1   thorpej 	 * David A. Hinds).
    555        1.1   thorpej 	 */
    556       1.31     perry 
    557        1.1   thorpej 	/* Map the ISRPOWEREG. */
    558        1.1   thorpej 	if (pcmcia_mem_alloc(sc->sc_pf, 0x1000, &memh) != 0) {
    559        1.1   thorpej 		printf("%s: unable to allocate memory space\n",
    560        1.1   thorpej 		    sc->sc_dev.dv_xname);
    561        1.1   thorpej 		return (1);
    562        1.1   thorpej 	}
    563        1.1   thorpej 
    564        1.1   thorpej 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_ATTR, 0, 0x1000,
    565        1.1   thorpej 	    &memh, &memoff, &memwin)) {
    566        1.1   thorpej 		printf("%s: unable to map memory space\n",
    567        1.1   thorpej 		    sc->sc_dev.dv_xname);
    568        1.1   thorpej 		pcmcia_mem_free(sc->sc_pf, &memh);
    569        1.1   thorpej 		return (1);
    570        1.1   thorpej 	}
    571        1.1   thorpej 
    572        1.1   thorpej 	/*
    573        1.1   thorpej 	 * The magic sequence:
    574        1.1   thorpej 	 *
    575        1.1   thorpej 	 *	- read/write the CCR option register.
    576        1.1   thorpej 	 *	- read the ISRPOWEREG 2 times.
    577        1.1   thorpej 	 *	- read/write the CCR option register again.
    578        1.1   thorpej 	 */
    579        1.1   thorpej 
    580        1.1   thorpej 	reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
    581        1.1   thorpej 	pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
    582        1.1   thorpej 
    583        1.1   thorpej 	reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
    584        1.1   thorpej 	delay(5);
    585        1.1   thorpej 	reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
    586        1.1   thorpej 
    587       1.29   mycroft 	tsleep(&mhzc_em3336_enable, PWAIT, "mhz3en", hz * 200 / 1000);
    588        1.1   thorpej 
    589        1.1   thorpej 	reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
    590        1.1   thorpej 	delay(5);
    591        1.1   thorpej 	pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
    592        1.1   thorpej 
    593        1.1   thorpej 	pcmcia_mem_unmap(sc->sc_pf, memwin);
    594        1.1   thorpej 	pcmcia_mem_free(sc->sc_pf, &memh);
    595        1.1   thorpej 
    596        1.1   thorpej 	return (0);
    597        1.1   thorpej }
    598        1.1   thorpej 
    599        1.1   thorpej int
    600        1.1   thorpej mhzc_em3336_lannid_ciscallback(tuple, arg)
    601        1.1   thorpej 	struct pcmcia_tuple *tuple;
    602        1.1   thorpej 	void *arg;
    603        1.1   thorpej {
    604        1.1   thorpej 	u_int8_t *myla = arg, addr_str[ETHER_ADDR_LEN * 2];
    605        1.1   thorpej 	int i;
    606        1.1   thorpej 
    607        1.1   thorpej 	if (tuple->code == 0x81) {
    608        1.1   thorpej 		/*
    609        1.1   thorpej 		 * We have a string-encoded address.  Length includes
    610        1.1   thorpej 		 * terminating 0xff.
    611        1.1   thorpej 		 */
    612        1.1   thorpej 		if (tuple->length != (ETHER_ADDR_LEN * 2) + 1)
    613        1.1   thorpej 			return (0);
    614        1.1   thorpej 
    615        1.1   thorpej 		for (i = 0; i < tuple->length - 1; i++)
    616        1.1   thorpej 			addr_str[i] = pcmcia_tuple_read_1(tuple, i);
    617        1.1   thorpej 
    618        1.1   thorpej 		/*
    619        1.1   thorpej 		 * Decode the string into `myla'.
    620        1.1   thorpej 		 */
    621        1.1   thorpej 		return (mhzc_em3336_ascii_enaddr(addr_str, myla));
    622        1.1   thorpej 	}
    623        1.1   thorpej 	return (0);
    624        1.1   thorpej }
    625        1.1   thorpej 
    626        1.1   thorpej /* XXX This should be shared w/ if_sm_pcmcia.c */
    627        1.1   thorpej int
    628        1.1   thorpej mhzc_em3336_ascii_enaddr(cisstr, myla)
    629        1.1   thorpej 	const char *cisstr;
    630        1.1   thorpej 	u_int8_t *myla;
    631        1.1   thorpej {
    632        1.1   thorpej 	u_int8_t digit;
    633        1.1   thorpej 	int i;
    634        1.1   thorpej 
    635        1.1   thorpej 	memset(myla, 0, ETHER_ADDR_LEN);
    636        1.1   thorpej 
    637        1.1   thorpej 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    638        1.1   thorpej 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    639        1.1   thorpej 			digit |= cisstr[i] - '0';
    640        1.1   thorpej 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    641        1.1   thorpej 			digit |= (cisstr[i] - 'a') + 10;
    642        1.1   thorpej 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    643        1.1   thorpej 			digit |= (cisstr[i] - 'A') + 10;
    644        1.1   thorpej 		else {
    645        1.1   thorpej 			/* Bogus digit!! */
    646        1.1   thorpej 			return (0);
    647        1.1   thorpej 		}
    648        1.1   thorpej 
    649        1.1   thorpej 		/* Compensate for ordering of digits. */
    650        1.1   thorpej 		if (i & 1) {
    651        1.1   thorpej 			myla[i >> 1] = digit;
    652        1.1   thorpej 			digit = 0;
    653        1.1   thorpej 		} else
    654        1.1   thorpej 			digit <<= 4;
    655        1.1   thorpej 	}
    656        1.1   thorpej 
    657        1.1   thorpej 	return (1);
    658        1.1   thorpej }
    659        1.1   thorpej 
    660        1.1   thorpej /****** Here begins the com attachment code. ******/
    661        1.1   thorpej 
    662        1.1   thorpej #if NCOM_MHZC > 0
    663       1.30     perry int	com_mhzc_match(struct device *, struct cfdata *, void *);
    664       1.30     perry void	com_mhzc_attach(struct device *, struct device *, void *);
    665       1.30     perry int	com_mhzc_detach(struct device *, int);
    666        1.1   thorpej 
    667        1.1   thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
    668       1.12   thorpej CFATTACH_DECL(com_mhzc, sizeof(struct com_softc),
    669       1.13   thorpej     com_mhzc_match, com_mhzc_attach, com_detach, com_activate);
    670        1.1   thorpej 
    671       1.30     perry int	com_mhzc_enable(struct com_softc *);
    672       1.30     perry void	com_mhzc_disable(struct com_softc *);
    673        1.1   thorpej 
    674        1.1   thorpej int
    675        1.1   thorpej com_mhzc_match(parent, match, aux)
    676        1.1   thorpej 	struct device *parent;
    677        1.1   thorpej 	struct cfdata *match;
    678        1.1   thorpej 	void *aux;
    679        1.1   thorpej {
    680        1.1   thorpej 	extern struct cfdriver com_cd;
    681        1.1   thorpej 	const char *name = aux;
    682        1.1   thorpej 
    683        1.1   thorpej 	/* Device is always present. */
    684        1.1   thorpej 	if (strcmp(name, com_cd.cd_name) == 0)
    685        1.1   thorpej 		return (1);
    686        1.1   thorpej 
    687        1.1   thorpej 	return (0);
    688        1.1   thorpej }
    689        1.1   thorpej 
    690        1.1   thorpej void
    691        1.1   thorpej com_mhzc_attach(parent, self, aux)
    692        1.1   thorpej 	struct device *parent, *self;
    693        1.1   thorpej 	void *aux;
    694        1.1   thorpej {
    695        1.1   thorpej 	struct com_softc *sc = (void *)self;
    696        1.1   thorpej 	struct mhzc_softc *msc = (void *)parent;
    697        1.1   thorpej 
    698       1.16   mycroft 	aprint_normal("\n");
    699       1.16   mycroft 
    700        1.1   thorpej 	sc->sc_iot = msc->sc_modem_pcioh.iot;
    701        1.1   thorpej 	sc->sc_ioh = msc->sc_modem_pcioh.ioh;
    702        1.1   thorpej 
    703        1.1   thorpej 	sc->enabled = 1;
    704        1.1   thorpej 
    705        1.1   thorpej 	sc->sc_iobase = -1;
    706        1.1   thorpej 	sc->sc_frequency = COM_FREQ;
    707        1.1   thorpej 
    708        1.1   thorpej 	sc->enable = com_mhzc_enable;
    709        1.1   thorpej 	sc->disable = com_mhzc_disable;
    710        1.1   thorpej 
    711       1.16   mycroft 	aprint_normal("%s", sc->sc_dev.dv_xname);
    712       1.16   mycroft 
    713        1.1   thorpej 	com_attach_subr(sc);
    714        1.1   thorpej 
    715        1.1   thorpej 	sc->enabled = 0;
    716        1.1   thorpej }
    717        1.1   thorpej 
    718        1.1   thorpej int
    719        1.1   thorpej com_mhzc_enable(sc)
    720        1.1   thorpej 	struct com_softc *sc;
    721        1.1   thorpej {
    722        1.1   thorpej 
    723  1.34.12.1      tron 	return (mhzc_enable((struct mhzc_softc *)device_parent(&sc->sc_dev),
    724        1.1   thorpej 	    MHZC_MODEM_ENABLED));
    725        1.1   thorpej }
    726        1.1   thorpej 
    727        1.1   thorpej void
    728        1.1   thorpej com_mhzc_disable(sc)
    729        1.1   thorpej 	struct com_softc *sc;
    730        1.1   thorpej {
    731        1.1   thorpej 
    732  1.34.12.1      tron 	mhzc_disable((struct mhzc_softc *)device_parent(&sc->sc_dev),
    733        1.1   thorpej 	    MHZC_MODEM_ENABLED);
    734        1.1   thorpej }
    735        1.1   thorpej 
    736        1.1   thorpej #endif /* NCOM_MHZC > 0 */
    737        1.1   thorpej 
    738        1.1   thorpej /****** Here begins the sm attachment code. ******/
    739        1.1   thorpej 
    740        1.1   thorpej #if NSM_MHZC > 0
    741       1.30     perry int	sm_mhzc_match(struct device *, struct cfdata *, void *);
    742       1.30     perry void	sm_mhzc_attach(struct device *, struct device *, void *);
    743        1.1   thorpej 
    744        1.1   thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
    745       1.12   thorpej CFATTACH_DECL(sm_mhzc, sizeof(struct smc91cxx_softc),
    746       1.13   thorpej     sm_mhzc_match, sm_mhzc_attach, smc91cxx_detach, smc91cxx_activate);
    747        1.1   thorpej 
    748       1.30     perry int	sm_mhzc_enable(struct smc91cxx_softc *);
    749       1.30     perry void	sm_mhzc_disable(struct smc91cxx_softc *);
    750        1.1   thorpej 
    751        1.1   thorpej int
    752        1.1   thorpej sm_mhzc_match(parent, match, aux)
    753        1.1   thorpej 	struct device *parent;
    754        1.1   thorpej 	struct cfdata *match;
    755        1.1   thorpej 	void *aux;
    756        1.1   thorpej {
    757        1.1   thorpej 	extern struct cfdriver sm_cd;
    758        1.1   thorpej 	const char *name = aux;
    759        1.1   thorpej 
    760        1.1   thorpej 	/* Device is always present. */
    761        1.1   thorpej 	if (strcmp(name, sm_cd.cd_name) == 0)
    762        1.1   thorpej 		return (1);
    763        1.1   thorpej 
    764        1.1   thorpej 	return (0);
    765        1.1   thorpej }
    766        1.1   thorpej 
    767        1.1   thorpej void
    768        1.1   thorpej sm_mhzc_attach(parent, self, aux)
    769        1.1   thorpej 	struct device *parent, *self;
    770        1.1   thorpej 	void *aux;
    771        1.1   thorpej {
    772        1.1   thorpej 	struct smc91cxx_softc *sc = (void *)self;
    773        1.1   thorpej 	struct mhzc_softc *msc = (void *)parent;
    774        1.1   thorpej 	u_int8_t myla[ETHER_ADDR_LEN];
    775        1.1   thorpej 
    776       1.17   mycroft 	aprint_normal("\n");
    777       1.16   mycroft 
    778        1.1   thorpej 	sc->sc_bst = msc->sc_ethernet_pcioh.iot;
    779        1.1   thorpej 	sc->sc_bsh = msc->sc_ethernet_pcioh.ioh;
    780        1.1   thorpej 
    781        1.1   thorpej 	sc->sc_enable = sm_mhzc_enable;
    782        1.1   thorpej 	sc->sc_disable = sm_mhzc_disable;
    783        1.1   thorpej 
    784        1.1   thorpej 	if ((*msc->sc_product->mp_enaddr)(msc, myla) != 1)
    785        1.1   thorpej 		return;
    786        1.1   thorpej 
    787        1.1   thorpej 	/* Perform generic initialization. */
    788        1.1   thorpej 	smc91cxx_attach(sc, myla);
    789        1.1   thorpej }
    790        1.1   thorpej 
    791        1.1   thorpej int
    792        1.1   thorpej sm_mhzc_enable(sc)
    793        1.1   thorpej 	struct smc91cxx_softc *sc;
    794        1.1   thorpej {
    795        1.1   thorpej 
    796  1.34.12.1      tron 	return (mhzc_enable((struct mhzc_softc *)device_parent(&sc->sc_dev),
    797        1.1   thorpej 	    MHZC_ETHERNET_ENABLED));
    798        1.1   thorpej }
    799        1.1   thorpej 
    800        1.1   thorpej void
    801        1.1   thorpej sm_mhzc_disable(sc)
    802        1.1   thorpej 	struct smc91cxx_softc *sc;
    803        1.1   thorpej {
    804        1.1   thorpej 
    805  1.34.12.1      tron 	mhzc_disable((struct mhzc_softc *)device_parent(&sc->sc_dev),
    806        1.1   thorpej 	    MHZC_ETHERNET_ENABLED);
    807        1.1   thorpej }
    808        1.1   thorpej 
    809        1.1   thorpej #endif /* NSM_MHZC > 0 */
    810