Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.54.22.1
      1  1.54.22.1     joerg /*	$NetBSD: com_pcmcia.c,v 1.54.22.1 2007/10/02 18:28:36 joerg Exp $	 */
      2       1.13   mycroft 
      3       1.13   mycroft /*-
      4       1.45   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5       1.13   mycroft  * All rights reserved.
      6       1.13   mycroft  *
      7       1.13   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.13   mycroft  * by Charles M. Hannum.
      9       1.13   mycroft  *
     10       1.13   mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.13   mycroft  * modification, are permitted provided that the following conditions
     12       1.13   mycroft  * are met:
     13       1.13   mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.13   mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.13   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.13   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.13   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.13   mycroft  * 3. All advertising materials mentioning features or use of this software
     19       1.13   mycroft  *    must display the following acknowledgement:
     20       1.13   mycroft  *        This product includes software developed by the NetBSD
     21       1.13   mycroft  *        Foundation, Inc. and its contributors.
     22       1.13   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.13   mycroft  *    contributors may be used to endorse or promote products derived
     24       1.13   mycroft  *    from this software without specific prior written permission.
     25       1.13   mycroft  *
     26       1.13   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.13   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.13   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.13   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.13   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.13   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.13   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.13   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.13   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.13   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.13   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37       1.13   mycroft  */
     38        1.2   thorpej 
     39        1.2   thorpej /*-
     40        1.2   thorpej  * Copyright (c) 1991 The Regents of the University of California.
     41        1.2   thorpej  * All rights reserved.
     42        1.2   thorpej  *
     43        1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     44        1.2   thorpej  * modification, are permitted provided that the following conditions
     45        1.2   thorpej  * are met:
     46        1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     47        1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     48        1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     49        1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     50        1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     51       1.33       agc  * 3. Neither the name of the University nor the names of its contributors
     52        1.2   thorpej  *    may be used to endorse or promote products derived from this software
     53        1.2   thorpej  *    without specific prior written permission.
     54        1.2   thorpej  *
     55        1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56        1.2   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57        1.2   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58        1.2   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59        1.2   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60        1.2   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61        1.2   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62        1.2   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63        1.2   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64        1.2   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65        1.2   thorpej  * SUCH DAMAGE.
     66        1.2   thorpej  *
     67        1.2   thorpej  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     68        1.2   thorpej  */
     69       1.22     lukem 
     70       1.22     lukem #include <sys/cdefs.h>
     71  1.54.22.1     joerg __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.54.22.1 2007/10/02 18:28:36 joerg Exp $");
     72        1.2   thorpej 
     73        1.2   thorpej #include <sys/param.h>
     74        1.2   thorpej #include <sys/systm.h>
     75        1.2   thorpej #include <sys/ioctl.h>
     76        1.2   thorpej #include <sys/select.h>
     77        1.2   thorpej #include <sys/tty.h>
     78        1.2   thorpej #include <sys/proc.h>
     79        1.2   thorpej #include <sys/user.h>
     80        1.2   thorpej #include <sys/conf.h>
     81        1.2   thorpej #include <sys/file.h>
     82        1.2   thorpej #include <sys/uio.h>
     83        1.2   thorpej #include <sys/kernel.h>
     84        1.2   thorpej #include <sys/syslog.h>
     85        1.2   thorpej #include <sys/device.h>
     86        1.2   thorpej 
     87        1.2   thorpej #include <machine/intr.h>
     88        1.2   thorpej #include <machine/bus.h>
     89        1.2   thorpej 
     90        1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     91        1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     92        1.9  christos #include <dev/pcmcia/pcmciadevs.h>
     93        1.2   thorpej 
     94        1.2   thorpej #include <dev/ic/comreg.h>
     95        1.2   thorpej #include <dev/ic/comvar.h>
     96        1.2   thorpej 
     97        1.2   thorpej #include <dev/isa/isareg.h>
     98        1.2   thorpej 
     99       1.50     perry int com_pcmcia_match(struct device *, struct cfdata *, void *);
    100       1.50     perry int com_pcmcia_validate_config(struct pcmcia_config_entry *);
    101       1.50     perry void com_pcmcia_attach(struct device *, struct device *, void *);
    102       1.50     perry int com_pcmcia_detach(struct device *, int);
    103        1.2   thorpej 
    104       1.50     perry int com_pcmcia_enable(struct com_softc *);
    105       1.50     perry void com_pcmcia_disable(struct com_softc *);
    106        1.3      marc 
    107        1.2   thorpej struct com_pcmcia_softc {
    108        1.2   thorpej 	struct com_softc sc_com;		/* real "com" softc */
    109        1.2   thorpej 
    110        1.2   thorpej 	/* PCMCIA-specific goo */
    111        1.2   thorpej 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    112        1.2   thorpej 	void *sc_ih;				/* interrupt handler */
    113       1.44   mycroft 	int sc_attached;
    114        1.2   thorpej };
    115        1.2   thorpej 
    116       1.31   thorpej CFATTACH_DECL(com_pcmcia, sizeof(struct com_pcmcia_softc),
    117       1.32   thorpej     com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
    118        1.2   thorpej 
    119       1.47   mycroft static const struct pcmcia_product com_pcmcia_products[] = {
    120       1.47   mycroft 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    121       1.47   mycroft 	  PCMCIA_CIS_MEGAHERTZ_XJ2288 },
    122  1.54.22.1     joerg 	{ PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_BLUETOOTH_PCCARD,
    123  1.54.22.1     joerg 	  PCMCIA_CIS_INVALID },
    124       1.47   mycroft };
    125       1.47   mycroft static const size_t com_pcmcia_nproducts =
    126       1.47   mycroft     sizeof(com_pcmcia_products) / sizeof(com_pcmcia_products[0]);
    127        1.3      marc 
    128        1.2   thorpej int
    129       1.54  christos com_pcmcia_match(struct device *parent, struct cfdata *match,
    130       1.53  christos     void *aux)
    131        1.2   thorpej {
    132       1.11   nathanw 	int comportmask;
    133        1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    134        1.2   thorpej 	struct pcmcia_config_entry *cfe;
    135        1.2   thorpej 
    136       1.11   nathanw 	/* 1. Does it claim to be a serial device? */
    137       1.11   nathanw 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
    138       1.20  christos 		return 1;
    139       1.11   nathanw 
    140       1.11   nathanw 	/* 2. Does it have all four 'standard' port ranges? */
    141       1.11   nathanw 	comportmask = 0;
    142       1.29     lukem 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    143       1.20  christos 		switch (cfe->iospace[0].start) {
    144       1.20  christos 		case IO_COM1:
    145       1.20  christos 			comportmask |= 1;
    146       1.20  christos 			break;
    147       1.20  christos 		case IO_COM2:
    148       1.20  christos 			comportmask |= 2;
    149       1.20  christos 			break;
    150       1.20  christos 		case IO_COM3:
    151       1.20  christos 			comportmask |= 4;
    152       1.20  christos 			break;
    153       1.20  christos 		case IO_COM4:
    154       1.20  christos 			comportmask |= 8;
    155       1.20  christos 			break;
    156       1.20  christos 		}
    157       1.11   nathanw 	}
    158       1.11   nathanw 
    159       1.11   nathanw 	if (comportmask == 15)
    160       1.20  christos 		return 1;
    161       1.11   nathanw 
    162       1.11   nathanw 	/* 3. Is this a card we know about? */
    163       1.47   mycroft 	if (pcmcia_product_lookup(pa, com_pcmcia_products, com_pcmcia_nproducts,
    164       1.47   mycroft 	    sizeof(com_pcmcia_products[0]), NULL))
    165       1.20  christos 		return 1;
    166        1.2   thorpej 
    167       1.11   nathanw 	return 0;
    168        1.2   thorpej }
    169        1.2   thorpej 
    170       1.44   mycroft int
    171       1.44   mycroft com_pcmcia_validate_config(cfe)
    172       1.44   mycroft 	struct pcmcia_config_entry *cfe;
    173       1.44   mycroft {
    174       1.44   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    175       1.44   mycroft 	    cfe->num_iospace != 1)
    176       1.44   mycroft 		return (EINVAL);
    177       1.44   mycroft 	/* Some cards have a memory space, but we don't use it. */
    178       1.44   mycroft 	cfe->num_memspace = 0;
    179       1.44   mycroft 	return (0);
    180       1.44   mycroft }
    181       1.44   mycroft 
    182        1.2   thorpej void
    183       1.54  christos com_pcmcia_attach(struct device *parent, struct device *self,
    184       1.53  christos     void *aux)
    185        1.2   thorpej {
    186        1.2   thorpej 	struct com_pcmcia_softc *psc = (void *) self;
    187        1.2   thorpej 	struct com_softc *sc = &psc->sc_com;
    188        1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    189        1.2   thorpej 	struct pcmcia_config_entry *cfe;
    190       1.44   mycroft 	int error;
    191        1.2   thorpej 
    192        1.2   thorpej 	psc->sc_pf = pa->pf;
    193        1.2   thorpej 
    194       1.44   mycroft 	error = pcmcia_function_configure(pa->pf, com_pcmcia_validate_config);
    195       1.44   mycroft 	if (error) {
    196       1.44   mycroft 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    197       1.44   mycroft 		    error);
    198        1.2   thorpej 		return;
    199        1.2   thorpej 	}
    200       1.42   mycroft 
    201       1.44   mycroft 	cfe = pa->pf->cfe;
    202       1.52   gdamore 	COM_INIT_REGS(sc->sc_regs, cfe->iospace[0].handle.iot,
    203       1.52   gdamore 	    cfe->iospace[0].handle.ioh, -1);
    204       1.44   mycroft 
    205       1.44   mycroft 	error = com_pcmcia_enable(sc);
    206       1.48   mycroft 	if (error)
    207       1.44   mycroft 		goto fail;
    208       1.42   mycroft 
    209       1.42   mycroft 	sc->enabled = 1;
    210       1.42   mycroft 
    211        1.2   thorpej 	sc->sc_frequency = COM_FREQ;
    212        1.2   thorpej 
    213        1.3      marc 	sc->enable = com_pcmcia_enable;
    214        1.3      marc 	sc->disable = com_pcmcia_disable;
    215       1.20  christos 
    216       1.39   mycroft 	aprint_normal("%s", self->dv_xname);
    217        1.5  christos 
    218        1.2   thorpej 	com_attach_subr(sc);
    219        1.2   thorpej 
    220        1.3      marc 	sc->enabled = 0;
    221       1.20  christos 
    222       1.44   mycroft 	psc->sc_attached = 1;
    223       1.42   mycroft 	com_pcmcia_disable(sc);
    224       1.44   mycroft 	return;
    225       1.44   mycroft 
    226       1.44   mycroft fail:
    227       1.44   mycroft 	pcmcia_function_unconfigure(pa->pf);
    228       1.16   thorpej }
    229       1.16   thorpej 
    230       1.16   thorpej int
    231       1.16   thorpej com_pcmcia_detach(self, flags)
    232       1.20  christos 	struct device  *self;
    233       1.16   thorpej 	int flags;
    234       1.16   thorpej {
    235       1.20  christos 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
    236       1.16   thorpej 	int error;
    237       1.16   thorpej 
    238       1.44   mycroft 	if (!psc->sc_attached)
    239       1.44   mycroft 		return (0);
    240       1.28  christos 
    241       1.28  christos 	if ((error = com_detach(self, flags)) != 0)
    242       1.28  christos 		return error;
    243       1.16   thorpej 
    244       1.44   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    245       1.44   mycroft 	return (0);
    246        1.3      marc }
    247        1.3      marc 
    248        1.5  christos int
    249        1.5  christos com_pcmcia_enable(sc)
    250        1.5  christos 	struct com_softc *sc;
    251        1.3      marc {
    252        1.3      marc 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    253        1.3      marc 	struct pcmcia_function *pf = psc->sc_pf;
    254       1.25  christos 	int error;
    255       1.25  christos 
    256        1.2   thorpej 	/* establish the interrupt. */
    257        1.3      marc 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    258        1.2   thorpej 	if (psc->sc_ih == NULL) {
    259        1.2   thorpej 		printf("%s: couldn't establish interrupt\n",
    260       1.20  christos 		    sc->sc_dev.dv_xname);
    261       1.43   mycroft 		return (1);
    262        1.3      marc 	}
    263       1.42   mycroft 
    264       1.43   mycroft 	if ((error = pcmcia_function_enable(pf)) != 0) {
    265       1.42   mycroft 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    266       1.44   mycroft 		psc->sc_ih = 0;
    267       1.43   mycroft 		return (error);
    268       1.42   mycroft 	}
    269       1.42   mycroft 
    270       1.19      marc 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    271       1.21      tron 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    272       1.21      tron 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    273        1.3      marc 		int reg;
    274        1.3      marc 
    275        1.3      marc 		/* turn off the ethernet-disable bit */
    276        1.3      marc 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    277        1.3      marc 		if (reg & 0x08) {
    278       1.20  christos 			reg &= ~0x08;
    279       1.20  christos 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    280        1.3      marc 		}
    281        1.2   thorpej 	}
    282       1.43   mycroft 
    283       1.43   mycroft 	return (0);
    284        1.3      marc }
    285        1.3      marc 
    286        1.5  christos void
    287        1.5  christos com_pcmcia_disable(sc)
    288        1.5  christos 	struct com_softc *sc;
    289        1.5  christos {
    290        1.5  christos 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    291        1.5  christos 
    292       1.43   mycroft 	pcmcia_function_disable(psc->sc_pf);
    293       1.24  christos 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    294       1.44   mycroft 	psc->sc_ih = 0;
    295        1.2   thorpej }
    296